|
我在網(wǎng)上看到51單片機(jī)似乎不建議使用malloc函數(shù),但這段程序也是看了論壇一位大佬寫(xiě)的多級(jí)菜單的程序,不知道怎么改
求教怎么才能正確初始化菜單 使用的是stc89c52rc
- #include <reg52.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- /********IO引腳定義***********************************************************/
- sbit LCD_RS=P1^0;
- sbit LCD_RW=P1^1;
- sbit LCD_E=P1^2;
- sbit PSB =P1^6; //PSB腳為12864-12系列的串、并通訊功能切換,我們使用8位并行接口,PSB=1
- sbit KEY1=P3^7;
- sbit KEY2=P3^6;
- sbit KEY3=P3^5;
- sbit KEY4=P3^4;
- sbit KEY5=P3^3;
- sbit KEY6=P3^2; //定義按鍵1~7
- /********宏定義***********************************************************/
- #define LCD_Data P0
- #define Busy 0x80 //用于檢測(cè)LCD狀態(tài)字中的Busy標(biāo)識(shí)
- /********函數(shù)聲明*************************************************************/
- void WriteDataLCD(unsigned char WDLCD);
- void WriteCommandLCD(unsigned char WCLCD,BuysC);
- unsigned char ReadDataLCD(void);
- unsigned char ReadStatusLCD(void);
- void LCDInit(void);
- void LCDClear(void);
- void LCDFlash(void);
- void Delay5Ms(void);
- void Delay400Ms(void);
- void highlight(unsigned char x,unsigned char y,unsigned char width,unsigned char mode);
- unsigned char key_scan(void);
- void display(unsigned char line);
- void func(void);
- void LCDClear(void);
- void LCD_0(void);
- void LCD_Set_XY(unsigned char x,unsigned char y);
- void Lcd_Disp(unsigned char x,unsigned char y,unsigned char *s);
- //首先定義一個(gè)菜單結(jié)構(gòu)體
- typedef struct menu //定義一個(gè)菜單
- {
- unsigned char range_from,range_to; //當(dāng)前顯示的項(xiàng)開(kāi)始及結(jié)束序號(hào)
- unsigned char itemCount; //項(xiàng)目總數(shù)
- unsigned char selected; //當(dāng)前選擇項(xiàng)
- unsigned char *menuItems[17]; //菜單項(xiàng)目
- struct menu **subMenus; //子菜單
- struct menu *parent; //上級(jí)菜單 ,如果是頂級(jí)則為null
- void (**func)(); //選擇相應(yīng)項(xiàng)按確定鍵后執(zhí)行的函數(shù)
- }Menu;
- Menu MainMenu = { //定義主菜單
- 0,3,4,0, //默認(rèn)顯示0-3項(xiàng),總共4項(xiàng),當(dāng)前選擇第0項(xiàng)
- {
- "測(cè)量表 ", // >表示有下級(jí)選項(xiàng)
- "其他設(shè)置 ",
- "保護(hù)設(shè)定 ",
- "查詢 >"
- }
- };
- Menu searchMenu = { //查詢菜單
- 0,3,6,0,
- {
- "記帳記錄明細(xì) ",
- "未采集記錄數(shù) ",
- "設(shè)備機(jī)號(hào) ",
- "本機(jī)IP地址 ",
- "記錄空間大小 ",
- "軟件版本 "
- }
- };
- Menu *currentMenu; //當(dāng)前的菜單
- //初始化菜單:
- void initMenu()
- {
- char mempool[512];
- init_mempool (&mempool, sizeof(mempool));
- MainMenu.subMenus = malloc(sizeof(&MainMenu)*4);
- MainMenu.subMenus[0] = &searchMenu;//第1到3項(xiàng)沒(méi)有子菜單置null,選擇后程序會(huì)調(diào)用func中相應(yīng)項(xiàng)中的函數(shù)
- MainMenu.subMenus[1] = NULL;
- MainMenu.subMenus[2] = NULL;
- MainMenu.subMenus[3] = NULL;//第四項(xiàng)查詢有子菜單
-
- MainMenu.func = malloc(sizeof(&func)*4);
- MainMenu.func[0] = NULL;
- MainMenu.func[1] = NULL;
- MainMenu.func[2] = &func;//當(dāng)選擇了并按了確定,會(huì)執(zhí)行func函數(shù)
- MainMenu.func[3] = NULL;
- MainMenu.parent = NULL;//表示是頂級(jí)菜單
-
- searchMenu.subMenus = malloc(sizeof(&searchMenu)*6);
- searchMenu.subMenus[0] = searchMenu.subMenus[1] = searchMenu.subMenus[2] = searchMenu.subMenus[3] = searchMenu.subMenus[4] = searchMenu.subMenus[5] = NULL;
- searchMenu.func = malloc(sizeof(&printf)*6);
- searchMenu.func[0] = searchMenu.func[1] = searchMenu.func[2] = searchMenu.func[3] = searchMenu.func[4] = searchMenu.func[5] = NULL;
- searchMenu.parent = &MainMenu;//上一級(jí)菜單是MainMenu.進(jìn)入查詢子菜單后按返回鍵,將會(huì)顯示這個(gè)菜單項(xiàng)
-
- currentMenu = &MainMenu;
-
- }
- //main
- int main(void)
- {
- Delay400Ms(); //啟動(dòng)等待,等LCD講入工作狀態(tài)
- LCDInit(); //LCD初始化
- Delay5Ms(); //延時(shí)片刻
- initMenu();
- display(currentMenu->selected);
- while(1){
- unsigned char key = key_scan();
- if(key == 0)
- {
- Delay5Ms();
- Delay5Ms();
- continue;
- }
- switch(key)
- {
- case 3://向上
- if(currentMenu->selected == 0)//到了第一項(xiàng)
- break;
- else
- {
- currentMenu->selected--;
- if(currentMenu->selected < currentMenu->range_from)//更新顯示的區(qū)域
- {
- currentMenu->range_from = currentMenu->selected;
- currentMenu->range_to = currentMenu->range_from+3;
- }
- display(currentMenu->selected);
- break;
- }
- case 4://向下
- if(currentMenu->selected == currentMenu->itemCount-1)
- break;
- else
- {
- currentMenu->selected++;
- if(currentMenu->selected>currentMenu->range_to)
- {
- currentMenu->range_to = currentMenu->selected;
- currentMenu->range_from = currentMenu->range_to-3;
- }
- display(currentMenu->selected);
- break;
- }
- case 1://Enter鍵
- {
- if(currentMenu->subMenus[currentMenu->selected] !=NULL)
- {
- currentMenu=currentMenu->subMenus[currentMenu->selected];
- display(0);
- }
- else
- {
- if(currentMenu->func[currentMenu->selected] != NULL)
- {
- currentMenu->func[currentMenu->selected]();//執(zhí)行相應(yīng)的函數(shù)
-
- display(currentMenu->selected);//返回后恢復(fù)原來(lái)的菜單狀態(tài)
- }
- }
- break;
- }
- case 6://返回鍵
- {
- if(currentMenu->parent!=NULL)//父菜單不為空,將顯示父菜單
- {
- currentMenu = currentMenu->parent;
- display(currentMenu->selected);
- }
- break;
- }
- default:
- break;
- }
- Delay5Ms();
- Delay5Ms();
- }
- }
-
- /*************************反白顯示*************************************/
- void highlight(unsigned char x,unsigned char y,unsigned char width,unsigned char mode) //反白,X值為0-7,Y值為0-3,width為行反白格數(shù)
- {
- unsigned char i,j,flag=0x00;
- if(y>1)
- {
- flag=0x08;
- y=y-2;
- }
- WriteCommandLCD(0x34,1); //寫(xiě)數(shù)據(jù)時(shí),關(guān)閉圖形顯示,且打開(kāi)擴(kuò)展指令集
- for(i=0;i<16;i++)
- {
- WriteCommandLCD(0x80+(y<<4)+i,1);
- WriteCommandLCD(0x80+flag+x,1);
- for(j=0;j<width;j++)
- {
- WriteDataLCD(mode);
- WriteDataLCD(mode);
- }
- }
- WriteCommandLCD(0x36,1); //寫(xiě)完數(shù)據(jù),開(kāi)圖形顯示
- }
- /***********寫(xiě)數(shù)據(jù)********************************************************/
- void WriteDataLCD(unsigned char WDLCD)
- {
- ReadStatusLCD(); //檢測(cè)忙
- LCD_RS = 1;
- LCD_RW = 0;
- LCD_Data = WDLCD;
- LCD_E = 1;
- LCD_E = 1;
- LCD_E = 0;
- }
- /***********寫(xiě)指令********************************************************/
- void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC為0時(shí)忽略忙檢測(cè)
- {
- if (BuysC) ReadStatusLCD(); //根據(jù)需要檢測(cè)忙
- LCD_RS = 0;
- LCD_RW = 0;
- LCD_Data = WCLCD;
- LCD_E = 1;
- LCD_E = 1;
- LCD_E = 0;
- }
- /***********讀狀態(tài)*******************************************************/
- unsigned char ReadStatusLCD(void)
- {
- LCD_Data = 0xFF;
- LCD_RS = 0;
- LCD_RW = 1;
- LCD_E = 1;
- LCD_E = 1;
- while (LCD_Data & Busy); //檢測(cè)忙信號(hào)
- LCD_E = 0;
- return(1);
- }
- /***********初始化********************************************************/
- void LCDInit(void)
- {
- WriteCommandLCD(0x30,1); //顯示模式設(shè)置,開(kāi)始要求每次檢測(cè)忙信號(hào)
- WriteCommandLCD(0x01,1); //顯示清屏
- WriteCommandLCD(0x06,1); // 顯示光標(biāo)移動(dòng)設(shè)置
- WriteCommandLCD(0x0C,1); // 顯示開(kāi)及光標(biāo)設(shè)置
- }
- /***********短延時(shí)********************************************************/
- void Delay5Ms(void)
- {
- unsigned int TempCyc = 11104;
- while(TempCyc--);
- }
- /***********長(zhǎng)延時(shí)********************************************************/
- void Delay400Ms(void)
- {
- unsigned char TempCycA = 5;
- unsigned int TempCycB;
- while(TempCycA--){
- TempCycB=7269;
- while(TempCycB--);
- }
- }
- void LCD_0 (void){
- unsigned char x,y,i;
- unsigned int tmp=0;
- for(i=0;i<9;){ //分兩屏,上半屏和下半屏,因?yàn)槠鹗嫉刂凡煌,需要分開(kāi)
- for(x=0;x<32;x++){ //32行
- WriteCommandLCD(0x34,1);
- WriteCommandLCD((0x80+x),1); //列地址
- WriteCommandLCD((0x80+i),1); //行地址,下半屏,即第三行地址0X88
- WriteCommandLCD(0x30,1);
- for(y=0;y<16;y++)
- WriteDataLCD(0x00);//讀取數(shù)據(jù)寫(xiě)入LCD
- tmp+=16;
- }
- i+=8;
- }
- WriteCommandLCD(0x36,1); //擴(kuò)充功能設(shè)定
- WriteCommandLCD(0x30,1);
- }
- //掃描鍵盤(pán)并返回按鍵值
- unsigned char key_scan()
- {
- unsigned char sta = 1;//設(shè)置一個(gè)狀態(tài),表示按鍵是否按下,1為抬起
- if(sta == 1&&(KEY1 == 0||KEY2 == 0||KEY3 ==0||KEY4 ==0||KEY5 ==0||KEY6 ==0))//如果有一個(gè)按鍵按下,則進(jìn)入下面程序,注意此時(shí)使用的邏輯或的關(guān)系
- {
- Delay5Ms();//消抖5ms
- sta = 0;//設(shè)置一個(gè)狀態(tài),表示按鍵是否按下,0為按下
-
- if(KEY1== 0) return 1;
- else if(KEY2 ==0) return 2;
- else if(KEY3 ==0) return 3;
- else if(KEY4 ==0) return 4;
- else if(KEY5 ==0) return 5;
- else if(KEY6 ==0) return 6;
- }
- else if (KEY1 == 1&&KEY2 == 1&&KEY3 ==1&&KEY4 ==1&&KEY5 ==1&&KEY6 ==1) sta = 1;//表示沒(méi)有按鍵按下,注意此時(shí)使用的是邏輯與的關(guān)系;
- return 0;//沒(méi)有按鍵按下
- }
- //用于顯示菜單項(xiàng)
- void display(unsigned char line) //顯示菜單項(xiàng)并設(shè)置選中的項(xiàng)反白
- {
- int i;
- line = 3-(currentMenu->range_to-line);
- LCD_0();
- for(i = 0;i<4;i++)
- {
- Lcd_Disp(i+1,0,currentMenu->menuItems[i+currentMenu->range_from]);
- }
- highlight(0,line,8,255);//反白顯示指定行
- }
- void func(void)
- {
- }
- void LCD_set_xy( unsigned char x, unsigned char y )
- { //設(shè)置LCD顯示的起始位置,X為行,Y為列
- unsigned char address;
- switch(x)
- {
- case 0: address = 0x80 + y; break;
- case 1: address = 0x80 + y; break;
- case 2: address = 0x90 + y; break;
- case 3: address = 0x88 + y; break;
- case 4: address = 0x98 + y; break;
- default:address = 0x80 + y; break;
- }
- WriteCommandLCD(address, 1);
- }
- void Lcd_Disp(unsigned char x,unsigned char y,unsigned char *s)
- {
- WriteCommandLCD(0x30,1); //進(jìn)入標(biāo)準(zhǔn)模式
- LCD_Set_XY(x,y);
- while (*s)
- {
- WriteDataLCD(*s);
- s++;
- }
- WriteCommandLCD(0x36,1); //返回圖形模式
- }
復(fù)制代碼
|
-
-
液晶顯示器.zip
2021-5-8 16:57 上傳
點(diǎn)擊文件名下載附件
8.39 KB, 下載次數(shù): 13
|