Professional Documents
Culture Documents
Experiment-9
Aim: Write code to communicate with a PC through UART protocol using STM32 discovery
board.
Apparatus: STM32 discovery board
Software: Keil micro vision, STlink, Flash
Code:
/* USART3 IS USED WITH PD8 AND PD9 AS Tx PIN AND Rx PIN
RESPECTIVELY. */
#include "stm32f4xx.h"
#include <stdio.h>
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
int main(void)
{
// BaudRate = 19200 baud
// SystemCoreClockUpdate();
USART_InitStructure.USART_BaudRate = 9200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx|
USART_Mode_Rx;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* USART configuration */
USART_Init(USART3, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
/* Enable USART */
USART_Cmd(USART3, ENABLE);
while (1)
{
}
}