1 #include"uart_4.h" | 1 #include"uart_4.h" | 15 volatile unsigned int TDR;
2 | 2 | 16 volatile unsigned int PRESC;
3 int main(int argc, const char *argv[])| 3 //初始化函数 | 17 }uart_t;
4 { | 4 void uart4_init() | 18
5 uart4_init(); | 5 { | 19 #define USART1 ((uart_t *)0x5C000000)
6 char str[50]={0}; | 6 //初始化使能三个控制器 | 20 #define USART2 ((uart_t *)0x4000E000)
7 while(1) | 7 GPIOB|=(0x1<<1); | 21 #define USART3 ((uart_t *)0x4000F000)
8 { | 8 GPIOG|=(0x1<<6); | 22 #define USART4 ((uart_t *)0x40010000)
9 puts_char(gets_char());//传参数调用 | 9 (RCC_ADD->USART4)|=(0x1<<16); | 23 #define USART5 ((uart_t *)0x40011000)
10 } | 10 //进行pb2针脚复用 | 24 #define USART6 ((uart_t *)0x44003000)
11 | 11 GPIOB&=(~(0x3<<4)); | 25 #define USART7 ((uart_t *)0x40018000)
12 return 0; | 12 GPIOB|=(0x1<<4); | 26 #define USART8 ((uart_t *)0x40019000)
13 } | 13 //设置pb2在uart4_tx发送(接收外部发送信号) | 27 #define RCC_ADD ((volutile unsigned int*)0x50000A28)//RCC地址
~ | 14 GB_AFRL&=(~(0Xff<<8));//将8-11位置为0 | 28 #define GPIOB ((volutile unsigned int*)0x50003000)//B类地址
~ | 15 GB_AFRL|=(0X1<<11); | 29 #define GPIOG ((volutile unsigned int*)0x50008000)//G类地址
~ | 16 //设置pg11复用 | 30 #define GB_AFRL ((volutile unsigned int*)0x50003020)//AFRL地址
~ | 17 GPIOG&=(~(0x3<<22)); | 31 #define GG_AFRH ((volatile unsigned int*)0x50003024)//AFRH地址
~ | 18 GPIOG|=(0X1<<22); | 32 #define USART4_ISR ((volutile unsigned int*)0x4001001c)//ICR地址
~ | 19 //设置pg11在uart4_rx接收(发送给外界信号) | 33 //初始化函数
~ | 20 GG_AFRH&=(~(0xff<<12)); | 34 void uart4_init();
~ | 21 GG_AFRH|=(0X3<<13); | 35 //发送一个字符
~ | 22 //设置初始化类型的CR1 | 36 void put_char(const char str);
~ | 23 (USART4->CR1)&=(~(0x1<<28)); | 37 //接收一个字符
~ | 24 (USART4->CR1)&=(~(0x1<<12));//设置串口8位数据位 | 38 char get_char();
~ | 25 (USART4->CR1)&=(~(0x1<<15));//设置串口16倍采样率 | 39 //发送一个字符串
~ | 26 (USART4->CR1)&=(~(0x1<<10));//设置串口无奇偶校验 | 40 void puts_char(const char str);
~ | 27 (USART4->CR1)&=(~(0x1<<3));//设置串口发送寄存器使能 | 41 //接收一个字符串
~ | 28 (USART4->CR1)&=(~(0x1<<2));//设置串口接收寄存器使能 | 42 char gets_char();
~ | 29 (USART4->CR1)&=(~(0x1<<0));//设置串口接收使能 | 43 #endif
~ | 30 //设置停止位 | 44
~ | 31 (USART4->CR2)&=(~(0x11<<12));//设置1位停止位 |~
~ | 32 //设置波特率bps |~
~ | 33 (USART4->BRR)=0x22b; |~
~ | 34 //设置串口寄存器不分频 |~
~ | 35 (USART4->PRESC)=(~(0xf<<0)); |~
~ | 36 } |~
~ | 37 //发送一个字符,这里的是开发板数据,相对于电脑主板来说 |~
~ | 38 //这边的先接收字符,再发送字符 |~
~ | 39 void put_char(const char str) |~
~ | 40 { |~
~ | 41 while(!(USART4_ISR&(0x1<<7)));//判断寄存器是否为空, |~
~ | 42 //若是寄存器为满,等待,为0为满,为1为空 |~
~ | 43 USART4->TDR=str; |~
~ | 44 while(!(USART4_ISR&(0x1<<6))); |~
~ | 45 //判断寄存器是不是发送成功, |~
~ | 46 //发送成功,6位为0,失败为6位1 |~
~ | 47 } |~
~ | 48 //接收一个字符 |~
~ | 49 char get_char() |~
~ | 50 { |~
~ | 51 char ch; |~
~ | 52 while(!(USART4_ISR&(0X1<<5)));//判断是否收到数据 |~
~ | 53 //0表示存在数据 |~
~ | 54 ch=USART4->RDR;//从接收端获取发送的数据 |~
~ | 55 return ch; |~
~ | 56 |~
~ | 57 } |~
~ | 58 //发送一个字符串 |~
~ | 59 void puts_char(const char *str) |~
~ | 60 { |~
~ | 61 while('\0'!=*str) |~
~ | 62 { |~
~ | 63 puts_char(*str); |~
~ | 64 str++; |~
~ | 65 } |~
~ | 66 put_char('\n'); |~
~ | 67 put_char('\r'); |~
~ | 68 } |~
~ | 69 //接收一个字符串 |~
~ | 70 char* gets_char(char *str) |~
~ | 71 { |~
~ | 72 char *p=str; |~
~ | 73 while((*p=get_char())!='\r') |~
~ | 74 { |~
~ | 75 put_char(*p); |~
~ | 76 p++; |~
~ | 77 |~
~ | 78 } |~
~ | 79 *p=0; |~
~ | 80 return p; |~
~ | 81 |~
~ | 82 }