djc
Advanced Member level 1
Hi,
I want to use timer0 for 1Khz interrupt,timer1 for 100Hz interrupt in 16F676 using mikroc. Code is as follows.
Timer0 is being used for software PWM. Initially i used ADC routine to change the pulse width of timer0. It worked for some time and suddenly stopped working. Means timer0 and timer1 interrupts are working but whatever is written in main is not working at all. So i removed ADC routine from main and simply toggled a port pin. I am checking it on oscilloscope. But port pin does not toggle in main. I tried with different port pins but none seems to be working. However if i stop Timer0 and Timer1 interrupt, whatever is written in main, starts working. Port pin toggling in Timer0 and Timer1 ae ok. I am using external interrupt on RA2. I disabled it. But still same result. What problem timer interrupt must be causing???
I want to use timer0 for 1Khz interrupt,timer1 for 100Hz interrupt in 16F676 using mikroc. Code is as follows.
Code:
#define channel_1 PORTC.B0
#define channel_2 PORTC.B1
unsigned char OnPulse = 0;
unsigned char OffPulse = 0;
char TOG = 0,Flag=0,Tenms=0;
unsigned int i = 0,j=0;
Port_Control(){
CMCON = 0x07;
OSCCAL = 0b11111100;
ANSEL = 0;
TRISA = 0x00; //PortA as output
TRISC = 0x00; //PortC as output
PORTC = 0x00; //Initial value on PortC
//INTE_bit = 1;
//INTEDG_bit = 1;
}
Timer0_Control(){
INTCON = 0b11100000;
T0IF_bit = 0;
TMR0 = 0;
T0CS_bit = 0;
PSA_bit = 0; //Prescaler is assigned to Timer0 module
PS0_bit = 0; //TMR0 rate 1:2
PS1_bit = 0;
PS2_bit = 0;
}
Timer1_Control(){
PIR1.TMR1IF = 0;
TMR1GE_bit = 0;
T1CON.T1CKPS0 = 0; //1:1 prescale value
T1CON.T1CKPS1 = 0; //1:1 prescale value
T1CON.TMR1CS = 0; //Internal clock Fosc/4
T1CON.TMR1ON = 0;
PIE1.TMR1IE = 1;
TMR1L = 0x40;
TMR1H = 0x9C;
T1CON.TMR1ON = 1;
}
void interrupt(){
/*if(PIR1.TMR1IF){ //10ms on/off
PIR1.TMR1IF = 0;
Tenms = Tenms+1;
//if(i==1){
if(Tenms==1){
//channel_2=1;
}
else if(Tenms==2){
//channel_2 = 0;
//channel_1 = 0;
Tenms = 0;
i = 0;
}
//}
TMR1L = 0x00;
TMR1H = 0xe4;
}*/
if(INTCON.T0IF){ //PWM Signal,2Khz
T0IF_bit = 0;
//if(channel_2 == 1){
if(Tog==1){
Tmr0 = OffPulse;
channel_1= 1;
Tog=0;
}
else{
Tmr0 = OnPulse;
channel_1 = 0;
Tog=1;
}
//}
TMR0=0;
}
}
void main() {
Port_Control();
Timer0_Control();
Timer1_Control();
//ADC_Init();
while(1){
PORTA.B0 =1;
delay_ms(10);
PORTA.B0=0;
delay_ms(10);
}
}
Timer0 is being used for software PWM. Initially i used ADC routine to change the pulse width of timer0. It worked for some time and suddenly stopped working. Means timer0 and timer1 interrupts are working but whatever is written in main is not working at all. So i removed ADC routine from main and simply toggled a port pin. I am checking it on oscilloscope. But port pin does not toggle in main. I tried with different port pins but none seems to be working. However if i stop Timer0 and Timer1 interrupt, whatever is written in main, starts working. Port pin toggling in Timer0 and Timer1 ae ok. I am using external interrupt on RA2. I disabled it. But still same result. What problem timer interrupt must be causing???