标题:
PIC24HJ系列单片机串口通信的一点问题
[打印本页]
作者:
jkla
时间:
2021-8-5 20:26
标题:
PIC24HJ系列单片机串口通信的一点问题
这几天在入门PIC24HJ128GP506A单片机,之前没有接触过,在学习它的UART外设时,遇到了单片机接收上位机数据不准确的问题,单片机向上位机发送数据,上位机接收正确。下面是的我代码:
// PIC24HJ128GP506A Configuration Bit Settings
// 'C' source line config statements
// FBS
#pragma config BWRP = WRPROTECT_OFF // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
#pragma config RBS = NO_RAM // Boot Segment RAM Protection (No Boot RAM)
// FSS
#pragma config SWRP = WRPROTECT_OFF // Secure Segment Program Write Protect (Secure segment may be written)
#pragma config SSS = NO_FLASH // Secure Segment Program Flash Code Protection (No Secure Segment)
#pragma config RSS = NO_RAM // Secure Segment Data RAM Protection (No Secure RAM)
// FGS
#pragma config GWRP = OFF // General Code Segment Write Protect (User program memory is not write-protected)
#pragma config GSS = OFF // General Segment Code Protection (User program memory is not code-protected)
// FOSCSEL
#pragma config FNOSC = PRIPLL // Oscillator Mode (Primary Oscillator (XT, HS, EC) w/ PLL)
#pragma config IESO = ON // Two-speed Oscillator Start-Up Enable (Start up with FRC, then switch)
// FOSC
#pragma config POSCMD = HS // Primary Oscillator Source (HS Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config FCKSM = CSECMD // Clock Switching and Monitor (Clock switching is enabled, Fail-Safe Clock Monitor is disabled)
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR128 // WDT Prescaler (1:128)
#pragma config WINDIS = ON // Watchdog Timer Window (Watchdog Timer in Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog timer enabled/disabled by user software)
// FPOR
#pragma config FPWRT = PWR128 // POR Timer Value (128ms)
// FICD
#pragma config ICS = PGD1 // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config JTAGEN = ON // JTAG Port Enable (JTAG is Enabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define FCY 40000000
#define BAUDRATE 9600
#define BRGVAL ((FCY/BAUDRATE)/16)-1
unsigned int i;
char RecvData;
void uart_init(void)
{
//Configure System Clock.
/* Fosc = Fin * M /(N1 * N2) */
PLLFBD = 18; //M = 20
CLKDIVbits.PLLPOST = 0; //N1 = 2
CLKDIVbits.PLLPRE = 0; //N2 = 2
OSCTUN = 0;
RCONbits.SWDTEN = 0;
//Wait for PLL to lock
while(OSCCONbits.LOCK != 1);
U1MODEbits.STSEL = 0;
U1MODEbits.PDSEL = 0;
U1MODEbits.ABAUD = 0;
U1MODEbits.BRGH = 0;
U1BRG = BRGVAL; //波特率9600
U1STAbits.UTXISEL0 = 0;
U1STAbits.URXISEL = 0;
IEC0bits.U1TXIE = 1;
IEC0bits.U1RXIE = 1;
IPC2bits.U1RXIP = 0x02;
IPC3bits.U1TXIP = 0x01;
U1MODEbits.UARTEN = 1; //使能UART
U1STAbits.UTXEN = 1; //使能UART 发送
for(i = 0; i < 4160; i++){
Nop();
}
U1TXREG = 'a';
}
void led_init(void)
{
TRISDbits.TRISD10 = 0; //设置D7端口为输出模式
}
int main(void)
{
uart_init();
led_init(); //初始化LED
while(1){
// if(U1STAbits.URXDA == 1){
// RecvData = U1RXREG;
// U1TXREG = RecvData;
// }
if(RecvData == 'H'){
LATDbits.LATD10 = 0;
}else if(RecvData == 'N'){
LATDbits.LATD10 = 1;
}
}
return 0;
}
void __attribute__((__interrupt__, __no_auto_psv__)) _U1TXInterrupt(void)
{
IFS0bits.U1TXIF = 0;
}
void __attribute__((__interrupt__, __no_auto_psv__)) _U1RXInterrupt(void)
{
if(U1STAbits.URXDA == 1){
RecvData = U1RXREG;
}
IFS0bits.U1RXIF = 0;
}
复制代码
可以在主函数中看到,我判断接收的到的字符是否是“H”或“N”,其实这是我在DEBUG时发现的,原来我判断的是“o”和“c” open & close, 但是实际RecvData的值为
“H”或“N”。实在是不知道问题出哪里了,有懂的大神帮忙看看。
作者:
shumivan
时间:
2021-8-6 10:31
比特率是不是不对,停止位和校验位呢
作者:
jkla
时间:
2021-8-9 11:11
shumivan 发表于 2021-8-6 10:31
比特率是不是不对,停止位和校验位呢
解决了,代码没问题,线接错了
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1