Author Topic: PIC Help!!!  (Read 3179 times)

0 Members and 1 Guest are viewing this topic.

Offline Frank3point14Topic starter

  • Newbie
  • Posts: 8
PIC Help!!!
« on: September 15, 2013, 06:43:00 am »
Hello
I am trying to configure the external interrupts int1 and int2 of a pic18f4431
Please check out and tell me what is going wrong with it..???
Code: [Select]
//******************************************************************************
//LCD connections
sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB0_bit;
sbit LCD_D4 at RD6_bit;
 
sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB0_bit;
sbit LCD_D4_Direction at TRISD6_bit;
//******************************************************************************
//functions
void timer1_init();             // initialize TIMER1
void inbuiltF_init();           // initialize inbuilt functions
void interrupts_init();         // initialize interrupts
void interrupt();
void display_lcd();
//******************************************************************************
// global variables
int calpid = 0;                 // pid loop run command
int ledon  = 0;
int x      = 0;
   
//******************************************************************************
void interrupt(){           
     if(PIR1.TMR1IF==1){                           
          calpid = 1;           // run PID loop
          TMR1L         = 181;  //// preload timer1
          TMR1H         = 179;  ////                         
          PIR1.TMR1IF = 0;                                           
     } 
     if(INTCON.INT0IF==1){
          ledon = 1;               
          INTCON.INT0IF = 0;
     }
     if(INTCON3.INT1IF==1){
          ledon = 1;                               
          INTCON3.INT1IF==0;
     }   
     if(INTCON3.INT2IF==1){     
          ledon = 1;                                         
          INTCON3.INT2IF==0;
     }             
}
//******************************************************************************
void main(){
 
     TRISC.F7 = 0;
     PORTC.F7 = 0;
     inbuiltF_init(); 
     interrupts_init();     
     timer1_init();           
//******************************************************************************
     while(1) {
          while(ledon==1){
               Lcd_Out(1,1,"led on");
               Delay_ms(100);
               Lcd_Out(1,1,"      ");
               ledon = 0;
          }         
          while(calpid==1){
               x = x + 1;
               if(x<128){           
                    PORTC.F7 = 1;                 
               }
               else if(x<512){
                    PORTC.F7 = 0;
               }
               else if(x==512){
                    x = 0;
               }             
               calpid = 0;                           
          }
     }
}
//******************************************************************************
void timer1_init(){                // initialized to give 256Hz interrupt for PID loop execution
     TMR1L         = 181;          // preload timer1
     TMR1H         = 179;
     
     T1CON.RD16    = 1;          // enable register read/write of TIMER1 in two 16-bit operations[7](1)
     T1CON.T1RUN   = 0;          // system clock is derived from another source                  [6](0)
     T1CON.T1CKPS1 = 0;          // ##increments with 1:2 prescale value                         [5](0)
     T1CON.T1CKPS0 = 1;          // ##                                                           [4](1)
     T1CON.T1OSCEN = 0;          // TIMER1 oscillator is shut-off                                [3](0)
     T1CON.TMR1CS  = 0;          // TIMER1 uses internal clock (FOSC/4)                          [1](0)
     T1CON.TMR1ON  = 1;          // enable TIMER1                                                [0](1)
}
//******************************************************************************
void inbuiltF_init(){
     Lcd_Init();
     Lcd_Cmd(_LCD_CLEAR);
     Lcd_Cmd(_LCD_CURSOR_OFF);
}
//******************************************************************************
void interrupts_init(){
     RCON.IPEN   = 0;           // disable priority levels on interrupts         (0)
     
     INTCON.GIE  = 1;           // global interrupts enabled                     (1)
     INTCON.PEIE = 1;           // enable all unmasked peripheral interrupts     (1)
     
     PIE1.TMR1IE = 1;           // TIMER1 overflow interrupt enabled             (1)
     IPR1.TMR1IP = 0;           // TIMER1 overflow interrupt set to low priority (0)
     PIR1.TMR1IF = 0;           // clear TIMER1 overflow interrupt flag          (0)   
     
     
     INTCON.INT0IE   = 1;       // RC3 external interrupt enable                 (1)
     INTCON2.INTEDG0 = 1;       // external interrupt0 RC3 on rising edge        (1)
     INTCON.INT0IF   = 0;       // clear RC3 INT0 interrupt flag   
     
     INTCON3.INT1IP  = 0;       // INT1 external interrupt priority - low        (0)
     INTCON2.INTEDG1 = 1;       // external interrupt1 RC4 on rising edge
     INTCON3.INT1IF  = 0;       // clear RC4 INT1 interrupt flag
     INTCON3.INT1IE  = 1;       // RC4 external interrupt enable     
     
     INTCON3.INT2IP  = 0;       // INT2 external interrupt priority - low         (0)
     INTCON2.INTEDG2 = 1;       // external interrupt2 RC5 on rising edge
     INTCON3.INT2IF  = 0;       // clear RC5 INT2 interrupt flag
     INTCON3.INT2IE  = 1;       // RC5 external interrupt enable                             
}
 

Offline GeoffS

  • Supporter
  • ****
  • Posts: 1272
  • Country: au
Re: PIC Help!!!
« Reply #1 on: September 15, 2013, 07:13:43 am »
What indications do you get that the program is not working properly?
error messages?
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC Help!!!
« Reply #2 on: September 15, 2013, 01:11:02 pm »
Quote
tell me what is going wrong with it

Your code does exactly what you wanted it to do - whatever that is.
================================
https://dannyelectronics.wordpress.com/
 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2184
  • Country: au
Re: PIC Help!!!
« Reply #3 on: September 15, 2013, 02:28:49 pm »
what compiler is that? I 've never seen this "sbit...... at..."

At any rate I don't see in your code any directives that tell the compiler what the interrupt is , let alone wether its a high or low priority one.
Usually a #pragma of some kind will precede the interrupt code, followed by some other directive/pragma telling the compiler you are back to "normal" code

This is normally required so that the compiler can generate context saving code upon entering the interrupt and execute a RETFIE asm instruction at the end of it rather than the usual RETURN for normal fucntions
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC Help!!!
« Reply #4 on: September 15, 2013, 02:51:00 pm »
Quote
what compiler is that? I 've never seen this "sbit...... at..."

mikroc.

Quote
At any rate I don't see in your code any directives that tell the compiler what the interrupt is ,

Code: [Select]
void interrupt() {...
Quote
let alone wether its a high or low priority one.

You don't have to do that.
================================
https://dannyelectronics.wordpress.com/
 

Offline jc101

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: gb
Re: PIC Help!!!
« Reply #5 on: September 15, 2013, 03:54:10 pm »
Is your problem no LCD output at all?  If so, try altering the definitions used for the LCD to this....

Code: [Select]
sbit LCD_RS at LATD5_bit;
sbit LCD_EN at LATD4_bit;
sbit LCD_D7 at LATB5_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D5 at LATB0_bit;
sbit LCD_D4 at LATD6_bit;

I always use this style of definition with MikroC
 

Offline Frank3point14Topic starter

  • Newbie
  • Posts: 8
Re: PIC Help!!!
« Reply #6 on: September 21, 2013, 10:49:33 pm »
Thanks everyone ....
jc101.... :-+
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf