欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

專注電子技術學習與研究
當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

51單片機按鍵搶答器程序

作者:huqin   來源:本站原創   點擊數:  更新時間:2014年03月15日   【字體:

本程序由好幾個頭文件組成,都在下面,你可以復制代碼并保存為獨立的文件
頭文件STC12C5A.H下載: http://www.raoushi.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

關閉窗口

相關文章