我目前再做一个项目,其中涉及到利用STM32的IrDA接口接收红外遥控器的信号,遥控器信号频率是37.92khz,接收芯片采用TSOP34838,频率为38khz,TSOP34838输出接STM32F103VCH6的UART5_RX。
file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/T7NEC6JZ`Y$Q)QA`F6KVU40.jpg
但调试好像总是不对。首先一个问题是,载波频率和波特率有什么关系?IrDA初始化代码中需设置波特率,该参数具体如何设置?
按照遥控器的产品说明,每次按键发送数据格式为0x00+0xFF+按键码,但实测各种波特率(600~115200),接收到数据不同,例如实测1200波特率单次按键7接收14个数据,分别为0x00 0x00 0x80 0x66 0x66 0x86 0x00 0x18 0x66 0x98 0x00 0xFE 0x00 0xFE。
IrDA初始化配置代码:
/*******************************************************************************
* Function Name : UART5_Configuration
* Description : 配置UART5,实现红外遥控器接收
* Input : None
* Output : None
* Return : None
* Note : 遥控器SNP-2102载波频率37.92KHz;接收芯片TSOP34838为38KHz;
*******************************************************************************/
void UART5_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Configure USART5 Rx (PD.2)as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 1200;
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_Init(UART5, &USART_InitStructure);
/* Enable the USART5 */
USART_Cmd(UART5, ENABLE);
/* Set the USART5 prescaler,在红外的通常模式下PSC[7:0]只能设置为00000001 */
USART_SetPrescaler(UART5, 0x1);
/* Configure the USART5 IrDA mode */
USART_IrDAConfig(UART5, USART_IrDAMode_Normal);
/* Enable the USART5 IrDA mode */
USART_IrDACmd(UART5, ENABLE);
}
主程序中等待接收红外控制信号:
while (1)
{
for(i=0;i<28;i++)
{
/* Wait until a byte is received */
while(USART_GetFlagStatus(UART5, USART_FLAG_RXNE) == RESET)
{
}
/* Read the received byte */
ReceivedData = USART_ReceiveData(UART5);
}
switch(ReceivedData[2])
{
case 0x42: //7
break;
case 0x52: //8
break;
case 0x4A: //9
break;
default:
break;
}
}
再次深表谢意! |