2017-08-21 07:18 AM
Hello. I am trying to perform usart communication between the stm32f429 discovery board and the arduino uno. I have tried both sending and receiving data with the stm board but it is not working. I am sharing the codes I have used for receiving data with the stm board. Any help would be appreciated.
STM:
&sharpinclude <stm32f4xx.h>
void led_init(){ RCC ->AHB1ENR |= RCC_AHB1ENR_GPIOGEN; //Enables port G input output pins by setting the corresponding bit in advanced high performance bus 1 register. GPIOG ->MODER |= GPIO_MODER_MODER13_0; //Configures pin 13 as a general purpose output pin by setting the corresponding bit in the mode register GPIOG ->OTYPER &= ~(GPIO_OTYPER_OT_13); //Makes the output type as push-pull by resetting the corresponding bit in output type register. GPIOG ->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR13_0;//Makes the output medium speed. GPIOG ->PUPDR &= ~(GPIO_PUPDR_PUPDR13);//No pull up or pull down. }void usart_setup(){RCC ->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
GPIOA->MODER |= GPIO_MODER_MODER3_1; GPIOA->AFR[0] |= GPIO_AF7_USART2<<12; RCC->APB1ENR |= RCC_APB1ENR_USART2EN; USART2->CR1 |= USART_CR1_UE; USART2->BRR |= 0x2580; USART2->CR1 |= USART_CR1_RE;}
char usart_read(){ char ch; while(!(USART2->SR & USART_SR_RXNE)) ch = USART2->DR; return ch;}
int main(void){ led_init();usart_setup(); while(1) { GPIOG->BSRRH|=GPIO_BSRR_BS_13; if(usart_read()) GPIOG->BSRRL|=GPIO_BSRR_BS_13; }}Arduino:
&sharpinclude <SoftwareSerial.h>
int incoming = 0; SoftwareSerial mySerial(10, 11); // RX, TXvoid setup() { // put your setup code here, to run once:Serial.begin(9600);
mySerial.begin(9600);}void loop() {
mySerial.write('a');}As arduino uno runs on 5 volt logic, I am already using a level shifter between the stm board and the arduino.
Thanks
#usart #stm32 #arduino2017-08-21 07:55 AM
Don't think you've mastered register level programming. When writing a value to BSRRL/H you don't need to OR it on, just write the ONE bit you want to set or reset, the peripheral does the combination.
Also the BRR is NOT the baud rate itself, ie not 9600 but the APB clock divided by the rate, ie 42,000,000 / 9,600 for a 42 MHz APB1
ie USART2->BRR = 42000000 / 9600; // just write it, don't OR against random content, you want an absolute value