#include "stm32f4xx.h"
#include <stdio.h>
// 뒷받침 문장 (초등학교 3년 교과서에서, 중심문장, 뒷받침 문장)
void USART2_Preparation (void);
int main(void) // 중심문장
{
int n;
char str[100];
USART2_Preparation();
printf("Hello from the other side\r\n");
fprintf(stdout, "test for stdout\r\n");
fprintf(stderr, "test for stderr\r\n");
while(1)
{
printf("How old are you?");
scanf("%d", &n);
printf ("your age is : %d\r\n",n);
gets(str);
printf("I like your style: ");
puts(str);
printf ("\r\n");
}
}
void USART2_Preparation (void) // 뒷받침 문장
{
RCC -> AHB1ENR |= 1; // GPIOA Clock
RCC -> APB1ENR |= 0x20000; // UART2 Clock
GPIOA -> AFR[0] = 0x7700; // not 0x7000
GPIOA -> MODER |= 0x00A0; // PA2, PA3
USART2->BRR = 0x0683; // Baud rate 9600, 16MHz
USART2->CR1 = 0x000C; // TX, RX 가능
USART2->CR1 |= 0x2000; // USART2 가능
}
int USART2_Sender(int character)
{
while(!(USART2->SR & 0x0080)) { } // TX가 빌때를 기다림
USART2->DR= (character & 0xFF);
return character ; // USART2->DR = (character & 0xFF);
}
char USART2_Receiver(void) {
// 빌 때까지 기다림
while(!(USART2->SR & 0x0020)) { } // character 가 올 때까지 기다림
return USART2->DR;
}
//retargeting printf(), https://bit.ly/3rH6rcc / https://shawnhymel.com/1873/how-to-use-printf-on-stm32/
struct __FILE
{
int handle;
};
FILE __stdin = {0};
FILE __stdout = {1};
FILE __stderr = {2};
int fgetc(FILE *f)
{
int c;
c = USART2_Receiver();
if (c == '\r') {
USART2_Sender(c);
c='\n';
}
USART2_Sender(c);
return c;
}
int fputc(int c, FILE *f)
{
return USART2_Sender(c);
}
카테고리 없음