-
Notifications
You must be signed in to change notification settings - Fork 0
/
UART.c
77 lines (52 loc) · 1.12 KB
/
UART.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "uart.h"
void USART2_Init(void){
RCC->APB1ENR |= 0x20000;
RCC->AHB1ENR |=0x01;
GPIOA->MODER &=~0x00F0;
GPIOA->MODER |= 0x00A0;
GPIOA->AFR[0] &= ~0xFF00;
GPIOA->AFR[0] |= 0x7700;
USART2->BRR = 0x0683;
USART2->CR1 = 0x000C;
USART2->CR2 = 0x000;
USART2->CR3 = 0x000;
USART2->CR1 |= 0x2000;
}
int USART2_write(int ch){
while(!(USART2->SR & 0x0080)){}
USART2->DR = (ch & 0xFF);
return ch;
}
int USART2_read(void){
while(!(USART2->SR & 0x0020)){}
return USART2->DR;
}
struct __FILE { int handle; };
FILE __stdin = {0};
FILE __stdout = {1};
FILE __stderr = {2};
int fgetc(FILE *f) {
int c;
c = USART2_read();
if (c == '\r') {
USART2_write(c);
c = '\n';
}
USART2_write(c);
return c;
}
int fputc(int c, FILE *f) {
return USART2_write(c);
}
int n;
char str[80];
void test_setup(void){
printf("please enter a number: ");
scanf("%d", &n);
printf("the number entered is: %d\r\n", n);
printf("please type a character string: ");
gets(str);
printf("the character string entered is: ");
puts(str);
printf("\r\n");
}