1. 51单片机按键控制数码管
#include <reg52.h> // 可以参考一下
#define uchar unsigned char
#include <stdio.h>
unsigned char* c;
sbit k0=P3^2;
sbit k1=P3^3;
sbit k2=P3^4;
sbit k3=P3^5;
sbit k4=P3^7;
sbit led1=P1^7;
uchar k;
bit y0,y1,y2,y3,y4,y5;
bit t,s,ld;
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d};
void SendOneChar(char c) //发送字节到PC
{
TI=0;
SBUF = c;
while(!TI);
TI=0;
}
void SendString(char *st) //发送字符串到PC
{
while(*st)
{
SendOneChar(*st++);
}
}
void main()
{
SCON = 0x50; //SCON: mode 1, 8-bit UART, enable rcvr
TMOD = 0x20; //TMOD: timer 1, mode 2, 8-bit reload
PCON = 0x80; // 数据位8、停止位1。效验位无 (11.0592M)
TH1 = 0xF4; //TH1: reload value for [email protected]
TR1 = 1; //启动定时器1
ES = 1; //允许串口中断位
EA = 1; //允许总中断位
c=0;
s=1;
k=0;
P1 =~table[k];
while(1)
{
if(k0==0 && y0==0)
{
y0=1;
c = "as";
SendString(c); //发送字符串
}
if(k0==1 ) { y0=0; }
if(k1==0 && y1==0)
{
y1=1;
c = "bs";
SendString(c); //发送字符串
}
if(k1==1 ) { y1=0; }
if(k2==0 && y2==0)
{
y2=1;
c = "ds";
SendString(c); //发送字符串
}
if(k2==1 ) { y2=0; }
if(k3==0 && y3==0)
{
y3=1;
c = "es";
SendString(c); //发送字符串
}
if(k3==1 ) { y3=0; }
if(k4==0 && y4==0)
{
y4=1;
c = "fs";
SendString(c); //发送字符串
}
if(k4==1 ) { y4=0; }
// for(i = 0; i < 4000; i++); //延迟一小段时间
}
}
void chuankou(void) interrupt 4
{
if(RI==1)
{
RI = 0;
if(t==1)
{
if(SBUF=='z') { k++;ld=~ld; }
s=1; t=0;
if(k==10) { k=0;}
P1 =~table[k];
led1=ld;
}
if(SBUF=='a' && s==1) { t=1;s=0;}
}
}
/////////////////////////////////////////
#include <reg51.h> // 接收 试试
#define uchar unsigned char
unsigned char* c;
sbit led1=P3^2; // 接收 az , P.2 输出低电平, P3.3 输出高电平。
sbit led2=P3^3; // 接收 aa , P.2 输出高电平, P3.3 输出低电平。
sbit k0=P3^5;
bit y0,t,s;
uchar k;
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d};
void SendOneChar(char c) //发送字节到PC
{
TI=0;
SBUF = c;
while(!TI);
TI=0;
}
void SendString(char *st) //发送字符串到PC
{
while(*st)
{
SendOneChar(*st++);
}
}
void main (void)
{
SCON = 0x50; // REN=1允许串行接受状态,串口工作模式1
TMOD = 0x21; // 定时器工作方式2
PCON = 0x80; // 数据位8、停止位1。效验位无 (11.0592M)
TH1 = 0xF4; // TH1 = 0xFD; 波特率 2400
TH0=(65536-54253)/256; // 50 mS
TL0=(65536-54253)%256;
TR1 = 1;
ES = 1; // 开串口中断
EA = 1; // 开总中断
led1=1;led2=0;
// ET0=1;
// TR0=1;
s=1;
k=0;
P1 =~table[k];
while(1)
{
if(k0==0 && y0==0)
{
y0=1;
c = "az";
SendString(c); //发送字符串
}
if(k0==1 ) { y0=0; }
}
}
void chuankou(void) interrupt 4
{
if(RI==1)
{
RI = 0;
if(t==1)
{
if(SBUF=='s') {led1=~led1;led2=~led2; k++; }
//if(SBUF=='s') {led1=1;led2=0; k--; }
s=1; t=0;
if(k==10) { k=0;}
if(k==255) { k=9; }
P1 =~table[k];
}
// 1号 a 2号 b ---
if(SBUF=='a' && s==1) { t=1;s=0;}
}
}
void T0_time()interrupt 1
{
TH0=(65536-54253)/256; // 50 mS
TL0=(65536-54253)%256;
k++;
if(k==10) // 这里修改时间
{
k=0;
led1=1;
led2=1;
}
}
2. 51单片机c语言设计,按键控制数码管,依次按下显示0到9循环
#include<reg51.h>
sbitk1=P1^0;
#define uint16 unsigned int
#define uchar unsigned char
uchar code shuzu[ ]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay()
{
uint16 i=1000;
while(i--);
}
void main()
{
uchar n ;
P2=0xfe;
P1=0xff;
while(1)
{
if(k1==0)
{
if(n>=9) n=0;
else n++;
P0=shuzu[n];
while(!k1);
delay();
while(!k1);
}
}
}
(2)51单片机按键干扰数码管扩展阅读:
51单片机的功能特性
1,可以仿真63K程序空间,接近64K 的16位地址空间;
2,可以仿真64Kxdata 空间,全部64K 的16位地址空间;
3,可以真实仿真全部32 条IO脚;
4,完全兼容keilC51 UV2 调试环境,可以通过UV2 环境进行单步,断点, 全速等操作;
5,可以使用C51语言或者ASM汇编语言进行调试 ;
6,可以非常方便地进行所有变量观察,包括鼠标取值观察,即鼠标放在某 变量上就会立即显示出它此的值;
7,可选 使用用户晶振,支持0-40MHZ晶振频率;
8,片上带有768字节的xdata,您可以在仿真时选 使用他们,进行xdata 的仿真;
9,可以仿真双DPTR 指针;
10,可以仿真去除ALE 信号输出. ;
11,自适应300-38400bps 的所有波特率通讯;
12,体积非常细小,非常方便插入到用户板中.插入时紧贴用户板,没有连接电缆,这样可以有效地减少运行中的干扰,避免仿真时出现莫名其妙的故障;
13,仿真插针采用优质镀金插针,可以有效地防止日久生锈,选择优质园脚IC插座,保护仿真插针,同时不会损坏目标板上的插座. ;
14,仿真时监控和用户代码分离,不可能产生不能仿真的软故障;
15,RS-232接口不计成本采用MAX202集成电路,串行通讯稳定可靠,绝非一般三极管的简易电路可比。
3. 51单片机怎样用键盘控制数码管显示
51单片机怎样用键盘控制数码管显示的方法。
如下参考:
1.首先,编写代码并点亮数码管。
4. 51单片机怎么用按键控制一个数码管,按一下显示一个数字,顺序显示
1、首先,要把代码写好,点亮数码管。
5. 51单片机怎样设按键切换数码管显示时间和日期
你要设置一个标志变量,如flag,在进入按键程序后,flag++;
然后加个控制判断语句
if(flag>3)
flag=0;
这样,在显示模块中再加入开关语句;
switch(flag)
{
case 0:...显示时间..
case 1:...显示日期。。.
case 2:...显示温度。。。
}