Skip to content

Instantly share code, notes, and snippets.

View hocarm's full-sized avatar
🎃
Focusing

hocarm

🎃
Focusing
View GitHub Profile
GPIO_InitStruct.GPIO_PIN = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA , &GPIO_InitStruct);
main()
{
// configure button
// configure led
while (1)
{
if (read(button))
led = on;
else
led = off;
USART_FLAG_TXE -- Transmit data register empty
USART_FLAG_RXNE -- Receive data register not empty
int putchar(int c) {
while (USART_GetFlagStatus(USART1 , USART_FLAG_TXE) == RESET);
USART1 ->DR = (c & 0xff);
return 0;
}
int getchar(void) {
while (USART_GetFlagStatus(USART1 , USART_FLAG_RXNE) == RESET);
return USART1 ->DR & 0xff;
}
#include <stm32f10x.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_rcc.h>
#include <stm32f10x_usart.h>
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 |
RCC_APB2Periph_AFIO |
RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_StructInit (& GPIO_InitStruct);
// Initialize USART1_Tx
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA , &GPIO_InitStruct);
// Initialize USART1_RX
GPIO_InitStruct.GPIO_PIN = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
// see stm32f10x_usart.h
USART_InitTypeDef USART_InitStructure;
// Initialize USART structure
USART_StructInit (& USART_InitStructure);
// Modify USART_InitStructure for non -default values , e.g.
// USART_InitStructure.USART_BaudRate = 38400;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1 ,& USART_InitStructure);
USART_Cmd(USART1 , ENABLE);
int uart_open(USART_TypeDef* USARTx , uint32_t baud , uint32_t flags);
int uart_close(USART_TypeDef* USARTx);
int uart_putc(int c, USART_TypeDef* USARTx);
int uart_getc(USART_TypeDef* USARTx);