Program to connect to LCD display using 8051

Publisher:精灵宠儿Latest update time:2016-09-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Use 8051 to connect to LCD display. When the program is executed, the LCD display will show the digital clock. Modify the time by pressing the button B on the 4ⅹ4 keypad. You can modify the time (2009-08-04 18:57)

/*

Target:89S51

Program description: This example shows how to use Port 1 of 8051 to connect to LCD display. P3.3, P3.4 and P3.5 of Port 3 are connected to the control lines of LCD display. When the program is executed, the LCD display will show a digital clock. Port 2 is connected to the 4×4 keypad, which can modify the time. Button B of the 4×4 keypad can modify the time.

/************************************************* ***** */

#include

#include

#define TIMER0_COUNT 0xD8F0 /*10000h-((12,000,000/(12*100))*/

                             /* Digital clock working mode*/

#define SET 11

#define TRUE 1

#define FALSE 0

#define putchar write_LCD_da ta

typedef struct {

           char hour;

           char minute;

           char second;

} time;

typedef struct {

           char year;

           char month;

           char day;

} date;

time now={23,59,0},display;

date today={04,05,29},tmpday;

static unsigned timer0_tick=100,mode=0,operation;

char co de dayofmonth[]={31,28,31,30,31,30,31,31,30,31,30,31};

char co de weekday[7][4]={"MON","TUE","WED","THU","FRI","SAT",

                         "SUN"};

char co de command[1][6]={"Watch",};

char co de int2char[]="0123456789";

char gotkey();

void display_time(void)

{

  gotoxy(1,0);

        display_LCD_number(display.hour);

        display_LCD_string(":");

        display_LCD_number(display.minute);

        display_LCD_string(":");

        display_LCD_number(display.second);

}

void display_date()

{

    char i,days=4;

  gotoxy(2,2);

        display_LCD_number(today.year);

        display_LCD_string("/");

        display_LCD_number(today.month);

        display_LCD_string("/");

        display_LCD_number(today.day);

        display_LCD_string(" ");

  if (today.month > 1)

        for(i=0;i<=today.month-2;i++)

            days+=(dayofmonth[i]%7);

        if( today.year !=0 ) days+=((today.year-1)/4)+today.year+1;

        if (today.year%4==0 && today.month >2) days++;

        days=(days+today.day) % 7;

        display_LCD_string(&weekday[days][0]);

  }

  int getdigit(unsigned char x,unsigned char y)

  {

        char keys;

        do {

              gotoxy(x,y);

              putchar('_');

              keys = gotkey();

              gotoxy(x,y);

              putchar(int2char[keys]);

        } while(keys>9);

        return(keys);

  }

  int gettime()

  {

        char temp;

        do {

             while((temp=getdigit(1,0))>2); //The tens digit cannot be greater than 2

             temp=temp*10+getdigit(1,1);

             if (temp > 23) display_time();

        } while (temp > 23);

        display.hour=temp;

          while((temp=getdigit(1,3))>5);

        display.minute=temp*10+getdigit(1,4);

        return(TRUE);

  }

  char monthday(char year,char month)

  {

        if(month==2 && year%4==0) // February in a leap year has 29 days

             return(29);

        else

             return (dayofmonth[month-1]); //The number of days in the month in a non-leap year

  }

  int getdate()

  {

        char temp,days;

           temp=getdigit(2,2);

        tmpday.year=temp*10+getdigit(2,3);

        do {

             while((temp=getdigit(2,5))>1); //The ten digit of the month cannot be greater than 1

             temp=temp*10+getdigit(2,6);

             if (temp > 12) display_date(); //The month number cannot be greater than 12

        } while (temp > 12);

        tmpday.month=temp;

        do {

             while((temp=getdigit(2,8))>3); //The ten digit of the day cannot be greater than 3

             temp=temp*10+getdigit(2,9);

             days=monthday(tmpday.year,tmpday.month);

               if(temp > days || temp==0) display_date();

        } while (temp > days || temp==0);

                                  //If the entered date is greater than the date of the month, re-enter

          tmpday.day=temp;

          return(TRUE);

  }

  static void timer0_isr(void) interrupt TF0_VECTOR using 1

  {

        TR0=0;

        TL0=(TIMER0_COUNT & 0x00FF);

        TH0=(TIMER0_COUNT >> 8);

        TR0=1;

        if(--timer0_tick) return;

        timer0_tick=100;

        now.second++; //second plus 1

        if (now.second==60) { //If seconds equals 60

                now.second=0; //seconds are restored to 0

                now.minute++; // add 1 to the minute

                if (now.minute==60) { //If the minute is equal to 60

                      now.minute=0; //Minutes are restored to 0

                      now.hour++; // add 1 to the hour

                      if (now.hour==24) { //If the hour is equal to 24

                            now.hour=0; //The hour is restored to 0

                            today.day++; // add 1 to the day

                            if (today.day>monthday(today.year,

                                today.month)) {

                                  today.day=1;   

//If the day exceeds the maximum number of days in the month, it becomes 1

                                  today.month++;         

//month plus 1

                                  if (today.month == 13) {  

//If month is equal to 13

                                       today.month=1;    

//Month returns to 1

                                       today.year++;     

//Year plus 1

                                  }

                             }

                             display_date();

                      }

                 }

         }

         if (operation==SET ) return;

         display=now;

         display_time();

}

static void timer0_initialize(void)

{

  EA=0;  

  TR0=0;

  TMOD &= 0XF0;

  TMOD |= 0x01;

  TL0=(TIMER0_COUNT & 0x00FF);

  TH0=(TIMER0_COUNT >> 8);

  PT0=0;

  ET0=1;

  TR0=1;

  EA=1;

}

void main (void) {

        char keys;

        init_LCD();

        clear_LCD();

        gotoxy(2,0);

        display_LCD_string("20");

        display=now;

        display_time();

        display_date();

        gotoxy(1,9);

        display_LCD_string(&command[mode][0]);

        timer0_initialize();

        do {

        keys = gotkey();

             if(keys==SET) {

                  operation=SET;

                  if ( gettime()) now=display;

                  if ( getdate()) {

                  today=tmpday;

                  display_date();

             }

        }

             operation=0;

        } while(1);

}

Reference address:Program to connect to LCD display using 8051

Previous article:89S51 plays the song Happy Birthday c program
Next article:PWM square wave generation 51 program

Recommended ReadingLatest update time:2024-11-16 18:04

Construction and application of large-size LCD optical measurement system
◎Problems faced in measurement/testing To build an LCD optical measurement system to inspect panel quality, the following problems will be faced: 1. Establish a measurement system that can quickly measure the uniformity of 9, 13, and 25 points on a certain point on the panel, including Luminance, CIE (
[Test Measurement]
Construction and application of large-size LCD optical measurement system
AVR MCU Study Notes-LCD1602 Module
After learning 51 MCU, I learned AVR. It feels easy to get started. LCD1602 was learned when I was learning 51, so I can directly modify the related IO port operations. Now I will review it. 1602 can display two lines of characters, each line can display 16 characters. Unfortunately, it can't display Chinese. It helped
[Microcontroller]
AVR MCU Study Notes-LCD1602 Module
S3C2440 bare metal -------LCD_Drawing dots, lines and circles
1. Image point We first implement the function of drawing points.   #include "lcd.h"   /* Implement drawing points*/   /* Get LCD parameters */ static unsigned int fb_base; static int xres, yres, bpp;   void fb_get_lcd_params(void) { get_lcd_params(&fb_base, &xres, &yres, &bpp); }   /* rgb: 0x00RRGGBB */ unsigned sh
[Microcontroller]
Multi-channel data acquisition system based on MSC1210Y5
1 Introduction The data collection part with MSC1210Y5 as the core is placed underwater and works in an unattended self-contained mode. Due to the harsh marine environment, the instrument must have good reliability and accuracy. Otherwise, it may provide inaccurate marine environment parameters to ships sailing in th
[Industrial Control]
Design of wireless control system for model car based on C8051F040
0 Introduction Automobile durability test is an important part of automobile test. During the test, the change of driving behavior of test personnel often leads to inconsistent experimental results, thus reducing the validity of experimental data. Therefore, major automobile companies have successively adopted drivin
[Microcontroller]
Design of wireless control system for model car based on C8051F040
51 Study Notes: Using Bus to Connect 1602 and 8051
//Use bus mode to realize the communication between 8051 and 1602 //The main function of this program is to send a string of characters to 1602 and display it #include reg52.h #include absacc.h #define uchar unsigned char #define uint unsigned int #define
[Microcontroller]
51 Study Notes: Using Bus to Connect 1602 and 8051
8051 MCU (STC89C52) timer achieves 10ms accurate timing
8051 has two 16-bit counters integrated inside, which can be used as timers as needed. At this time, the counting frequency of the timer is system CLK/12. If the crystal frequency of the CPU is 12MHz, then the frequency of the timer signal source is fixed to 12MHz/12 = 1MHz. If the timing period of the timer is set to
[Microcontroller]
8051 MCU (STC89C52) timer achieves 10ms accurate timing
51 single chip microcomputer controls LCD12864 LCD screen to display graphic menu
I have been thinking for a long time. Every time I make a menu, I am limited by various restrictions of the 12864 character mode, such as font size, icons cannot be displayed, etc., and there is no effect. This time, I will be completely relieved. I will write a single function that can complete all functions. Only 2 p
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号