Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Timer interrupt and main conflict in PIC16F676 using mikroc

Status
Not open for further replies.

djc

Advanced Member level 1
Joined
Jan 27, 2013
Messages
402
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
India
Activity points
4,554
Hi,
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???
 

in the main Timer1_Control();is called that will configure timer 1 for isr. bt it seems you have commented the code in isr routine that will clear timer 1 isr flag i.e. PIR1.TMR1IF = 0; hence its not working ok
 
  • Like
Reactions: djc

    djc

    Points: 2
    Helpful Answer Positive Rating
Thanx a lot. Before commenting it also it was not working. But don't know how is it working now. I have another piece of code using that i used to create a sine wave. Now it is giving me pure triangular wave.
Code:
while(1){
                 if(Flag==1){
                 OnPulse = wave[j++];
                 OffPulse = 255-OnPulse;
                 Flag=0;
                 if(j>50) j =0;
                 }
wave table being,
const unsigned char wave[51] = {128,143,159,174,189,202,215,226,
235,243,249,253,255,255,253,249,
243,235,226,215,202,189,174,159,
143,128,112,96,81,66,53,40,
29,20,12,6,2,0,0,2,
6,12,20,29,40,53,66,81,96,112,128};
an you tell me now why is this happening.
 

well for this may need other code data like where your making Flag set or reset, your checking this Flag in while loop but from where its getting set ? is it same in continuation with above code or its somthing different code ?
 

Thanx again. I got d sine wave again. Issue was timer was getting reloaded with false values instead of specified values.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top