zoukankan      html  css  js  c++  java
  • PIC32MZ tutorial -- UART Communication

      At this moment, I accomplish the interface of UART communication for PIC32MZ EC Starter Kit. This interface configures the PIC32MZ for communication with a host PC at 115200 baud. There are five functions in the interface -- Uart_Init(), Uart_Getc(), Uart_Gets(), Uart_Putc() and Uart_Puts().  

      Uart_Init() configures PIC32MZ UART1 with 115200-8-None-1. It uses PPS to select RPC13, RPC14 as TX and RX.

          Uart_Getc() is a reception funtion for a character. It will be blocked until a character got.

      Uart_Gets() is a reception funtion for string. It will receive multiple characters until the ' ' ' ' or the buffer is full.

      Uart_Putc() is a transmit function for a character.

          Uart_Puts() is a transmit funtion for string. It will transmit multiple character until ''.

      Below is the code. 

    void Uart_Init(void)
    {
        LATCSET = 0x6000; /* Both RC13 and RC14 HIGH */
        TRISCSET = 0x4000; /* RC14 Input */
        TRISCCLR = 0x2000; /* RC13 Output */
        
        Seq_UnLock();     
        RPC13Rbits.RPC13R = 1;  /* U1TX on RPC13 */
        U1RXRbits.U1RXR = 7;    /* U1RX on RPC14 */
        Seq_Lock();
        
        U1BRG = ((PBCLK2_FREQUENCY / BAUDRATE) / 16) - 1;
        U1MODE = 0x8000;        // Loopback mode is enabled
        U1STA = 0x1400;
        
        IFS3bits.U1RXIF = 0;
        IFS3bits.U1TXIF = 0;
    }
    
    char Uart_Getc(void)
    {
        if (U1STAbits.OERR)
        {
            U1STAbits.OERR = 0;
            return 0;
        }
        while (!U1STAbits.URXDA);
        char ret = U1RXREG;
        IFS3bits.U1RXIF = 0;
        return ret;
    }
    
    void Uart_Gets(char *s)
    {
        char c;
        int size = 0;
        if (s == (void *)0) return;
        while (size < U1RX_BUFSIZE)
        {
            c = Uart_Getc();
            if ((c == '
    ')||(c == '
    ')) 
            {
                s[size] = '';
                break;
            }
            s[size++] = c;
        }
    }
    
    void Uart_Putc(char c)
    {
        U1TXREG = c;
        while (U1STAbits.UTXBF);
        IFS3bits.U1TXIF = 0;
    }
    
    void Uart_Puts(char *s)
    {
        if (s == (void *)0)
        {
            return;
        }
        while (*s != '')
        {
            Uart_Putc(*s++);
        }
    }

      

  • 相关阅读:
    [NOI2003],[AHOI2006]文本编辑器
    luogu P5151 HKE与他的小朋友
    [NOI2005]维护数列
    [HNOI2012]永无乡
    luogu P4146 序列终结者
    [SCOI2016]美味
    UVA1451 Average
    [JSOI2007]字符加密
    luogu P3809 【模板】后缀排序
    CentOS 7系统启动后怎么从命令行模式切换到图形界面模式
  • 原文地址:https://www.cnblogs.com/geekygeek/p/pic32mz_uartcommunication.html
Copyright © 2011-2022 走看看