Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How TMR0 value decrement/increment before TMR0 overflow

Status
Not open for further replies.

maci

New Member
I'm new to embedded program and mikroC.I am writing a program that uses a time before TMR0 overflow happens.how can I Decrement increment the TMR0 value before overflow?what is wrong in following program?

void interrupt(void)
{
if(INTCON.INTF) {

PORTA.f2 ^= 0x01; //toggle LED
PORTA.f2 ^= 0x01; //toggle LED
INTCON.INTF = 0;
//TMR0 = 128;
}
else
{
PORTA.f2 = 0x00;
INTCON.INTF = 0;
}

// INTCON &= ~0x04; //clear Timer0 interrupt flag (T0IF = 0)
}

// A program that initalize TMR0 register

void init_TMR0(void)
{
//initialize TMR0

INTCON &= ~0x80; //disable all interrupts (GIE = 0)
OPTION_REG.T0CS = 0x00;
// OPTION_REG &= ~0x20; //TMR0 uses the internal clock, Fosc/4 (T0CS = 0)
OPTION_REG |= 1; //prescaler is assigned to the Watch Dog Timer (PSA = 1)
//The WDT is disabled, however.
//If prescaler is assigned to WDT, then TMR0 will increment
//EVERY clock cycle
INTCON &= ~0x04; //clear any previous/pending TMR0 interrupt (T0IF = 0)
INTCON |= 0x20; //enable TMR0 interrupt (T0IE = 1)
INTCON |= 0x80; //enable all interrupts (GIE = 1)

}

void main()
{


//initialize a trigger variable
int trigger = 128;
TMR0 = 128;
init_TMR0();
//initialize RA2
CMCON = 0x07;
TRISA.f2 = 0x00; //RA2 pin is output
TRISA.f0 = 0xff; //RA0 pin is INPUT
TRISA.f1 = 0xff; //RA0 pin is INPUT
TRISB = 0xff; //making port B as input
PORTA.f2 = 0x00;
PORTB.f0 = 0xff;
PORTA.f0 = 0xff;
PORTA.f1 = 0xff;


// The flowchart starts here

if(PORTB.f0 == 1)
{
if(PORTA.f0==1)
{
if(TMR0==0){
TMR0 = trigger;
}
else{
trigger--;
TMR0 = trigger;

}
}
if(PORTA.f1==1)
{
if(TMR0==255){
TMR0 = trigger;
//init_TMR0();
}
else{
trigger++;
TMR0 = trigger;
}
}
else
{
TMR0 = trigger;

}
} //end of if statement
while(1);
}
 
I've read through you post a couple of times now... I don't really know what you need to do..

Are you trying to sample a Pulse Width Signal? Why are you trying to adjust TMR0 outside of the interrupt. If you tell us what you are trying to achieve, we'll be able to help.

I'm thinking you may be better off with capture compare....
 
I've read through you post a couple of times now... I don't really know what you need to do..

Are you trying to sample a Pulse Width Signal? Why are you trying to adjust TMR0 outside of the interrupt. If you tell us what you are trying to achieve, we'll be able to help.

I'm thinking you may be better off with capture compare....

I am working on project which is about Automatic voltage Regulator using triac and I'm using PIC16f628 to control the output pulse.
As soon as the PIC accepts a zero cross pulse from the zero cross detection circuit(pin 6) , it checks the status of the two
inputs, RA0 and RA1 (pin 17 and 18).These two pins are configured as over and under voltage indicators for regulator terminal voltage.The TMR0 overflow interrupt is
used to get the required delay for the trigger pulse, 128 is put as an initial count.If pin 17, over-voltage indicator, is high, that is, the output voltage is greater than 220V; the PIC has to reduce that over-voltage to 220V. Thus the PIC has to take more delay to send a gate-trigger pulse for the triac than that taken to give the desired 220V. Delay is taken by decreasing the value of Trigger, which is then put into the TMR0. When the TMR0 overflows, the PIC sends the trigger pulse for the triac.In the case under voltage it increases the value of the Trigger.I am using the above program to increase or decrease the value of the trigger based on the input from pin 17 and pin 18 (or to manage the delay before overflow).
 
So.. When the signal crosses zero, you receive this signal on RB0.... You will need to wait 20mS untill the peak voltage.... As you are not using the comparator, you have two signals coming in on RA0 and RA1 ( from an external comparator?) .... If RA0 is high reduce TMR0... If RA1 is High increase TMR0....

If you just use the trigger variable out side of the interrupt.... Then reload TMR0 with the trigger value every time the interrupt fires...
 
Status
Not open for further replies.

Latest threads

Back
Top