无法执行中断功能

时间:2019-02-12 10:30:58

标签: pic microchip mplab xc8

在pic18f4550中使用Mplab ide 5.10和xc8编译器我无法获得进入中断功能的代码,目的是使J在后台计数,直到某些东西触发它在LCD中输出一个值。当前,只有LCD显示第一条消息,并且使用ICD 3时,J的值不会改变,并且看起来程序根本不会运行中断功能

#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

        }
    }

}

0 个答案:

没有答案