PIC24F16KL402开发板使用笔记三: 串口1控制继电器

本程序使用了UART1的发送和接收中断,分享给大家参考希望对大家有一点帮助。下载后,在上位机使用SecureCRT or 串口调试助手调试效果很好,很稳定。

  1. #include "xc.h"
  2. //#include <string.h>
  3. #include <stdlib.h>
  4. _FBS
  5. (
  6.     BWRP_OFF &      // Boot Segment Write Protect (Disabled)
  7.     BSS_OFF         // Boot segment Protect (No boot flash segment)
  8. )
  9. //_FGS
  10. //(
  11. //    GWRP_OFF &      // General Segment Flash Write Protect (General segment may be written)
  12. //    GSS0_OFF        // General Segment Code Protect (No Protection)
  13. //)
  14. _FOSCSEL
  15. (
  16.     FNOSC_FRC &  // Oscillator Select (8MHz FRC with Postscaler (FRCDIV))
  17.   //  SOSCSRC_ANA &   // SOSC Source Type (Analog Mode for use with crystal)
  18.   //  LPRCSEL_HP &    // LPRC Power and Accuracy (High Power/High Accuracy)
  19.     IESO_ON         // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))
  20. )
  21. _FOSC
  22. (
  23.    // POSCMD_HS &   // Primary Oscillator Mode (Primary oscillator disabled)
  24.     OSCIOFNC_ON &   // CLKO Enable Configuration bit (CLKO output signal enabled)
  25.     POSCFREQ_MS &   // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock frequency between 100kHz to 8MHz)
  26.     SOSCSEL_SOSCHP &// SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
  27.     FCKSM_CSECME    // Clock Switching and Monitor Selection (Clock Switching and Fail-safe Clock Monitor Enabled)
  28. )
  29. _FWDT
  30. (
  31.     WDTPS_PS32768 & // Watchdog Timer Postscale Select bits (1:32768)
  32.     FWPSA_PR128 &   // WDT Prescaler bit (WDT prescaler ratio of 1:128)
  33.     FWDTEN_OFF &    // Watchdog Timer Enable bits (WDT disabled in hardware; SWDTEN bit disabled)
  34.     WINDIS_OFF      // Windowed Watchdog Timer Disable bit (Standard WDT selected (windowed WDT disabled))
  35. )
  36. // Warning:
  37. // Always enable MCLRE_ON config bit setting so that the MCLR pin function will
  38. // work for low-voltage In-Circuit Serial Programming (ICSP). The Microstick
  39. // programming circuitry only supports low-voltage ICSP. If you disable MCLR pin
  40. // functionality, a high-voltage ICSP tool will be required to re-program the
  41. // part in the future.
  42. _FPOR
  43. (
  44.     BOREN_BOR3 &    // Brown-out Reset Enable bits (Enabled in hardware; SBOREN bit disabled)
  45.     PWRTEN_ON &     // Power-up Timer Enable (PWRT enabled)
  46.     I2C1SEL_PRI &   // Alternate I2C1 Pin Mapping bit (Default SCL1/SDA1 Pins for I2C1)
  47.     BORV_V18 &      // Brown-out Reset Voltage bits (Brown-out Reset at 1.8V)
  48.     MCLRE_ON        // MCLR Pin Enable bit (RA5 input disabled; MCLR enabled)
  49. )
  50. _FICD
  51. (
  52.     ICS_PGx3        // ICD Pin Placement Select (EMUC/EMUD share PGC3/PGD3)
  53. )
  54. #define FP 4000000
  55. #define BAUDRATE 9600
  56. #define BRGVAL ((FP/BAUDRATE)/16) - 1
  57. char ReceivedChar;
  58. void delay(unsigned int uiCnt)
  59. {
  60.     int i,j;
  61.     for(i=0;i<uiCnt;i++)
  62.         for(j=0;j<110;j++);
  63. }
  64. int main(void)
  65. {
  66.     TRISAbits.TRISA0 = 0;
  67.     TRISAbits.TRISA1 = 0;
  68.     TRISAbits.TRISA2 = 0;
  69.     TRISAbits.TRISA3 = 0;
  70.     TRISBbits.TRISB3 = 0;
  71.     
  72.     ANSBbits.ANSB2 = 0;
  73.     TRISBbits.TRISB2 = 1;
  74.     
  75.     U1MODEbits.UARTEN = 0;
  76.     U1MODEbits.UEN0 = 0;
  77.     U1MODEbits.UEN1 = 0;
  78.     
  79.     U1MODEbits.STSEL = 0; // 1-stop bit
  80.     U1MODEbits.PDSEL = 0; // No Parity, 8-data bits
  81.     U1MODEbits.ABAUD = 0; // Auto-Baud disabled
  82.     U1MODEbits.BRGH = 0; // Standard-Speed mode
  83.     U1BRG = BRGVAL;
  84.     //U1BRG = 103;
  85.     U1STAbits.URXISEL = 0; // Interrupt after one RX character is received;
  86.     
  87.     U1STAbits.UTXISEL0 = 0;
  88.     U1STAbits.UTXISEL1 = 0;
  89.     
  90.     U1STAbits.URXISEL0 = 1;
  91.     U1STAbits.URXISEL1 = 1;
  92.     
  93.     IEC0bits.U1TXIE = 1;
  94.     IFS0bits.U1TXIF = 1;
  95.     
  96.     IEC0bits.U1RXIE = 1;
  97.     IFS0bits.U1RXIF = 1;
  98.     
  99.     U1MODEbits.UARTEN = 1; // Enable UART
  100.     U1STAbits.UTXEN = 1;
  101.     
  102.     U1TXREG = 'H';
  103.     
  104.     while(1)
  105.     {
  106.         delay(100);
  107.         LATAbits.LATA0 ^= 1;
  108.         //PORTB3接一个继电器
  109.         switch(ReceivedChar)
  110.         {
  111.             case 'o':
  112.                 LATBbits.LATB3 = 1;
  113.                 break;
  114.             case 'p':
  115.                 LATBbits.LATB3 = 0;
  116.                 break;
  117.         }
  118.     }
  119. }
  120. void __attribute__((interrupt, shadow, no_auto_psv)) _U1TXInterrupt(void)
  121. {
  122.     IFS0bits.U1TXIF = 0; // Clear TX Interrupt flag
  123.     //U1TXREG = 'a'; // Transmit one character
  124. }
  125. void __attribute__((interrupt, shadow, no_auto_psv)) _U1RXInterrupt(void)
  126. {
  127.     IFS0bits.U1RXIF = 0; // Clear TX Interrupt flag
  128.     //U1TXREG = 'a'; // Transmit one character
  129.     LATBbits.LATB3 ^= 1; 
  130.     /* Get the data */
  131.     if(U1STAbits.URXDA == 1)
  132.     {
  133.         ReceivedChar = U1RXREG;
  134.         U1TXREG = ReceivedChar;
  135.     }    
  136. }
粽子糖果 发表于10-26 10:41 浏览65535次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

粽子糖果
粽子糖果(总统)
金币:41623个|学分:51975个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号