[DemoCode下载] M051 UART的RS485模式

[复制链接]
3384|14
 楼主| 捉虫天师 发表于 2016-7-28 19:32 | 显示全部楼层 |阅读模式


主机模式
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 7 $
  5. * $Date: 14/01/28 11:45a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 Series UART Interface Controller Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M051Series.h"


  14. #define PLL_CLOCK           50000000


  15. #define MATCH_ADDRSS1     0xC0
  16. #define MATCH_ADDRSS2     0xA2
  17. #define UNMATCH_ADDRSS1   0xB1
  18. #define UNMATCH_ADDRSS2   0xD3


  19. /*---------------------------------------------------------------------------------------------------------*/
  20. /* Define functions prototype                                                                              */
  21. /*---------------------------------------------------------------------------------------------------------*/
  22. extern char GetChar(void);
  23. int32_t main(void);
  24. void RS485_SendAddressByte(uint8_t u8data);
  25. void RS485_SendDataByte(uint8_t *pu8TxBuf, uint32_t u32WriteBytes);
  26. void RS485_9bitModeMaster(void);


  27. /*---------------------------------------------------------------------------------------------------------*/
  28. /*  RS485 Transmit Control  (Address Byte: Parity Bit =1 , Data Byte:Parity Bit =0)                        */
  29. /*---------------------------------------------------------------------------------------------------------*/
  30. void RS485_SendAddressByte(uint8_t u8data)
  31. {
  32.     /* Set UART parity as MARK and ship baud rate setting */
  33.     UART_SetLine_Config(UART1, 0, UART_WORD_LEN_8, UART_PARITY_MARK, UART_STOP_BIT_1);

  34.     /* Send data */
  35.     UART_WRITE(UART1, u8data);
  36. }

  37. void RS485_SendDataByte(uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
  38. {
  39.     /* Set UART parity as SPACE and ship baud rate setting */
  40.     UART_SetLine_Config(UART1, 0, UART_WORD_LEN_8, UART_PARITY_SPACE, UART_STOP_BIT_1);

  41.     /* Send data */
  42.     UART_Write(UART1, pu8TxBuf, u32WriteBytes);
  43. }

  44. /*---------------------------------------------------------------------------------------------------------*/
  45. /*  RS485 Transmit Test                                                                                    */
  46. /*---------------------------------------------------------------------------------------------------------*/
  47. void RS485_9bitModeMaster()
  48. {
  49.     int32_t i32;
  50.     uint8_t g_u8SendDataGroup1[10] = {0};
  51.     uint8_t g_u8SendDataGroup2[10] = {0};
  52.     uint8_t g_u8SendDataGroup3[10] = {0};
  53.     uint8_t g_u8SendDataGroup4[10] = {0};

  54.     printf("\n");
  55.     printf("+-----------------------------------------------------------+\n");
  56.     printf("|     Pin Configure                                         |\n");
  57.     printf("+-----------------------------------------------------------+\n");
  58.     printf("|    _______                                    _______     |\n");
  59.     printf("|   |       |                                  |       |    |\n");
  60.     printf("|   |Master |---TXD1(P1.3) <====> RXD1(P1.2)---| Slave |    |\n");
  61.     printf("|   |       |---RTS1(P0.1) <====> RTS1(P0.1)---|       |    |\n");
  62.     printf("|   |_______|                                  |_______|    |\n");
  63.     printf("|                                                           |\n");
  64.     printf("+-----------------------------------------------------------+\n");

  65.     /*
  66.         The sample code is used to test RS485 9-bit mode and needs
  67.         two Module test board to complete the test.
  68.         Master:
  69.             1.Set AUD mode and HW will control RTS pin. LEV_RTS is set to '0'.
  70.             2.Master will send four different address with 10 bytes data to test Slave.
  71.             3.Address bytes : the parity bit should be '1'. (Set UA_LCR = 0x2B)
  72.             4.Data bytes : the parity bit should be '0'. (Set UA_LCR = 0x3B)
  73.             5.RTS pin is low in idle state. When master is sending,
  74.               RTS pin will be pull high.

  75.         Slave:
  76.             1.Set AAD and AUD mode firstly. LEV_RTS is set to '0'.
  77.             2.The received byte, parity bit is '1' , is considered "ADDRESS".
  78.             3.The received byte, parity bit is '0' , is considered "DATA".  (Default)
  79.             4.AAD: The slave will ignore any data until ADDRESS match ADDR_MATCH value.
  80.               When RLS and RDA interrupt is happened,it means the ADDRESS is received.
  81.               Check if RS485_ADD_DETF is set and read UA_RBR to clear ADDRESS stored in rx_fifo.

  82.               NMM: The slave will ignore data byte until disable RX_DIS.
  83.               When RLS and RDA interrupt is happened,it means the ADDRESS is received.
  84.               Check the ADDRESS is match or not by user in UART_IRQHandler.
  85.               If the ADDRESS is match,clear RX_DIS bit to receive data byte.
  86.               If the ADDRESS is not match,set RX_DIS bit to avoid data byte stored in FIFO.
  87.     */

  88.     printf("\n");
  89.     printf("+-----------------------------------------------------------+\n");
  90.     printf("|           RS485  Function Test (9-bit Master)             |\n");
  91.     printf("+-----------------------------------------------------------+\n");
  92.     printf("| The function will send different address with 10 data bytes|\n");
  93.     printf("| to test RS485 9-bit mode. Please connect TX/RX to another |\n");
  94.     printf("| board and wait its ready to receive.                      |\n");
  95.     printf("| Press any key to start...                                 |\n");
  96.     printf("+-----------------------------------------------------------+\n\n");
  97.     GetChar();

  98.     /* Set RS485-Master as AUD mode */
  99.     /* Enable AUD mode to HW control RTS pin automatically */
  100.     /* You also can use GPIO to control RTS pin for replacing AUD mode*/
  101.     UART_SelectRS485Mode(UART1, UART_ALT_CSR_RS485_AUD_Msk, 0);

  102.     /* Set RTS pin active level as low level active */
  103.     UART1->MCR &= ~UART_MCR_LEV_RTS_Msk;
  104.     UART1->MCR |= UART_RTS_IS_LOW_LEV_ACTIVE;

  105.     /* Prepare Data to transmit*/
  106.     for(i32 = 0; i32 < 10; i32++)
  107.     {
  108.         g_u8SendDataGroup1[i32] = i32;
  109.         g_u8SendDataGroup2[i32] = i32 + 10;
  110.         g_u8SendDataGroup3[i32] = i32 + 20;
  111.         g_u8SendDataGroup4[i32] = i32 + 30;
  112.     }

  113.     /* Send For different Address and data for test */
  114.     printf("Send Address %x and data 0~9\n", MATCH_ADDRSS1);
  115.     RS485_SendAddressByte(MATCH_ADDRSS1);
  116.     RS485_SendDataByte(g_u8SendDataGroup1, 10);

  117.     printf("Send Address %x and data 10~19\n", UNMATCH_ADDRSS1);
  118.     RS485_SendAddressByte(UNMATCH_ADDRSS1);
  119.     RS485_SendDataByte(g_u8SendDataGroup2, 10);

  120.     printf("Send Address %x and data 20~29\n", MATCH_ADDRSS2);
  121.     RS485_SendAddressByte(MATCH_ADDRSS2);
  122.     RS485_SendDataByte(g_u8SendDataGroup3, 10);

  123.     printf("Send Address %x and data 30~39\n", UNMATCH_ADDRSS2);
  124.     RS485_SendAddressByte(UNMATCH_ADDRSS2);
  125.     RS485_SendDataByte(g_u8SendDataGroup4, 10);

  126.     printf("\nTransfer Done\n");

  127. }

  128. void SYS_Init(void)
  129. {
  130.     /*---------------------------------------------------------------------------------------------------------*/
  131.     /* Init System Clock                                                                                       */
  132.     /*---------------------------------------------------------------------------------------------------------*/

  133.     /* Enable Internal RC 22.1184MHz clock */
  134.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  135.     /* Waiting for Internal RC clock ready */
  136.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  137.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  138.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  139.     /* Enable external XTAL 12MHz clock */
  140.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  141.     /* Waiting for external XTAL clock ready */
  142.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  143.     /* Set core clock as PLL_CLOCK from PLL */
  144.     CLK_SetCoreClock(PLL_CLOCK);

  145.     /* Enable UART module clock */
  146.     CLK_EnableModuleClock(UART0_MODULE);
  147.     CLK_EnableModuleClock(UART1_MODULE);

  148.     /* Select UART module clock source */
  149.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
  150.     CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  151.     /*---------------------------------------------------------------------------------------------------------*/
  152.     /* Init I/O Multi-function                                                                                 */
  153.     /*---------------------------------------------------------------------------------------------------------*/

  154.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  155.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  156.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);

  157.     /* Set P1 multi-function pins for UART1 RXD and TXD */
  158.     SYS->P1_MFP &= ~(SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
  159.     SYS->P1_MFP |= (SYS_MFP_P12_RXD1 | SYS_MFP_P13_TXD1);

  160.     /* Set P0 multi-function pins for UART1 RTS */
  161.     SYS->P0_MFP = SYS->P0_MFP & (~SYS_MFP_P01_Msk) | SYS_MFP_P01_RTS1;


  162. }

  163. void UART0_Init()
  164. {
  165.     /*---------------------------------------------------------------------------------------------------------*/
  166.     /* Init UART                                                                                               */
  167.     /*---------------------------------------------------------------------------------------------------------*/
  168.     /* Reset UART0 module */
  169.     SYS_ResetModule(UART0_RST);

  170.     /* Configure UART0 and set UART0 Baudrate */
  171.     UART_Open(UART0, 115200);
  172. }

  173. void UART1_Init()
  174. {
  175.     /*---------------------------------------------------------------------------------------------------------*/
  176.     /* Init UART                                                                                               */
  177.     /*---------------------------------------------------------------------------------------------------------*/
  178.     /* Reset UART1 module */
  179.     SYS_ResetModule(UART1_RST);

  180.     /* Configure UART1 and set UART1 Baudrate */
  181.     UART_Open(UART1, 115200);
  182. }

  183. /*---------------------------------------------------------------------------------------------------------*/
  184. /* MAIN function                                                                                           */
  185. /*---------------------------------------------------------------------------------------------------------*/
  186. int main(void)
  187. {
  188.     /* Unlock protected registers */
  189.     SYS_UnlockReg();

  190.     /* Init System, IP clock and multi-function I/O */
  191.     SYS_Init();

  192.     /* Lock protected registers */
  193.     SYS_LockReg();

  194.     /* Init UART0 for printf */
  195.     UART0_Init();

  196.     /* Init UART1 for testing */
  197.     UART1_Init();

  198.     /*---------------------------------------------------------------------------------------------------------*/
  199.     /* SAMPLE CODE                                                                                             */
  200.     /*---------------------------------------------------------------------------------------------------------*/

  201.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

  202.     printf("\n\nUART Sample Program\n");

  203.     /* UART RS485 sample master function */
  204.     RS485_9bitModeMaster();

  205.     while(1);

  206. }


 楼主| 捉虫天师 发表于 2016-7-28 19:32 | 显示全部楼层
从机模式
  1. /****************************************************************************
  2. * @file     main.c
  3. * @version  V1.00
  4. * $Revision: 7 $
  5. * $Date: 14/01/29 2:05p $
  6. * @brief    M051 Series UART Interface Controller Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M051Series.h"


  14. #define PLL_CLOCK           50000000


  15. #define IS_USE_RS485NMM   0      //1:Select NMM_Mode , 0:Select AAD_Mode
  16. #define MATCH_ADDRSS1     0xC0
  17. #define MATCH_ADDRSS2     0xA2
  18. #define UNMATCH_ADDRSS1   0xB1
  19. #define UNMATCH_ADDRSS2   0xD3


  20. /*---------------------------------------------------------------------------------------------------------*/
  21. /* Define functions prototype                                                                              */
  22. /*---------------------------------------------------------------------------------------------------------*/
  23. extern char GetChar(void);
  24. int32_t main(void);
  25. void RS485_HANDLE(void);
  26. void RS485_9bitModeSlave(void);


  27. /*---------------------------------------------------------------------------------------------------------*/
  28. /* ISR to handle UART Channel 0 interrupt event                                                            */
  29. /*---------------------------------------------------------------------------------------------------------*/
  30. void UART1_IRQHandler(void)
  31. {
  32.     RS485_HANDLE();
  33. }

  34. /*---------------------------------------------------------------------------------------------------------*/
  35. /* RS485 Callback function                                                                                 */
  36. /*---------------------------------------------------------------------------------------------------------*/
  37. void RS485_HANDLE()
  38. {
  39.     volatile uint32_t addr = 0;
  40.     volatile uint32_t u32IntSts = UART1->ISR;

  41.     /* RLS INT & RDA INT */  //For RS485 Detect Address
  42.     if((u32IntSts & UART_ISR_RLS_INT_Msk) && (u32IntSts & UART_ISR_RDA_INT_Msk))
  43.     {
  44.         if(UART1->FSR & UART_FSR_RS485_ADD_DETF_Msk)    /* ADD_IF, RS485 mode */
  45.         {
  46.             addr = UART1->RBR;
  47.             UART_RS485_CLEAR_ADDR_FLAG(UART1);          /* clear ADD_IF flag */
  48.             printf("\nAddr=0x%x,Get:", addr);

  49. #if (IS_USE_RS485NMM ==1) //RS485_NMM

  50.             /* if address match, enable RX to receive data, otherwise to disable RX */
  51.             /* In NMM mode, user can decide multi-address filter */
  52.             /* In AAD mode, only one address can set */
  53.             if((addr == MATCH_ADDRSS1) || (addr == MATCH_ADDRSS2))
  54.             {
  55.                 UART1->FCR &= ~ UART_FCR_RX_DIS_Msk;  /* Enable RS485 RX */
  56.             }
  57.             else
  58.             {
  59.                 printf("\n");
  60.                 UART1->FCR |= UART_FCR_RX_DIS_Msk;    /* Disable RS485 RX */
  61.                 UART1->FCR |= UART_FCR_RFR_Msk;       /* Clear data from RX FIFO */
  62.             }
  63. #endif
  64.         }
  65.     }
  66.     else if((u32IntSts & UART_ISR_RDA_INT_Msk) || (u32IntSts & UART_ISR_TOUT_INT_Msk))  /* Rx Ready or Time-out INT*/
  67.     {
  68.         /* Handle received data */
  69.         printf("%d,", UART1->RBR);
  70.     }

  71.     else if(u32IntSts & UART_ISR_BUF_ERR_INT_Msk)     /* Buffer Error INT */
  72.     {
  73.         printf("\nBuffer Error...\n");
  74.         UART_ClearIntFlag(UART1, UART_ISR_BUF_ERR_INT_Msk);
  75.     }
  76. }

  77. /*---------------------------------------------------------------------------------------------------------*/
  78. /*  RS485 Receive Test  (IS_USE_RS485NMM: 0:AAD  1:NMM)                                                    */
  79. /*---------------------------------------------------------------------------------------------------------*/
  80. void RS485_9bitModeSlave()
  81. {

  82.     printf("\n");
  83.     printf("+-----------------------------------------------------------+\n");
  84.     printf("|     Pin Configure                                         |\n");
  85.     printf("+-----------------------------------------------------------+\n");
  86.     printf("|    _______                                    _______     |\n");
  87.     printf("|   |       |                                  |       |    |\n");
  88.     printf("|   |Master |---TXD1(P1.3) <====> RXD1(P1.2)---| Slave |    |\n");
  89.     printf("|   |       |---RTS1(P0.1) <====> RTS1(P0.1)---|       |    |\n");
  90.     printf("|   |_______|                                  |_______|    |\n");
  91.     printf("|                                                           |\n");
  92.     printf("+-----------------------------------------------------------+\n");
  93.     printf("|           RS485  Function Test (9-bit Slave)              |\n");
  94.     printf("+-----------------------------------------------------------+\n");

  95.     /*
  96.         The sample code is used to test RS485 9-bit mode and needs
  97.         two Module test board to complete the test.
  98.         Master:
  99.             1.Set AUD mode and HW will control RTS pin. LEV_RTS is set to '0'.
  100.             2.Master will send four different address with 10 bytes data to test Slave.
  101.             3.Address bytes : the parity bit should be '1'. (Set UA_LCR = 0x2B)
  102.             4.Data bytes : the parity bit should be '0'. (Set UA_LCR = 0x3B)
  103.             5.RTS pin is low in idle state. When master is sending,
  104.               RTS pin will be pull high.

  105.         Slave:
  106.             1.Set AAD and AUD mode firstly. LEV_RTS is set to '0'.
  107.             2.The received byte, parity bit is '1' , is considered "ADDRESS".
  108.             3.The received byte, parity bit is '0' , is considered "DATA".  (Default)
  109.             4.AAD: The slave will ignore any data until ADDRESS match ADDR_MATCH value.
  110.               When RLS and RDA interrupt is happened,it means the ADDRESS is received.
  111.               Check if RS485_ADD_DETF is set and read UA_RBR to clear ADDRESS stored in rx_fifo.

  112.               NMM: The slave will ignore data byte until disable RX_DIS.
  113.               When RLS and RDA interrupt is happened,it means the ADDRESS is received.
  114.               Check the ADDRESS is match or not by user in UART_IRQHandler.
  115.               If the ADDRESS is match,clear RX_DIS bit to receive data byte.
  116.               If the ADDRESS is not match,set RX_DIS bit to avoid data byte stored in FIFO.
  117.     */

  118.     /* Set Data Format, Only need parity enable whenever parity ODD/EVEN */
  119.     UART_SetLine_Config(UART1, 0, UART_WORD_LEN_8, UART_PARITY_EVEN, UART_STOP_BIT_1);

  120.     /* Set RTS pin active level as low level active */
  121.     UART1->MCR &= ~UART_MCR_LEV_RTS_Msk;
  122.     UART1->MCR |= UART_RTS_IS_LOW_LEV_ACTIVE;

  123. #if(IS_USE_RS485NMM == 1)

  124.     printf("\n");
  125.     printf("+-----------------------------------------------------------+\n");
  126.     printf("|    Normal Multidrop Operation Mode                        |\n");
  127.     printf("+-----------------------------------------------------------+\n");
  128.     printf("| The function is used to test 9-bit slave mode.            |\n");
  129.     printf("| Only Address %x and %x,data can receive                   |\n", MATCH_ADDRSS1, MATCH_ADDRSS2);
  130.     printf("+-----------------------------------------------------------+\n");

  131.     /* Set RX_DIS enable before set RS485-NMM mode */
  132.     UART1->FCR |= UART_FCR_RX_DIS_Msk;

  133.     /* Set RS485-NMM Mode */
  134.     UART_SelectRS485Mode(UART1, UART_ALT_CSR_RS485_NMM_Msk | UART_ALT_CSR_RS485_AUD_Msk, 0);

  135.     /* Set RS485 address detection enable */
  136.     UART1->ALT_CSR |= UART_ALT_CSR_RS485_ADD_EN_Msk;

  137. #else
  138.     printf("Auto Address Match Operation Mode\n");
  139.     printf("+-----------------------------------------------------------+\n");
  140.     printf("| The function is used to test 9-bit slave mode.            |\n");
  141.     printf("|    Auto Address Match Operation Mode                      |\n");
  142.     printf("+-----------------------------------------------------------+\n");
  143.     printf("|Only Address %x,data can receive                           |\n", MATCH_ADDRSS1);
  144.     printf("+-----------------------------------------------------------+\n");

  145.     /* Set RS485-AAD Mode and address match is 0xC0 */
  146.     UART_SelectRS485Mode(UART1, UART_ALT_CSR_RS485_AAD_Msk | UART_ALT_CSR_RS485_AUD_Msk, MATCH_ADDRSS1);

  147.     /* Set RS485 address detection enable */
  148.     UART1->ALT_CSR |= UART_ALT_CSR_RS485_ADD_EN_Msk;

  149. #endif

  150.     /* Enable RDA\RLS\Time-out Interrupt */
  151.     UART_ENABLE_INT(UART1, (UART_IER_RDA_IEN_Msk | UART_IER_RLS_IEN_Msk | UART_IER_RTO_IEN_Msk));

  152.     /* Enable UART1 IRQ */
  153.     NVIC_EnableIRQ(UART1_IRQn);

  154.     printf("Ready to receive data...(Press any key to stop test)\n");
  155.     GetChar();

  156.     /* Flush Rx FIFO */
  157.     UART1->FCR |= UART_FCR_RFR_Msk;

  158.     /* Disable RDA\RLS\Time-out Interrupt */
  159.     UART_DISABLE_INT(UART1, (UART_IER_RDA_IEN_Msk | UART_IER_RLS_IEN_Msk | UART_IER_RTO_IEN_Msk));

  160.     printf("\nEnd test\n");

  161. }


  162. void SYS_Init(void)
  163. {
  164.     /*---------------------------------------------------------------------------------------------------------*/
  165.     /* Init System Clock                                                                                       */
  166.     /*---------------------------------------------------------------------------------------------------------*/

  167.     /* Enable Internal RC 22.1184MHz clock */
  168.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  169.     /* Waiting for Internal RC clock ready */
  170.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  171.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  172.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  173.     /* Enable external XTAL 12MHz clock */
  174.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  175.     /* Waiting for external XTAL clock ready */
  176.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  177.     /* Set core clock as PLL_CLOCK from PLL */
  178.     CLK_SetCoreClock(PLL_CLOCK);

  179.     /* Enable UART module clock */
  180.     CLK_EnableModuleClock(UART0_MODULE);
  181.     CLK_EnableModuleClock(UART1_MODULE);

  182.     /* Select UART module clock source */
  183.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
  184.     CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  185.     /*---------------------------------------------------------------------------------------------------------*/
  186.     /* Init I/O Multi-function                                                                                 */
  187.     /*---------------------------------------------------------------------------------------------------------*/

  188.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  189.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  190.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);

  191.     /* Set P1 multi-function pins for UART1 RXD and TXD */
  192.     SYS->P1_MFP &= ~(SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
  193.     SYS->P1_MFP |= (SYS_MFP_P12_RXD1 | SYS_MFP_P13_TXD1);

  194.     /* Set P0 multi-function pins for UART1 RTS */
  195.     SYS->P0_MFP = SYS->P0_MFP & (~SYS_MFP_P01_Msk) | SYS_MFP_P01_RTS1;


  196. }

  197. void UART0_Init()
  198. {
  199.     /*---------------------------------------------------------------------------------------------------------*/
  200.     /* Init UART                                                                                               */
  201.     /*---------------------------------------------------------------------------------------------------------*/
  202.     /* Reset UART0 module */
  203.     SYS_ResetModule(UART0_RST);

  204.     /* Configure UART0 and set UART0 Baudrate */
  205.     UART_Open(UART0, 115200);
  206. }

  207. void UART1_Init()
  208. {
  209.     /*---------------------------------------------------------------------------------------------------------*/
  210.     /* Init UART                                                                                               */
  211.     /*---------------------------------------------------------------------------------------------------------*/
  212.     /* Reset UART1 module */
  213.     SYS_ResetModule(UART1_RST);

  214.     /* Configure UART1 and set UART1 Baudrate */
  215.     UART_Open(UART1, 115200);
  216. }

  217. /*---------------------------------------------------------------------------------------------------------*/
  218. /* MAIN function                                                                                           */
  219. /*---------------------------------------------------------------------------------------------------------*/
  220. int32_t main(void)
  221. {

  222.     /* Unlock protected registers */
  223.     SYS_UnlockReg();

  224.     /* Init System, IP clock and multi-function I/O */
  225.     SYS_Init();

  226.     /* Lock protected registers */
  227.     SYS_LockReg();

  228.     /* Init UART0 for printf */
  229.     UART0_Init();

  230.     /* Init UART1 for testing */
  231.     UART1_Init();

  232.     /*---------------------------------------------------------------------------------------------------------*/
  233.     /* SAMPLE CODE                                                                                             */
  234.     /*---------------------------------------------------------------------------------------------------------*/

  235.     printf("\n\nCPU @ %dHz\n", SystemCoreClock);

  236.     printf("\n\nUART Sample Program\n");

  237.     /* UART RS485 sample slave function */
  238.     RS485_9bitModeSlave();

  239. }


 楼主| 捉虫天师 发表于 2016-7-28 19:46 | 显示全部楼层
RS-485 模式
UART 支持RS-485 9位模式. RS-485模式通过设置UA_FUN_SEL寄存器选择RS-485功能设定。使用来
自异步串行口的RTS控制信号来使能RS-485驱动器,执行RS-485驱动器控制。在RS-485模式下, RX
与TX的许多特性与UART相同。
RS-485 模式下,控制器可以配置成可寻址的RS-485从机, RS-485主机发送器将通过设置校验位 (第9
位)为1来识别一个地址字符。对于数据字符,校验位设置为0. 软件可通过设置寄存器UA_LCR 控制第9
位 (PBE , EPE 和 SPE置位, 第9位发送0 , PBE 和 SPE 置位, EPE清零, 第9位发送1). 该控制器支持三
种操作模式: RS-485 普通多点模式(NMM), RS-485 自动地址识别模式 (AAD) 和RS-485 自动方向控制
模式(AUD), 可通过编程UA_RS-485_CSR寄存器选择其中一种工作模式, 通过设置UA_TOR [DLY] 可以
设置上一个停止位与下一个开始位之间的延迟时间
 楼主| 捉虫天师 发表于 2016-7-28 19:48 | 显示全部楼层
RS-485 普通多点模式 (NMM)
RS-485 普通多点模式, 首先必须软件配置在地址位之前检测到的数据是否存储于RX-FIFO中。如果想软
件忽略地址位之前检测到的所有数据,则接下来是设置UART_FCR[RS485_RX_DIS]和使能UA_RS-
485[RS485_NMM],这样,接收器忽略所有数据直至检测到地址字节( bit9=1)并将地址字节数据存储
于 RX-FIFO 中 。 如 果 想 软 件 接 收 检 测 到 地 址 位 之 前 的 所 有 数 据 , 接 下 来 就 是 禁 止 UART_FCR
[RS485_RX_DIS]和使能UA_RS-485[RS485_NMM],这样,接收器就会接收所有数据。如果检测到地
址位, RS-485控制器会向CPU产生一个中断,软件可通过设定UA_RS-485_FCR [RX_DIS]来决定是否
使能接收器来接收接下来的数据字节。如果使能接收器接收,所有接收的数据都将被接收并存储于RXFIFO中,如果禁用接收器,则接收到的所有数据都将被忽略直至下一个地址位被检测到。若软件设置
UA_RS-485禁用接收器, 当检测到下一个地址字节,控制器清UA_RS-485_FCR [RX_DIS]位,地址字节
数据存储到RX-FIFO.
 楼主| 捉虫天师 发表于 2016-7-28 19:51 | 显示全部楼层
RS-485 自动地址识别模式 (AAD)
RS-485自动地址识别模式下, 接收器在检测到地址字节( bit9=1)并且地址字节数据与UA_ALT_CSR
[ADDR_MATCH]的值相匹配之前,忽略所有数据. 地址字节数据将被存储在RX-FIFO. 所有接收字节数
据将被接受,并存储于RX-FIFO 直到地址字节不匹配UA_ALT_CSR [ADDR_MATCH] 的值为止.
RS-485 自动方向模式 (AUD)
RS-485控制器的另一个功能是自动方向控制. 使用来自异步串行口的RTS控制信号来使能RS-485驱动
器,执行RS-485驱动器控制. RTS线被连接到RS-485驱动器使能,以便设置RTS线为高(逻辑1)使能
RS-485 驱动器. 设置RTS为低(逻辑0),使驱动器进入tri-state状态. 用户通过设置寄存器UA_MCR 中
的LEV_RTS位改变 RTS 驱动电平.
 楼主| 捉虫天师 发表于 2016-7-28 20:00 | 显示全部楼层
编程流程示例:
1. 设置寄存器UA_FUN_SEL中的FUN_SEL位选择RS-485功能.
2. 设置寄存器UA_FCR 中的RX_DIS 位使能或禁用RS-485 接收器
3. 设置RS-485_NMM 或 RS-485_AAD 模式.
4. 如果选择RS-485_AAD 模式, ADDR_MATCH设置成自动地址匹配值.
5. 设置RS-485_AUD选择自动方向控制
 楼主| 捉虫天师 发表于 2016-7-28 20:01 | 显示全部楼层
zhuomuniao110 发表于 2016-7-29 00:10 | 显示全部楼层

1、通讯距离

RS232口最大通讯距离是15米,而RS422/485最大通讯距离是1200米。

2、所连接设备个数

RS232只能连接一个设备,而RS485可以连接多个设备。

3、端口的定义

RS232是标准接口,为D形9针头,所连接设备的接口的信号定义是一样的,其信号定义如下:

669692a6tc92e0c1bc2f4&amp;690.gif

    而RS422/RS485为非标准接口,一般为15针串行接口(也有使用9针接口的),每个设备的引脚定义也不一样。另外还需要说明的是,RS422和RS485也有区别:RS422为4线制,全双工模式;RS485为两线制,半双工模式。



zhuomuniao110 发表于 2016-7-29 00:13 | 显示全部楼层

1、通讯距离

RS232口最大通讯距离是15米,而RS422/485最大通讯距离是1200米。

2、所连接设备个数

RS232只能连接一个设备,而RS485可以连接多个设备。

3、端口的定义

RS232是标准接口,为D形9针头,所连接设备的接口的信号定义是一样的,其信号定义如下:

669692a6tc92e0c1bc2f4&amp;690.gif

    而RS422/RS485为非标准接口,一般为15针串行接口(也有使用9针接口的),每个设备的引脚定义也不一样。另外还需要说明的是,RS422和RS485也有区别:RS422为4线制,全双工模式;RS485为两线制,半双工模式。



 楼主| 捉虫天师 发表于 2016-7-30 16:21 | 显示全部楼层
这个DB9的接口没找到VCC供电,还要单独搞个供电的,要不可以给外挂省了个电源了
稳稳の幸福 发表于 2016-7-30 17:06 | 显示全部楼层
通信距离决定了使用场合,比如同一个设备内的通信是可以用232的,一般是TTL232,如果是一两米内通信就是用RS232.
如果是2米以上,1000米内,那就是用485通信了。
 楼主| 捉虫天师 发表于 2016-8-4 08:22 | 显示全部楼层
我们用外场设备传输,几十米的都使用485,性能很稳定。
quray1985 发表于 2016-8-8 16:44 | 显示全部楼层
串口怎么控制485器件呢?
gejigeji521 发表于 2016-8-8 22:35 | 显示全部楼层
RS422/RS485为非标准接口,一般为15针串行接口(也有使用9针接口的)
 楼主| 捉虫天师 发表于 2016-8-14 15:24 | 显示全部楼层
至于怎么用,还看具体使用环境。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

213

主题

3276

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部