找回密码
 立即注册

QQ登录

只需一步,快速开始

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

3线USART程序,MPLAB

[复制链接]
跳转到指定楼层
楼主
3线USART程序,MPLAB


源程序如下:
  1. /**********************************************************************
  2. * ?2008 Microchip Technology Inc.
  3. *
  4. * FileName:        main.c
  5. * Dependencies:    uart2.h
  6. * Processor:       PIC24F
  7. * Compiler:        MPLAB?C30
  8. * Tested On:           PIC24F Explorer 16
  9. *
  10. * SOFTWARE LICENSE AGREEMENT:
  11. * Microchip Technology Incorporated ("Microchip") retains all ownership and
  12. * intellectual property rights in the code accompanying this message and in all
  13. * derivatives hereto.  You may use this code, and any derivatives created by
  14. * any person or entity by or on your behalf, exclusively with Microchip's
  15. * proprietary products.  Your acceptance and/or use of this code constitutes
  16. * agreement to the terms and conditions of this notice.
  17. *
  18. * CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP "AS IS".  NO
  19. * WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
  20. * TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
  21. * PARTICULAR PURPOSE APPLY TO THIS CODE, ITS INTERACTION WITH MICROCHIP'S
  22. * PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
  23. *
  24. * YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE LIABLE, WHETHER
  25. * IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY),
  26. * STRICT LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL,
  27. * PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR EXPENSE OF
  28. * ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN
  29. * ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
  30. * ALLOWABLE BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO
  31. * THIS CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP SPECIFICALLY TO
  32. * HAVE THIS CODE DEVELOPED.
  33. *
  34. * You agree that you are solely responsible for testing the code and
  35. * determining its suitability.  Microchip has no obligation to modify, test,
  36. * certify, or support the code.
  37. *
  38. * REVISION HISTORY:
  39. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40. * Author            Date              Comments on this revision
  41. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  42. * Albert Z.         12/26/08    Original Release
  43. *
  44. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  45. * ADDITIONAL NOTES:
  46. * Small, bare bones program to guide designers with UART implementation
  47. * To run the example, plug DB9 cable to Terminal (9600,8,N,1) -
  48. * Data sent from TERMINAL will be represented as binary equivalent
  49. * of ASCII on 7 LSB LEDs of Explorer 16 development board
  50. *
  51. * Data is sent from Explorer 16 by pressing S3 - S6.  A single letter will
  52. * be transmitted upon each keypress.
  53. *
  54. * Peripheral Library was not utilized, Bit Addressing was used in order
  55. * to show all details involved in initialization of UART.
  56. *
  57. * This code example has been tested on Explorer 16 Development Board
  58. * with PIC24FJ128GA010, PIC24FJ256GA110 and PIC24FJ256GB110 PIMs.
  59. **********************************************************************/
  60. #include "p24fxxxx.h"
  61. #include "uart2.h"

  62. #if defined (__PIC24FJ256GB110__)        //Defined by MPLAB when using 24FJ256GB110 device
  63.     _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
  64.     _CONFIG2( IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI & PLLDIV_DIV2 & IOL1WAY_ON)
  65.     _CONFIG3( WPCFG_WPCFGDIS & WPDIS_WPDIS)                //Disable erase/write protect of all memory regions.
  66. #elif defined (__PIC24FJ256GA110__)        //Defined by MPLAB when using 24FJ256GA110 device
  67.     _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2 )
  68.     _CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI & IOL1WAY_ON)
  69.     _CONFIG3( WPCFG_WPCFGDIS & WPDIS_WPDIS)                //Disable erase/write protect of all memory regions.
  70. #elif defined (__PIC24FJ128GA010__)
  71.         // JTAG/Code Protect/Write Protect/Clip-on Emulation mode
  72.         // Watchdog Timer/ICD pins select
  73.         _CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
  74.         // Disable CLK switch and CLK monitor, OSCO or Fosc/2, HS oscillator,
  75.         // Primary oscillator
  76.         _CONFIG2(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI)
  77. #endif

  78. #define TRUE        1
  79. #define FALSE        0

  80. unsigned char S3Flag, S4Flag, S5Flag, S6Flag;

  81. void __attribute__ ((interrupt, no_auto_psv)) _U2RXInterrupt(void) {
  82.         LATA = U2RXREG;
  83.         IFS1bits.U2RXIF = 0;
  84. }
  85. void __attribute__ ((interrupt, no_auto_psv)) _U2TXInterrupt(void) {
  86.         IFS1bits.U2TXIF = 0;
  87. }


  88. void InitUART2(void)
  89. {
  90.         // This is an EXAMPLE, so brutal typing goes into explaining all bit sets

  91.         // The Explorer 16 board has a DB9 connector wired to UART2, so we will
  92.         // be configuring this port only

  93.         // configure U2MODE
  94.         U2MODEbits.UARTEN = 0;        // Bit15 TX, RX DISABLED, ENABLE at end of func
  95.         U2MODEbits.USIDL = 0;        // Bit13 Continue in Idle
  96.         U2MODEbits.IREN = 0;        // Bit12 No IR translation
  97.         U2MODEbits.RTSMD = 0;        // Bit11 Simplex Mode
  98.         U2MODEbits.UEN = 0;                // Bits8,9 TX,RX enabled, CTS,RTS not
  99.         U2MODEbits.WAKE = 0;        // Bit7 No Wake up (since we don't sleep here)
  100.         U2MODEbits.LPBACK = 0;        // Bit6 No Loop Back
  101.         U2MODEbits.ABAUD = 0;        // Bit5 No Autobaud (would require sending '55')
  102.         U2MODEbits.RXINV = 0;        // Bit4 IdleState = 1
  103.         U2MODEbits.BRGH = 0;        // Bit3 16 clocks per bit period
  104.         U2MODEbits.PDSEL = 0;        // Bits1,2 8bit, No Parity
  105.         U2MODEbits.STSEL = 0;        // Bit0 One Stop Bit
  106.        
  107.         U2BRG = BAUDRATEREG2;        // baud rate

  108.         // Load all values in for U1STA SFR
  109.         U2STAbits.UTXISEL1 = 0;        //Bit15 Int when Char is transferred (1/2 config!)
  110.         U2STAbits.UTXINV = 0;        //Bit14 N/A, IRDA config
  111.         U2STAbits.UTXISEL0 = 0;        //Bit13 Other half of Bit15
  112.         U2STAbits.UTXBRK = 0;        //Bit11 Disabled
  113.         U2STAbits.UTXEN = 0;        //Bit10 TX pins controlled by periph
  114.         U2STAbits.UTXBF = 0;        //Bit9 *Read Only Bit*
  115.         U2STAbits.TRMT = 0;                //Bit8 *Read Only bit*
  116.         U2STAbits.URXISEL = 0;        //Bits6,7 Int. on character recieved
  117.         U2STAbits.ADDEN = 0;        //Bit5 Address Detect Disabled
  118.         U2STAbits.RIDLE = 0;        //Bit4 *Read Only Bit*
  119.         U2STAbits.PERR = 0;                //Bit3 *Read Only Bit*
  120.         U2STAbits.FERR = 0;                //Bit2 *Read Only Bit*
  121.         U2STAbits.OERR = 0;                //Bit1 *Read Only Bit*
  122.         U2STAbits.URXDA = 0;        //Bit0 *Read Only Bit*

  123.         IFS1bits.U2TXIF = 0;        // Clear the Transmit Interrupt Flag
  124.         IEC1bits.U2TXIE = 1;        // Enable Transmit Interrupts
  125.         IFS1bits.U2RXIF = 0;        // Clear the Recieve Interrupt Flag
  126.         IEC1bits.U2RXIE = 1;        // Enable Recieve Interrupts

  127.         U2MODEbits.UARTEN = 1;        // And turn the peripheral on

  128.         U2STAbits.UTXEN = 1;
  129. }

  130. void InitPorts(void)
  131. {
  132.         // S3 (portD Pin 6, chosen as trigger for sending 'M' to UART)
  133.         // S6 (portD Pin 7, chosen as trigger for sending 'C' to UART)
  134.         // S5 (portA Pin 7, chosen as trigger for sending 'H' to UART)
  135.         // S4 (portD Pin 13, chosen as trigger for sending 'P' to UART)

  136.         TRISD = 0x20C0;        // D6,7,13 inputs
  137.         TRISA = 0x0080;        // only 0th bit needs be output. A7 is input
  138.         S3Flag = S4Flag = S5Flag = S6Flag = 0;        // Some Debounce Flags
  139. }

  140. void SoftwareDebounce(void)
  141. {
  142.         if(PORTDbits.RD6 == FALSE) {
  143.                 if( S3Flag == FALSE ) {
  144.                         S3Flag = TRUE;
  145.                 }
  146.         } else {
  147.                 if ( S3Flag == TRUE ) {
  148.                         U2TXREG = 'M';
  149.                         S3Flag = FALSE;
  150.                 }       
  151.         }

  152.         if(PORTDbits.RD7 == FALSE) {
  153.                 if( S6Flag == FALSE ) {
  154.                         S6Flag = TRUE;
  155.                 }
  156.         } else {
  157.                 if ( S6Flag == TRUE ) {
  158.                         U2TXREG = 'C';
  159.                         S6Flag = FALSE;
  160.                 }       
  161.         }

  162.         if(PORTAbits.RA7 == FALSE) {
  163.                 if( S5Flag == FALSE ) {
  164.                         S5Flag = TRUE;
  165.                 }
  166.         } else {
  167.                 if ( S5Flag == TRUE ) {
  168.                         U2TXREG = 'H';
  169.                         S5Flag = FALSE;
  170.                 }       
  171.         }

  172.         if(PORTDbits.RD13 == FALSE) {
  173.                 if( S4Flag == FALSE ) {
  174.                         S4Flag = TRUE;
  175.                 }
  176.         } else {
  177.                 if ( S4Flag == TRUE ) {
  178.                         U2TXREG = 'P';
  179.                         S4Flag = FALSE;
  180.                 }       
  181.         }
  182. }

  183. int main(void)
  184. {
  185.         unsigned char i;
  186.         // Disable Watch Dog Timer
  187.         RCONbits.SWDTEN = 0;
  188.        
  189.         // I/O remap, PPS
  190. #if defined (__PIC24FJ256GB110__) || defined (__PIC24FJ256GA110__)
  191.         // remap pins before intialising SPI2
  192.         // Unlock Registers
  193.         __builtin_write_OSCCONL(OSCCON & 0xbf);
  194.         // Configure Input Functions **********************
  195.         // Assign UART2RX To Pin RP10
  196.         RPINR19bits.U2RXR = 10;
  197.         // Configure Output Functions *********************
  198.         // Assign UART2TX To Pin RP17
  199.         RPOR8bits.RP17R = U2TX_IO;
  200.         // Lock Registers
  201.         __builtin_write_OSCCONL(OSCCON | 0x40);
  202. #endif


  203.         InitUART2();        // Initialize UART2 for 9600,8,N,1 TX/RX

  204.         InitPorts();        // LEDs outputs, Switches Inputs

  205.         while(1) {        // The ever versatile Infinite Loop!
  206.                 for (i = 0; i < 100; i++);
  207.                 SoftwareDebounce();
  208.         }
  209. }
复制代码

所有资料51hei提供下载:
3wire-uart.zip (7.5 KB, 下载次数: 5)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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