我正在研究 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'