51单片机按键控制数码管

51单片机按键控制数码管,第1张

#include <reg52h>    //  可以参考一下

#define uchar unsigned char

#include <stdioh>

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。效验位无 (110592M)

TH1 = 0xF4;     //TH1:  reload value for 2400@110592MHz

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 <reg51h> // 接收  试试

#define uchar unsigned char

unsigned char c;

sbit   led1=P3^2;     //  接收 az  , P2 输出低电平, P33  输出高电平。

sbit   led2=P3^3;     //  接收 aa  , P2 输出高电平, P33   输出低电平。

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。效验位无 (110592M)

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;

  }

}

采用共阴极LED数码管,51单片机P1口输出数据:ORG 0000H

JMP MAIN

ORG 0100H

MAIN:MOV DPTR,#TAB

MOV R1,#00H

DISP:MOV A,R1

MOVC A,@A+DPTR

MOV P1,A

LCALL DELAY

INC R1

CJNE R1,#0AH,DISP

SJMP $

TAB: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FHDELAY: MOV R4,#2

DELAY100MS:MOV R5,#200

DELAY5MS: MOV R6,#250

LOOP: DJNZ R6,LOOP

DJNZ R5,DELAY5MS

DJNZ R4,DELAY100MS

RET

#include <reg51h>

unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

void show(unsigned char n);

void delay();

void main()

{

unsigned char i;

int j;

while(1)

{

for(i=0;j=100,i<100;++i)

while(j--)

show(i);

}

}

void show(unsigned char n)

{

P0=table[n%10];

P2=~0x01;

delay();

P2=0xFF;

P0=table[n/10];

P2=~0x02;

delay();

P2=0xFF;

}

void delay()

{

int i=100;

while(i--);

}

两只数码管段码分别由P0,P2输出控制,P0是十位,P2是个位。P32,P34分别接两只按键,P32清0,P34计数。

以上程序来自《单片机C语言程序设计实训100例——基于8051+Proteus仿真》

#include <reg52h>

#define uchar unsigned char

#define uint unsigned int

uchar DSY_CODE[]=

{

0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f

};

uchar Count = 0;

void main()

{

P0 = 0x00;

P2 = 0x00;

TMOD = 0x06;

TH0=255;

TL0=255;

ET0=1;

EX0=1;

EA =1;

IP =0x02;

IT0=1;

TR0=1;

while(1)

{

P0 = DSY_CODE[Count/10];

P2 = DSY_CODE[Count%10];

}

}

void Clear_Counter() interrupt 0

{

Count = 0;

}

void Key_Counter() interrupt 1

{

Count = (Count + 1) %100;

}

以上就是关于51单片机按键控制数码管全部的内容,包括:51单片机按键控制数码管、51单片机控制数码管循环显示0-9程序、51单片机控制2个数码管动态实现0到99(C语言)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9453819.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存