微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 动态数码管--C51单片机--从零开始的学习笔记

动态数码管--C51单片机--从零开始的学习笔记

时间:10-02 整理:3721RD 点击:

动态数码管: (采用138译码器进行位选)
代码:
//==========================================
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
//------------------------------------------
uchar shi_temp;
uchar shi_cf;
uchar shi_cf_shi;
uchar shi_cf_ge;
uchar code table[]={
0x3f , 0x06 , 0x5b , 0x4f ,
0x66 , 0x6d , 0x7d , 0x07 ,
0x7f , 0x6f , 0x77 , 0x7c ,
0x39 , 0x5e , 0x79 , 0x71
};
uchar code table2[]={
0xbf , 0x86 , 0xdb , 0xcf ,
0xe6 , 0xed , 0xfd , 0x87 ,
0xff , 0xef , 0x77 , 0x7c ,
0x39 , 0x5e , 0x79 , 0x71
};// 更好的方法是:table[N]|0x80
void delayms( uint );
void display( uchar , uchar );
uchar num , num1 , num2 , shi , ge;
//===========================================
void main () {
TMOD = 0x11;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
EA = 1;
ET1 = 1;//TR0 = 1;
TR1 = 1;
while (1) {
  display( shi , ge );
}
}
//============================================
void display( uchar shi , uchar ge ) {
if ( shi < shi_temp ) {
  shi_cf++;
  if ( 24 == shi_cf ) {
   shi_cf = 0 ;
  }
  shi_cf_ge = shi_cf%10;
  shi_cf_shi = shi_cf/10;
}
shi_temp = shi;
P2 = 0xf3;
P0 = 0x00;
P0 = table[shi_cf_shi];
delayms( 2 );
P2 = 0xf7;
P0 = 0x00;
P0 = table2[shi_cf_ge];
delayms( 2 );
P2 = 0xfb;
P0 = 0x00;
P0 = table[shi];
delayms( 2 );
P2 = 0xff;
P0 = 0x00;
P0 = table[ge];

    delayms( 2 );
}
//----------------------------------------
void delayms( uint xms ) {
uint i , j;
for ( i = xms ; i>0 ; i-- ) {
  for ( j = 110 ; j>0 ; j-- );
}
}
//=========================================
void T1_time () interrupt 3 {
  TH1 = (65536-50000)/256;
  TL1 = (65536-50000)%256;
  num2++;
  if ( 2 == num2 ) {
   num2 = 0;
  num++;
  if ( 60 == num ) {
   num = 0;
  }
  shi = num/10;
  ge = num%10;
  }
}
//===========================================
心得:学会了动态数码管的扫描显示方法,同时初步接触了定时器中断。(忆)





好好努力  继续加油

谢谢分享

顶                                         

要是能上个图就好了

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top