0% found this document useful (0 votes)
11 views

SPI Code

The document is a C program for configuring and using USART1 on an STM32F4 microcontroller. It initializes USART1 for transmitting data at a baud rate of 115200 and continuously sends the character 'L'. The program includes functions for setting the baud rate and writing data to the USART transmit data register.

Uploaded by

jimjammyjimmyjam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SPI Code

The document is a C program for configuring and using USART1 on an STM32F4 microcontroller. It initializes USART1 for transmitting data at a baud rate of 115200 and continuously sends the character 'L'. The program includes functions for setting the baud rate and writing data to the USART transmit data register.

Uploaded by

jimjammyjimmyjam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include "stm32f4xx.

h"

//Defines
#define GPIOAEN (1U << 0)
#define USART1EN (1 << 4)
#define SYS_FREQ 16000000UL
#define APB1_CLK SYS_FREQ
#define UART_BAUDRATE 115200
#define CR1_TE (1U << 3)
#define CR1_UE (1U << 13)
#define SR_TXE (1U << 7)

//Function Prototypes
static void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk, uint32_t
BaudRate);
static uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t Baudrate);
void usart1_tx_init();
void uart1_write(int ch);

int main()
{
usart1_tx_init();

while (1)
{
uart1_write('L');
}

return 0;
}

void uart1_write(int ch)


{
//Make sure transmit data register is empty!
//Write to transmit data register (data register)
while (!(USART1->SR & SR_TXE));
USART1->DR = (ch & 0xFF);
}

void usart1_tx_init()
{
//Configure USART GPIO PIN
//Enable clock to GPIOA
RCC->AHB1ENR |= GPIOAEN;
//Set PA9 to alternate function mode
GPIOA->MODER &= ~(1U << 18);

GPIOA->MODER |= (1U << 19);

//Set PA9 alternate function type to USART_TX (AF07)


GPIOA->AFR[1] &= ~(0x0F << 4);
GPIOA->AFR[1] |= (0x07 << 4);

//Configure USART MODULE


//Enable clock access to USART1
RCC->APB2ENR |= USART1EN;
//Configure Baudrate
uart_set_baudrate(USART1, APB1_CLK, UART_BAUDRATE);
//Configure transfer direction
USART1->CR1 = CR1_TE; //We should clear the UART to ensure default
settings, hence no "|".
//Enable USART module
USART1->CR1 |= CR1_UE;

//Delay to prevent issues when plugging board in, then opening COM (was
sending incorrect data).
for(int index = 0;index < 1000000;index++);
//Looks like the issue is still there.
}

static void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk, uint32_t


BaudRate)
{
USARTx->BRR = compute_uart_bd(PeriphClk, BaudRate);
}

static uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t Baudrate)


{
return ((PeriphClk + (Baudrate / 2U)) / Baudrate);
//That pretty much exactly equates to PeriphClk / Baudrate (16000000 /
115200)!!!!!!!!!!!!
//return (PeriphClk / Baudrate);
//Looks like that's all it really is according to a lot of online
discussions...!
}

You might also like