PIC Help!!! - Page 1
Forum section: electronics / general

author: Frank3point14
author_rank: Newbie
timestamp: September 15, 2013, 06:43:00 am
content: HelloI am trying to configure the external interrupts int1 and int2 of a pic18f4431Please check out and tell me what is going wrong with it..Code://******************************************************************************//LCD connectionssbit 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;//******************************************************************************//functionsvoid timer1_init(); // initialize TIMER1void inbuiltF_init(); // initialize inbuilt functionsvoid interrupts_init(); // initialize interruptsvoid interrupt();void display_lcd();//******************************************************************************// global variablesint calpid = 0; // pid loop run commandint ledon = 0;int x = 0;//******************************************************************************void interrupt(){if(PIR1.TMR1IF==1){calpid = 1; // run PID loopTMR1L = 181; //// preload timer1TMR1H = 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 executionTMR1L = 181; // preload timer1TMR1H = 179;T1CON.RD16 = 1; // enable register read/write of TIMER1 in two 16-bit operations(1)T1CON.T1RUN = 0; // system clock is derived from another source (0)T1CON.T1CKPS1 = 0; // ##increments with 1:2 prescale value (0)T1CON.T1CKPS0 = 1; // ## (1)T1CON.T1OSCEN = 0; // TIMER1 oscillator is shut-off (0)T1CON.TMR1CS = 0; // TIMER1 uses internal clock (FOSC/4) (0)T1CON.TMR1ON = 1; // enable TIMER1 (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 flagINTCON3.INT1IP = 0; // INT1 external interrupt priority - low (0)INTCON2.INTEDG1 = 1; // external interrupt1 RC4 on rising edgeINTCON3.INT1IF = 0; // clear RC4 INT1 interrupt flagINTCON3.INT1IE = 1; // RC4 external interrupt enableINTCON3.INT2IP = 0; // INT2 external interrupt priority - low (0)INTCON2.INTEDG2 = 1; // external interrupt2 RC5 on rising edgeINTCON3.INT2IF = 0; // clear RC5 INT2 interrupt flagINTCON3.INT2IE = 1; // RC5 external interrupt enable}

author: GeoffS
author_rank: Unknown
timestamp: September 15, 2013, 07:13:43 am
content: What indications do you get that the program is not working properly?error messages?

author: dannyf
author_rank: Super Contributor
timestamp: September 15, 2013, 01:11:02 pm
content: QuoteYour code does exactly what you wanted it to do - whatever that is.

author: AlfBaz
author_rank: Super Contributor
timestamp: September 15, 2013, 02:28:49 pm
content: 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" codeThis 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

author: dannyf
author_rank: Super Contributor
timestamp: September 15, 2013, 02:51:00 pm
content: Quotemikroc.QuoteCode:void interrupt() {...QuoteYou don't have to do that.

author: jc101
author_rank: Frequent Contributor
timestamp: September 15, 2013, 03:54:10 pm
content: Is your problem no LCD output at all? If so, try altering the definitions used for the LCD to this....Code: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

author: Frank3point14
author_rank: Newbie
timestamp: September 21, 2013, 10:49:33 pm
content: Thanks everyone ....jc101....