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

QQ登錄

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

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

單片機(jī)驅(qū)動(dòng)LCM1604四行液晶例程

[復(fù)制鏈接]
ID:276663 發(fā)表于 2021-6-18 14:31 | 顯示全部樓層 |閱讀模式
51hei圖片_20210618143308.jpg

單片機(jī)源程序如下:
  1. /* Module---------------------------------------------------------------------*/
  2. #define    _MAIN_MODULE_

  3. /* Includes ------------------------------------------------------------------*/
  4. #include "main.h"

  5. /* Private typedef -----------------------------------------------------------*/
  6. #define DATAPORT  P0
  7. sbit  LCD_RS = P1^0;//0指令 1數(shù)據(jù)
  8. sbit  LCD_RW = P1^1;//0寫 1讀
  9. sbit  LCD_EN = P1^2;//使能信號(hào)腳
  10. /* Private define ------------------------------------------------------------*/


  11. /* Private macro -------------------------------------------------------------*/
  12. code const uint8_t revision_date[12] = __DATE__;        //固件生成日期
  13. code const uint8_t revision_time[16] = __TIME__;        //固件生成時(shí)間
  14. code const uint8_t revision_ver[13]={" Ver:1.0 \r\n"};//版本信息


  15. /* Private variables ---------------------------------------------------------*/

  16. /* Private function prototypes -----------------------------------------------*/

  17. /* Private functions ---------------------------------------------------------*/

  18. unsigned int time_ms;
  19. unsigned char  seg[]={
  20. "  LCD1604 Demo  "
  21. "  By    Liming  "
  22. "   2021-06-15   "
  23. "  PM: --:--:--  "
  24. };

  25. void IO_Init(void)
  26. {
  27.   P0M0 = 0x00;P0M1 = 0X00;
  28.     P1M0 = 0x07;P1M1 = 0X00;
  29. }
  30. /*******************************************************************************
  31.   * @brief  None
  32.   * @param  None
  33.   * @retval None
  34. ****************************************************************Author:Liming**/
  35. void TIM0_Init(void)    //1毫秒@22.1184MHz
  36. {
  37.   AUXR |= 0x80;   //定時(shí)器時(shí)鐘1T模式
  38.   TMOD &= 0xF0;   //設(shè)置定時(shí)器模式
  39.   TL0 = 0x9A;   //設(shè)置定時(shí)初值
  40.   TH0 = 0xA9;   //設(shè)置定時(shí)初值
  41.   TF0 = 0;    //清除TF0標(biāo)志
  42.   ET0 = 1;    //定時(shí)器0中斷開(kāi)啟
  43.   TR0 = 1;    //定時(shí)器0開(kāi)始計(jì)時(shí)
  44.   EA = 1;
  45. }
  46. void TIM0_int (void) interrupt 1
  47. {
  48.   if(time_ms>0)time_ms--;
  49.   if(task1s)task1s--;  
  50. }
  51. void Delay(unsigned int t)
  52. {   
  53.     time_ms = t;
  54.     while(time_ms);
  55. }

  56. uint8_t LCD1604_ReadSta(void)
  57. {
  58.   uint8_t res;

  59.   LCD_RS = 0;
  60.   LCD_RW = 1;
  61.   LCD_EN = 1;Delay(5);
  62.   res = DATAPORT;
  63.   LCD_EN = 0;
  64.   return res;
  65. }


  66. void LCD1604_BusyWait(void)
  67. {
  68.   /*當(dāng)P0^7為低電平時(shí),P0口為空閑狀態(tài)*/
  69.   while((LCD1604_ReadSta()&0x80)==0x80);
  70.   Delay(1);
  71. }

  72. void LCD1604_WriteCmd(unsigned char Cmd)
  73. {
  74.   LCD1604_BusyWait();
  75.   LCD_RS = 0;
  76.   LCD_RW = 0;
  77.   DATAPORT = Cmd;
  78.   LCD_EN = 1;Delay(10);
  79.   LCD_EN = 0;
  80. }


  81. void LCD1604_WriteDat(unsigned char Dat)
  82. {
  83.   LCD1604_BusyWait();
  84.   LCD_RS = 1;
  85.   LCD_RW = 0;
  86.   DATAPORT = Dat;
  87.   LCD_EN = 1;
  88.   Delay(10);
  89.   LCD_EN = 0;
  90. }

  91. void initial(void){
  92.   LCD1604_WriteCmd(0x38);
  93.   LCD1604_WriteCmd(0x01);
  94.   LCD1604_WriteCmd(0x06);
  95.   LCD1604_WriteCmd(0x0c);
  96. }
  97.                                                                         
  98. void main(void){

  99.   unsigned char data_index;
  100.   unsigned char i;
  101.   
  102.   TIM0_Init();  //SysTick 1ms
  103.   initial();        //初始化LCD
  104.   while(1){
  105.     if(task1s==0)
  106.     {
  107.       task1s = 1000;sec++;
  108.       if(sec>=60){
  109.         sec = 0;min++;
  110.         if(min>=60){
  111.           min = 0;hour++;
  112.           if(hour>=12)hour = 0;
  113.         }
  114.       }
  115.       seg[54] = (hour/10)+'0';
  116.       seg[55] = (hour%10)+'0';
  117.       seg[57] = (min/10)+'0';
  118.       seg[58] = (min%10)+'0';
  119.       seg[60] = (sec/10)+'0';
  120.       seg[61] = (sec%10)+'0';
  121.   
  122.       data_index = 0;
  123.       LCD1604_WriteCmd(0x80);//第一行
  124.       while(data_index<=15){LCD1604_WriteDat(seg[data_index]);data_index++;}
  125.       LCD1604_WriteCmd(0xc0);//第二行
  126.       while(data_index<=31){LCD1604_WriteDat(seg[data_index]);data_index++;}
  127.       LCD1604_WriteCmd(0x90);//第三行
  128.       while(data_index<=47){LCD1604_WriteDat(seg[data_index]);data_index++;}
  129.       LCD1604_WriteCmd(0xd0);//第四行
  130.       while(data_index<=63){LCD1604_WriteDat(seg[data_index]);data_index++;}
  131.     }
  132. }
  133. }
復(fù)制代碼

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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