普中科技51纯代码(数码管)
Posted 贩卖星辰点点
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了普中科技51纯代码(数码管)相关的知识,希望对你有一定的参考价值。
数码管
1.点亮一个数码管
#include <reg51.h>//阴极数码管
typedef unsigned char u8;
typedef unsigned int u16;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
u8 code smgduan[]=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
;
void main()
while(1)
LSA=0; //这里是开发板上的段选芯片,选择最后一个数码管
LSB=0; //平时点亮一个数码管,正常点亮即可
LSC=0;
P0=smgduan[5];
2.点亮两个数码管
#include <reg51.h>
typedef unsigned char u8;
typedef unsigned int u16;
u8 code smgduan[]=0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F;
sbit SLA=P2^2;
sbit SLB=P2^3;
sbit SLC=P2^4;
void delay(u16 i)
while(i--);
void DigDisplay()
u8 i;
for(i=0;i<2;i++)
switch(i)
case(0):SLA=0;SLB=0;SLC=0;break;
case(1):SLA=1;SLB=0;SLC=0;break;
default: break;
P0=smgduan[i];
delay(100);
P0=0X00;
void main()
while(1)
DigDisplay();
3.显示八个数码管
#include <reg51.h>
typedef unsigned char u8;
typedef unsigned int u16;
u8 code smgduan[]=0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F;
sbit SLA=P2^2;
sbit SLB=P2^3;
sbit SLC=P2^4;
void delay(u16 i)
while(i--);
void DigDisplay()
u8 i;
for(i=0;i<8;i++)
switch(i)
case(0):SLA=0;SLB=0;SLC=0;break;
case(1):SLA=1;SLB=0;SLC=0;break;
case(2):SLA=0;SLB=1;SLC=0;break;
case(3):SLA=1;SLB=1;SLC=0;break;
case(4):SLA=0;SLB=0;SLC=1;break;
case(5):SLA=1;SLB=0;SLC=1;break;
case(6):SLA=0;SLB=1;SLC=1;break;
case(7):SLA=1;SLB=1;SLC=1;break;
default: break;
P0=smgduan[i];
delay(100);
P0=0X00;
void main()
while(1)
DigDisplay();
4.一位数码管倒计时
#include <reg51.h>
typedef unsigned char u8;
typedef unsigned int u16;
u8 code smgduan[]=0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
void delay(u16 i)
while(i--);
void main()
u16 m;
LSA=0;
LSB=0;
LSC=0;
while(1)
for(m=9;m>0;m--)
P0=smgduan[m];
delay(50000);
5.两位数码管倒计时
#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit LSA = P2^2;
sbit LSB = P2^3;
sbit LSC = P2^4;
sbit beep= P2^7;
u8 code smgduan[10] =
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
; //共阴
u16 n = 0;
u16 m = 99;
void Timer0Init(void) //1毫秒@11.0592MHz
TMOD &= 0xF0; //设置定时器模式 定时器0 是 13位的
TL0 = 0x30; //设置定时初值
TH0 = 0xE3; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
void DIGdisplay()
u8 i;
for (i=0;i<2;i++)
switch(i)
case(0): LSA = 0 ;LSB = 1 ; LSC = 1 ;P0 = smgduan[m/10] ; break; //6
case(1): LSA = 1 ;LSB = 1 ; LSC = 1 ;P0 = smgduan[m%10] ; break; //7 第八个
P0 =0X00;
void Delay200us() //@11.0592MHz
unsigned char i, j;
for(i=99;i>0;i--)
for(j=0;j<110;j++);
/* i = 3;
j = 35;
do
while (--j);
while (--i);
*/
void warning()
beep = ~beep;
Delay200us();
void main()
Timer0Init();
ET0 = 1;
EA = 1;
P0 = 0x00;
while(1)
DIGdisplay();
void InterruptTime0() interrupt 1
TL0 = 0x30; //设置定时初值
TH0 = 0xE3;
n++;
if (n == 1000)
n = 0;
if(m != 0) m--;
if(m == 0) warning();
//代码书写有参考,侵权立删
以上是关于普中科技51纯代码(数码管)的主要内容,如果未能解决你的问题,请参考以下文章