¿Cómo uso usart1 y usart2 juntos en stm32f103?

0

¿Cómo puedo usar tanto usart1 (PA.09, PA.10) como usart2 (PA.02, PA.03) al mismo tiempo?

Puedo enviar y recibir datos a través de Usart1, pero cuando habilito usart2, ¡no se pueden enviar datos por usart1 y usart2!

Yo uso stm32f103c8t6.

Código:

int main(void)
{
    GPIO_CONFIG();

    /* Configure the USART1 */          
    USART_Configuration();      
    printf("\r\nWELCOME usart1\r\n");// data received by terminal 1

    /* Configure the USART2 */              
    USART2_Configuration();
    USART2_Send("Hello usart2",9);// data not received by terminal 2

    printf("\r\nWELCOME usart1\r\n");// data not received by terminal 1

    while(1){}

}

Las configuraciones de usart1 y usart2 son las mismas, y uso 2 terminales para cada usart por separado.

Editar:
La configuración de usart1 y 2 es la misma,
Configuraciones de usart2 en 'usart2.c':

void USART2_IRQHandler(void)
{
  uint8_t receivedChar;  
   if ((USART2->SR & USART_FLAG_RXNE) != (u16)RESET)           
   {          
        receivedChar = USART_ReceiveData(USART2);
        if(USART2_Rx_BufferIndex == Max_Rx_Buffer_Size ||    USART2_Rx_BufferIndex==USART2_Desired_Buffer_Size ||  receivedChar=='\n')
        {
            USART2_RxBuffer[USART2_Rx_BufferIndex] = receivedChar;
                          USART2_flag = 1;
        }
        else
        {
            USART2_RxBuffer[USART2_Rx_BufferIndex++] = receivedChar;
        }
        USART2_RxBuffer[USART2_Rx_BufferIndex] = '
void GPIO_CONFIG(void)
   {
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
      /* Configure USART1 Tx (PA.09) and UARTT2 Tx (PA.02) as alternate   function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART1 Rx (PA.10) and USART2 Rx (PA.03) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 }
'; } } void USART2_Recieve(uint16_t size) { if(size < Max_Rx_Buffer_Size) { USART2_Desired_Buffer_Size = size; } else { USART2_Desired_Buffer_Size = Max_Rx_Buffer_Size; } USART2_flag = 0; while(USART2_flag == 0); USART2_recievedData = USART2_RxBuffer; USART2_recievedData[USART2_Rx_BufferIndex+1] ='
int main(void)
{
    GPIO_CONFIG();

    /* Configure the USART1 */          
    USART_Configuration();      
    printf("\r\nWELCOME usart1\r\n");// data received by terminal 1

    /* Configure the USART2 */              
    USART2_Configuration();
    USART2_Send("Hello usart2",9);// data not received by terminal 2

    printf("\r\nWELCOME usart1\r\n");// data not received by terminal 1

    while(1){}

}
'; USART2_Rx_BufferIndex=0; } void USART2_RecieveFix(uint16_t size) { if(size < Max_Rx_Buffer_Size) { USART2_Desired_Buffer_Size = size; } else { USART2_Desired_Buffer_Size = Max_Rx_Buffer_Size; } USART2_Rx_BufferIndex = 0; while(USART2_Rx_BufferIndex < size) { USART2_flag = 0; while(USART2_flag ==0); USART2_Rx_BufferIndex++; } USART2_recievedData = USART2_RxBuffer; USART2_recievedData[USART2_Rx_BufferIndex+1] ='
void USART2_IRQHandler(void)
{
  uint8_t receivedChar;  
   if ((USART2->SR & USART_FLAG_RXNE) != (u16)RESET)           
   {          
        receivedChar = USART_ReceiveData(USART2);
        if(USART2_Rx_BufferIndex == Max_Rx_Buffer_Size ||    USART2_Rx_BufferIndex==USART2_Desired_Buffer_Size ||  receivedChar=='\n')
        {
            USART2_RxBuffer[USART2_Rx_BufferIndex] = receivedChar;
                          USART2_flag = 1;
        }
        else
        {
            USART2_RxBuffer[USART2_Rx_BufferIndex++] = receivedChar;
        }
        USART2_RxBuffer[USART2_Rx_BufferIndex] = '
void GPIO_CONFIG(void)
   {
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
      /* Configure USART1 Tx (PA.09) and UARTT2 Tx (PA.02) as alternate   function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART1 Rx (PA.10) and USART2 Rx (PA.03) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 }
'; } } void USART2_Recieve(uint16_t size) { if(size < Max_Rx_Buffer_Size) { USART2_Desired_Buffer_Size = size; } else { USART2_Desired_Buffer_Size = Max_Rx_Buffer_Size; } USART2_flag = 0; while(USART2_flag == 0); USART2_recievedData = USART2_RxBuffer; USART2_recievedData[USART2_Rx_BufferIndex+1] ='%pre%'; USART2_Rx_BufferIndex=0; } void USART2_RecieveFix(uint16_t size) { if(size < Max_Rx_Buffer_Size) { USART2_Desired_Buffer_Size = size; } else { USART2_Desired_Buffer_Size = Max_Rx_Buffer_Size; } USART2_Rx_BufferIndex = 0; while(USART2_Rx_BufferIndex < size) { USART2_flag = 0; while(USART2_flag ==0); USART2_Rx_BufferIndex++; } USART2_recievedData = USART2_RxBuffer; USART2_recievedData[USART2_Rx_BufferIndex+1] ='%pre%'; USART2_Rx_BufferIndex=0; } char *USART2_GetReceivedData(void) { return USART2_recievedData; } void USART2_NVIC_Configuration(void){ NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USARTx Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void USART2_Configuration(void){ USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); /* Enable USART2 */ USART_Cmd(USART2, ENABLE); USART2_NVIC_Configuration(); /* Enable the USART2 Receive interrupt: this interrupt is generated when the USART2 receive data register is not empty */ USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); } void USART2_Send(unsigned char *pucBuffer, unsigned long ulCount) { // // Loop while there are more characters to send. // while(ulCount--) { USART_SendData(USART2, *pucBuffer++);// Last Version USART_SendData(USART2,(uint16_t) *pucBuffer++); /* Loop until the end of transmission */ while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) { } } }
'; USART2_Rx_BufferIndex=0; } char *USART2_GetReceivedData(void) { return USART2_recievedData; } void USART2_NVIC_Configuration(void){ NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USARTx Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void USART2_Configuration(void){ USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); /* Enable USART2 */ USART_Cmd(USART2, ENABLE); USART2_NVIC_Configuration(); /* Enable the USART2 Receive interrupt: this interrupt is generated when the USART2 receive data register is not empty */ USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); } void USART2_Send(unsigned char *pucBuffer, unsigned long ulCount) { // // Loop while there are more characters to send. // while(ulCount--) { USART_SendData(USART2, *pucBuffer++);// Last Version USART_SendData(USART2,(uint16_t) *pucBuffer++); /* Loop until the end of transmission */ while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) { } } }

y en 'main.c'

%pre%     
pregunta Hamid

2 respuestas

1

El problema estaba en la función GPIO_CONFIG(void) , tengo que usar la función RCC_APB2PeriphClockCmd para usart1 y RCC_APB1PeriphClockCmd para usart2 . Dado que usart1 pertenece a APB2PeriphClock y usart2 pertenece a RCC_APB1PeriphClock . Por lo tanto, la solución final es:

void GPIO_CONFIG(void)
 {
   GPIO_InitTypeDef GPIO_InitStructure;    
   // enable uart 1 preph clock mode
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA ,     ENABLE);

    // enable uart 2 preph clock mode
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

 /* Configure USART1 Tx (PA.09) and UARTT2 Tx (PA.02) as alternate        function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART1 Rx (PA.10) and USART2 Rx (PA.03) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
    
respondido por el Hamid
1

El único lugar donde veo que habilita los relojes periféricos es esta línea en GPIO_CONFIG , que solo habilita USART1 y GPIOA:

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

USART2 no funcionará a menos que también habilites su reloj. Asegúrate de habilitarlo en el bus correcto.

    
respondido por el duskwuff

Lea otras preguntas en las etiquetas