USART of pic18f4550

时间:2014-03-08 17:27:31

标签: pic18 usart

我正在研究PIC18f4550。我希望它通过USART进行通信。我能够传输一个角色,但无法接收任何数据。我根据我检查了所有的SFR和r ri8。我正在使用mplab c18 v3.46编译器和MPLAB v8.40。

#include <p18f4550.h>
#include<usart.h>
#pragma config VREGEN = OFF         // Voltage regulator USB , is Suspended
#pragma config WDT = OFF                // Watchdog timer is suspended
#pragma config PLLDIV = 1                // Internal Oscillator engaged
#pragma config MCLRE = ON
#pragma config WDTPS = 32768
#pragma config CCP2MX = ON
#pragma config PBADEN = OFF
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2
#pragma config FOSC = INTOSCIO_EC
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config BORV = 3
#pragma config LPT1OSC = OFF
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config ICPRT = OFF
#pragma config XINST = OFF
#pragma config DEBUG = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CPB = OFF                                                                              
#pragma config CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTC = OFF
#pragma config WRTB = OFF
#pragma config WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF
#define a PORTD
int i,j;
unsigned char serial_data;
extern void delay(int);
extern void tx_data(unsigned char);
extern unsigned char rx_data(void);
void tx_data(unsigned char data1)
{
    TXREG=data1;                               
    while(PIR1bits.TXIF==0);                   
} 
unsigned char rx_data(void)
{
    while(PIR1bits.RCIF==0);                            // Wait until RCIF gets low
    return RCREG;                                
}
void main(void)
{
OSCCON=0x74;
TRISD= 0x00;
TRISC= 0x80;
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 12);
RCON=0x90;
INTCON=0xC0;
IPR1=0x00;
BAUDCON=0x00;
RCSTA=0x90;
tx_data('o');        // Transmit the same data back to PC
serial_data=rx_data();    // Receive data from PC
tx_data('k');       
}

我在网上找到了这个代码并进行了相应的修改。它传输'o'并且永远不会再响应'k'

1 个答案:

答案 0 :(得分:1)

脱离我的头脑......我没有使用C18编译器,但如果它像任何常规编译器一样,那么它可能是由于这样的事情:

使用USART_RX_INT_ON标志激活UART接收中断,然后在INTCON中使能GIEH / GIEL位。

但是你没有提供中断服务程序。所以我的猜测是,坐在中断向量位置(通常为PIC18的PROGRAM ADDRESS 0x08和0x18)是NOP汇编指令。因此,当接收事件发生时,它会跳转到地址0x18的高优先级中断向量(因为IPEN被使能并且IPR1被清除),然后它会溢出到下一个有效指令来自那里,因为没有GOTO指令在该向量地址处正确地使其进入特定的ISR功能,然后在发生中断事件时正确返回到最后的已知代码位置...