专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

51单片机按键抢答器程序

作者:huqin   来源:本站原创   点击数:  更新时间:2014年03月15日   【字体:

本程序由好几个头文件组成,都在下面,你可以复制代码并保存为独立的文件
头文件STC12C5A.H下载: http://www.51hei.com/mcu/2564.html

#include <STC12C5A.H>
#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
/*
单片机:STC89CXX, 晶体: 12M  编译:KEIL uVision4
LCD1602命令代码:
 代码     说明
 1  清除显示
 2  光标复位
输入模式选择:
 X  0000 01(I/D)S
   I/D:光标移动方向,1右移,0左移
   S:屏幕文字是否移动.1移动.
显示开/关控制:
 X       0000 1DCB
   D:整体显示开关,1为开
   C:光标开关,1为开
   B:光标闪烁,1为闪烁
光标或字符移位:
 X  0001 (S/C)(R/L)**
   S/C: 1时移动文字,0时移动光标
   R/L:1为右移,0为左移
功能设置:
 X  001(DL) NF**
   DL:1为8线模式,0为4线模式
   N: 0为单行显示,1为2行显示
   F: 0为5X7字符,1为5X10字符
字符地址:
 X  01xx xxxx
   xxxxx:对应的ASCII地址
显示地址:
 X  1xxx xxxx
   xxx xxxx:屏幕显示地址
内部显示和屏幕的地址对应注明:
     00--0x0F 对应屏幕第一行,00--0x27内部第一行
     0x40--0x4F对应屏幕第二行,0x40--0x67内部第二行
*/
#define uchar unsigned char
#define uint unsigned int
//定义硬件接口
#define LCD_DATA  P0
sbit LCD_EN=P2^5;
sbit LCD_RS=P2^3;
sbit LCD_RW=P2^4;
/**********************************************
函数名称:
函数功能:
函数调用:
输入参数:
输出参数:
版权信息:
时间版本:   V1.0
***********************************************/
/**********************************************
函数名称: delay_ms(uint num)
函数功能: 延时
函数调用:
输入参数: ms
输出参数:
版权信息:
时间版本: V1.0
备注:     晶体:12M
***********************************************/
void delay_ms(uint temp)
{
 uint x,y;
 for(x=temp;x>0;x--)
  for(y=110;y>0;y--);
}
/**********************************************
函数名称: write_com(uchar com)
函数功能: 写命令
函数调用: delay_ms(uint num)
输入参数: 命令代码
输出参数:
版权信息:
时间版本:  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;
}
/**********************************************
函数名称:write_data(uchar date)
函数功能:写数据
函数调用:delay_ms(uint num)
输入参数:数据
输出参数:
版权信息:
时间版本:  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;
}
/**********************************************
函数名称:
函数功能:LCD_init()
函数调用:LCD_w_com()
输入参数:
输出参数:
版权信息:
时间版本: V1.0
***********************************************/
void LCD_init()
{
 LCD_EN=0;
 LCD_w_com(0x38);//8线2行5X7字符模式
 LCD_w_com(0x0c);//显示开,光标不显示,光标不闪烁
 LCD_w_com(0x06);//光标右移,文字不移动
 LCD_w_com(0x01);//清除显示,光标复位 
}
/**********************************************
函数名称: LCD_disp_cher(uchar x,uchar y,uchar *p)
函数功能: 指定位置显示字符串
函数调用: LCD_w_com(),LCD_w_data()
输入参数: x,y坐标,字符串指针数组
输出参数:
版权信息:
时间版本: V1.0
备注:    x为列,x<16,y为行,y<2. 字符串长+x不大于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;   //开总中断
 ET0=1;   // T0(定时中断0) 的溢出中断允许位
 TR0=1;   //0的运行控制位
 TMOD=0x1;  //16位定时器 
 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; //独立按键S1~S4
sbit S2=P3^5;
sbit S3=P3^6;
sbit S4=P3^7; //用复位键退出这样节省按键
void delay(uint ms)   //延时
{
 uchar mus;
 for(ms; ms>0; ms--)
  for(mus=110; mus>0; mus--);
}
uint chiclet_keyboard_() //按键检测子程序有返回值用于退出该子程序
{
 uchar dsa[10];   //用于储存显示的数组
 while(1)    //检测死循环,直至检测道按键或时间到sec=0;
 {
  if(S1==0)
  {
   delay(5);
   if(S1==0)
   {
    LCD_disp_cher(0,1,"      S1      "); //显示先按的按键
    return 0;
   }
  }
  if(S2==0)
  {
   delay(5);
   if(S2==0)
   {
    LCD_disp_cher(0,1,"     S2    "); //显示先按的按键
    return 0;
   }
  }
  if(S3==0)
  {
   delay(5);
   if(S3==0)
   {
    LCD_disp_cher(0,1,"     S3    "); //显示先按的按键
    return 0;
   }
  }
  if(S4==0)
  {
   delay(5);
   if(S4==0)
   {
    LCD_disp_cher(0,1,"    S4    "); //显示先按的按键
    return 0;
   }
  }
  if(sec==0)
   {
    LCD_disp_cher(0,1,"  over  time  ");
    return 0;
   }
  sprintf(dsa,"  time  %d  ",sec);   //显示时间
  LCD_disp_cher(0,1,dsa);
 }
}
#endif

关闭窗口

相关文章