We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
#include "stm32f10x.h"#include <stdio.h>
#define RS 0x0020;#define EN 0x0080;
void USART2_write(int ch);char USART2_read (void);void delayMs (int delay);
void LCD_command (unsigned char command);void LCD_data (char data);void delayMs (int delay);
int main(void){ char ch; //***GPIO PORT*** //the clock for the GPIOA and USART2 RCC->APB2ENR|=0X0C; RCC->APB2ENR|=0x0004; //0000 0100..Enable Clock for GPIOA RCC->APB1ENR|=0x20000; //0010 0000 0000 0000 0000..Enable Clock for USART2..PIN17 //Configure GPIOA2 as alternate function//Configure GPIOA2(Tx) as push-pull output alternate function at low frequency (0b1010)//Configure GPIOA4(Rx) as a floating input (0b0100) which is its default state GPIOA->CRL = 0x00004A00; //1010 //Enable Tx,Rx and USART2..both in CR1 Register USART2->CR1|=0X2000; //enable usart..pin13 of CR1.. 0010 0000 0000 0000 USART2->CR1|=0X000C; //enable tx and rx..pin3 and pin2 of cr1..1100 //Set Baudrate to 9600 @16Mhz //BAUD RATE(36000000/(16*9600)) USART2->BRR = 0X0EA6; //******* while(1) { ch=USART2_read(); //READ char from Serial Comm USART2_write(ch); //return the char delayMs(50); } GPIOB->CRL = 0x33333333; GPIOA->CRL = 0x30300000; LCD_command(0x38); delayMs(10); LCD_command(0x38); delayMs(1); LCD_command(0x38); delayMs(1); LCD_command(0x0E); delayMs(1); LCD_command(0x01); delayMs(1); LCD_command(0x06); delayMs(1); LCD_command(0x80); delayMs(1); }
void USART2_write(int ch){ //*wait while tx buffer is empty* while(!(USART2->SR & 0X0080)){} USART2->DR = (ch & 0xFF); }
char USART2_read (void){ //*wait until char arrive* while(!(USART2->SR & 0X0020)){} return USART2->DR; } void delayMs(int delay) { for(;delay > 0; delay--) { for(int i=0; i<5000; i++) {} } }