2017-09-04 314 views
6

我是8位PIC的固件編寫新手,可以使用我的代碼。我正在使用PIC16F1829來獲取RX命令的LED模塊。我只是試圖在RX引腳接收到特定值時開啓LED等基本設置,但甚至無法獲得該設置。PIC16F1829 UART RX中斷不能使用MPLABX和XC8編譯器

想通過中斷讓UART工作,但在主循環中甚至無法使用輪詢工作。我的中斷向量在下面的代碼中被註釋掉了。

RX引腳:RC5

TX引腳:RB7

引腳來開啓和關閉切換LED:RA5

引腳RA5工作正常打開和關閉切換LED。 TX引腳工作,雖然我還沒有確認如果中斷TXIF也不像RCIF不工作一樣工作。

我試過閱讀RCIF和PIR1bits.RCIF。他們都編譯了。兩人都沒有工作。我已經在兩個不同的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*/ 
+1

寫入寄存器之間的5ms延遲是完全無意義的。 5毫秒即使在蹩腳的PIC上也是永恆的。這些是片上存儲器映射寄存器 - 你根本不需要任何延遲。 (在某些特殊情況下,在爲端口I/O設置數據方向後,您可能需要等待幾個時鐘週期,但這只是關於它。)首先刪除所有5ms延遲。 – Lundin

+1

(作爲未來參考的一個便箋:雖然這個問題沒有問題,但是如果您有關於特定硬件外設的問題,最好在https://electronics.stackexchange.com/上詢問,例如在某些PIC上的UART,潛伏在該站點上的許多PIC專家以及微控制器固件問題也完全在此處討論)。 – Lundin

+0

儘管如此,我不得不贊成這一點,只是爲了'已經在示波器上確認信號進入RX並且使用相同波特率在TX引腳之外,所以我認爲波特率配置正確(300波特,8N1)。我還在示波器RX引腳上接收到強烈乾淨的5V信號。我們得到了太多的嵌入式問題,絕對沒有任何硬件工作的跡象,因此必須立即下載/關閉/刪除投票,因爲軟件問答網站本質上是無法解析的;( –

回答

0

解決了這個問題

我不知道究竟解決了這個問題,但我會分享我做了重大的變化和新的代碼。

  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*/ 
+0

我想你現在正在清除外設的任何未決錯誤(通過重置外設),然後接收器再次開始工作...... –

0

某些微控制器停止接收字節,如果存在某種錯誤。一定要清除這些錯誤。通常通過清除一些UART控制寄存器位。