找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3732|回復(fù): 1
收起左側(cè)

關(guān)于51單片機(jī)使用malloc函數(shù)的問(wèn)題

[復(fù)制鏈接]
ID:909487 發(fā)表于 2021-5-8 16:57 | 顯示全部樓層 |閱讀模式
我在網(wǎng)上看到51單片機(jī)似乎不建議使用malloc函數(shù),但這段程序也是看了論壇一位大佬寫(xiě)的多級(jí)菜單的程序,不知道怎么改
求教怎么才能正確初始化菜單 使用的是stc89c52rc
  1. #include <reg52.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>


  5. /********IO引腳定義***********************************************************/
  6. sbit LCD_RS=P1^0;
  7. sbit LCD_RW=P1^1;
  8. sbit LCD_E=P1^2;
  9. sbit PSB =P1^6;                //PSB腳為12864-12系列的串、并通訊功能切換,我們使用8位并行接口,PSB=1
  10. sbit KEY1=P3^7;
  11. sbit KEY2=P3^6;
  12. sbit KEY3=P3^5;
  13. sbit KEY4=P3^4;
  14. sbit KEY5=P3^3;
  15. sbit KEY6=P3^2;   //定義按鍵1~7

  16. /********宏定義***********************************************************/
  17. #define LCD_Data P0
  18. #define Busy    0x80 //用于檢測(cè)LCD狀態(tài)字中的Busy標(biāo)識(shí)

  19. /********函數(shù)聲明*************************************************************/
  20. void WriteDataLCD(unsigned char WDLCD);
  21. void WriteCommandLCD(unsigned char WCLCD,BuysC);
  22. unsigned char ReadDataLCD(void);
  23. unsigned char ReadStatusLCD(void);
  24. void LCDInit(void);
  25. void LCDClear(void);
  26. void LCDFlash(void);
  27. void Delay5Ms(void);
  28. void Delay400Ms(void);
  29. void highlight(unsigned char x,unsigned char y,unsigned char width,unsigned char mode);
  30. unsigned char key_scan(void);
  31. void display(unsigned char line);
  32. void func(void);
  33. void LCDClear(void);
  34. void LCD_0(void);
  35. void LCD_Set_XY(unsigned char x,unsigned char y);
  36. void Lcd_Disp(unsigned char x,unsigned char y,unsigned char *s);


  37. //首先定義一個(gè)菜單結(jié)構(gòu)體
  38. typedef struct menu                      //定義一個(gè)菜單
  39. {
  40. unsigned char range_from,range_to;     //當(dāng)前顯示的項(xiàng)開(kāi)始及結(jié)束序號(hào)
  41. unsigned char itemCount;               //項(xiàng)目總數(shù)
  42. unsigned char selected;                //當(dāng)前選擇項(xiàng)
  43. unsigned char *menuItems[17];                  //菜單項(xiàng)目
  44. struct menu **subMenus;                //子菜單
  45. struct menu *parent;                   //上級(jí)菜單 ,如果是頂級(jí)則為null
  46. void (**func)();                       //選擇相應(yīng)項(xiàng)按確定鍵后執(zhí)行的函數(shù)

  47. }Menu;

  48. Menu MainMenu = {                       //定義主菜單
  49. 0,3,4,0,                               //默認(rèn)顯示0-3項(xiàng),總共4項(xiàng),當(dāng)前選擇第0項(xiàng)
  50.   {
  51.   "測(cè)量表          ",                   //  >表示有下級(jí)選項(xiàng)
  52.   "其他設(shè)置        ",
  53.   "保護(hù)設(shè)定        ",
  54.   "查詢           >"
  55.   }
  56. };

  57. Menu searchMenu = {                    //查詢菜單
  58. 0,3,6,0,
  59. {
  60.   "記帳記錄明細(xì)   ",
  61.   "未采集記錄數(shù)   ",
  62.   "設(shè)備機(jī)號(hào)       ",
  63.   "本機(jī)IP地址     ",
  64.   "記錄空間大小   ",
  65.   "軟件版本       "
  66. }
  67. };
  68. Menu *currentMenu;                               //當(dāng)前的菜單



  69. //初始化菜單:
  70. void initMenu()
  71. {
  72. char mempool[512];
  73. init_mempool (&mempool, sizeof(mempool));
  74. MainMenu.subMenus = malloc(sizeof(&MainMenu)*4);
  75. MainMenu.subMenus[0] = &searchMenu;//第1到3項(xiàng)沒(méi)有子菜單置null,選擇后程序會(huì)調(diào)用func中相應(yīng)項(xiàng)中的函數(shù)
  76. MainMenu.subMenus[1] = NULL;
  77. MainMenu.subMenus[2] = NULL;
  78. MainMenu.subMenus[3] = NULL;//第四項(xiàng)查詢有子菜單
  79.         
  80. MainMenu.func = malloc(sizeof(&func)*4);
  81. MainMenu.func[0] = NULL;
  82. MainMenu.func[1] = NULL;
  83. MainMenu.func[2] = &func;//當(dāng)選擇了并按了確定,會(huì)執(zhí)行func函數(shù)
  84. MainMenu.func[3] = NULL;
  85. MainMenu.parent = NULL;//表示是頂級(jí)菜單

  86. searchMenu.subMenus = malloc(sizeof(&searchMenu)*6);
  87. searchMenu.subMenus[0] = searchMenu.subMenus[1] = searchMenu.subMenus[2] = searchMenu.subMenus[3] = searchMenu.subMenus[4] = searchMenu.subMenus[5] = NULL;
  88. searchMenu.func = malloc(sizeof(&printf)*6);
  89. searchMenu.func[0] = searchMenu.func[1] = searchMenu.func[2] = searchMenu.func[3] = searchMenu.func[4] = searchMenu.func[5] = NULL;
  90. searchMenu.parent = &MainMenu;//上一級(jí)菜單是MainMenu.進(jìn)入查詢子菜單后按返回鍵,將會(huì)顯示這個(gè)菜單項(xiàng)

  91. currentMenu = &MainMenu;

  92. }

  93. //main

  94. int main(void)
  95. {

  96.          Delay400Ms();         //啟動(dòng)等待,等LCD講入工作狀態(tài)
  97.          LCDInit();                 //LCD初始化
  98.          Delay5Ms();         //延時(shí)片刻
  99.   initMenu();
  100.   display(currentMenu->selected);
  101.   while(1){
  102. unsigned char key = key_scan();
  103. if(key == 0)
  104.   {
  105.   Delay5Ms();
  106.         Delay5Ms();
  107.    continue;
  108.   }
  109.   switch(key)
  110.   {
  111.    case 3://向上
  112.     if(currentMenu->selected == 0)//到了第一項(xiàng)
  113.      break;
  114.     else
  115.     {
  116.      currentMenu->selected--;
  117.      if(currentMenu->selected < currentMenu->range_from)//更新顯示的區(qū)域
  118.      {
  119.       currentMenu->range_from = currentMenu->selected;
  120.       currentMenu->range_to = currentMenu->range_from+3;
  121.      }
  122.      display(currentMenu->selected);
  123.      break;
  124.     }
  125.    case 4://向下
  126.     if(currentMenu->selected == currentMenu->itemCount-1)
  127.      break;
  128.     else
  129.     {
  130.      currentMenu->selected++;
  131.      if(currentMenu->selected>currentMenu->range_to)
  132.      {
  133.       currentMenu->range_to = currentMenu->selected;
  134.       currentMenu->range_from = currentMenu->range_to-3;
  135.      }
  136.      display(currentMenu->selected);
  137.      break;
  138.     }
  139.    case 1://Enter鍵
  140.    {
  141.     if(currentMenu->subMenus[currentMenu->selected] !=NULL)
  142.     {
  143.      currentMenu=currentMenu->subMenus[currentMenu->selected];
  144.      display(0);
  145.     }
  146.     else
  147.     {
  148.      if(currentMenu->func[currentMenu->selected] != NULL)
  149.      {
  150.       currentMenu->func[currentMenu->selected]();//執(zhí)行相應(yīng)的函數(shù)
  151.                         
  152.       display(currentMenu->selected);//返回后恢復(fù)原來(lái)的菜單狀態(tài)
  153.      }
  154.     }
  155.     break;
  156.    }
  157.    case 6://返回鍵
  158.    {
  159.     if(currentMenu->parent!=NULL)//父菜單不為空,將顯示父菜單
  160.     {
  161.      currentMenu = currentMenu->parent;
  162.      display(currentMenu->selected);
  163.     }
  164.     break;
  165.    }
  166.    default:
  167.      break;
  168.   }
  169.     Delay5Ms();
  170.           Delay5Ms();
  171. }
  172. }


  173. /*************************反白顯示*************************************/
  174. void highlight(unsigned char x,unsigned char y,unsigned char width,unsigned char mode)                //反白,X值為0-7,Y值為0-3,width為行反白格數(shù)
  175. {
  176.   unsigned char i,j,flag=0x00;
  177.   if(y>1)
  178.   {
  179.    flag=0x08;
  180.    y=y-2;
  181.   }
  182.   WriteCommandLCD(0x34,1);        //寫(xiě)數(shù)據(jù)時(shí),關(guān)閉圖形顯示,且打開(kāi)擴(kuò)展指令集
  183.         for(i=0;i<16;i++)
  184.         {
  185.                 WriteCommandLCD(0x80+(y<<4)+i,1);
  186.                 WriteCommandLCD(0x80+flag+x,1);
  187.                 for(j=0;j<width;j++)
  188.                 {
  189.                         WriteDataLCD(mode);
  190.                         WriteDataLCD(mode);
  191.                 }
  192.         }
  193.         WriteCommandLCD(0x36,1);       //寫(xiě)完數(shù)據(jù),開(kāi)圖形顯示
  194. }



  195. /***********寫(xiě)數(shù)據(jù)********************************************************/
  196. void WriteDataLCD(unsigned char WDLCD)
  197. {
  198.          ReadStatusLCD(); //檢測(cè)忙
  199.          LCD_RS = 1;
  200.          LCD_RW = 0;
  201.          LCD_Data = WDLCD;
  202.          LCD_E = 1;
  203.          LCD_E = 1;
  204.          LCD_E = 0;
  205. }

  206. /***********寫(xiě)指令********************************************************/
  207. void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC為0時(shí)忽略忙檢測(cè)
  208. {
  209.          if (BuysC) ReadStatusLCD(); //根據(jù)需要檢測(cè)忙
  210.          LCD_RS = 0;
  211.          LCD_RW = 0;
  212.          LCD_Data = WCLCD;
  213.          LCD_E = 1;
  214.          LCD_E = 1;
  215.          LCD_E = 0;  
  216. }


  217. /***********讀狀態(tài)*******************************************************/
  218. unsigned char ReadStatusLCD(void)
  219. {
  220.          LCD_Data = 0xFF;
  221.          LCD_RS = 0;
  222.          LCD_RW = 1;
  223.         LCD_E = 1;
  224.         LCD_E = 1;
  225.          while (LCD_Data & Busy); //檢測(cè)忙信號(hào)        
  226.          LCD_E = 0;
  227.          return(1);
  228. }

  229. /***********初始化********************************************************/
  230. void LCDInit(void)
  231. {
  232.          WriteCommandLCD(0x30,1); //顯示模式設(shè)置,開(kāi)始要求每次檢測(cè)忙信號(hào)
  233.          WriteCommandLCD(0x01,1); //顯示清屏
  234.          WriteCommandLCD(0x06,1); // 顯示光標(biāo)移動(dòng)設(shè)置
  235.          WriteCommandLCD(0x0C,1); // 顯示開(kāi)及光標(biāo)設(shè)置
  236. }





  237. /***********短延時(shí)********************************************************/
  238. void Delay5Ms(void)
  239. {
  240.          unsigned int TempCyc = 11104;
  241.          while(TempCyc--);
  242. }

  243. /***********長(zhǎng)延時(shí)********************************************************/
  244. void Delay400Ms(void)
  245. {
  246.          unsigned char TempCycA = 5;
  247.          unsigned int TempCycB;
  248.          while(TempCycA--){
  249.                   TempCycB=7269;
  250.                                 while(TempCycB--);
  251.          }
  252. }

  253. void LCD_0        (void){
  254.         unsigned char x,y,i;
  255.         unsigned int tmp=0;
  256.         for(i=0;i<9;){                                                //分兩屏,上半屏和下半屏,因?yàn)槠鹗嫉刂凡煌,需要分開(kāi)
  257.         for(x=0;x<32;x++){                                        //32行
  258.                 WriteCommandLCD(0x34,1);
  259.                 WriteCommandLCD((0x80+x),1);        //列地址
  260.                 WriteCommandLCD((0x80+i),1);        //行地址,下半屏,即第三行地址0X88
  261.                 WriteCommandLCD(0x30,1);               
  262.                 for(y=0;y<16;y++)        
  263.                         WriteDataLCD(0x00);//讀取數(shù)據(jù)寫(xiě)入LCD
  264.                 tmp+=16;               
  265.         }
  266.         i+=8;
  267.         }
  268.         WriteCommandLCD(0x36,1);                        //擴(kuò)充功能設(shè)定
  269.         WriteCommandLCD(0x30,1);
  270. }

  271. //掃描鍵盤(pán)并返回按鍵值
  272. unsigned char key_scan()
  273. {
  274.         unsigned char sta = 1;//設(shè)置一個(gè)狀態(tài),表示按鍵是否按下,1為抬起
  275.         if(sta == 1&&(KEY1 == 0||KEY2 == 0||KEY3 ==0||KEY4 ==0||KEY5 ==0||KEY6 ==0))//如果有一個(gè)按鍵按下,則進(jìn)入下面程序,注意此時(shí)使用的邏輯或的關(guān)系
  276.         {
  277.                Delay5Ms();//消抖5ms
  278.                 sta = 0;//設(shè)置一個(gè)狀態(tài),表示按鍵是否按下,0為按下
  279.                
  280.                 if(KEY1== 0)       return 1;
  281.                 else if(KEY2 ==0)  return 2;
  282.                 else if(KEY3 ==0)  return 3;
  283.                 else if(KEY4 ==0)  return 4;         
  284.                 else if(KEY5 ==0)  return 5;                        
  285.                                               else if(KEY6 ==0)  return 6;         
  286.         }
  287.         else if (KEY1 == 1&&KEY2 == 1&&KEY3 ==1&&KEY4 ==1&&KEY5 ==1&&KEY6 ==1) sta = 1;//表示沒(méi)有按鍵按下,注意此時(shí)使用的是邏輯與的關(guān)系;
  288.         return 0;//沒(méi)有按鍵按下
  289. }

  290. //用于顯示菜單項(xiàng)
  291. void display(unsigned char line)                    //顯示菜單項(xiàng)并設(shè)置選中的項(xiàng)反白
  292. {
  293.          int i;
  294. line = 3-(currentMenu->range_to-line);
  295.    LCD_0();
  296. for(i = 0;i<4;i++)
  297. {
  298.           Lcd_Disp(i+1,0,currentMenu->menuItems[i+currentMenu->range_from]);
  299. }
  300. highlight(0,line,8,255);//反白顯示指定行
  301. }

  302. void func(void)
  303. {

  304. }



  305. void LCD_set_xy( unsigned char x, unsigned char y )
  306. {        //設(shè)置LCD顯示的起始位置,X為行,Y為列
  307.     unsigned char address;
  308.         switch(x)
  309.         {
  310.             case 0: address = 0x80 + y; break;   
  311.             case 1: address = 0x80 + y; break;
  312.             case 2: address = 0x90 + y; break;
  313.             case 3: address = 0x88 + y; break;
  314.             case 4: address = 0x98 + y; break;
  315.             default:address = 0x80 + y; break;
  316.         }
  317.     WriteCommandLCD(address, 1);
  318. }

  319. void Lcd_Disp(unsigned char x,unsigned char y,unsigned char *s)
  320. {
  321.         WriteCommandLCD(0x30,1); //進(jìn)入標(biāo)準(zhǔn)模式
  322.         LCD_Set_XY(x,y);
  323.         while (*s)  
  324.         {
  325.                 WriteDataLCD(*s);
  326.                 s++;
  327.         }
  328.        WriteCommandLCD(0x36,1); //返回圖形模式
  329. }
復(fù)制代碼


液晶顯示器.zip

8.39 KB, 下載次數(shù): 13

回復(fù)

使用道具 舉報(bào)

ID:831047 發(fā)表于 2022-3-4 19:21 | 顯示全部樓層
子菜單和執(zhí)行函數(shù)都應(yīng)該用一級(jí)指針吧?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表