找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1417|回复: 2
打印 上一主题 下一主题
收起左侧

PIC24HJ系列单片机串口通信的一点问题

[复制链接]
回帖奖励 10 黑币 回复本帖可获得 10 黑币奖励! 每人限 1 次(中奖概率 80%)
跳转到指定楼层
楼主
ID:523178 发表于 2021-8-5 20:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这几天在入门PIC24HJ128GP506A单片机,之前没有接触过,在学习它的UART外设时,遇到了单片机接收上位机数据不准确的问题,单片机向上位机发送数据,上位机接收正确。下面是的我代码:
  1. // PIC24HJ128GP506A Configuration Bit Settings

  2. // 'C' source line config statements

  3. // FBS
  4. #pragma config BWRP = WRPROTECT_OFF     // Boot Segment Write Protect (Boot Segment may be written)
  5. #pragma config BSS = NO_FLASH           // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
  6. #pragma config RBS = NO_RAM             // Boot Segment RAM Protection (No Boot RAM)

  7. // FSS
  8. #pragma config SWRP = WRPROTECT_OFF     // Secure Segment Program Write Protect (Secure segment may be written)
  9. #pragma config SSS = NO_FLASH           // Secure Segment Program Flash Code Protection (No Secure Segment)
  10. #pragma config RSS = NO_RAM             // Secure Segment Data RAM Protection (No Secure RAM)

  11. // FGS
  12. #pragma config GWRP = OFF               // General Code Segment Write Protect (User program memory is not write-protected)
  13. #pragma config GSS = OFF                // General Segment Code Protection (User program memory is not code-protected)

  14. // FOSCSEL
  15. #pragma config FNOSC = PRIPLL           // Oscillator Mode (Primary Oscillator (XT, HS, EC) w/ PLL)
  16. #pragma config IESO = ON                // Two-speed Oscillator Start-Up Enable (Start up with FRC, then switch)

  17. // FOSC
  18. #pragma config POSCMD = HS              // Primary Oscillator Source (HS Oscillator Mode)
  19. #pragma config OSCIOFNC = OFF           // OSC2 Pin Function (OSC2 pin has clock out function)
  20. #pragma config FCKSM = CSECMD           // Clock Switching and Monitor (Clock switching is enabled, Fail-Safe Clock Monitor is disabled)

  21. // FWDT
  22. #pragma config WDTPOST = PS32768        // Watchdog Timer Postscaler (1:32,768)
  23. #pragma config WDTPRE = PR128           // WDT Prescaler (1:128)
  24. #pragma config WINDIS = ON              // Watchdog Timer Window (Watchdog Timer in Window mode)
  25. #pragma config FWDTEN = OFF             // Watchdog Timer Enable (Watchdog timer enabled/disabled by user software)

  26. // FPOR
  27. #pragma config FPWRT = PWR128           // POR Timer Value (128ms)

  28. // FICD
  29. #pragma config ICS = PGD1               // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
  30. #pragma config JTAGEN = ON              // JTAG Port Enable (JTAG is Enabled)

  31. // #pragma config statements should precede project file includes.
  32. // Use project enums instead of #define for ON and OFF.

  33. #include <xc.h>

  34. #define FCY         40000000
  35. #define BAUDRATE    9600
  36. #define BRGVAL      ((FCY/BAUDRATE)/16)-1
  37. unsigned int i;
  38. char RecvData;


  39. void uart_init(void)
  40. {
  41.     //Configure System Clock.
  42.     /* Fosc = Fin * M /(N1 * N2) */
  43.     PLLFBD = 18;                        //M = 20
  44.     CLKDIVbits.PLLPOST = 0;             //N1 = 2
  45.     CLKDIVbits.PLLPRE  = 0;             //N2 = 2
  46.     OSCTUN = 0;
  47.     RCONbits.SWDTEN = 0;
  48.     //Wait for PLL to lock
  49.     while(OSCCONbits.LOCK != 1);
  50.    
  51.     U1MODEbits.STSEL = 0;
  52.     U1MODEbits.PDSEL = 0;
  53.     U1MODEbits.ABAUD = 0;
  54.     U1MODEbits.BRGH  = 0;
  55.    
  56.    
  57.     U1BRG = BRGVAL;                          //波特率9600
  58.    
  59.    
  60.     U1STAbits.UTXISEL0 = 0;
  61.     U1STAbits.URXISEL = 0;
  62.    
  63.     IEC0bits.U1TXIE = 1;
  64.     IEC0bits.U1RXIE = 1;
  65.     IPC2bits.U1RXIP = 0x02;
  66.     IPC3bits.U1TXIP = 0x01;
  67.    
  68.     U1MODEbits.UARTEN = 1;                    //使能UART
  69.     U1STAbits.UTXEN   = 1;                    //使能UART 发送
  70.    
  71.    
  72.     for(i = 0; i < 4160; i++){
  73.         Nop();
  74.     }
  75.     U1TXREG = 'a';
  76. }

  77. void led_init(void)
  78. {
  79.     TRISDbits.TRISD10 = 0;                    //设置D7端口为输出模式
  80. }


  81. int main(void)
  82. {
  83.    
  84.     uart_init();
  85.     led_init();                              //初始化LED
  86.    
  87.     while(1){
  88. //        if(U1STAbits.URXDA == 1){
  89. //            RecvData = U1RXREG;
  90. //            U1TXREG = RecvData;
  91. //        }
  92.         if(RecvData == 'H'){
  93.             LATDbits.LATD10 = 0;
  94.         }else if(RecvData == 'N'){
  95.             LATDbits.LATD10 = 1;
  96.         }
  97.     }
  98.    
  99.     return 0;
  100.    
  101. }

  102. void __attribute__((__interrupt__, __no_auto_psv__)) _U1TXInterrupt(void)
  103. {
  104.    
  105.     IFS0bits.U1TXIF = 0;
  106. }


  107. void __attribute__((__interrupt__, __no_auto_psv__)) _U1RXInterrupt(void)
  108. {
  109.     if(U1STAbits.URXDA == 1){
  110.         RecvData = U1RXREG;
  111.     }
  112.     IFS0bits.U1RXIF = 0;
  113. }
复制代码
可以在主函数中看到,我判断接收的到的字符是否是“H”或“N”,其实这是我在DEBUG时发现的,原来我判断的是“o”和“c” open & close, 但是实际RecvData的值为
“H”或“N”。实在是不知道问题出哪里了,有懂的大神帮忙看看。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:81196 发表于 2021-8-6 10:31 | 只看该作者
比特率是不是不对,停止位和校验位呢
回复

使用道具 举报

板凳
ID:523178 发表于 2021-8-9 11:11 | 只看该作者
shumivan 发表于 2021-8-6 10:31
比特率是不是不对,停止位和校验位呢

解决了,代码没问题,线接错了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表