• UART驱动


    基于I.MX6ULL的串口驱动

    1.uart驱动

     1 #include "bsp_uart.h"
     2 #include "bsp_gpio.h"
     3 
     4 
     5 
     6 /*初始化UART1,波特率为115200*/
     7 void uart_init(void)
     8 {
     9     /*初始化UART1的IO*/
    10     uart_io_init();
    11     /*初始化UART1*/
    12     uart_disable(UART1);//关闭uart
    13     uart_softreset(UART1);//复位UART1
    14     /*配置UART1*/
    15     UART1->UCR1 &= ~(1<<14);//0 Disable automatic detection of baud rate
    16     /*配置UART1的数据位、奇偶校验、停止位*/
    17     //UART1->UCR2=0;
    18     UART1->UCR2 |= (1<<1)|(1<<2)|(1<<5)|(1<<14);
    19     UART1->UCR3 |= (1<<2);
    20     /*设置波特率为115200*/
    21     UART1->UFCR = 5 <<7;//1分频,uart_clk=80MHz
    22     UART1->UBIR=71;//UBIR=(UBMR+1)*72/3125-1,UBIR=71,UBMR=3124
    23     UART1->UBMR=3124;//UBIR=(UBMR+1)*72/3125-1,UBIR=71,UBMR=3124
    24     /*使能串口*/
    25     uart_enable(UART1);
    26 }
    27 /*关闭UART*/
    28 void uart_disable(UART_Type *base)
    29 {
    30     base->UCR1 &= ~(1<<0);  
    31 }
    32 /*打开UART*/
    33 void uart_enable(UART_Type *base)
    34 {
    35     base -> UCR1 |= (1<<0);
    36 }
    37 /*复位UART*/
    38 void uart_softreset(UART_Type *base)
    39 {
    40     base ->UCR2 &= ~(1<<0);
    41     while((base->UCR2 &0x1)==0);
    42 }
    43 /*UART1的IO初始化*/
    44 void uart_io_init(void)
    45 {
    46     IOMUXC_SetPinMux(IOMUXC_UART1_TX_DATA_UART1_TX,0);//复用为UART1_TX
    47     IOMUXC_SetPinMux(IOMUXC_UART1_RX_DATA_UART1_RX,0);//复用为UART1_RX
    48     IOMUXC_SetPinConfig(IOMUXC_UART1_TX_DATA_UART1_TX,0X10B0);
    49     IOMUXC_SetPinConfig(IOMUXC_UART1_RX_DATA_UART1_RX,0X10B0);
    50 }
    51 /*通过UART1发送一个字符*/
    52 void uart_putc(unsigned char c)
    53 {
    54     while(((UART1->USR2>>3)&0X01)==0);
    55     UART1->UTXD=c&0XFF;//发送数据
    56 }
    57 unsigned char uart_getc(void)//通过串口接收数据
    58 {
    59     while((UART1->USR2&0X01)==0);//等待,直到有数据为止
    60     return UART1->URXD;
    61 }
    62 
    63 /*通过UART1发送一串字符*/
    64 void uart_puts(char *str)
    65 {
    66     char *p = str;
    67     while(*p)
    68         uart_putc(*p++);
    69 }

    2.main.c

     1 #include "bsp_clk.h"
     2 #include "bsp_delay.h"
     3 #include "bsp_led.h"
     4 #include "bsp_uart.h"
     5 
     6 int main(void)
     7 {
     8     unsigned char state = OFF;
     9     unsigned char a = 0;
    10     delay_init();//延时初始化
    11     uart_init();//初始化串口
    12     clk_enable();        /* 使能所有的时钟             */
    13     led_init();            /* 初始化led             */
    14 
    15     while(1)            
    16     {    
    17         a=uart_getc();
    18         uart_puts("切换灯光倒计时");
    19         uart_putc(a);//回显,我认为比较重要!!!
    20         uart_puts("
    ");
    21         delay_ms(1000);
    22 
    23         uart_puts("倒计时3秒");
    24         uart_putc(a);
    25         uart_puts("
    ");
    26         delay_ms(1000);
    27 
    28 
    29         uart_puts("倒计时2秒");
    30         uart_putc(a);
    31         uart_puts("
    ");
    32         delay_ms(1000);
    33 
    34         uart_puts("倒计时1秒");
    35         uart_putc(a);
    36         uart_puts("
    ");
    37         delay_ms(1000);
    38 
    39         state =!state;
    40         led_switch(LED0,state);
    41         uart_puts("灯光已经切换
    ");
    42         delay_ms(2000);
    43 
    44         uart_puts("*******重新开始新的切换*******
    ");
    45         uart_putc(a);
    46         uart_puts("
    ");
    47     }
    48     return 0;
    49 }

    3.SecureCRT界面

  • 相关阅读:
    设计模式--单例模式Singleton
    CSS选择器
    操作数据表
    HTML
    Shell学习
    LINUX常用命令
    多线程-互斥变量
    多线程-临界区
    每天一个linux命令:file(11)
    JavaScript设计模式小抄集(持续更新)
  • 原文地址:https://www.cnblogs.com/kunshanpipixia/p/14248928.html
Copyright © 2020-2023  润新知