从arduino接收数据并将其发送到stml476RG

时间:2018-10-03 13:38:40

标签: c arduino arm stm32 usart

我希望从arduino接收串行数据,然后将接收到的数据发送回arduino,但是我在接收正确的数据时遇到了麻烦。以下是我的代码,我不确定哪里出了问题,有人可以看到任何问题吗? 我正在使用寄存器级编程,并在STM32L476RG开发板上使用了USART 3,我正在使用ADM485芯片在每个串行端口之间传输数据。

#include "stm32L476XX.h"
#include "stdio.h"

int T ;
int R ;
uint8_t z;

void USART3_init(void)
{

   USART3->BRR   |= 26UL<<4;  //BAUD RATE MANTISSA 234UL<<4
   USART3->BRR   |= 1UL<<0;   // BAUD RATE FRACTION 6UL<<0  
   USART3->CR1   |= 1UL<<0;  //enable USART
   USART3->CR1      ^= 0UL<<28; //1 start bit, 8 data bits, n stop bit
   USART3->CR1   ^= 0UL<<12; //also sets word length to 8 data bits
    USART3->CR1     ^= 0UL<<10; //no parity control
    USART3->CR1   |= 1UL<<6;   //Transmission complete interrupt enabled
   USART3->CR1      |= 1UL<<7;   //TXE interrupt enable
    USART3->CR1   |= 1UL<<5;   //RXNEIE interrupt enable    
    USART3->CR2   ^=00UL<<12;    //1 stop bit
    USART3->CR1     |= 1UL<<3;   // enable transmitter
    USART3->CR1   |= 1UL<<2;  // enable receiver

      NVIC_EnableIRQ(USART3_IRQn); //enable USART interrupts 

}


void Delay(void)
{
        uint32_t i=0;
        for (i=0; i<50000; i++){}
}


void USART3_IRQHandler(void)
{
 //Delay();

    if ((USART3->ISR>>5)&1UL) //Check if RXNE interrupt is high
{ 
  z = USART3->RDR; //put what is on RDR into Z (also clears RXNE flag)
    GPIOB->ODR    |= 1UL<<5; //set transceiver into transmit mode       USART3->TDR = z; // Clears TXIE flag and outputs z on TX

 }

 if ((USART3->ISR>>7)&1UL) //Check if TXIE interrupt is high
 {

GPIOB->ODR    &= 0UL<<5; //sets transceiver in receive mode

 } 

 if  ((USART3->ISR>>6)&1UL) //Check if TC interrupt is high
 {  
    USART3->ICR   |= 1UL<<6; //Clear TC flag
  GPIOB->ODR    &= 0UL<<5;  //Set transceiver in receive mode    
 } 

}

void RCC_GPIO_init(void)
 {
RCC->APB1ENR1 |= 1UL<<18; //Enable USART 3 clock
    RCC->AHB2ENR    |= 1UL<<1; //Enable GPIOB clock

  GPIOB->MODER   =  0; //reset all register bits
  GPIOB->MODER  |= 2UL<<20; //enable GPIOB pin 10 as alternate function
  GPIOB->MODER  |= 2UL<<22; //enable GPIOB pin 11 as alternate function
  GPIOB->MODER  |= 1UL<<10; //enable GPIOB pin 5 as output

  GPIOB->AFR[1]  =  0;   //reset all register bits
  GPIOB->AFR[1] |= 7UL<<8; //enable GPIOB pin 10 as TX
  GPIOB->AFR[1] |= 7UL<<12; //enable GPIOB pin 10 as RX

  GPIOB->ODR    ^= 0UL<<5;  //GPIOB pin 5 as low (to put ADM485 transceiver in receive mode)
}


int main (void)
{

   RCC_GPIO_init();
 USART3_init();
while(1)
        {                       
    }       

}

0 个答案:

没有答案