环境:keil
语言:C
框架:无
思路:
串口初始化(设置引脚、优先级、通讯参数、使能等)
迪文屏->主板,中断接收
迪文屏<-主板,按需发送
部分代码:
串口初始化
void USART3_Init(void)//DWIN
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// Configure one bit for preemption priority
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// Enable the USART1 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Configure USART3 Rx as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Configure USART3 Tx as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_StructInit(&USART_InitStructure);
// USART1 configured as follow:
// - BaudRate = 115200 baud
// - Word Length = 8 Bits
// - One Stop Bit
// - No parity
// - Receive and transmit enabled
// - Hardware flow control disabled (RTS and CTS signals)
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_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART3, &USART_InitStructure);
//Enable USART3 Receive and Send interrupts
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
/* Enable USART3 */
USART_Cmd(USART3, ENABLE);
}
迪文屏->主板
void USART3_IRQHandler(void) //迪文屏数据处理
{
while(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
DWDataBuf[DWDataStep]=(char)USART3->DR;
switch(DataModeStep)
{
case 0:
if(DWDataBuf[DWDataStep]==0x5A)
{
DWDataStep++;
DataModeStep++;
}
break;
case 1:
if(DWDataBuf[DWDataStep]==0xA5) //5a a5 包头
{
DWDataStep++;
DataModeStep++;
DWToPos=0;
}
else //非包头数据舍弃
DWDataStep=0;
break;
case 2:
DWDataLen=DWDataBuf[DWDataStep];//长度
DWDataStep++;
DataModeStep++;
break;
case 3:
DWCommand=DWDataBuf[DWDataStep];//命令
DWDataStep++;
DataModeStep++;
break;
case 4:
DWValidDatBuf[DWToPos]=DWDataBuf[DWDataStep];
DWToPos++;
DWDataStep++;
if(DWToPos==(DWDataLen-1))
{
DWDataTime=0; //迪文屏数据接收超时判断标志初始化
DataModeStep=0;
DWDataStep=0;
//触摸屏数据解析标志=true
//这部分代码就不给出来了,
//解析内容应该是由主循环进行处理
}
break;
}
}
}
//迪文屏接收数据超时判断,可以放到某个定时器里面
if(DataModeStep>0)//迪文屏数据接收超时
{
DWDataTime++;
if(DWDataTime>100)
{
DWDataTime=0;
DataModeStep=0;
DWDataStep=0;
}
}
迪文屏<-主板
这里只给出一个示例函数
//迪文屏 USART3 发送数据
void DWDataSend(unsigned char *buf ,unsigned short num )
{
unsigned short i;
for(i=0; i<num; i++)
{
while((USART3->SR & USART_FLAG_TXE) == RESET);
USART3->DR=buf[i];
}
}
//发送数据给迪文屏
void Send485RelayStatus(unsigned char status)
{
DWSendDatabuf[0]=0x5a;DWSendDatabuf[1]=0xa5;DWSendDatabuf[2]=0x05;DWSendDatabuf[3]=0x82;
DWSendDatabuf[4]=0x04;DWSendDatabuf[5]=0x11;
DWSendDatabuf[6]=0;DWSendDatabuf[7]=status;
DWDataSend(DWSendDatabuf,8);
}