C51单片机基础学习
点灯环节
点亮第一个灯
原理:从VCC正极电极过来,若碰到的组件是 1,那么就会不亮(因为两个都是高电平),若碰到的组件是 0,则会通过高低电平来促使灯发亮
#include <REGX52.H>
void main()
{
P2 = 0xFE;//由高往低数(从P27 ~ P20)1111 1110
}
让灯交互闪烁
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2 = 0xFE;
Delay500ms();
P2 = 0xFF;
Delay500ms();
}
}
/*
P2 = 0x49;
Delay500ms();
P2 = 0xB6;
Delay500ms();
*/
流水灯实现
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
while(1)
{
P2 = 0xFE;
Delay1ms(100);
P2 = 0xFD;
Delay1ms(100);
P2 = 0xFB;
Delay1ms(100);
P2 = 0xF7;
Delay1ms(100);
P2 = 0xEF;
Delay1ms(100);
P2 = 0xDF;
Delay1ms(100);
P2 = 0xBF;
Delay1ms(100);
P2 = 0x7F;
Delay1ms(100);
}
}
独立按键控制灯
#include <REGX52.H>
void main()
{
//P3_1 == 0表示按下
if(P3_1 == 0)//注意这里的独立按键串口,第一个独立按键是P3_1的串口,第二个独立按键是P3_0的串口
{
P2_0 = 1;//表示寄存器中8位中的一位
}else P2_0 = 0;
}
独立按键控制灯状态
注意:单片机上电后所有串口默认是高电平(1)
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms)
{
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
while(1)
{
//P2_0 = 0;
if(!P3_1)
{
Delay1ms(20);
while(!P3_1);
Delay1ms(20);
P2_0 = ~P2_0;
}
}
}
实现二进制点灯方式
用需要定义一个字符去表示对应的二进制数,不断累加
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
unsigned char erjinzhi = 0;
while(1)
{
if(P3_1 == 0)
{
Delay1ms(20);
while(P3_1 == 0);
Delay1ms(20);
erjinzhi++;
P2 = ~erjinzhi;
}
}
}
实现按键操控灯移向
单键操控
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
unsigned char Num = 0;
P2 = ~(0x01);//需要初始化P2的第一位,因为不初始化会直接跳过第一位
while(1)
{
if(P3_1 == 0)
{
Delay1ms(20);
while(P3_1 == 0);
Delay1ms(20);
Num++;
if(Num == 8)Num = 0;
P2 = ~(0x01<<Num);
}
}
}
双键操控
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
unsigned char Num = 0;
P2 = ~(0x01);//ÐèÒª³õʼ»¯P2´®¿ÚµÄÖµ£¬ÒòΪÈç¹û²»³õʼ»¯»áÌø¹ýµÚһλ
while(1)
{
if(P3_1 == 0)
{
Delay1ms(20);
while(P3_1 == 0);
Delay1ms(20);
Num++;
if(Num == 8)Num = 0;
P2 = ~(0x01<<Num);
}
if(P3_0 == 0)
{
Delay1ms(20);
while(P3_0 == 0);
Delay1ms(20);
if(Num == 0)Num = 7;
else Num --;
P2 = ~(0x01 << Num);
//这里为什么继续用左移
//因为你的Num在执行K1按键的时候,Num自增了1,然后你在决定按K2的时候,Num又自减了1,相当于在K1的基础上右移了一位
}
}
}
数码管操作
静态数码管显示
提要点:
1.51单片机上的数码管是共阴连接的,所以需要在位选的时候给定低电平(接地)选中其几号LED,而接下来的段选注意一定是从高位到低位输出哦,因为我前面定义的位选三个接口顺序是由高位到低位的!!!
#include <REGX52.H>
void main()
{
P2_4 = 1;
P2_3 = 0;
P2_2 = 1;
P0 = 0x7D;
}
自己写的 5 (共阳数码管)
#include <REGX52.H>
void digitalTube(unsigned char weizhi)
{
switch(weizhi)
{
case 1:P2_4 = 0;P2_3 = 0;P2_2 = 0;break;
case 2:P2_4 = 0;P2_3 = 0;P2_2 = 1;break;
case 3:P2_4 = 0;P2_3 = 1;P2_2 = 0;break;
case 4:P2_4 = 0;P2_3 = 1;P2_2 = 1;break;
case 5:P2_4 = 1;P2_3 = 0;P2_2 = 0;break;
case 6:P2_4 = 1;P2_3 = 0;P2_2 = 1;break;
case 7:P2_4 = 1;P2_3 = 1;P2_2 = 0;break;
case 8:P2_4 = 1;P2_3 = 1;P2_2 = 1;break;
}
P0 = 0x6D;//从高位到低位的二进制转换为十六进制
}
void main()
{
digitalTube(5);
}
动态数码管显示
提要点:
1.因为单片机实现是 位选 段选 位选 段选 ... 而且它的更新频率很快,所以如果直接这样写的话很容易会产生重叠,所以我们需要加一个延时函数,然后在位选 段选后重新清零数据,这样就可以有效解决数据重叠的问题
2.这种方式叫单片机的直接扫描
#include <REGX52.H>
unsigned char digital_Table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void digital_choice(unsigned char location,unsigned char Num)
{
switch(location)
{
case 1:P2_4 = 1; P2_3 = 1;P2_2 = 1;break;
case 2:P2_4 = 1; P2_3 = 1;P2_2 = 0;break;
case 3:P2_4 = 1; P2_3 = 0;P2_2 = 1;break;
case 4:P2_4 = 1; P2_3 = 0;P2_2 = 0;break;
case 5:P2_4 = 0; P2_3 = 1;P2_2 = 1;break;
case 6:P2_4 = 0; P2_3 = 1;P2_2 = 0;break;
case 7:P2_4 = 0; P2_3 = 0;P2_2 = 1;break;
case 8:P2_4 = 0; P2_3 = 0;P2_2 = 0;break;
}
P0 = digital_Table[Num];
Delay1ms(1);
P0 = 0x00;
}
void main()
{
while(1)
{
digital_choice(1,5);
digital_choice(2,2);
digital_choice(3,2);
digital_choice(4,2);
digital_choice(5,2);
digital_choice(6,2);
digital_choice(7,2);
digital_choice(8,1);
}
}
模块化思想
拿上一个我们写的数码管做例子
main函数里的内容
#include <REGX52.H>
#include "delay.h"
#include "nixie.h"
void nixie(unsigned char location,unsigned char Num);
void main()
{
while(1)
{
nixie(1,5);
Delay1ms(200);
nixie(2,2);
Delay1ms(200);
nixie(3,2);
Delay1ms(200);
nixie(4,2);
Delay1ms(200);
nixie(5,2);
Delay1ms(200);
nixie(6,2);
Delay1ms(200);
nixie(7,2);
Delay1ms(200);
nixie(8,1);
Delay1ms(200);
}
}
.h里的内容
delay函数
#ifndef __DELAY_H__
#define __DELAY_H__
void Delay1ms(unsigned int xms);
#endif
nixie函数
#ifdef __NIXIE_H__
#define __NIXIE_H__
void nixie(unsigned char location,unsigned char Num);
#endif
delay.c的内容
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
nixie.c内容
#include <REGX52.H>
#include "delay.h"
unsigned char digital_Table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void nixie(unsigned char location,unsigned char Num)
{
switch(location)
{
case 1:P2_4 = 1; P2_3 = 1;P2_2 = 1;break;
case 2:P2_4 = 1; P2_3 = 1;P2_2 = 0;break;
case 3:P2_4 = 1; P2_3 = 0;P2_2 = 1;break;
case 4:P2_4 = 1; P2_3 = 0;P2_2 = 0;break;
case 5:P2_4 = 0; P2_3 = 1;P2_2 = 1;break;
case 6:P2_4 = 0; P2_3 = 1;P2_2 = 0;break;
case 7:P2_4 = 0; P2_3 = 0;P2_2 = 1;break;
case 8:P2_4 = 0; P2_3 = 0;P2_2 = 0;break;
}
P0 = digital_Table[Num];
Delay1ms(1);
P0 = 0x00;
}
矩阵键盘操控LCD1602液晶屏
按列依次以低电平选中,再利用行选中
依旧是模块化思想
主函数代码:
#include <REGX52.H>
#include "delay.h"
#include "LCD1602.h"
#include "Matixkey.h"
unsigned char key_Num;
void main()
{
LCD_Init();
while(1)
{
key_Num = Matrixkey();
if(key_Num)
{
LCD_ShowString(1,1,"Hello");
LCD_ShowNum(2,2,key_Num,2);
}
}
}
Matrixkey().c内容
#include <REGX52.H>
#include "delay.h"
unsigned char Matrixkey()
{
unsigned char key_Num = 0;
P1 = 0xFF;
P1_3 = 0;
if(P1_7 == 0)
{
Delay1ms(20);
while(P1_7 == 0);
Delay1ms(20);
key_Num = 1;
}
if(P1_6 == 0)
{
Delay1ms(20);
while(P1_6 == 0);
Delay1ms(20);
key_Num = 5;
}
if(P1_5 == 0)
{
Delay1ms(20);
while(P1_5 == 0);
Delay1ms(20);
key_Num = 9;
}
if(P1_4 == 0)
{
Delay1ms(20);
while(P1_4 == 0);
Delay1ms(20);
key_Num = 13;
}
P1 = 0xFF;
P1_2 = 0;
if(P1_7 == 0)
{
Delay1ms(20);
while(P1_7 == 0);
Delay1ms(20);
key_Num = 2;
}
if(P1_6 == 0)
{
Delay1ms(20);
while(P1_6 == 0);
Delay1ms(20);
key_Num = 6;
}
if(P1_5 == 0)
{
Delay1ms(20);
while(P1_5 == 0);
Delay1ms(20);
key_Num = 10;
}
if(P1_4 == 0)
{
Delay1ms(20);
while(P1_4 == 0);
Delay1ms(20);
key_Num = 14;
}
P1 = 0xFF;
P1_1 = 0;
if(P1_7 == 0)
{
Delay1ms(20);
while(P1_7 == 0);
Delay1ms(20);
key_Num = 3;
}
if(P1_6 == 0)
{
Delay1ms(20);
while(P1_6 == 0);
Delay1ms(20);
key_Num = 7;
}
if(P1_5 == 0)
{
Delay1ms(20);
while(P1_5 == 0);
Delay1ms(20);
key_Num = 11;
}
if(P1_4 == 0)
{
Delay1ms(20);
while(P1_4 == 0);
Delay1ms(20);
key_Num = 15;
}
P1 = 0xFF;
P1_0 = 0;
if(P1_7 == 0)
{
Delay1ms(20);
while(P1_7 == 0);
Delay1ms(20);
key_Num = 4;
}
if(P1_6 == 0)
{
Delay1ms(20);
while(P1_6 == 0);
Delay1ms(20);
key_Num = 8;
}
if(P1_5 == 0)
{
Delay1ms(20);
while(P1_5 == 0);
Delay1ms(20);
key_Num = 12;
}
if(P1_4 == 0)
{
Delay1ms(20);
while(P1_4 == 0);
Delay1ms(20);
key_Num = 16;
}
return key_Num;
}
LCD1602.c内容
#include <REGX52.H>
//引脚配置:
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0
//函数定义:
/**
* @brief LCD1602延时函数,12MHz调用可延时1ms
* @param 无
* @retval 无
*/
void LCD_Delay()
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
/**
* @brief LCD1602写命令
* @param Command 要写入的命令
* @retval 无
*/
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602写数据
* @param Data 要写入的数据
* @retval 无
*/
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602设置光标位置
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @retval 无
*/
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1+0x40));
}
}
/**
* @brief LCD1602初始化函数
* @param 无
* @retval 无
*/
void LCD_Init()
{
LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
LCD_WriteCommand(0x01);//光标复位,清屏
}
/**
* @brief 在LCD1602指定位置上显示一个字符
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @param Char 要显示的字符
* @retval 无
*/
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
LCD_SetCursor(Line,Column);
LCD_WriteData(Char);
}
/**
* @brief 在LCD1602指定位置开始显示所给字符串
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param String 要显示的字符串
* @retval 无
*/
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
/**
* @brief 返回值=X的Y次方
*/
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
/**
* @brief 在LCD1602指定位置开始显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~65535
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以有符号十进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:-32768~32767
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
unsigned char i;
unsigned int Number1;
LCD_SetCursor(Line,Column);
if(Number>=0)
{
LCD_WriteData('+');
Number1=Number;
}
else
{
LCD_WriteData('-');
Number1=-Number;
}
for(i=Length;i>0;i--)
{
LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以十六进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~0xFFFF
* @param Length 要显示数字的长度,范围:1~4
* @retval 无
*/
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i,SingleNumber;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
SingleNumber=Number/LCD_Pow(16,i-1)%16;
if(SingleNumber<10)
{
LCD_WriteData(SingleNumber+'0');
}
else
{
LCD_WriteData(SingleNumber-10+'A');
}
}
}
/**
* @brief 在LCD1602指定位置开始以二进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~1111 1111 1111 1111
* @param Length 要显示数字的长度,范围:1~16
* @retval 无
*/
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
}
}
Matrixkey.h内容
#ifndef __MATIXKEY_h__
#define __MATIXKEY_h__
unsigned char Matrixkey();
#endif
LCD1602.h内容
#ifndef __LCD1602_H__
#define __LCD1602_H__
//用户调用函数:
void LCD_Init();
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char);
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
#endif
矩阵键盘设置密码
第一个人生的工程!
main函数部分
#include <REGX52.H>
#include "delay.h"
#include "LCD1602.h"
#include "Matixkey.h"
unsigned char key_Num;
unsigned int pass_word,cnt;
void main()
{
LCD_Init();
LCD_ShowString(1,1,"Password:");
while(1)
{
key_Num = Matrixkey();
if(key_Num)
{
if(key_Num == 11)
{
if(pass_word == 929)
{
LCD_ShowString(1,15,"AC");
pass_word = 0;
cnt = 0;
LCD_ShowNum(2,2,pass_word,3);
}
else
{
LCD_ShowString(1,15,"No");
pass_word = 0;
cnt = 0;
LCD_ShowNum(2,2,pass_word,3);
}
}
else if(key_Num <= 10)
{
if(cnt < 3)
{
pass_word *= 10;
pass_word += key_Num % 10;
cnt++;
LCD_ShowNum(2,2,pass_word,3);
}
}
//LCD_ShowString(1,1,"Hello");
//LCD_ShowNum(2,2,pass_word,3);
}
}
}
矩阵输入部分
#include <REGX52.H>
#include "delay.h"
unsigned char Matrixkey()
{
unsigned char key_Num = 0;
P1 = 0xFF;
P1_3 = 0;
if(P1_7 == 0)
{
Delay1ms(20);
while(P1_7 == 0);
Delay1ms(20);
key_Num = 1;
}
if(P1_6 == 0)
{
Delay1ms(20);
while(P1_6 == 0);
Delay1ms(20);
key_Num = 5;
}
if(P1_5 == 0)
{
Delay1ms(20);
while(P1_5 == 0);
Delay1ms(20);
key_Num = 9;
}
if(P1_4 == 0)
{
Delay1ms(20);
while(P1_4 == 0);
Delay1ms(20);
key_Num = 13;
}
P1 = 0xFF;
P1_2 = 0;
if(P1_7 == 0)
{
Delay1ms(20);
while(P1_7 == 0);
Delay1ms(20);
key_Num = 2;
}
if(P1_6 == 0)
{
Delay1ms(20);
while(P1_6 == 0);
Delay1ms(20);
key_Num = 6;
}
if(P1_5 == 0)
{
Delay1ms(20);
while(P1_5 == 0);
Delay1ms(20);
key_Num = 10;
}
if(P1_4 == 0)
{
Delay1ms(20);
while(P1_4 == 0);
Delay1ms(20);
key_Num = 14;
}
P1 = 0xFF;
P1_1 = 0;
if(P1_7 == 0)
{
Delay1ms(20);
while(P1_7 == 0);
Delay1ms(20);
key_Num = 3;
}
if(P1_6 == 0)
{
Delay1ms(20);
while(P1_6 == 0);
Delay1ms(20);
key_Num = 7;
}
if(P1_5 == 0)
{
Delay1ms(20);
while(P1_5 == 0);
Delay1ms(20);
key_Num = 11;
}
if(P1_4 == 0)
{
Delay1ms(20);
while(P1_4 == 0);
Delay1ms(20);
key_Num = 15;
}
P1 = 0xFF;
P1_0 = 0;
if(P1_7 == 0)
{
Delay1ms(20);
while(P1_7 == 0);
Delay1ms(20);
key_Num = 4;
}
if(P1_6 == 0)
{
Delay1ms(20);
while(P1_6 == 0);
Delay1ms(20);
key_Num = 8;
}
if(P1_5 == 0)
{
Delay1ms(20);
while(P1_5 == 0);
Delay1ms(20);
key_Num = 12;
}
if(P1_4 == 0)
{
Delay1ms(20);
while(P1_4 == 0);
Delay1ms(20);
key_Num = 16;
}
return key_Num;
}
其他的譬如delay函数和LCD1602函数与前面一样
定时器操控LED灯
原理其实就是时钟->计数器->中断系统
分别给他赋值即可
#include <REGX52.H>
void Timer0_Init()
{
//TMOD = 0x01;
TMOD = TMOD & 0xF0;
TMOD = TMOD | 0x01;
TF0 = 0;
TR0 = 1;
TH0 = 64535 / 256;//取高八位
TL0 = 64535 % 256;//取低八位
ET0 = 1;
EA = 1;
PT0 = 0;
}
unsigned int cnt = 0;
void Timer0_Routine() interrupt 1
{
//这里主要是将数据还原用的
TH0 = 64535 / 256;
TL0 = 64535 % 256;
cnt++;
if(cnt >= 1000)
{
cnt = 0;
P2_0 = ~P2_0;
}
}
void main()
{
Timer0_Init();
while(1)
{
}
}
定时器实现流水灯操作
main函数部分
#include <REGX52.H>
#include "key.h"
#include "Timer0Init.h"
#include <INTRINS.h>
//unsigned int cnt = 0;
unsigned int LED_MODE;
void Timer0_Routine() interrupt 1
{
static unsigned int cnt;
TL0 = 0x18;
TH0 = 0xFC;
cnt++;
if(cnt >= 200)
{
cnt = 0;
if(LED_MODE == 0)
{
P2 = _crol_(P2,1);
}
if(LED_MODE == 1)
{
P2 = _cror_(P2,1);
}
}
}
void main()
{
P2 = 0xFE;
Timer0Init();
while(1)
{
unsigned char key;
//unsigned int LED_MODE;
key = KeyNum();
if(key)
{
if(key == 1)
{
LED_MODE++;
if(LED_MODE >= 2)LED_MODE = 0;
}
}
}
}
各个分函数
定时器
#include <REGX52.H>
void Timer0Init()
{
TMOD &= 0xF0;
TMOD |= 0x01;
TL0 = 0x18;
TH0 = 0xFC;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
PT0 = 0;
}
按键代码
#include <REGX52.H>
#include "delay.h"
unsigned char key = 0;
unsigned char KeyNum()
{
if(P3_1 == 0){Delay1ms(20);while(P3_1 == 0);Delay1ms(20);key = 1;}
if(P3_0 == 0){Delay1ms(20);while(P3_0 == 0);Delay1ms(20);key = 2;}
if(P3_2 == 0){Delay1ms(20);while(P3_2 == 0);Delay1ms(20);key = 3;}
if(P3_3 == 0){Delay1ms(20);while(P3_3 == 0);Delay1ms(20);key = 4;}
return key;
}
定时器制作
main函数
#include <REGX52.H>
#include "LCD1602.h"
#include "delay.h"
#include "Timer0Init.h"
unsigned int hour1,min1,sec1;
void Timer0_Routine() interrupt 1
{
static unsigned int cnt;
TL0 = 0x18;
TH0 = 0xFC;
cnt++;
if(cnt >= 1000)
{
cnt = 0;
sec1++;
if(sec1 >= 60)
{
sec1 = 0;
min1++;
if(min1 >= 60)
{
min1 = 0;
hour1++;
if(hour1 >= 24)
{
hour1 = 0;
}
}
}
}
}
void main()
{
LCD_Init();
Timer0Init();
LCD_ShowString(1,1,"Clock:");
LCD_ShowString(2,1," : :");
while(1)
{
LCD_ShowNum(2,1,hour1,2);
LCD_ShowNum(2,4,min1,2);
LCD_ShowNum(2,7,sec1,2);
}
}
定时器设置
#include <REGX52.H>
void Timer0Init()
{
TMOD &= 0xF0;
TMOD |= 0x01;
TL0 = 0x18;
TH0 = 0xFC;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
PT0 = 0;
}
本文来自博客园,作者:Alaso_shuang,转载请注明原文链接:https://www.cnblogs.com/Alaso687/p/18771027

浙公网安备 33010602011771号