久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2095|回復: 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”。實在是不知道問題出哪里了,有懂的大神幫忙看看。
回復

使用道具 舉報

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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 久久久噜噜噜久久中文字幕色伊伊 | 日韩高清中文字幕 | 国产黄色免费网站 | 日韩精品一区二区三区中文字幕 | 亚洲成人高清 | 好姑娘影视在线观看高清 | 九九热视频这里只有精品 | 成人在线观看网址 | 国产成人精品久久 | 国产精品久久久久永久免费观看 | 成人免费一级 | 午夜理伦三级理论三级在线观看 | 久久r免费视频 | 国产精品视频一 | 久久午夜剧场 | 成人欧美一区二区三区在线播放 | 日本不卡免费新一二三区 | 精产国产伦理一二三区 | 精品九九久久 | 国产成人精品一区二区三区视频 | 在线看亚洲 | 国产精品91久久久久久 | 国产羞羞视频在线观看 | 亚洲97 | 国产成人网 | 久久精品青青大伊人av | 亚洲成人动漫在线观看 | 亚洲精品久久久久中文字幕欢迎你 | 色婷婷一区二区三区四区 | 亚洲成人精选 | 亚洲欧美一区二区三区在线 | 国产一区二区不卡 | 天天操操操操操 | 亚洲精品日本 | 亚洲一一在线 | 国产精品久久久久久久久免费樱桃 | 久久久.com | 成人亚洲精品 | 中文字幕第二区 | 91久久综合 | 精品美女在线观看 |