/******************************************************
*函数名称:IR_SendByte
*备注:IO模拟串口发送1bit
*入口参数:val 发送的值
*作者:han
******************************************************/
void IR_SendByte(uint8_t val)
{
uint8_t i;
JC24B_Set_L; //起始位,拉低电平
delay_us(IO_UASRT_bound);
for(i = 0; i < 8; i++) //8位数据位
{
if(val & 0x01)
{
JC24B_Set_H;
}
else
JC24B_Set_L;
delay_us(IO_UASRT_bound);
val >>= 1;
}
JC24B_Set_H; //停止位,拉高电平
delay_us(IO_UASRT_bound);
}
/******************************************************
*函数名称:IO_UASRT_Send
*备注:IO模拟串口发送一包数据
*入口参数:st 发送的数组 len 发送长度
*作者:han
******************************************************/
void IO_UASRT_Send(uint8_t *st,uint16_t len)
{
int i=0;
while ((len--)!=0)
{
IR_SendByte(st[i]);
i++;
}
}
①IO配置,中断接收
/**********************IO模拟接收 PB11**************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //浮空输入
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_11);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB,EXTI_PinSource11);//映射
EXTI_InitStructure.EXTI_Line=EXTI_Line11;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_15_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line11);//清楚中断标志位
②中断接收
/******************************************************
*函数名称:EXTI4_15_IRQHandler
*备注:IO模拟串口接收
*入口参数:
*作者:han
******************************************************/
uint8_t tmp;
uint8_t USART_RX_BUF[USART_REC_LEN];
uint16_t USART_RX_STA=0;
void EXTI4_15_IRQHandler(void)
{
uint8_t i=9;
if(EXTI_GetITStatus(EXTI_Line11) != RESET)
{
/* Disable the Selected IRQ Channels -------------------------------------*/
NVIC->ICER[EXTI4_15_IRQn >> 0x05] =(uint32_t)0x01 << (EXTI4_15_IRQn & (uint8_t)0x1F);
EXTI_ClearITPendingBit(EXTI_Line11);
delay_us(30);
while(i--)
{
tmp >>=1;
if(GPIOB->IDR&GPIO_Pin_11) tmp |=0x80;
delay_us(IO_UASRT_bound);
}
if((USART_RX_STA&0x8000)==0)
{
if(USART_RX_STA&0x4000)
{
if(tmp!=0x0a) USART_RX_STA=0;
else USART_RX_STA |=0x8000;
}
else
{
if(tmp==0x0d) USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=tmp ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
}
}
}
EXTI_ClearITPendingBit(EXTI_Line11);
NVIC->ISER[EXTI4_15_IRQn >> 0x05] =
(uint32_t)0x01 << (EXTI4_15_IRQn & (uint8_t)0x1F);
}
}
③其他部分
#define IO_UASRT_bound 104 //波特率计算:(1/波特率)*1000000