我刚开始为8位PIC编写固件,可以对我的代码使用一些帮助。我正在将PIC16F1829用于获取RX命令的LED模块。我只是在尝试获取基本设置,例如在RX引脚上收到某个值时打开LED,但甚至无法做到这一点。

希望通过中断使UART工作,但甚至无法使其与主循环中的轮询一起工作。我的中断 vector 在下面的代码中被注释掉了。

RX脚:RC5

TX引脚:RB7

通过引脚切换LED的开和关:RA5

RA5引脚可以正常工作,以打开和关闭LED。 TX引脚正常工作,尽管我还不确定中断TXIF是否也无法正常工作,例如RCIF是否无效。

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

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

我已经在示波器上确认使用相同的波特率将信号输入到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*/

最佳答案

如果发生某种错误,某些微 Controller 将停止接收字节。确保清除这些错误。通常通过清除一些UART控制寄存器位来实现。

关于c - PIC16F1829 UART RX中断在使用MPLABX和XC8编译器时不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46030610/

10-12 15:59