51 single chip microcomputer key answering program

Publisher:AningmengLatest update time:2015-04-30 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This program consists of several header files, all of which are below. You can copy the code and save it as a separate file

Download the header file STC12C5A.H:  http://www.51hei.com/mcu/2564.html

#include
#include "stdio.h"
#include "LCD1602.h"
#include "interrupt.h"
#include "chiclet_keyboard.h"
#define uchar unsigned char
#define uint unsigned int
void main()
{
 delay(500);
 LCD_init();
 interrupts_init();
 chiclet_keyboard_();
 EA=0;
 while(1);
}

#ifndef _LCD1602_H
#define _LCD1602_H
/*
MCU: STC89CXX, Crystal: 12M Compiler: KEIL uVision4 
LCD1602 Command Code:
 Code Description
 1 Clear display
 2 Cursor reset
Input mode selection:
 X 0000 01(I/D)S
   I/D: Cursor movement direction, 1 right, 0 left
   S: Whether the screen text moves. 1 move.
Display on/off control:
 X 0000 1DCB
   D: Overall display switch, 1 is on
   C: Cursor switch, 1 is on
   B: Cursor flashing, 1 is flashing
Cursor or character shift:
 X 0001 (S/C)(R/L)**
   S/C: 1 moves text, 0 moves cursor
   R/L: 1 is right, 0 is left
Function setting:
 X 001(DL) NF**
   DL: 1 is 8-line mode, 0 is 4-line mode
   N: 0 is single line display, 1 is 2 lines display
   F: 0 is 5X7 characters, 1 is 5X10 characters
Character address:
 X 01xx xxxx
   xxxxx: corresponding ASCII address
Display address:
 X 1xxx xxxx
   xxx xxxx: screen display address
Internal display and screen address correspondence note:
     00--0x0F corresponds to the first line of the screen, 00--0x27 internal first line
     0x40--0x4F corresponds to the second line of the screen, 0x40--0x67 internal second line
*/
#define uchar unsigned char
#define uint unsigned int 
//Define hardware interface
#define LCD_DATA P0
sbit LCD_EN=P2^5;
sbit LCD_RS=P2^3;
sbit LCD_RW=P2^4;
/**************************************************
Function name:
Function function:
Function call:
Input parameters:
Output parameters:
Copyright information:
Time version: V1.0
***************************************************/
/**************************************************
Function name: delay_ms(uint num)
Function function: delay
Function call:
Input parameter: msOutput
parameter:
Copyright information:
TimeVersion: V1.0Remarks
: Crystal: 12M
***************************************************/
void delay_ms(uint temp)
{
 uint x,y;
 for(x=temp;x>0;x--)
  for(y=110;y>0;y--);
}
/**************************************************
Function name: write_com(uchar com)
Function function: write command
Function call: delay_ms(uint num)
Input parameter: Command code
Output parameter:
Copyright information:
TimeVersion: V1.0
***********************************************/
void LCD_w_com(uchar com)
{
 LCD_RS=0;
 LCD_RW=0;
 LCD_DATA=com;
 delay_ms(5);
 LCD_EN=1;
 delay_ms(5);
 LCD_EN=0;
}[page]
/**********************************************
Function name: write_data(uchar date)
Function function: write data
Function call: delay_ms(uint num)
Input parameter: data
Output parameter:
copyright information:
time version: V1.0
***********************************************/
void LCD_w_data(uchar dat)
{
 LCD_RS=1;
 LCD_RW=0;
 LCD_DATA=dat;
 delay_ms(5);
 LCD_EN=1;
 delay_ms(5);
 LCD_EN=0;
}
/**********************************************
Function name:
Function function: LCD_init()
Function call: LCD_w_com()
Input parameter:
Output parameter:
Copyright information:
Time Version: V1.0
***************************************************/
void LCD_init()
{
 LCD_EN=0; 
 LCD_w_com(0x38); //8 lines, 2 rows, 5X7 character mode
 LCD_w_com(0x0c); //Display on, cursor not displayed, cursor not blinking
 LCD_w_com(0x06); //Cursor right move, text not moving
 LCD_w_com(0x01); //Clear display, cursor reset  
}
/**************************************************
Function name: LCD_disp_cher(uchar x,uchar y,uchar *p)
Function function: Display string at specified position
Function call: LCD_w_com(), LCD_w_data()
Input parameters: x, y coordinates, string pointer array
Output parameters:
Copyright information :
Time version: V1.0
Note: x is column, x<16, y is row, y<2. String length + x is not greater than 16.
***********************************************/
void LCD_disp_cher(uchar x,uchar y,uchar *p)
{
 if(y==1)
 {   
  LCD_w_com(0x80+x);
  while(*p)
  {
  LCD_w_data(*p);
  p++;
  }
 } 
 if(y==2)
 {    
  LCD_w_com(0x80+0x40+x);
  while(*p)
  {
  LCD_w_data(*p);
  p++;
  } 
 }
}
#endif//end LCD1602.H


#ifndef _interrupt_H_
#define _interrupt_H_
#define uint unsigned int
#define uchar unsigned char
uint sec=10;
sbit sb=P1^0;
sbit si=P1^1;
void interrupts_init()
{
 EA=1; //enable global interrupt
 ET0=1; //T0 (timer interrupt 0) overflow interrupt enable bit
 TR0=1; //0 run control bit
 TMOD=0x1; //16-bit timer  
 TH0=(65536-50000)/255;
 TL0=(65536-50000)%255; 
}
void ghjfgf() interrupt 1
{
 uint subtle;
 TH0=(65536-50000)/255; 
 TL0=(65536-50000)%255; 
 subtle++;
 if(subtle==20)
  {
   subtle=0;
   sec--;
  }
}
#endif

#ifndef _chiclet_keyboard_H_
#define _chiclet_keyboard_H_
sbit S1=P3^4; //Independent buttons S1~S4
sbit S2=P3^5;
sbit S3=P3^6;
sbit S4=P3^7; //Use the reset button to exit to save keystrokes
void delay(uint ms) //Delay
{
 uchar mus;
 for(ms; ms>0; ms--)
  for(mus=110; mus>0; mus--);

uint chiclet_keyboard_() //The key detection subroutine has a return value for exiting the subroutine
{
 uchar dsa[10]; //Array for storing display
 while(1) //Detection infinite loop until a key is detected or the time is up sec=0;
 {
  if(S1==0)
  {
   delay(5);
   if(S1==0)
   {
    LCD_disp_cher(0,1," S1 "); //Display the key pressed first
    return 0;
   }
  }
  if(S2==0)
  {
   delay(5);
   if(S2==0)
   {
    LCD_disp_cher(0,1," S2 "); //Display the key pressed first
    return 0;
   }
  }
  if(S3==0)
  {
   delay(5);
   if(S3==0)
   {
    LCD_disp_cher(0,1," S3 "); //Display the key pressed first
    return 0;
   }
  }
  if(S4==0)
  {
   delay(5);
   if(S4==0)
   {
    LCD_disp_cher(0,1," S4 "); //Display the key pressed first
    return 0;
   }
  }
  if(sec==0) 
   {
    LCD_disp_cher(0,1," over time ");
    return 0;
   }
  sprintf(dsa," time %d ",sec); //Display time
  LCD_disp_cher(0,1,dsa);
 }
}
#endif

Reference address:51 single chip microcomputer key answering program

Previous article:MCU + 1602 LCD digital clock program
Next article:Compile HEX microcontroller files using c51 command line

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号