|
此代碼為串口屏的代碼,stm32F103的板子,附帶里面的代碼可實(shí)現(xiàn)數(shù)據(jù)回傳,數(shù)據(jù)接收,以及圖像的顯示等等。是基于FIFO的串口接收方式,上傳上來給大家一起學(xué)習(xí)學(xué)習(xí)
工程路徑為..\STM32\DCDEMO7\RVMDK\DCDEMO7.uvproj ,請(qǐng)用KEIL4(MDK4)以上版本打開
單片機(jī)源程序如下:
- #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"
- #define TIME_100MS 10
- volatile uint32 timer_tick_count = 0; //定時(shí)器節(jié)拍
- uint8 cmd_buffer[CMD_MAX_SIZE];
- static int32 test_value = 0;
- static uint8 update_en = 0;
- void UpdateUI(void);
- //程序入口
- int main()
- {
- qsize size = 0;
- uint32 timer_tick_last_update = 0;
- /*配置時(shí)鐘*/
- Set_System();
- /*配置串口中斷*/
- Interrupts_Config();
- /*配置時(shí)鐘節(jié)拍*/
- systicket_init();
- /*串口初始化,波特率設(shè)置為115200*/
- UartInit(115200);
- /*清空串口接收緩沖區(qū)*/
- queue_reset();
- /*延時(shí)等待串口屏初始化完畢,必須等待300ms*/
- delay_ms(300);
- while(1)
- {
- size = queue_find_cmd(cmd_buffer,CMD_MAX_SIZE); //從緩沖區(qū)中獲取一條指令
- if(size>0)//接收到指令
- {
- ProcessMessage((PCTRL_MSG)cmd_buffer, size);//指令處理
- }
- /****************************************************************************************************************
- 特別注意
- MCU不要頻繁向串口屏發(fā)送數(shù)據(jù),否則串口屏的內(nèi)部緩存區(qū)會(huì)滿,從而導(dǎo)致數(shù)據(jù)丟失(緩沖區(qū)大小:標(biāo)準(zhǔn)型8K,基本型4.7K)
- 1) 一般情況下,控制MCU向串口屏發(fā)送數(shù)據(jù)的周期大于100ms,就可以避免數(shù)據(jù)丟失的問題;
- 2) 如果仍然有數(shù)據(jù)丟失的問題,請(qǐng)判斷串口屏的BUSY引腳,為高時(shí)不能發(fā)送數(shù)據(jù)給串口屏。
- ******************************************************************************************************************/
- //TODO: 添加用戶代碼
- //數(shù)據(jù)有更新時(shí),每100毫秒刷新一次
- if(update_en&&timer_tick_count-timer_tick_last_update>=TIME_100MS)
- {
- update_en = 0;
- timer_tick_last_update = timer_tick_count;
- UpdateUI();
- }
- }
- }
- /*!
- * \brief 消息處理流程,此處一般不需要更改
- * \param msg 待處理消息
- * \param size 消息長(zhǎng)度
- */
- 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);//數(shù)值
- switch(cmd_type)
- {
- case NOTIFY_SCREEN://切換畫面
- NotifyScreen(screen_id);
- break;
- 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:
- NotifyWriteFlash(1);
- break;
- case NOTIFY_WRITE_FLASH_FAILD:
- NotifyWriteFlash(0);
- break;
- case NOTIFY_READ_FLASH_OK:
- NotifyReadFlash(1,cmd_buffer+2,size-6);//去除幀頭幀尾
- break;
- case NOTIFY_READ_FLASH_FAILD:
- NotifyReadFlash(0,0,0);
- break;
- case NOTIFY_CONTROL:
- {
- 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: //進(jìn)度條控件
- NotifyProgress(screen_id,control_id,value);
- break;
- case kCtrlSlider: //滑動(dòng)條控件
- 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://倒計(jì)時(shí)控件
- NotifyTimer(screen_id,control_id);
- break;
- default:
- break;
- }
- }
- break;
- default:
- break;
- }
- }
- /*!
- * \brief 畫面切換通知
- * \details 當(dāng)前畫面改變時(shí)(或調(diào)用GetScreen),執(zhí)行此函數(shù)
- * \param screen_id 當(dāng)前畫面ID
- */
- void NotifyScreen(uint16 screen_id)
- {
- //TODO: 添加用戶代碼
- }
- /*!
- * \brief 觸摸坐標(biāo)事件響應(yīng)
- * \param press 1按下觸摸屏,3松開觸摸屏
- * \param x x坐標(biāo)
- * \param y y坐標(biāo)
- */
- void NotifyTouchXY(uint8 press,uint16 x,uint16 y)
- {
- //TODO: 添加用戶代碼
- }
- //文本控件顯示整數(shù)值
- void SetTextValueInt32(uint16 screen_id, uint16 control_id,int32 value)
- {
- char buffer[12] = {0};
- sprintf(buffer,"%ld",value);
- SetTextValue(screen_id,control_id,(uchar *)buffer);
- }
- //字符串轉(zhuǎn)整數(shù)
- int32 StringToInt32(uint8 *str)
- {
- int32 v = 0;
- sscanf((char *)str,"%ld",&v);
- return v;
- }
- //更新界面控件顯示
- void UpdateUI()
- {
- SetTextValueInt32(0,2,test_value);
- SetProgressValue(0,3,test_value);
- SetSliderValue(0,4,test_value);
- SetMeterValue(0,7,test_value);
- }
- /*!
- * \brief 按鈕控件通知
- * \details 當(dāng)按鈕狀態(tài)改變(或調(diào)用GetControlValue)時(shí),執(zhí)行此函數(shù)
- * \param screen_id 畫面ID
- * \param control_id 控件ID
- * \param state 按鈕狀態(tài):0彈起,1按下
- */
- void NotifyButton(uint16 screen_id, uint16 control_id, uint8 state)
- {
- //TODO: 添加用戶代碼
- if(screen_id==0&&control_id==5)//遞增按鈕
- {
- if(test_value<100)
- ++test_value;
- }
- else if(screen_id==0&&control_id==6)//遞減按鈕
- {
- if(test_value>0)
- --test_value;
- }
-
- //UpdateUI();
- update_en = 1;
- }
- /*!
- * \brief 文本控件通知
- * \details 當(dāng)文本通過鍵盤更新(或調(diào)用GetControlValue)時(shí),執(zhí)行此函數(shù)
- * \param screen_id 畫面ID
- * \param control_id 控件ID
- * \param str 文本控件內(nèi)容
- */
- void NotifyText(uint16 screen_id, uint16 control_id, uint8 *str)
- {
- //TODO: 添加用戶代碼
- test_value = StringToInt32(str);
- if(test_value>100)
- test_value = 100;
- else if(test_value<0)
- test_value = 0;
- //UpdateUI();
- update_en = 1;
- }
- /*!
- * \brief 進(jìn)度條控件通知
- * \details 調(diào)用GetControlValue時(shí),執(zhí)行此函數(shù)
- * \param screen_id 畫面ID
- * \param control_id 控件ID
- * \param value 值
- */
- void NotifyProgress(uint16 screen_id, uint16 control_id, uint32 value)
- {
- //TODO: 添加用戶代碼
- }
- /*!
- * \brief 滑動(dòng)條控件通知
- * \details 當(dāng)滑動(dòng)條改變(或調(diào)用GetControlValue)時(shí),執(zhí)行此函數(shù)
- * \param screen_id 畫面ID
- * \param control_id 控件ID
- * \param value 值
- */
- void NotifySlider(uint16 screen_id, uint16 control_id, uint32 value)
- {
- //TODO: 添加用戶代碼
- test_value = value;
- //UpdateUI();
- update_en = 1;
- }
- /*!
- * \brief 儀表控件通知
- * \details 調(diào)用GetControlValue時(shí),執(zhí)行此函數(shù)
- * \param screen_id 畫面ID
- * \param control_id 控件ID
- * \param value 值
- */
- void NotifyMeter(uint16 screen_id, uint16 control_id, uint32 value)
- {
- //TODO: 添加用戶代碼
- }
- /*!
- * \brief 菜單控件通知
- * \details 當(dāng)菜單項(xiàng)按下或松開時(shí),執(zhí)行此函數(shù)
- * \param screen_id 畫面ID
- * \param control_id 控件ID
- * \param item 菜單項(xiàng)索引
- * \param state 按鈕狀態(tài):0松開,1按下
- */
- void NotifyMenu(uint16 screen_id, uint16 control_id, uint8 item, uint8 state)
- {
- //TODO: 添加用戶代碼
- }
- /*!
- * \brief 選擇控件通知
- * \details 當(dāng)選擇控件變化時(shí),執(zhí)行此函數(shù)
- * \param screen_id 畫面ID
- * \param control_id 控件ID
- * \param item 當(dāng)前選項(xiàng)
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
KEIL_DEMO_STM32.rar
(1.7 MB, 下載次數(shù): 220)
2017-12-11 15:45 上傳
點(diǎn)擊文件名下載附件
|
|