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.

Interrupt on PIC18f4550

Status
Not open for further replies.

georgetwo

Member
Hi guys,
yesterday i started working on a project which must involve at least 2 timers and i wanted to use pic18f4550, the datasheet tells me about high and low priority, i placed the timers at high priority. I followed the datasheet and made a test code for only timer0. But it didn"t work. I realy need your help. Im using mikro c for pic , here is what i have done:
unsigned char george = 0;

void interrupt(){

if(intcon.tmr0if){
george++;

intcon.tmr0if = 0;
}

if(pir1.tmr1if){



pir1.tmr1if = 0;
}


if(pir1.tmr2if){


pir1.TMR2IF = 0;
}


}


void configuration(void) {

trisb = 0;
portb = 0x0f;
// set internal oscillator to 8Mhz
osctune.tun4 = 0;
osctune.tun3 = 1;
osctune.tun2 = 1;
osctune.tun1 = 1;
osctune.tun0 = 1;

//======== enable timer 0 interrupt and set as hi priority=================//
t0con = 0b11010101 ;
intcon.tmr0if = 0;
intcon.tmr0ie = 1;
intcon.TMR0IP = 1;

/*//========enable timer 1 interrupt and set as hi priority===================//

t1con.rd16 = 1;
t1con.TMR1CS = 0;
t1con.T1CKPS1 = 1;
t1con.T1CKPS0 = 1;

t1con.tmr1on = 1;

pie1.TMR1IE = 1;
ipr1.TMR1IP = 1;

//========enable timer 2 interrupt and set as hi priority===================//
t2con.t2ckps1 = 1;
t2con.t2ckps0 = 0;
t2con.tmr2on = 1;
pie1.TMR2IE = 1;
ipr1.TMR2IP = 1;



//======= enable global and peripheral int.=================================//
*/
intcon.peie = 1;
intcon.gie = 1;
intcon.gieh = 1;


}

void main(){
configuration();
while(1){

if (george == 100){
portb ++;
george = 0;
}



}



}
 
Whats not working... Does it compile? Most compilers use upper case definitions for internal registers.
The internal Xtal is set with the OSCCON register not the OSCTUNE, this is to slightly adjust the frequency.

I cant simulate as I use C18..
 
I activated internal xt with the project editor inside mikro c, it worked when i did a test with blinking led using delay_ms.
The code compiled without error. But it didn't work on the chip.
 
can any one pleas help me with a PIC18f4550 interrupt sample on any timer?
with that i might be able to find my way out of this problem. Thanks
 
Can you repost the code but using code tags. Type [code] before your code and [/code] after it.

I would check your compiler manual as well. Normally variables are lower case and constants are UPPER. So,
Code:
t0con = 0b11010101 ;
intcon.tmr0if = 0;
intcon.tmr0ie = 1;
intcon.TMR0IP = 1;

/*//========enable timer 1 interrupt and set as hi priority===================//

t1con.rd16 = 1;
t1con.TMR1CS = 0;
t1con.T1CKPS1 = 1;
t1con.T1CKPS0 = 1;
Might need to be,
Code:
t0con = 0b11010101 ;
intcon.TMR0IF = 0;
intcon.TMR0IE = 1;
intcon.TMR0IP = 1;

/*//========enable timer 1 interrupt and set as hi priority===================//

t1con.RD16 = 1;
t1con.TMR1CS = 0;
t1con.T1CKPS1 = 1;
t1con.T1CKPS0 = 1;
I.E. variable.CONSTANT

Edit, I don't use MicroC and so can't test it.

Mike.
 
Last edited by a moderator:
Thanks,
I will give it a try and get back to you
Code:
void intl()iv 0x000008 {

   if(intcon.tmr0if){
     george++;
     
    intcon.tmr0if = 0;
   }
   
   if(pir1.tmr1if){

   
   
    pir1.tmr1if = 0;
   }
   
   
   if(pir1.tmr2if){
   
   
    pir1.TMR2IF = 0;
   }


}


void configuration(void) {

 trisb = 0;
 portb = 0x0f;
// set internal oscillator to 8Mhz
 osctune.tun4 = 0;
 osctune.tun3 = 1;
 osctune.tun2 = 1;
 osctune.tun1 = 1;
 osctune.tun0 = 1;

//======== enable timer 0 interrupt and set as hi priority=================//
 t0con = 0b11010101 ;
 intcon.tmr0if = 0;
intcon.tmr0ie = 1;
intcon.TMR0IP = 1;

/*//========enable timer 1 interrupt and set as hi priority===================//

t1con.rd16 = 1;
t1con.TMR1CS = 0;
t1con.T1CKPS1 = 1;
t1con.T1CKPS0 = 1;

t1con.tmr1on = 1;

pie1.TMR1IE = 1;
ipr1.TMR1IP = 1;

 //========enable timer 2 interrupt and set as hi priority===================//
   t2con.t2ckps1 = 1;
   t2con.t2ckps0 = 0;
   t2con.tmr2on = 1;
   pie1.TMR2IE = 1;
   ipr1.TMR2IP = 1;



//======= enable global and peripheral int.=================================//
 */
intcon.peie = 1;
intcon.gie = 1;
intcon.gieh = 1;


}

 void main(){
  configuration();
  while(1){
    
    if (george == 100){
     portb ++;
     george = 0;
    }
    

  
  }
  
 
 
 }
 
Right, it is working,
Q: what realy hapened?
A: I sent data directly to portB, it use to work with other chips but i just don't understand why,
this is what i did:
Code:
 unsigned char george, count;

void interrupt() {

   if(intcon.TMROIF){
     george++;
     
    intcon.TMROIF = 0;
   }
   
   if(pir1.TMR1IF){

   
   
    pir1.TMR1IF = 0;
   }
   
   
   if(pir1.TMR2IF){
   
   
    pir1.TMR2IF = 0;
   }


}


void configuration(void) {

 trisb = 0;
 portb = 0x0f;
// set internal oscillator to 8Mhz
 osctune.tun4 = 0;
 osctune.tun3 = 1;
 osctune.tun2 = 1;
 osctune.tun1 = 1;
 osctune.tun0 = 1;

//======== enable timer 0 interrupt and set as hi priority=================//
 t0con = 0b11010101 ;
 intcon.TMR0IF = 0;
intcon.TMR0IE = 1;
intcon2.TMR0IP = 1;

/*//========enable timer 1 interrupt and set as hi priority===================//

t1con.rd16 = 1;
t1con.TMR1CS = 0;
t1con.T1CKPS1 = 1;
t1con.T1CKPS0 = 1;

t1con.tmr1on = 1;

pie1.TMR1IE = 1;
ipr1.TMR1IP = 1;

 //========enable timer 2 interrupt and set as hi priority===================//
   t2con.t2ckps1 = 1;
   t2con.t2ckps0 = 0;
   t2con.tmr2on = 1;
   pie1.TMR2IE = 1;
   ipr1.TMR2IP = 1;



//======= enable global and peripheral int.=================================//
 */
intcon.peie = 1;
intcon.gie = 1;
intcon.gieh = 1;


}

 void main(){
  configuration();
  while(1){
    
    if (george == 10){
     count++
     portb = count;
     george = 0;
    }
    

  
  }
  
 
 
 }
Thanks to you all for your attention.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top