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);
}
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);
}