2

Using Mplab ide 5.10 and xc8 compiler for the pic18f4550 I am unable to get the code to get into the interrupt function the goal is to get J to count up in the background until something trigger it to output a value in the lcd. Currently only the lcd display the first message and using ICD 3 the value of J does not change and does not look like the program runs the interrupt function at all

#define _XTAL_FREQ 48000000


#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"

unsigned char j, output = 0, i, outchar;

char buffer[2] = " ";
char Message[ ] = "Hands Position  ";
void interrupt timer0_isr();
void lcd_write_cmd(unsigned char cmd);
void lcd_write_data(unsigned char data);
void lcd_strobe(void); // Generate the E pulse
void lcd_init(void);

void interrupt timer0_ISR() // Timer0 Interrupt Service Routine (ISR)
{
    if (INTCONbits.TMR0IF) // TMR0IF:- Timer0 Overflow Interrupt Flag Bit
    {
        TMR0H = 0x48; // Timer0 start value = 0x48E5 for 1 second
        TMR0L = 0xE5;
        PORTCbits.RC1 = !PORTCbits.RC1; /* external timing check - toggle every 1ms */
        if (j <= 4) { //limit up to 7
            j++; // Increase count by 1
            PORTB = j; // Output to Demultiplexer
        } else {

            j = 0; // Reset count aftwr it hit 7
            PORTB = j; // Output to Demultiplexer
        }
        INTCONbits.TMR0IF = 0; // Reset TMR0IF at interrupt end
    }
}

void main(void) // Main Function
{

    ADCON1 = 0x0F;
    CMCON = 0x07;

    RCONbits.IPEN = 1; // Bit7 Interrupt Priority Enable Bit
    INTCONbits.GIEH = 1; // Bit7 Global Interrupt Enable bit
    INTCONbits.GIEL = 0; /* turn on low & high interrupts */
    T0CONbits.TMR0ON = 1; // Turn on timer
    T0CON = 0b00000111; // bit7:0 Stop Timer0
    // bit6:0 Timer0 as 16 bit timer
    // bit5:0 Clock source is internal 
    // bit4:0 Increment on lo to hi transition on TOCKI pin
    // bit3:0 Prescaler output is assigned to Timer0  
    // bit2-bit0:111 1:256 prescaler


    INTCON2 = 0b10000100; // bit7 :PORTB Pull-Up Enable bit
    //   1 All PORTB pull-ups are disabled
    // bit2 :TMR0 Overflow Int Priority Bit
    //   1 High Priority

    TMR0H = 0x48; // Initialising TMR0H
    TMR0L = 0xE5; // Initialising TMR0L for 1 second interrupt

    INTCONbits.TMR0IE = 1; // bit5 TMR0 Overflow Int Enable bit
    INTCONbits.TMR0IF = 0; // bit2 TMR0 Overflow Int Flag bit
    // 0 TMR0 register did not overflow

    TRISC = 0; /* all outputs */
    TRISAbits.TRISA5 = 1; // RA5 is the check for signal from input Multiplexer.

    TRISAbits.TRISA0 = 0; // RA0, RA1 & RA2 output to arduino
    TRISAbits.TRISA1 = 0;
    TRISAbits.TRISA2 = 0;

    TRISD = 0x00; // PortD connects to Demultiplexer
    TRISB = 0;
    lcd_init(); // LCD init
    lcd_write_cmd(0x80); // Cursor set at line 1 positon 1

    for (i = 0; i < 16; i++) {
        outchar = Message[i]; // Store Message in outchar
        lcd_write_data(outchar); // Display Message 
    }

    __delay_ms(100);

    PORTD = 0x00; // Clear PortD
    PORTB = 0;
    j = 0; // Start count from 0


    while (1) // Main Process
    {
        if (PORTAbits.RA5 == 1) { // If RA3 detect a signal



            switch (j) { // Switch case to determine hand position & output to RA0, RA1 & RA2 to transmit to arduino

                case(0):

                    output = 10;
                    PORTAbits.RA0 = 0;
                    PORTAbits.RA1 = 0;
                    PORTAbits.RA2 = 0;

                    break;

                case(1):

                    output = 20;

                    PORTAbits.RA0 = 1;
                    PORTAbits.RA1 = 0;
                    PORTAbits.RA2 = 0;

                    break;

                case(2):

                    output = 30;

                    PORTAbits.RA0 = 0;
                    PORTAbits.RA1 = 1;
                    PORTAbits.RA2 = 0;

                    break;

                case(3):

                    output = 40;

                    PORTAbits.RA0 = 1;
                    PORTAbits.RA1 = 1;
                    PORTAbits.RA2 = 0;

                    break;

                case(4):

                    output = 50;

                    PORTAbits.RA0 = 0;
                    PORTAbits.RA1 = 0;
                    PORTAbits.RA2 = 1;

                    break;


            }

            lcd_write_cmd(0xC0); // Cursor set at line 2 positon 1

            sprintf(buffer, "%d", output); // Convert numbers to character 
            for (i = 0; i < 2; i++)
                lcd_write_data(buffer[i]); // Display Hand Position

        }
    }

}
3
  • 1
    Your TMR0 is disabled in line: T0CON = 0b00000111; // bit7:0 Stop Timer0 Commented Feb 12, 2019 at 19:24
  • 1
    Also I think you have to declare the ISR differently if you are going to use interrupt priorities, try setting RCONbits.IPEN = 0; and INTCONbits.GIEL = 1; which will now be PEIE and see what happens. Commented Feb 13, 2019 at 11:24
  • 1
    sprintf(buffer, "%d", output); will add a 0-terminator to the string so you're certainly overflowing buffer. That's undefined behavior and should be fixed first because nasal demons. Commented Feb 14, 2019 at 19:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.