/* p4_3.c Send a string "Hello\r\n" to USART1 * * USART1 Tx signal is connected to pin PA9. To see the output of USART1 * on a PC, you need to use a USB-serial module. Connect the Rx pin of * the module to the PA9 pin of the STM32F030 Nucleo board. Make sure * the USB-serial module you use has a 3.3V interface. * * By default, the clock is running at 8 MHz. * The UART1 is configured for 9600 Baud. * PA9 - USART1 TX (AF1) * * This program was tested with Keil uVision v5.23 with DFP v2.0.0 */ #include "stm32F0xx.h" void USART1_init(void); void USART1_write(int c); void delayMs(int); /*---------------------------------------------------------------------------- MAIN function *----------------------------------------------------------------------------*/ int main (void) { char message[] = "Hello\r\n"; int i; USART1_init(); while (1) { for (i = 0; i < 7; i++) { USART1_write(message[i]); /* send a char */ } delayMs(500); /* leave a gap between messages */ } } /*---------------------------------------------------------------------------- Initialize USART pins, Baudrate *----------------------------------------------------------------------------*/ void USART1_init (void) { RCC->AHBENR |= RCC_AHBENR_GPIOAEN; /* Enable GPIOA clock */ RCC->APB2ENR |= RCC_APB2ENR_USART1EN; /* Enable USART1 clock */ /* Configure PA9 for USART1_TX */ GPIOA->AFR[1] &= ~0x00F0; GPIOA->AFR[1] |= 0x0010; /* alt1 for USART1 */ GPIOA->MODER &= ~0xC0000; GPIOA->MODER |= 0x80000; /* enable alternate function for PA9 */ USART1->BRR = 0x0341; /* 9600 baud @ 8 MHz */ USART1->CR1 = 0x0008; /* enable Tx, 8-bit data */ USART1->CR2 = 0x0000; /* 1 stop bit */ USART1->CR3 = 0x0000; /* no flow control */ USART1->CR1 |= 0x0001; /* enable USART1 */ } /* Write a character to USART1 */ void USART1_write (int ch) { while (!(USART1->ISR & USART_ISR_TXE)) {} // wait until Tx buffer empty USART1->TDR = (ch & 0xFF); } void delayMs(int n) { int i; for (; n > 0; n--) for (i = 0; i < 1597; i++) ; }