Truyền nhận UART giữa 2 VĐK

emeien_13

Sinh viên đại học
#1
Mình đang có chút vấn đề mong các bạn nào biết về dòng này giúp mình với
Hiện mình đang cần truyền thông giữa 2 con vđk. Nhưng mình k rõ cấu hình thiếu ở đâu mà k thể truyền nhận được
Code mình đây
PHP:
/**
  ******************************************************************************
  * @file    USART/main.c
  * @author  MCD Application Team
  * @version V1.5.1
  * @date    28-June-2013
  * @brief   Main program body
  ******************************************************************************
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"



/** @addtogroup STM8L15x_StdPeriph_Examples
  * @{
  */

/**
  * @addtogroup USART_IrDA
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define Bit_RESET  0
#define Bit_SET    1

/* Uncomment the line below according to the target USART Transmit/Receive used
   in the IrDA example.*/
//#define  USART_IrDA_TRANSMIT  /* USART in IrDA transmit mode. */
#define  USART_IrDA_RECEIVE   /* USART in IrDA receive mode. */
#define PC1 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void USART_Config(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
  /* USART configuration -------------------------------------------*/
  USART_Config();
#ifdef USART_IrDA_TRANSMIT
  GPIO_DeInit(GPIOC);
  GPIO_Init(GPIOC, GPIO_Pin_1, GPIO_Mode_In_FL_No_IT);
  GPIO_DeInit(GPIOE);
  GPIO_Init(GPIOE, GPIO_Pin_7, GPIO_Mode_Out_PP_Low_Fast);
#else /* USART_IrDA_RECEIVE */
  GPIO_DeInit(GPIOE);
  GPIO_Init(GPIOE, GPIO_Pin_LNib, GPIO_Mode_Out_PP_High_Fast);
  GPIO_Init(GPIOE, GPIO_Pin_7, GPIO_Mode_Out_PP_Low_Fast);
  uint8_t c;
#endif /*USART_IrDA_TRANSMIT*/



#ifdef USART_IrDA_TRANSMIT
  while(1)
  {
    if(PC1==0)
    {
      while(PC1==0){}
      USART_SendData8(USART1, '1');
      /* Loop until the end of transmission */
      while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
      GPIO_ToggleBits(GPIOE, GPIO_Pin_7);
    }
  }
#else /* USART_IrDA_RECEIVE */
  while (1)
  { 
    while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET){}
    //GPIO_ToggleBits(GPIOE, GPIO_Pin_7);
    //GPIO_ToggleBits(GPIOE, GPIO_Pin_0);
    c = USART_ReceiveData8(USART1);
    if(c=='1') GPIO_ToggleBits(GPIOE, GPIO_Pin_0);
  }
#endif /*USART_IrDA_TRANSMIT*/
}

/**
  * @brief  Configure USART peripheral
  * @param  None
  * @retval None
  */
static void USART_Config(void)
{
  /* High speed internal clock prescaler: 1*/
  CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
  CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);
  USART_DeInit(USART1);
  /* EVAL COM (USARTx) configuration -----------------------------------------*/
  /* USART configured as follow:
        - BaudRate = 9600 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Receive and transmit enabled
  */
  USART_Init(USART1, (uint32_t)9600, USART_WordLength_8b, USART_StopBits_1,
                   USART_Parity_No,(USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx));


  USART_Cmd(USART1, ENABLE);
}


#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {}
}
#endif

/**
  * @}
  */

/**
  * @}
  */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Mình đang muốn giao tiếp với Nút bấm PC1
Mỗi khi bấm sẽ đảo trạng thái của LED.
Nhưng bấm hoài mà led k sáng gì cả

//#define USART_IrDA_TRANSMIT /* USART in IrDA transmit mode. */
#define USART_IrDA_RECEIVE /* USART in IrDA receive mode. */

Nạp cho con nào sẽ define ở đây
 

ngoc_hân_bk

Học sinh phổ thông
#2
Mình đang có chút vấn đề mong các bạn nào biết về dòng này giúp mình với
Hiện mình đang cần truyền thông giữa 2 con vđk. Nhưng mình k rõ cấu hình thiếu ở đâu mà k thể truyền nhận được
Code mình đây
PHP:
/**
  ******************************************************************************
  * @file    USART/main.c
  * @author  MCD Application Team
  * @version V1.5.1
  * @date    28-June-2013
  * @brief   Main program body
  ******************************************************************************
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"



/** @addtogroup STM8L15x_StdPeriph_Examples
  * @{
  */

/**
  * @addtogroup USART_IrDA
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define Bit_RESET  0
#define Bit_SET    1

/* Uncomment the line below according to the target USART Transmit/Receive used
   in the IrDA example.*/
//#define  USART_IrDA_TRANSMIT  /* USART in IrDA transmit mode. */
#define  USART_IrDA_RECEIVE   /* USART in IrDA receive mode. */
#define PC1 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void USART_Config(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
  /* USART configuration -------------------------------------------*/
  USART_Config();
#ifdef USART_IrDA_TRANSMIT
  GPIO_DeInit(GPIOC);
  GPIO_Init(GPIOC, GPIO_Pin_1, GPIO_Mode_In_FL_No_IT);
  GPIO_DeInit(GPIOE);
  GPIO_Init(GPIOE, GPIO_Pin_7, GPIO_Mode_Out_PP_Low_Fast);
#else /* USART_IrDA_RECEIVE */
  GPIO_DeInit(GPIOE);
  GPIO_Init(GPIOE, GPIO_Pin_LNib, GPIO_Mode_Out_PP_High_Fast);
  GPIO_Init(GPIOE, GPIO_Pin_7, GPIO_Mode_Out_PP_Low_Fast);
  uint8_t c;
#endif /*USART_IrDA_TRANSMIT*/



#ifdef USART_IrDA_TRANSMIT
  while(1)
  {
    if(PC1==0)
    {
      while(PC1==0){}
      USART_SendData8(USART1, '1');
      /* Loop until the end of transmission */
      while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
      GPIO_ToggleBits(GPIOE, GPIO_Pin_7);
    }
  }
#else /* USART_IrDA_RECEIVE */
  while (1)
  {
    while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET){}
    //GPIO_ToggleBits(GPIOE, GPIO_Pin_7);
    //GPIO_ToggleBits(GPIOE, GPIO_Pin_0);
    c = USART_ReceiveData8(USART1);
    if(c=='1') GPIO_ToggleBits(GPIOE, GPIO_Pin_0);
  }
#endif /*USART_IrDA_TRANSMIT*/
}

/**
  * @brief  Configure USART peripheral
  * @param  None
  * @retval None
  */
static void USART_Config(void)
{
  /* High speed internal clock prescaler: 1*/
  CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
  CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);
  USART_DeInit(USART1);
  /* EVAL COM (USARTx) configuration -----------------------------------------*/
  /* USART configured as follow:
        - BaudRate = 9600 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Receive and transmit enabled
  */
  USART_Init(USART1, (uint32_t)9600, USART_WordLength_8b, USART_StopBits_1,
                   USART_Parity_No,(USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx));


  USART_Cmd(USART1, ENABLE);
}


#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {}
}
#endif

/**
  * @}
  */

/**
  * @}
  */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Mình đang muốn giao tiếp với Nút bấm PC1
Mỗi khi bấm sẽ đảo trạng thái của LED.
Nhưng bấm hoài mà led k sáng gì cả

//#define USART_IrDA_TRANSMIT /* USART in IrDA transmit mode. */
#define USART_IrDA_RECEIVE /* USART in IrDA receive mode. */

Nạp cho con nào sẽ define ở đây
 

eva

Kỹ sư
#3
Bạn dùng PC1 trong Transmit ... nên nếu để chế độ Receiver ... thì PC1 không hoạt động.
 

Quảng cáo Google