標(biāo)題: 51單片機(jī)之1602.c模塊化程序 [打印本頁]

作者: CxrK    時(shí)間: 2021-12-6 20:38
標(biāo)題: 51單片機(jī)之1602.c模塊化程序
單片機(jī)源程序如下:
  1. #include <reg51.h>

  2. // 對LCD1602的底層以及高層時(shí)序做封裝

  3. // IO接口定義
  4. #define LCD1602_DB  P0      //data bus 數(shù)據(jù)總線
  5. // 控制總線
  6. sbit LCD1602_RS = P2^6;
  7. sbit LCD1602_RW = P2^5;
  8. sbit LCD1602_EN = P2^7;       


  9. /************ 低層時(shí)序 ********************************/
  10. void Read_Busy()           //忙檢測函數(shù),判斷bit7是0,允許執(zhí)行;1禁止
  11. {
  12.     unsigned char sta;      //
  13.     LCD1602_DB = 0xff;
  14.     LCD1602_RS = 0;
  15.     LCD1602_RW = 1;
  16.     do
  17.     {
  18.         LCD1602_EN = 1;
  19.         sta = LCD1602_DB;
  20.         LCD1602_EN = 0;    //使能,用完就拉低,釋放總線
  21.     }while(sta & 0x80);
  22. }

  23. void Lcd1602_Write_Cmd(unsigned char cmd)     //寫命令
  24. {
  25.     Read_Busy();
  26.     LCD1602_RS = 0;
  27.     LCD1602_RW = 0;       
  28.     LCD1602_DB = cmd;
  29.     LCD1602_EN = 1;
  30.     LCD1602_EN = 0;   
  31. }

  32. void Lcd1602_Write_Data(unsigned char dat)   //寫數(shù)據(jù)
  33. {
  34.       Read_Busy();
  35.       LCD1602_RS = 1;
  36.       LCD1602_RW = 0;
  37.       LCD1602_DB = dat;
  38.       LCD1602_EN = 1;
  39.       LCD1602_EN = 0;
  40. }

  41. /************* 高層時(shí)序 ******************************/
  42. // 本函數(shù)用來設(shè)置當(dāng)前光標(biāo)位置,其實(shí)就是設(shè)置當(dāng)前正在編輯的位置,
  43. // 其實(shí)就是內(nèi)部的數(shù)據(jù)地址指針,其實(shí)就是RAM顯存的偏移量
  44. // x范圍是0-15,y=0表示上面一行,y=1表示下面一行
  45. void LcdSetCursor(unsigned char x,unsigned char y)  //坐標(biāo)顯示
  46. {
  47.     unsigned char addr;
  48.     if(y == 0)
  49.         addr = 0x00 + x;
  50.     else
  51.         addr = 0x40 + x;
  52.    
  53.     Lcd1602_Write_Cmd(addr|0x80);
  54. }

  55. // 函數(shù)功能是:從坐標(biāo)(x,y)開始顯示字符串str
  56. // 注意這個(gè)函數(shù)不能跨行顯示,因?yàn)轱@存地址是不連續(xù)的
  57. // 其實(shí)我們可以封裝出一個(gè)能夠折行顯示的函數(shù)的
  58. void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str)     //顯示字符串
  59. {
  60.     LcdSetCursor(x,y);      //當(dāng)前字符的坐標(biāo)
  61.     while(*str != '\0')
  62.     {
  63.         Lcd1602_Write_Data(*str++);
  64.     }
  65. }

  66. // 初始化LCD,使之能夠開始正常工作
  67. void InitLcd1602()              //1602初始化
  68. {
  69.     Lcd1602_Write_Cmd(0x38);    //打開,5*8,8位數(shù)據(jù)
  70.     //Lcd1602_Write_Cmd(0x0c);        // 打開顯示并且無光標(biāo)
  71.         Lcd1602_Write_Cmd(0x0f);        // 打開顯示并且光標(biāo)閃爍
  72.     Lcd1602_Write_Cmd(0x06);
  73.     Lcd1602_Write_Cmd(0x01);    //清屏   
  74. }
復(fù)制代碼


Keil代碼下載: lcd1602.zip (9.43 KB, 下載次數(shù): 17)






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1