利用stm32与串口屏通讯
工程路径为..\STM32\DCDEMO7\RVMDK\DCDEMO7.uvproj ,请用KEIL4(MDK4)以上版本打开
单片机源程序如下:
- #include "hmi_driver.h"
- #include "hmi_user_uart.h"
- #include "cmd_queue.h"
- #include "cmd_process.h"
- #include "stdio.h"
- #include "hw_config.h"
- #include "ulitity.h"
- #include "string.h"
- #include "rtc.h"
- #include "stm32f10x_bkp.h"
- #include "stm32f10x_rtc.h"
- #include "stm32f10x_pwr.h"
- //#include "key_IN.h"
- #define TIME_100MS 10 //100毫秒(10个单位)
- volatile uint32 timer_tick_count = 0; //定时器节拍
- uint8 cmd_buffer[CMD_MAX_SIZE]; //指令缓存
- static uint16 current_screen_id = 0; //当前画面ID
- static int32 test_value = 0; //测试值
- static uint8 update_en = 0; //更新标记
- static int32 meter_flag = 0; //仪表指针往返标志位
- static int32 num = 0; //曲线采样点计数
- static uint8 sec = 1; //时间秒
- static int32 curves_type = 0; //曲线标志位 0为正弦波,1为锯齿波
- static int32 second_flag=0; //时间标志位
- static int32 Progress_Value = 0; //进度条的值
- static int32 icon_flag = 0; //图标标志位
- //int32 key_flag = 0;
- static uint16 screenid = 0;
- extern uint32 OP_XGUS;
- int t1 = 5;
- void UpdateUI(void); //更新UI数据
- void picxdisp(uchar n,int x1,int y1,int x2,int y2) ;
- /*!
- * \brief 程序入口
- */
- int main()
- {
- static int32 test_value = 0; //测试值
- uint32 timer_tick_last_update = 0; //上一次更新的时间
- qsize size = 0;
- //int PRESS ;
- //配置时钟
- Set_System();
- //配置串口中断
- Interrupts_Config();
- //配置时钟节拍
- systicket_init();
-
- // KEY_Init();
- RTC_int();
-
- //串口初始化
- Uart2Init(9600);
- UartInit(9600);
-
- //清空串口接收缓冲区
- queue_reset();
- //延时等待串口屏初始化完毕,必须等待300ms
- delay_ms(300);
- while(1)
- {
- size = queue_find_cmd(cmd_buffer,CMD_MAX_SIZE); //从缓冲区中获取一条指令
- if(size>0&&cmd_buffer[1]!=0x07)//接收到指令
- {
- ProcessMessage((PCTRL_MSG)cmd_buffer, size);//指令处理
- }
-
- /*PRESS = KEY_Scan();
- switch(PRESS)
- {
-
- case 1 :
- while(key_flag)
- {
- KEY_Scan();
- SetScreen(screenid);
- screenid++;
- }
- break;
- case 2 :
- SetScreen(screenid);
- screenid -= 1;
- break;
- case 3 :
- SetScreen(screenid);
- screenid += 1;
- break;
- }*/
- }
- }
- /*!
- * \brief 消息处理流程
- * \param msg 待处理消息
- * \param size 消息长度
- */
- void ProcessMessage( PCTRL_MSG msg, uint16 size )
- {
- uint8 cmd_type = msg->cmd_type; //指令类型
- uint8 ctrl_msg = msg->ctrl_msg; //消息的类型
- uint8 control_type = msg->control_type; //控件类型
- uint16 screen_id = PTR2U16(&msg->screen_id); //画面ID
- uint16 control_id = PTR2U16(&msg->control_id); //控件ID
- uint32 value = PTR2U32(msg->param); //数值
- switch(cmd_type)
- {
- case NOTIFY_TOUCH_PRESS: //触摸屏按下
- case NOTIFY_TOUCH_RELEASE: //触摸屏松开
- NotifyTouchXY(cmd_buffer[1],PTR2U16(cmd_buffer+2),PTR2U16(cmd_buffer+4));
- break;
- case NOTIFY_WRITE_FLASH_OK: //写FLASH成功
- NotifyWriteFlash(1);
- break;
- case NOTIFY_WRITE_FLASH_FAILD: //写FLASH失败
- NotifyWriteFlash(0);
- break;
- case NOTIFY_READ_FLASH_OK: //读取FLASH成功
- NotifyReadFlash(1,cmd_buffer+2,size-6); //去除帧头帧尾
- break;
- case NOTIFY_READ_FLASH_FAILD: //读取FLASH失败
- NotifyReadFlash(0,0,0);
- break;
- case NOTIFY_READ_RTC: //读取RTC时间
- NotifyReadRTC(cmd_buffer[2],cmd_buffer[3],cmd_buffer[4],cmd_buffer[5],cmd_buffer[6],cmd_buffer[7],cmd_buffer[8]);
- break;
- case NOTIFY_CONTROL:
- {
- if(ctrl_msg==MSG_GET_CURRENT_SCREEN) //画面ID变化通知
- {
- NotifyScreen(screen_id); //画面切换调动的函数
- }
- else
- {
- switch(control_type)
- {
- case kCtrlButton: //按钮控件
- NotifyButton(screen_id,control_id,msg->param[1]);
- break;
- case kCtrlText: //文本控件
- NotifyText(screen_id,control_id,msg->param);
- break;
- case kCtrlProgress: //进度条控件
- NotifyProgress(screen_id,control_id,value);
- break;
- case kCtrlSlider: //滑动条控件
- NotifySlider(screen_id,control_id,value);
- break;
- case kCtrlMeter: //仪表控件
- NotifyMeter(screen_id,control_id,value);
- break;
- case kCtrlMenu: //菜单控件
- NotifyMenu(screen_id,control_id,msg->param[0],msg->param[1]);
- break;
- case kCtrlSelector: //选择控件
- NotifySelector(screen_id,control_id,msg->param[0]);
- break;
- case kCtrlRTC: //倒计时控件
-
- NotifyTimer(screen_id,control_id);
- break;
- default:
- break;
- }
- }
- break;
- }
- default:
- break;
- }
- }
- /*!
- * \brief 画面切换通知
- * \details 当前画面改变时(或调用GetScreen),执行此函数
- * \param screen_id 当前画面ID
- */
- void NotifyScreen(uint16 screen_id)
- {
- //TODO: 添加用户代码
- //在工程配置中开启画面切换通知,记录当前画面ID
-
- //进到二维码页面生成二维码
-
- }
- /*!
- * \brief 触摸坐标事件响应
- * \param press 1按下触摸屏,3松开触摸屏
- * \param x x坐标
- * \param y y坐标
- */
- void NotifyTouchXY(uint8 press,uint16 x,uint16 y)
- {
-
- }
- /*!
- * \brief 更新数据
- */
- void UpdateUI()
- {
-
-
- }
- /*!
- * \brief 按钮控件通知
- * \details 当按钮状态改变(或调用GetControlValue)时,执行此函数
- * \param screen_id 画面ID
- * \param control_id 控件ID
- * \param state 按钮状态:0弹起,1按下
- */
- void NotifyButton(uint16 screen_id, uint16 control_id, uint8 state)
- {
-
- if(screen_id == 0)
- {
- if(control_id==32)
- {
- GetControlValue(0,26);
-
- }
- if(control_id==33)
- {
- GetControlValue(0,10);
- }
- if(control_id==34)
- {
- GetControlValue(0,22);
- }
- if(control_id==37)
- {
- GetControlValue(0,25);
- }
- if(control_id==35)
- {
- SetScreen(screenid);
- screenid -= 1;
- }
- if(control_id==36)
- {
- SetScreen(screenid);
- screenid += 1;
- }
- }
- }
- /*!
- * \brief 文本控件通知
- * \details 当文本通过键盘更新(或调用GetControlValue)时,执行此函数
- * \param screen_id 画面ID
- * \param control_id 控件ID
- * \param str 文本控件内容
- */
- void NotifyText(uint16 screen_id, uint16 control_id, uint8 *str)
- {
- int32 value=0;
- int32 zalue;
- sscanf(str,"%ld",&value); //把字符串转换为整数
- UnlockDeviceConfig();
- if(screen_id==0) //画面ID2:文本设置和显示
- {
- if(control_id==26) //最高电压
- {
- //限定数值范围(也可以在文本控件属性中设置)
- switch(value)
- {
- case 2400:
- zalue=1;
- SetCommBps(zalue);
- UartInit(2400);
- SetTextInt32(0,41,zalue,0,1);
- break;
- case 4800:
- zalue=2;
- SetCommBps(zalue);
- UartInit(4800);
- SetTextInt32(0,41,zalue,0,1);
- break;
- case 9600:
- zalue=3;
- SetCommBps(zalue);
- UartInit(9600);
- SetTextInt32(0,41,zalue,0,1);
- break;
- case 19200:
- zalue=4;
- SetCommBps(zalue);
- UartInit(19200);
- SetTextInt32(0,41,zalue,0,1);
- break;
- case 38400:
- zalue=5;
- SetCommBps(zalue);
- UartInit(38400);
- SetTextInt32(0,41,zalue,0,1);
- break;
- case 57600:
- zalue=6;
- SetCommBps(zalue);
- UartInit(57600);
- SetTextInt32(0,41,zalue,0,1);
- break;
- case 115200:
- zalue=7;
- SetCommBps(zalue);
- UartInit(115200);
- SetTextInt32(0,41,zalue,0,1);
- break;
- }
- }
- }
- LockDeviceConfig();
- }
- /*!
- * \brief 进度条控件通知
- * \details 调用GetControlValue时,执行此函数
- * \param screen_id 画面ID
- * \param control_id 控件ID
- * \param value 值
- */
- void NotifyProgress(uint16 screen_id, uint16 control_id, uint32 value)
- {
-
- }
- /*!
- * \brief 滑动条控件通知
- * \details 当滑动条改变(或调用GetControlValue)时,执行此函数
- * \param screen_id 画面ID
- * \param control_id 控件ID
- * \param value 值
- */
- void NotifySlider(uint16 screen_id, uint16 control_id, uint32 value)
- {
-
- }
- /*!
- * \brief 仪表控件通知
- * \details 调用GetControlValue时,执行此函数
- * \param screen_id 画面ID
- * \param control_id 控件ID
- * \param value 值
- */
- void NotifyMeter(uint16 screen_id, uint16 control_id, uint32 value)
- {
- //TODO: 添加用户代码
- }
- /*!
- * \brief 菜单控件通知
- * \details 当菜单项按下或松开时,执行此函数
- * \param screen_id 画面ID
- * \param control_id 控件ID
- * \param item 菜单项索引
- * \param state 按钮状态:0松开,1按下
- */
- void NotifyMenu(uint16 screen_id, uint16 control_id, uint8 item, uint8 state)
- {
- //TODO: 添加用户代码
- }
- /*!
- * \brief 选择控件通知
- * \details 当选择控件变化时,执行此函数
- * \param screen_id 画面ID
- * \param control_id 控件ID
- * \param item 当前选项
- */
- void NotifySelector(uint16 screen_id, uint16 control_id, uint8 item)
- {
-
- }
- /*!
- * \brief 定时器超时通知处理
- * \param screen_id 画面ID
- * \param control_id 控件ID
- */
- void NotifyTimer(uint16 screen_id, uint16 control_id)
- {
- //TODO: 添加用户代码
- }
- /*!
- * \brief 读取用户FLASH状态返回
- * \param status 0失败,1成功
- * \param _data 返回数据
- * \param length 数据长度
- */
- void NotifyReadFlash(uint8 status,uint8 *_data,uint16 length)
- {
- //TODO: 添加用户代码
- }
- /*!
- * \brief 写用户FLASH状态返回
- * \param status 0失败,1成功
- */
- void NotifyWriteFlash(uint8 status)
- {
- //TODO: 添加用户代码
- }
- /*!
- * \brief 读取RTC时间,注意返回的是BCD码
- * \param year 年(BCD)
- * \param month 月(BCD)
- * \param week 星期(BCD)
- * \param day 日(BCD)
- * \param hour 时(BCD)
- * \param minute 分(BCD)
- * \param second 秒(BCD)
- */
- void NotifyReadRTC(uint8 year,uint8 month,uint8 week,uint8 day,uint8 hour,uint8 minute,uint8 second)
- {
- //sec =(0xff & (second>>4))*10 +(0xf & second); //BCD码转十进制
- }
复制代码
所有资料51hei提供下载:
液晶屏测试工装程序.rar
(379.37 KB, 下载次数: 76)
|