I need help with getting the UART message to recieve a communication on my LCD.
ID: 2315227 • Letter: I
Question
I need help with getting the UART message to recieve a communication on my LCD. I have done the UART message transmission test which I pasted below. Port C 11 - UART4 _RX/ is for receiving. Please use STM32F4 discovery and keil as a compiler, Thanks.
* UART message transmission test
*
* Using UART4 for this exercise:
* Port C10 - UART4_TX/
* Port C11 - UART4_RX/
*/
#include "stm32f4xx.h" // Device header
char DataString[] = "I am FUNKY!!"; // Init string & ptr ; // string must be terminated by ''
int i=0;
int main(){
// ENABLE CLocks for PORT C and UART4
RCC->AHB1ENR |= 0x4;
RCC->APB1ENR |= 0x80000;
// Set Mode Pins on Port C pins 10 and 11 to Alternate Function
GPIOC->MODER |= 0xa00000;
// Set Alternate Function Register for Port C 10 and 11
// AF8 on AFRH
GPIOC->AFR[1] |= 0x8800;
// UART setup:
// 8 data bits (default), no parity (default), one stop bit (default) & no flow control (default)
UART4->CR1 &= ~(0x1000);
UART4->CR2 &= ~(0x3000); //1 stop bit
// UART Baud Rate = 19.2 Kbps
UART4->BRR = 0x341; //from table value in BRR=52.0625
// Enable TRANSMITTER (TE bit)
UART4->CR1 |= 0x8;
// WE WANT TO TRANSMIT SO ENABLE TRANSMIT INTERRUPT
UART4->CR1 |= 0x80;
// Enable UART (UE bit in CR1 register)
UART4->CR1 |= 0x2000;
// NVIC to handle interrupts
__enable_irq();
NVIC_ClearPendingIRQ(UART4_IRQn);
NVIC_SetPriority(UART4_IRQn,0);
NVIC_EnableIRQ(UART4_IRQn);
while(1);
}
// Interrupt reoutine for UART1
void UART4_IRQHandler(void){
// TX IRQ
// If TXE flag in SR is on then
NVIC_ClearPendingIRQ(UART4_IRQn);
if((UART4->SR & 0x80)!=0){
if(DataString[i]==''){
i=0;
}else{
UART4->DR = DataString[i];
i++;
}
}
}
Explanation / Answer
#include void uart_init(); // Initializes UART peripheral char uart_getc(); // Polled method to Receive a byte(8bit) void uart_putc (char c); // Polled method to Transmit a byte(8bit) void uart_puts(char *s); // Typical puts for uart Transmit a null-terminated-string void uart_init() // Initializes UART peripheral { TMOD = 0x20; // Timer1, mode 2 TH1 = 0xFD; // 9600 bps 11059200 Clock, core(12cycle), uart(SMOD=0) TL1 = 0xFD; // First byte timing SCON = 0x52; // 0x52 : 0101 0010 Receive Enable, TI as 1 (set as ready) TR1 = 1; } char uart_getc() // Polled method to Receive a byte(8bit) { while (RI == 0) // Wait until a byte has been received ; // RI = 0; // Acknowledge RI return SBUF; // } void uart_putc (char c) // Polled method to Transmit a byte(8bit) { while (TI == 0) // TI as 0 indicates TX busy, ; // if busy then wait for TX completion of the previous byte SBUF = c; // Does not wait for TX of current byte to complete, // so gives time to caller for other actions } void uart_puts(char *s) // Typical puts for uart Transmit a string-null-terminated { while (*s) // Check for null byte termination uart_putc(*s++); // and send each byte using uart } void main () { char c; uart_init(); // Init UART to desired settings uart_puts("Hello, echoback testing serial communication "); while (1) { // This loop echoes back whatever is typed on PC Keyboard c = uart_getc(); // get next byte from PC uart_putc(c); // and send it back as echo } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.