【Hanker M4】 第11篇 645规约简单uart通信+LCD+EEPROM
1.前言
前面几篇简单的实现了IIC——EEPROM数据的读写,LCD的按键和循环显示,以及简单的645规约,现在将这些整合起来
实现如下大致功能:
(1)开发板下载程序后上电,
(2)如果不进行按键操作,则LCD进行循环显示,循环屏数为6屏,分别显示为:
“Current :”屏
“software:”屏
电压采样值“Voltage=”屏,
“Thetemperature:”屏
“Baud Rate:115200”屏
“ALLDisplay Screen:6”屏。
(3)如果在循环显示过程中,按键按下,则进入键显状态,按键按下一翻一屏,不按则保持上次按键按下的屏显,如果十几秒内检测不到按键按下了,则回到上次循环显示的屏数进行显示。
键盘显示屏分别为:
"Correspondence Address : "屏
"Meter No:"屏
电压采样值屏“Voltage=”屏
"The temperature:6 "屏
"BaudRate:115200"屏
"ALL Display Screen:6 "屏
(4)这里
"Correspondence Address : "屏
"Meter No:"屏
在没有进行串口645规约通信时,通信地址和表号是显示为00000000000或者无显示,如果通过串口助手发送645规约对应的帧,即会从EEPROM里读取相应的通信地址和表号,并且在LCD上显示,串口会收到相应的响应帧。
2.部分源代码
(1)预先写EEPROM
- //文件信息,排列顺序必须与文件编号保持一致
- const sDl645FileInfo_TypeDef Dl645FileInfo[] =
- {
-
- { Dl645FileAddr_HighPara , Dl645FileLen_HighPara , Dl645FileItemInfoNum_HighPara_Max , Dl645FileItemInfo_HighPara }
- };
-
- const sDl645HighParaFile_TypeDef HighParaDefault =
- {
- {0x24 , 0x51 , 0x00 , 0x00 , 0x05 , 0x08} ,
- {0x11 , 0x11 , 0x11 , 0x22 , 0x22 , 0x22} ,
- }
- void fnDl645File_Init(void)
- {
- // fnDl645File_Write(Dl645FileId_HighPara , 0 , (u8 *)&HighParaDefault , Dl645FileLen_HighPara);
- AtmelWrite((u8 *)&HighParaDefault, 1, 12);
- }
(2) LCD显示表号
- LCD_Clear();
- GrContextFontSet(&sContext, &g_sFontCm20);
- GrStringDraw(&sContext, "Key Display No: 2", -1, 0, 0, 0);
- GrStringDraw(&sContext, "Meter No: ", -1, 0, 60, 0);
- m[10]=fnHexToBcd_u8(bhaoData[5]-0x33)/10+'0';//取下一个数字
- m[11]=fnHexToBcd_u8(bhaoData[5]-0x33)%10+'0';//取下一个数字
- m[8]=fnHexToBcd_u8(bhaoData[4]-0x33)/10+'0';//取下一个数字
- m[9]=fnHexToBcd_u8(bhaoData[4]-0x33)%10+'0';
- m[6]=fnHexToBcd_u8(bhaoData[3]-0x33)/10+'0';
- m[7]=fnHexToBcd_u8(bhaoData[3]-0x33)%10+'0';
- m[4]=fnHexToBcd_u8(bhaoData[2]-0x33)/10+'0';
- m[5]=fnHexToBcd_u8(bhaoData[2]-0x33)%10+'0';
- m[2]=fnHexToBcd_u8(bhaoData[1]-0x33)/10+'0';
- m[3]=fnHexToBcd_u8(bhaoData[1]-0x33)%10+'0';
- m[0]=fnHexToBcd_u8(bhaoData[0]-0x33)/10+'0';
- m[1]=fnHexToBcd_u8(bhaoData[0]-0x33)%10+'0';
- GrStringDraw(&sContext, m, -1, 210, 60, 1);
(3)串口中断
- void UARTIntHandler(void)
- {
- u32 ulStatus;
- //获取中断状态
- ulStatus = ROM_UARTIntStatus(UART5_BASE, true);
- //清除中断标志
- ROM_UARTIntClear(UART5_BASE, ulStatus);
- //直到串口FIFO中没有数据时才退出循环
- while(ROM_UARTCharsAvail(UART5_BASE))
- {
- //读串口接收的字符并回发
- //// ROM_UARTCharPutNonBlocking(UART5_BASE,
- // temp= ROM_UARTCharGetNonBlocking(UART5_BASE);
- //temp = UARTCharGet(UART5_BASE);
- // temp=temp-1;
- uartflag=1;
- temp= ROM_UARTCharGetNonBlocking(UART5_BASE);
- // UARTprintf("bbbbbb");
- if(ComPack[0].EFlag == SCOMPK_EFLAG_IDLE)
- { // UARTprintf("68686868");
- RxLen =0;
- if(temp == 0x68)
- {
- ComPack[0].RxBuf[RxLen++]=temp;
- ComPack[0].EFlag=SCOMPK_EFLAG_RXHEAD1;
- }
- }
- else if(ComPack[0].EFlag == SCOMPK_EFLAG_RXHEAD1)
- {
- if(RxLen>6)
- {
- if(temp == 0x68)
- {// UARTprintf("6868686822");
- ComPack[0].EFlag=SCOMPK_EFLAG_RXHEAD2;
- ComPack[0].RxBuf[RxLen++]=temp;
- }
- else
- {
- RxLen=0x0;
- ComPack[0].EFlag = SCOMPK_EFLAG_IDLE ;
- }
- }
- else
- {
- ComPack[0].RxBuf[RxLen++]=temp;
- }
-
- }
- else if(ComPack[0].EFlag == SCOMPK_EFLAG_RXHEAD2)
- { // UARTprintf("data");
- ComPack[0].RxBuf[RxLen++]=temp;
- if(RxLen>8)ComPack[0].EFlag=SCOMPK_EFLAG_RXDATALEN;
-
- }
- else if(ComPack[0].EFlag == SCOMPK_EFLAG_RXDATALEN)
- {
- ComPack[0].RxBuf[RxLen++]=temp;
- if(RxLen>(ComPack[0].RxBuf[9]+10))ComPack[0].EFlag=SCOMPK_EFLAG_RXEND;
- }
- else if(ComPack[0].EFlag == SCOMPK_EFLAG_RXEND)
- {
- if(temp == 0x16)
- { // UARTprintf("16161616");
- ComPack[0].EFlag=SCOMPK_EFLAG_RXFINISH;
- ComPack[0].RxBuf[RxLen++]=temp;
- // RxLen=0;
- }
- else
- {
- RxLen=0x0;
- ComPack[0].EFlag =SCOMPK_EFLAG_IDLE;
- }
-
- }
- }
- }
3.显示的图片
按键显示第一屏:
按键显示第2屏:
自动循环显示第5屏(当按键不触发时,自动会回到自动循环显示)
串口帧发送与应答
PS:这里收到的55 55 55 44 44 44
即是对应的表号,因为645规约要求数据+0x33处理
本帖最后由 常见泽1 于 2015-6-7 15:35 编辑