Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Experiment#10

Question#1
Code:
void main ()
{ RCC_APB2ENR = 0x00000004;
RCC_APB1ENR = 0x00020001;
GPIOA_CRL &= 0xFF2FFFFF;
GPIOA_CRL |= 0xFF2FFFFF;
GPIOA_CRL &=0xFFFF89FF;
GPIOA_CRL |=0x00008900;
GPIOA_BSRR = 0x8;
USART2_CR1 = 0x200C; //Enable USART 2 and Tx/Rx
USART2_BRR = 0x45; //Select Mantissa and Fraction of Baud rate
while (1)
{
GPIOA_BSRR = 0x00000020; //SET
usart1_sendByte ('O');
usart1_sendByte ('N');
usart1_sendByte ('\n'); //go to new line
usart1_sendByte ('\r'); //carrier return
Delay (1000);
GPIOA_BSRR = 0x00200000; //RESET
usart1_sendByte('O');
usart1_sendByte('F');
usart1_sendByte('F');
usart1_sendByte('\n'); //go to new line
usart1_sendByte('\r'); //carrier return
Delay (1000);
}
}
void Delay (int x) {
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_PSC = 7999;
TIM2_ARR = x - 1;
TIM2_CR1 = 1;
while ((TIM2_SR & 0x0001) == 0);
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_SR = 0;
}
void usart1_sendByte (unsigned char c) {
USART2_DR = c;
while ((USART2_SR & (1 << 6)) == 0);//wait until the TC flag is set
}
//END
Question#2
Code:
void main ()
{ RCC_APB2ENR = 0x00000014;
RCC_APB1ENR = 0x00020001;
GPIOA_CRL &= 0xFF2FFFFF;
GPIOA_CRL |= 0xFF2FFFFF;
GPIOA_CRL &=0xFFFF89FF;
GPIOA_CRL |=0x00008900;
GPIOC_CRH &= 0xFF8FFFFF;
GPIOC_CRH |= 0x00800000;
GPIOA_BSRR = 0x8;
USART2_CR1 = 0x200C; //Enable USART 2 and Tx/Rx
USART2_BRR = 0x45; //Select Mantissa and Fraction of Baud rate
while (1)
{
while ((GPIOC_IDR & 0x2000) == 0); //wait till user button is
pressed
GPIOA_BSRR = 0x00000100; //Turn ON LED
usart1_sendByte ('O');
usart1_sendByte ('N');
usart1_sendByte ('\n'); //go to new line
usart1_sendByte ('\r'); //carrier return
while ((GPIOC_IDR & 0x2000) != 0); //Debouncing
while ((GPIOC_IDR & 0x2000) == 0);//wait till button press
GPIOA_BSRR = 0x01000000; //Turn OFF LED
usart1_sendByte('O');
usart1_sendByte('F');
usart1_sendByte('F');
usart1_sendByte('\n'); //go to new line
usart1_sendByte('\r'); //carrier return
while ((GPIOC_IDR & 0x2000) != 0); //Debouncing
}
}
void Delay (int x) {
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_PSC = 7999;
TIM2_ARR = x - 1;
TIM2_CR1 = 1;
while ((TIM2_SR & 0x0001) == 0);
TIM2_CR1 = 0;
TIM2_SR = 0;
}
void usart1_sendByte (unsigned char c) {
USART2_DR = c;
while ((USART2_SR & (1 << 6)) == 0);//wait until the TC flag is set
}//END
Question#3
Code:
void main ()
{ RCC_APB2ENR = 0x00000004;
RCC_APB1ENR = 0x00020001;
GPIOA_CRL &=0xFFFF89FF;
GPIOA_CRL |=0x00008900;
GPIOA_BSRR = 0x8;
USART2_CR1 = 0x200C; //Enable USART 2 and Tx/Rx
USART2_BRR = 0x45; //Select Mantissa and Fraction of Baud rate
usart1_string ("Abdul Moeed");
}
void Delay (int x) {
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_PSC = 7999;
TIM2_ARR = x - 1;
TIM2_CR1 = 1;
while ((TIM2_SR & 0x0001) == 0);
TIM2_CR1 = 0;
TIM2_CNT = 0;
TIM2_SR = 0;
}
void usart1_string (unsigned char *word) {
while (*word) {
USART2_DR = word;
*word ++;
Delay (50);
}
}//END

Conclusion:
in this experiment it is learnt that how to use UART on STM32 Nucleo board. USART
(universal synchronous asynchronous receiver transmitter) is a full duplex data exchange with an
external equipment. USART is a serial data transfer standard. In this experiment a monitor is used to
display data using a console view. It is learnt that the port A pins’ 2 & 3 are the transmission and
receiving pins of USART2 and these pins are directly connected to the computer through the USB
cable. The baud rate is also a factor that needs to be considered when transferring data using USART.
It is the rate at which the data is to be transferred. As we know that different controllers run on different
speeds, therefore it is needed that the data transfer between them always happens on the same rate. It
is also studied and implemented that the baud rate should be same for proper data transfer.

You might also like