为啥我的串口发送数据正常,但是接收不到数据。刚上电时blueled闪了一下,好像可以接收到数据,但是后来通过pc机
串口调试工具发数据,单片机就是收不到,灯也不闪,谢谢指教!
#include <p24Fxxxx.h>;
#define INPUT (1)
#define OUTPUT (0)
#define LOW (0)
#define HIGH (1)
#define TRUE (1)
#define FALSE (1)
#define REDLED_DIR TRISCbits.TRISC6
#define REDLED_INPIN PORTCbits.RC6
#define REDLED_OUTPIN LATCbits.LATC6
#define REDLED_ON() REDLED_OUTPIN=HIGH
#define REDLED_OFF() REDLED_OUTPIN=LOW
#define BLUELED_DIR TRISCbits.TRISC7
#define BLUELED_INPIN PORTCbits.RC7
#define BLUELED_OUTPIN LATCbits.LATC7
#define BLUELED_ON() BLUELED_OUTPIN=HIGH
#define BLUELED_OFF() BLUELED_OUTPIN=LOW
#define SYSCLK 8000000
#define BAUDRATE 9600
#define BAUDRATEREG (SYSCLK/32/BAUDRATE-1)
unsigned char UartReceiveDataFlag=0;
unsigned char UartReceiveData;
void Delay1mS(unsigned int n);
void Delay(void);
void Delay1mS(unsigned int n)
{
int i;
unsigned int k;
for (k=0;k<n;k++)
{
for(i=0;i<256;i++)
;
}
}
void Delay(void)
{
//int i;
unsigned int k;
for (k=0;k<5000;k++)
{
;
}
}
void InitUart1(void) //串口0初始化
{
TRISBbits.TRISB0=INPUT; //B0脚为RX
TRISBbits.TRISB1=OUTPUT; //B1脚为TX
IPC2bits.U1RXIP2=1;
IPC2bits.U1RXIP1=0;
IPC2bits.U1RXIP0=0;
// Unlock Registers
__builtin_write_OSCCONL(OSCCON & 0xbf);
// Configure Input Functions **********************
// Assign UART0RX To Pin RP0
RPINR18bits.U1RXR=0;
// Configure Output Functions *********************
// Assign UART0TX To Pin RP1
RPOR0bits.RP1R=3;
// Lock Registers
__builtin_write_OSCCONL(OSCCON | 0x40);
//RPINR18bits.U1RXR=0;
//RPOR0bits.RP1R=3;
// configure U1MODE
U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func
U1MODEbits.USIDL = 0; // Bit13 Continue in Idle
U1MODEbits.IREN = 0; // Bit12 No IR translation
U1MODEbits.RTSMD = 0; // Bit11 Simplex Mode
U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U1MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
U1MODEbits.LPBACK = 0; // Bit6 No Loop Back
U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U1MODEbits.RXINV = 0; // Bit4 IdleState = 1
U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U1MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U1MODEbits.STSEL = 0; // Bit0 One Stop Bit
U1BRG = BAUDRATEREG; // baud rate
// Load all values in for U1STA SFR
U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U1STAbits.UTXINV = 0; //Bit14 N/A, IRDA config
U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
U1STAbits.UTXBRK = 0; //Bit11 Disabled
U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U1STAbits.UTXBF = 0; //Bit9 *Read Only Bit*
U1STAbits.TRMT = 0; //Bit8 *Read Only bit*
U1STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved
U1STAbits.ADDEN = 0; //Bit5 Address Detect Disabled
U1STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
U1STAbits.PERR = 0; //Bit3 *Read Only Bit*
U1STAbits.FERR = 0; //Bit2 *Read Only Bit*
U1STAbits.OERR = 0; //Bit1 *Read Only Bit*
U1STAbits.URXDA = 0; //Bit0 *Read Only Bit*
IFS0bits.U1TXIF = 0; // Clear the Transmit Interrupt Flag
IEC0bits.U1TXIE = 0; // Disable Transmit Interrupts
IFS0bits.U1RXIF = 0; // Clear the Recieve Interrupt Flag
IEC0bits.U1RXIE = 1; // Enable Recieve Interrupts
U1MODEbits.UARTEN = 1; // And turn the peripheral on
U1STAbits.UTXEN = 1;
}
void __attribute__ ((interrupt, no_auto_psv)) _U1RXInterrupt(void) {
//LATA = U2RXREG;
BLUELED_OUTPIN=1;
UartReceiveData = U1RXREG;
UartReceiveDataFlag = 1;
IFS0bits.U1RXIF = 0;
}
//串口1接收发送中断
void _ISR _U1TXInterrupt(void)
{
_U1TXIF = 0;
}
//发送单字符
void uart_char(unsigned char c)
{
U1TXREG = c;
while(!U1STAbits.TRMT);
}
void main(void)
{
unsigned char cc[10]={0x01,0x02,0x03,0x04,0x5,0x6,0x7,0x08,0x09,0x0a};
UartReceiveDataFlag=0;
REDLED_DIR=OUTPUT;
BLUELED_DIR=OUTPUT;
InitUart1(); //这个串口初始化
INTCON1 = 0x8000; //禁止中断嵌套
INTCON2 = 0;
IEC0 = 0x1800; //允许UART1收发中断
while(1)
{
REDLED_ON();
Delay();
Delay1mS(1000);
if (UartReceiveDataFlag==1)
{
UartReceiveDataFlag=0;
uart_char(UartReceiveData);
BLUELED_ON();
}
REDLED_OFF();
Delay1mS(1000);
BLUELED_OFF();
Delay();
}
}