c51单片机用c程序如何将数码管的各位数字显示到十位,且能让各位在显示其他的数字,例如计算器的显示那种..
这个就要看希望显示的是什么呢,比如现在要做一个数字时钟,那么,那些时间变量就是要显示的,可以建立一个数组变量,作为显示缓冲,在主程序里,不停的把当前的时间变量放入显示缓冲区, 数码管动态刷新的时候,就直接把显示缓冲区你的数值进行显示就可以了
#include#define uchar unsigned char;uchar LED1,LED2,LED3,LED4;uchar keyval,calflag,s_dat,d_dat;uchar distab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};unsigned int number,r_dat;
void delay(unsigned int a){ unsigned int i,j; for(i=0;i<a;i++) for(j=0;j<1000;j++);}void dealdat(){ switch(calflag) { case 0: r_dat=s_dat+d_dat; break; case 1: r_dat=s_dat-d_dat; break; case 2: r_dat=s_dat*d_dat; break; case 3: r_dat=s_dat/d_dat; break; default:break; } LED1=(r_dat/1000)%10; LED2=(r_dat/100)%10; LED3=(r_dat/10)%10; LED4=r_dat%10;}uchar kbscan(void){ unsigned char sccode,recode; P3=0x0f; //发0扫描,列线输入 if ((P3 & 0x0f) != 0x0f) //有键按下 { delay(20); //延时去抖动 if ((P3&0x0f)!= 0x0f) { sccode = 0xef; //逐行扫描初值 while((sccode&0x01)!=0) { P3=sccode; if((P3&0x0f)!=0x0f) { recode=(P3&0x0f)|0xf0; return((~sccode)+(~recode)); } else sccode=(sccode<<1)|0x01; } } } return 0; //无键按下,返回0}
void getkey(void){ unsigned char key; key=kbscan(); if(key==0){keyval=0xff;return;} switch(key) { case 0x11:keyval=7;break; case 0x12:keyval=4;break; case 0x14:keyval=1;break; case 0x18:keyval=10;break; case 0x21:keyval=8;break; case 0x22:keyval=5;break; case 0x24:keyval=2;break; case 0x28:keyval=0;break; case 0x41:keyval=9;break; case 0x42:keyval=6;break; case 0x44:keyval=3;break; case 0x48:keyval=11;break; case 0x81:keyval=12;break; case 0x82:keyval=13;break; case 0x84:keyval=14;break; case 0x88:keyval=15;break; default:keyval=0xff;break; }}
void t0isr() interrupt 1{ TH0=0xf4; TL0=0x48; switch(number) { case 0:P2=0x04;P0=distab[LED1];break; case 1:P2=0x08;P0=distab[LED2];break; case 2:P2=0x01;P0=distab[LED3];break; case 3:P2=0x02;P0=distab[LED4];break; default:break; } number++; if(number>3)number=0;}main(){ TMOD = 0x01; number = 0; TH0=0xf4; TL0=0x48; TR0=1; ET0=1; EA=1; LED1=0; LED2=0; LED3=0; LED4=0; while(1) { getkey(); switch(keyval) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: LED3=LED4; LED4=keyval; LED1=0; LED2=0; break; case 10:break; //"ON" case 11: //"=" d_dat=LED3*10+LED4; dealdat(); break; case 12: //"/" calflag=3; s_dat=LED3*10+LED4; LED3=0; LED4=0; break; case 13: //"*" calflag=2; s_dat=LED3*10+LED4; LED3=0; LED4=0; break; case 14: //"-" calflag=1; s_dat=LED3*10+LED4; LED3=0; LED4=0; break; case 15: //"+" calflag=0; s_dat=LED3*10+LED4; LED3=0; LED4=0; break; default:break; } }}
单片机 群228319245
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar k,m,n;
uchar bdata FLAG;
sbit FLAG_anjian = FLAG^0;// 按键标志 1:有键 0:无键
sbit FLAG_12 = FLAG^1;// 显示选择 1:十位数 0:个位数
unsigned D_code[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07, 0x7F,0x6F } ;
//-------- 延时 -----------------------------------------------------------------------
void Delay_1ms(uint x)
{
uint j;
while(x--)
{
for(j=0;j<125;j++) //
{;}
}
}
//-------- T1中断 ---------------------------------------------------------------------
void int_t1() interrupt 3 using 1// 显示 (采用中断方式显示)
{
TH1=(65536-2000)/256; // 2ms中断一次
TL1=(65536-2000)%256;
FLAG_12=~FLAG_12;// 在十位数和个位数之间切换
P2=0xff;
if (FLAG_12)
{
P0=D_code[m];
P2=0xef;
}
else
{
P0=D_code[n];
P2=0xdf;
}
}
void key_scan()// 键扫描
{
unsigned char x,y,z;
P1=0x0f;
if(P1!=0x0f)
Delay_1ms(10);
if(P1!=0x0f)
{
FLAG_anjian=1;
x=P1&0x0f;
P1=0xf0;
y=P1&0xf0;
z=x|y;
switch(z)
{
case 0xee:k=1; break;
case 0xed:k=2; break;
case 0xeb:k=3;break;
case 0xe7:k=4;break;
case 0xde:k=5;break;
case 0xdd:k=6;break;
case 0xdb:k=7;break;
case 0xd7:k=8;break;
case 0xbe:k=9;break;
case 0xbd:k=0;break;
// case 0xbb:k=11;break;
// case 0xb7:k=12;break;
// case 0x7e:k=13;break;
// case 0x7d:k=14;break;
// case 0x7b:k=15;break;
// case 0x77:k=16;break;
}
} else FLAG_anjian=0;
}
void main()
{
FLAG_anjian=FLAG_12=0;
m=n=0;
TMOD=0x11;// 显示定时器初始化
TH1=(65536-2000)/256; // 2ms中断一次
TL1=(65536-2000)%256;
TR1=ET1=EA=1;
while(1)
{
key_scan();// 按键扫描
if (FLAG_anjian)
{
m=n;
n=k;
while(FLAG_anjian) // 等待按键放开
{
key_scan();
}
}
}
}
51单片机编程,用C语言。
给你一个矩阵键盘的参考程序 行列扫描:通过高四位全部输出低电平,低四位输出高电平。当接收到的数据,低四位不全为高电平时,说明有按键按下,然后通过接收的数据值,判断是哪一列有按键按下,然后再反过来,高四位输出高电平,低四位输出低电平,然后根据接收到的高四位的值判断是那一行有按键按下...
51单片机设计跑马灯的程序用(c语言)编写
|P1口接8个发光二极管共阳 include <AT89X51.H> unsigned char i;unsigned char temp;unsigned char a,b;void delay(void){ unsigned char m,n,s;for(m=20;m>0;m--)for(n=20;n>0;n--)for(s=248;s>0;s--);} void main(void){ while(1){ temp=0xfe;P1=temp;delay();for(...
用C语言如何开51单片机的计数器,最好编个实例程序
include <reg51.h> define uchar unsigend uchar define uint unsigned int unsigned char code LED7Code[] = {~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71};\/\/数码管段码 void main(){ uint k;TMOD=0X50;\/\/...
一道51单片机的C语言程序,关于串口通讯的,很简单...
void serial() interrupt 4 \/\/串口中断服务程序,优先级为4 { RI=0; \/\/当接收完成之后RI被置一,发生串口中断,清标志位 a=SBUF; \/\/读取收到的字符,并赋给变量a flag=1; \/\/置接收完成标志flag为一,便于主程序中发送响应 } ...
求一个用C语言编写的51单片机音乐程序代码。
0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, \/\/1,2,3,4,5,6,7,8,i 0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,};void delay(unsigned char t){ unsigned char t1;unsigned long t2;for(t1=0;t1<t;t1++){ for(t2=0;t2<8000;t2++){ ...
大家帮忙找一些51单片机的基本C语言程序例子,最好带说明,谢啦_百度知 ...
中断控制程序:include <AT89X52.H> define uchar unsigned char define uint unsigned int define port_count P2 \/\/P2接8LED接口 \/\/将计数器的二进制值用8个LED显示出来 uchar count;\/\/计数器(存储中断次数)void main(void){ count=0; \/\/清零计数器 port_count=~count;\/\/清零P2口 IT0=...
求用C语言编程实现51单片机的LED灯移动
include<reg51.h> define uchar unsigned char sbit c10=P1^0;sbit c11=P1^1;sbit c12=P1^2;sbit c13=P1^3;uchar led1,led3;void delay(uchar a){ uchar i,j;for(i=0;i<a;i++)for(j=0;j<120;j++);} main(){ uchar keyval=0xff;led1=0xfe;led3=0xf8;while(1){ if(c10...
51单片机只能用c语言编程吗
编程的目的是让51单片机运行,在记忆中我只记得51单片机可以运行C语言和汇编,其实C语言编译后也就是汇编,在单片机上运行的只是一个个指令,简单的用51,复杂的用arm处理器。百度试试
用c语言循环语句怎么写一个51单片机的时钟啊,用8个数码管显示时间...
include<reg51.h> define uchar unsigned charuchar code ledtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};\/\/0-9unsigned char sec=0,min=0,hour=12,scanled;unsigned char key,flashbit,mode,time; unsigned char disdat[8];sbit led=P1^0;void delay(...
怎么用51单片机写这个C程序
写一下吧:include<reg51.h> sbit K=P1^0;sbit K1=P1^1;sbit K2=P1^2;sbit L1=P1^4;sbit L2=P1^5;sbit L3=P1^6;main(){ while(1){ if((K==0)&&(K2==0)){ L1=0;L2=0;L3=0;} if(K2==1){ L3=1;} if((K1==0)&&(K2==0)){ L1=1;L2=1;L3=1;} } } ...
謇叙丽英: 1、delay函数不行!delay(int i) { int a,b; for(a=i;a>0;a--) for(b=110;b>0;b--);//110可以改其他的 }2、选通的管脚你怎么处理了?共阳,给选通低电平了吗或接到地3、主函数里不行:不说要用for吗??while(1) { for(i=10;i>0;i--) { P0=tab[i]; delay(1000); } i=10; }
大武口区18336924459: 求用51单片机接两个数码管,0 - 99动态显示程序(用C语言) - ?
謇叙丽英: #include #define uchar unsigned char #define uint unsigned int uchar DSY_CODE[]= { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f }; void delay(uint x) { uchar i; while(x--)for(i=0;i<120;i++); } void main() { uchar i; P0 = 0x00; P2 = 0x00; while(1)...
大武口区18336924459: 请求一段单片机控制数码管的C语言编程 - ?
謇叙丽英: 展开全部#include#define uchar unsigned char#define uint unsigned int sbit dula=P2^6; sbit wela=P2^7; uchar num; uchar code table[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x07,0x7d,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//我用的是573 还有延时也很...
大武口区18336924459: 51单片机c语言设计,按键控制数码管,依次按下显示0到9循环 - ?
謇叙丽英: #include<reg51.h> sbit k1=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; ...
大武口区18336924459: 51单片机用C语言如何写四个数码管的流水灯程序? - ?
謇叙丽英: 用个移位处理就行了 set(void) {static char cnt = 0;P1 = 0x01 << cnt; cnt ++;if (cnt == 4) {cnt = 0; } }
大武口区18336924459: c51单片机 写一个数码管与流水灯的混合c程序 用串口连接起来 - ?
謇叙丽英: #include#include#define uchar unsigned char uchar distab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; uchar led=0xfe,num=0,times=0,sel=0,segw=0; void t0isr() interrupt 1 { TH0=(65536-5000)/256; TL0=(65536-5000)%256; if(sel==2){...
大武口区18336924459: C51单片机如何用c语言让四位共阴数码管显示1314,要c程序 - ?
謇叙丽英: 这是学51单片机的基础入门课程.看你电路咋设计,数码管是共阳还是共阴.没有固定的程序,建议你弄一个开发板,配的有课件,跟着学学,两三天就会了.
大武口区18336924459: 如何用C语言实现数码管显示数字12345和HELLO - ?
謇叙丽英: 看一下我编的123456 的显示,是共阴极数码管接P1口 ,用了两个74HC573锁存器段选位选,HELLO的编码表 你自己编一下,然后定义一个数组就可以了.我也刚学 ,加油! #include<reg52.h> #define uint unsigned int #define uchar unsigned...
大武口区18336924459: 51单片机控制8个数码管并显示1 - 8 - ?
謇叙丽英: 数码管段选连接到单片机P0口,位选连接到P2口 ORG 0000h MAIN:CALL DISPLAYAJMP MAIN DISPLAY:MOV R0,#0MOV R1,#0x01MOV DPTR,#TABLOOP:INC R0MOV A,R0MOVC A,@A+DPTRMOV P0,#0 ;消隐MOV P0,A ;...
大武口区18336924459: 如何用51单片机C程序控制数码管从9到0的显 - ?
謇叙丽英: #include<reg51.h> #define uchar unsigned char uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f}; sbit rstkey=P2^2; sbit speaker=P2^0; void delay()//延时约400毫秒 { uchar i,j; for(i=0;i<200;i++) for(j=0;j<250;j++); } main() { uchar i,key...