利用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)
2018-8-27 18:28 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|