欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
關于51單片機使用malloc函數的問題
[打印本頁]
作者:
居然是二狗
時間:
2021-5-8 16:57
標題:
關于51單片機使用malloc函數的問題
我在網上看到51單片機似乎不建議使用malloc函數,但這段程序也是看了論壇一位大佬寫的多級菜單的程序,不知道怎么改
求教怎么才能正確初始化菜單 使用的是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 //用于檢測LCD狀態(tài)字中的Busy標識
/********函數聲明*************************************************************/
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);
//首先定義一個菜單結構體
typedef struct menu //定義一個菜單
{
unsigned char range_from,range_to; //當前顯示的項開始及結束序號
unsigned char itemCount; //項目總數
unsigned char selected; //當前選擇項
unsigned char *menuItems[17]; //菜單項目
struct menu **subMenus; //子菜單
struct menu *parent; //上級菜單 ,如果是頂級則為null
void (**func)(); //選擇相應項按確定鍵后執(zhí)行的函數
}Menu;
Menu MainMenu = { //定義主菜單
0,3,4,0, //默認顯示0-3項,總共4項,當前選擇第0項
{
"測量表 ", // >表示有下級選項
"其他設置 ",
"保護設定 ",
"查詢 >"
}
};
Menu searchMenu = { //查詢菜單
0,3,6,0,
{
"記帳記錄明細 ",
"未采集記錄數 ",
"設備機號 ",
"本機IP地址 ",
"記錄空間大小 ",
"軟件版本 "
}
};
Menu *currentMenu; //當前的菜單
//初始化菜單:
void initMenu()
{
char mempool[512];
init_mempool (&mempool, sizeof(mempool));
MainMenu.subMenus = malloc(sizeof(&MainMenu)*4);
MainMenu.subMenus[0] = &searchMenu;//第1到3項沒有子菜單置null,選擇后程序會調用func中相應項中的函數
MainMenu.subMenus[1] = NULL;
MainMenu.subMenus[2] = NULL;
MainMenu.subMenus[3] = NULL;//第四項查詢有子菜單
MainMenu.func = malloc(sizeof(&func)*4);
MainMenu.func[0] = NULL;
MainMenu.func[1] = NULL;
MainMenu.func[2] = &func;//當選擇了并按了確定,會執(zhí)行func函數
MainMenu.func[3] = NULL;
MainMenu.parent = NULL;//表示是頂級菜單
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;//上一級菜單是MainMenu.進入查詢子菜單后按返回鍵,將會顯示這個菜單項
currentMenu = &MainMenu;
}
//main
int main(void)
{
Delay400Ms(); //啟動等待,等LCD講入工作狀態(tài)
LCDInit(); //LCD初始化
Delay5Ms(); //延時片刻
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)//到了第一項
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í)行相應的函數
display(currentMenu->selected);//返回后恢復原來的菜單狀態(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為行反白格數
{
unsigned char i,j,flag=0x00;
if(y>1)
{
flag=0x08;
y=y-2;
}
WriteCommandLCD(0x34,1); //寫數據時,關閉圖形顯示,且打開擴展指令集
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); //寫完數據,開圖形顯示
}
/***********寫數據********************************************************/
void WriteDataLCD(unsigned char WDLCD)
{
ReadStatusLCD(); //檢測忙
LCD_RS = 1;
LCD_RW = 0;
LCD_Data = WDLCD;
LCD_E = 1;
LCD_E = 1;
LCD_E = 0;
}
/***********寫指令********************************************************/
void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC為0時忽略忙檢測
{
if (BuysC) ReadStatusLCD(); //根據需要檢測忙
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); //檢測忙信號
LCD_E = 0;
return(1);
}
/***********初始化********************************************************/
void LCDInit(void)
{
WriteCommandLCD(0x30,1); //顯示模式設置,開始要求每次檢測忙信號
WriteCommandLCD(0x01,1); //顯示清屏
WriteCommandLCD(0x06,1); // 顯示光標移動設置
WriteCommandLCD(0x0C,1); // 顯示開及光標設置
}
/***********短延時********************************************************/
void Delay5Ms(void)
{
unsigned int TempCyc = 11104;
while(TempCyc--);
}
/***********長延時********************************************************/
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;){ //分兩屏,上半屏和下半屏,因為起始地址不同,需要分開
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);//讀取數據寫入LCD
tmp+=16;
}
i+=8;
}
WriteCommandLCD(0x36,1); //擴充功能設定
WriteCommandLCD(0x30,1);
}
//掃描鍵盤并返回按鍵值
unsigned char key_scan()
{
unsigned char sta = 1;//設置一個狀態(tài),表示按鍵是否按下,1為抬起
if(sta == 1&&(KEY1 == 0||KEY2 == 0||KEY3 ==0||KEY4 ==0||KEY5 ==0||KEY6 ==0))//如果有一個按鍵按下,則進入下面程序,注意此時使用的邏輯或的關系
{
Delay5Ms();//消抖5ms
sta = 0;//設置一個狀態(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;//表示沒有按鍵按下,注意此時使用的是邏輯與的關系;
return 0;//沒有按鍵按下
}
//用于顯示菜單項
void display(unsigned char line) //顯示菜單項并設置選中的項反白
{
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 )
{ //設置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); //進入標準模式
LCD_Set_XY(x,y);
while (*s)
{
WriteDataLCD(*s);
s++;
}
WriteCommandLCD(0x36,1); //返回圖形模式
}
復制代碼
液晶顯示器.zip
2021-5-8 16:57 上傳
點擊文件名下載附件
8.39 KB, 下載次數: 13
作者:
myhello51hei
時間:
2022-3-4 19:21
子菜單和執(zhí)行函數都應該用一級指針吧?
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1