标题:
关于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状态字中的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)(); //选择相应项按确定键后执行的函数
}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;//当选择了并按了确定,会执行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讲入工作状态
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)//更新显示的区域
{
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]();//执行相应的函数
display(currentMenu->selected);//返回后恢复原来的菜单状态
}
}
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;
}
/***********读状态*******************************************************/
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;//设置一个状态,表示按键是否按下,1为抬起
if(sta == 1&&(KEY1 == 0||KEY2 == 0||KEY3 ==0||KEY4 ==0||KEY5 ==0||KEY6 ==0))//如果有一个按键按下,则进入下面程序,注意此时使用的逻辑或的关系
{
Delay5Ms();//消抖5ms
sta = 0;//设置一个状态,表示按键是否按下,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
子菜单和执行函数都应该用一级指针吧?
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1