PIC16F1829 UART RX 中断无法使用 MPLABX 和 XC8 编译器工作

PIC16F1829 UART RX Interrupt not working using MPLABX and XC8 compiler

我不熟悉为 8 位 PIC 编写固件,我的代码需要一些帮助。我将 PIC16F1829 用于获取 RX 命令的 LED 模块。我只是想进行一些基本设置,例如在 RX 引脚上收到某个值时打开 LED,但我什至无法做到这一点。

想通过中断让 UART 工作,但甚至不能让它在主循环中使用轮询。我的中断向量在下面的代码中被注释掉了。

RX 引脚:RC5

TX 引脚:RB7

用于打开和关闭 LED 的引脚:RA5

引脚 RA5 可以很好地打开和关闭 LED。 TX 引脚正在工作,但我还没有确认中断 TXIF 是否也像 RCIF 一样没有工作。

我试过阅读 RCIF 和 PIR1bits.RCIF。两人都编了。没有一个工作。我已经在 2 个不同的 LED 模块上的两个不同的 PIC 上尝试过这个。他们打开,但读取 RX 引脚也不起作用。

变量 RXIN 最初定义为 3,因此由于主循环中的 RXIN-- 循环,灯在启动时闪烁 3 次,所以我知道它正在进入主循环。但据我所知,RCIF 中断没有在 RX 引脚接收时触发。

我已经在示波器上确认进入 RX 和从 TX 引脚输出的信号使用相同的波特率,所以我认为波特率配置正确(300 波特,8N1。)我也已经在示波器 RX 引脚上确认接收强和干净的 5V 信号。到目前为止,轮询 RCIF 或使用中断服务路由均无效。如果有人能看到我没有看到的代码问题,将不胜感激。

我的代码:

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

// This is for 300 baud rate
#define _BAUD_PRESCALER_LOW_ 0x2A
#define _BAUD_PRESCALER_HIGH_ 0x68
#define _XTAL_FREQ 32000000

#pragma config FOSC = INTOSC    // Oscillator Selection->INTOSC oscillator: I/O function on CLKIN pin
#pragma config WDTE = OFF    // Watchdog Timer Enable->WDT enabled
#pragma config PWRTE = OFF    // Power-up Timer Enable->PWRT disabled
#pragma config MCLRE = OFF    // MCLR Pin Function Select->MCLR/VPP pin function is digital input
#pragma config CP = OFF    // Flash Program Memory Code Protection->Program memory code protection is disabled
#pragma config CPD = OFF    // Data Memory Code Protection->Data memory code protection is disabled
#pragma config BOREN = ON    // Brown-out Reset Enable->Brown-out Reset enabled
#pragma config CLKOUTEN = OFF    // Clock Out Enable->CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
#pragma config IESO = OFF    // Internal/External Switchover->Internal/External Switchover mode is disabled
#pragma config FCMEN = OFF    // Fail-Safe Clock Monitor Enable->Fail-Safe Clock Monitor is disabled

// CONFIG2
#pragma config WRT = OFF    // Flash Memory Self-Write Protection->Write protection off
#pragma config PLLEN = ON    // PLL Enable->4x PLL enabled
#pragma config STVREN = ON    // Stack Overflow/Underflow Reset Enable->Stack Overflow or Underflow will cause a Reset
#pragma config BORV = LO    // Brown-out Reset Voltage Selection->Brown-out Reset Voltage (Vbor), low trip point selected.
#pragma config LVP = OFF

int flagRXFramingError = 0;
int flagRXOverrunError = 0;
volatile unsigned char RXIN = 3;

unsigned char UARTRead(){
    return RCREG;
}

void writeRXIN(unsigned char a){
    RXIN = a;
}

void TX(unsigned char a){
    while(!TXIF){}
    TXREG = a;
}

int main(int argc, char** argv) {

    // SCS FOSC; SPLLEN disabled; IRCF 8MHz_HF; 
    OSCCON = 0xF0;
    // TUN 0; 
    OSCTUNE = 0x00;
    // Set the secondary oscillator
    // Wait for PLL to stabilize
    while(PLLR == 0)
    {
    }

    // WDTPS 1:65536; SWDTEN OFF; 
    WDTCON = 0x16;
    __delay_ms(5);

    GIE = 1; // Global interrupts enabled
    __delay_ms(5);
    PEIE = 1; // Active peripheral interrupts enabled
    __delay_ms(5);
    RCIE = 1; // Enable USART Receive interrupt
    __delay_ms(5);
    TXIE = 1; // Enable USART Transmitter interrupt
    __delay_ms(5);
    ADIE = 1; // Enable ADC interrupts
    __delay_ms(5);
    RXDTSEL = 0; // RX is on RC5 pin
    __delay_ms(5);
    TXCKSEL = 0; // TX is on RB7 pin
    __delay_ms(5);

    TRISC5 = 1; // RX pin set as input
    __delay_ms(5);

    SPEN = 1; // Serial Port Enabled
    __delay_ms(5);
    SYNC = 0; // Asynchronous mode
    __delay_ms(5);
    RX9 = 0; // 8 bit reception
    __delay_ms(5);
    TX9 = 0; // 8-bit transmission
    __delay_ms(5);
    CREN = 1; // Receiver enabled
    __delay_ms(5);
    TXEN = 1; // Transmitter enabled 
   __delay_ms(5);
    BRG16 = 1; // 16-bit baud generation
    __delay_ms(5);
    BRGH = 1; // High baud rate enabled
    __delay_ms(5);
    ABDEN = 0; // Auto baud detect disabled
    __delay_ms(5);

    // Baud prescaler n = [Fosc/(D*BR)] - 1

    SPBRGH = _BAUD_PRESCALER_HIGH_;
    __delay_ms(5);
    SPBRGL = _BAUD_PRESCALER_LOW_;
    __delay_ms(5);

    TRISC6 = 0; // IadjPWM pin configured as output
    __delay_ms(5);
    ANSC6 = 0; // IadjPWM pin not analog input
    __delay_ms(5);
    TRISA5 = 0; // DimPWM pin configured as output
    __delay_ms(5);

    LATC6 = 1; // Max current for now until PWM written
    __delay_ms(5);

    while(1){

    // Inline assembly code to clear watchdog timer
    //asm("CLRWDT");

    /*if(RXIN == 5){
        RA5 = 1;
    }
    else{
        RA5 = 0;
    }*/

        if(PIR1bits.RCIF){
            writeRXIN(UARTRead());
            //RA5 = 0;
            TX(RXIN);
        } // end if RCIF

        while(RXIN > 0){
            RA5 = 1;
            __delay_ms(100);
            RA5 = 0;
            __delay_ms(100);
            RXIN--;
        }

    } 
    // infinite loop
    // never leave this loop

    RA5 = 1;
    return (EXIT_SUCCESS);
} // end main

/*void interrupt ISR(void){
    if(RCIF){// if USART Receive interrupt flag
        RA5 = 1;

        if(FERR){
            flagRXFramingError = 1;
            SPEN = 0;
            SPEN = 1;

        }
        if(OERR){
            flagRXOverrunError = 1;
            CREN = 0;
            CREN = 1;
        }

        while(RCIF){ // RCIF high as long as there is data in FIFO register. Read RCREG to clear RCIF flag
            writeRXIN(UARTRead());
        }

        RA5 = 0;
    }

    if (TXIF){// if USART Transmit interrupt
        TXIF = 0; // Clear interrupt flag
    }
} // end ISRs*/

如果出现某种错误,某些微控制器会停止接收字节。请务必清除这些错误。通常通过清除一些 UART 控制寄存器位。

解决了问题

我不确定究竟是什么解决了问题,但我会分享我所做的主要更改和新代码。

  1. 我启用了 TXIE。 TXIF 几乎总是高电平,因此它产生 连续中断。我看不出启用 TX 中断的理由, 虽然可能有一个很好的。如果你想 TX 等到 TXIF 是 不为零而发送,否则为什么要使用标志?

  2. 我以错误的顺序启用了中断。我应该 启用外围设备,然后他们各自的中断,如果 必要的,然后是PEIE,最后是GIE。

  3. 我没有在中断中处理 FERR 和 OERR,尽管它们可能 正在触发并导致中断。

我的原始代码中的 RXDTSEL 设置也有误。这是新的工作代码。现在它所做的只是回应 RX 信号并使 LED 闪烁传输的次数。

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

// This is for 300 baud rate
#define _BAUD_PRESCALER_LOW_ 0x2A
#define _BAUD_PRESCALER_HIGH_ 0x68
#define _XTAL_FREQ 32000000
#define _PIN_DIMPWMPIN_ RA5

#pragma config FOSC = INTOSC    // Oscillator Selection->INTOSC oscillator: I/O function on CLKIN pin
#pragma config WDTE = OFF    // Watchdog Timer Enable->WDT enabled
#pragma config PWRTE = OFF    // Power-up Timer Enable->PWRT disabled
#pragma config MCLRE = OFF    // MCLR Pin Function Select->MCLR/VPP pin function is digital input
#pragma config CP = OFF    // Flash Program Memory Code Protection->Program memory code protection is disabled
#pragma config CPD = OFF    // Data Memory Code Protection->Data memory code protection is disabled
#pragma config BOREN = ON    // Brown-out Reset Enable->Brown-out Reset enabled
#pragma config CLKOUTEN = OFF    // Clock Out Enable->CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
#pragma config IESO = OFF    // Internal/External Switchover->Internal/External Switchover mode is disabled
#pragma config FCMEN = OFF    // Fail-Safe Clock Monitor Enable->Fail-Safe Clock Monitor is disabled

// CONFIG2
#pragma config WRT = OFF    // Flash Memory Self-Write Protection->Write protection off
#pragma config PLLEN = ON    // PLL Enable->4x PLL enabled
#pragma config STVREN = ON    // Stack Overflow/Underflow Reset Enable->Stack Overflow or Underflow will cause a Reset
#pragma config BORV = LO    // Brown-out Reset Voltage Selection->Brown-out Reset Voltage (Vbor), low trip point selected.
#pragma config LVP = OFF

int flagRXFramingError = 0;
int flagRXOverrunError = 0;
volatile unsigned char RXIN = 3;

unsigned char RX(){
    return RCREG;
}

void writeRXIN(volatile unsigned char a){
    RXIN = a;
}

void TX(unsigned char a){
    while(!PIR1bits.TXIF); // TXIF is usually 1, only 0 when busy transmitting
    TXREG = a;
}

int main(int argc, char** argv) {

    // SCS FOSC; SPLLEN disabled; IRCF 8MHz_HF; 
    OSCCON = 0xF0;
    // TUN 0; 
    OSCTUNE = 0x00;
    // Set the secondary oscillator
    // Wait for PLL to stabilize
    while(OSCSTATbits.PLLR == 0){}

    ADCON0bits.ADON = 0;
    ANSELA = 0x00;
    ANSELB = 0x00;
    ANSELC = 0x00;
    PIE1bits.ADIE = 0; // Disable ADC interrupts

    TRISCbits.TRISC5 = 1; // RX pin set to input
    TRISCbits.TRISC6 = 0; // IadjPWM pin configured as output
    TRISAbits.TRISA5 = 0; // DimPWM pin configured as output

    LATCbits.LATC6 = 1; // Max current for now until PWM written

    //UART Init
    BAUDCONbits.BRG16 = 1; // 16-bit baud generation
    TXSTAbits.BRGH = 1; // High baud rate enabled
    BAUDCONbits.ABDEN = 0; // Auto baud detect disabled

    // Baud prescaler n = [Fosc/(D*BR)] - 1
    SPBRGH = _BAUD_PRESCALER_HIGH_;
    __delay_ms(1);
    SPBRGL = _BAUD_PRESCALER_LOW_;
    __delay_ms(1);

    APFCON0bits.RXDTSEL = 1; // RX is on RC5 pin
    APFCON0bits.TXCKSEL = 0; // TX is on RB7 pin
    TXSTAbits.SYNC = 0; // Asynchronous mode
    RCSTAbits.SPEN = 1; // Serial Port Enabled
    RCSTAbits.RX9 = 0; // 8 bit reception
    TXSTAbits.TX9 = 0; // 8-bit transmission

    RCSTAbits.CREN = 1; // Receiver enabled
    TXSTAbits.TXEN = 1; // Transmitter enabled 

    PIE1bits.TXIE = 0; // Enable USART Transmitter interrupt
    PIE1bits.RCIE = 1; // Enable USART Receive interrupt
    while(PIR1bits.RCIF){
        writeRXIN(RX());
    }

    INTCONbits.PEIE = 1; // Enable peripheral interrupts
    INTCONbits.GIE = 1; // Enable global interrupts

    while(1){
        while(RXIN > 0){
            TX(RXIN);
            _PIN_DIMPWMPIN_ = 1;
            __delay_ms(100);
            _PIN_DIMPWMPIN_ = 0;
            __delay_ms(100);
            RXIN--;
        }

    } 
    // infinite loop
    // never leave this loop
    return (EXIT_SUCCESS);
} // end main

void interrupt ISR(void){

    if(PIE1bits.RCIE && PIR1bits.RCIF){ // handle RX pin interrupts
        while(PIR1bits.RCIF){
            writeRXIN(RX());
        }
        if(RCSTAbits.FERR){
            flagRXFramingError = 1;
            SPEN = 0;
            SPEN = 1;

        }
        if(RCSTAbits.OERR){
            flagRXOverrunError = 1;
            CREN = 0;
            CREN = 1;
        }
    } // end RX pin interrupt handlers

} // end ISRs*/