51 MCU C language program (III) Digital tube

Publisher:Meilin8888Latest update time:2015-10-27 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Procedure 1

The dynamic scanning method is used to display a stable 654321 on the six-digit digital tube.
The clock frequency is 11.0592M

#include  //52 MCU header file
#include //Library containing left and right circular shift sub-functions
#define uint unsigned int    //Macro definition
#define uchar unsigned char  //Macro definition
sbit dula=P2^6;       //Digital tube segment selection latch end
sbit wela=P2^7;       ////Digital tube bit selection latch end
uchar code table[]={   //Digital tube display code
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void display(uchar,uchar,uchar,uchar,uchar,uchar); //Function declaration
void delay(uint);
void main()
{
 while(1)
 {
  display(6,5,4,3,2,1);         //Always display
 }
}

void display(uchar one,uchar two,uchar three,uchar four,uchar five,uchar six)
{
       dula=1;
  P0=table[one];   //send segment data
  dula=0;
  P0=0xff;   //turn off all displays before sending bit data to prevent the segment select data from passing through the bit select latch after the bit select latch is turned on
  wela=1;
  P0=0xfe;
  wela=0;
  delay(1);

  dula=1;
  P0=table[two];
  dula=0;
  P0=0xff;
  wela=1;
  P0=0xfd;
  wela=0;
  delay(1);

  dula=1;
  P0=table[three];
  dula=0;
  P0=0xff;
  wela=1;
  P0=0xfb;
  wela=0;
  delay(1);

  dula=1;
  P0=table[four];
  dula=0;
  P0=0xff;
  wela=1;
  P0=0xf7;
  wela=0;
  delay(1);

  dula=1;
  P0=table[five];
  dula=0;
  P0=0xff;
  wela=1;
  P0=0xef;
  wela=0;
  delay(1);

  dula=1;
  P0=table[six];
  dula=0;
  P0=0xff;
  wela=1;
  P0=0xdf;
  wela=0;
  delay(1);
}

void delay(uint z)       //delay sub-function
{
 uint x,y;
 for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}

Procedure 2

Use the dynamic scanning method and timer 1 to display the stopwatch in the first three digits of the digital tube,
accurate to 1% of a second, that is, the last digit displays 1% of a second, and the cycle continues.

#include  //52 MCU header file
#include //Library containing left and right circular shift sub-functions
#define uint unsigned int    //Macro definition
#define uchar unsigned char  //Macro definition
sbit dula=P2^6;       //Digital tube segment selection latch end
sbit wela=P2^7;       ////Digital tube bit selection latch end
uchar ge,shi,bai;
uint tt;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void display(uchar,uchar,uchar); //Function declaration
void delay(uint);
void main()
{
 TMOD=0x10; //Set timer 1 to working mode 1
 TH1=(65536-10000)/256;
 TL1=(65536-10000)%256;
 EA=1; //Open general interrupt
 ET1=1; //Open timer 1 interrupt
 TR1=1; //Start timer 1
 while(1)
 {
  display(bai,shi,ge);
 }
}


void exter0() interrupt 3   // Timer 1 interrupt
{
 TH1=(65536-10000)/256;
 TL1=(65536-10000)%256;
 tt++;
 if(tt==1000)
 tt=0;
 bai=tt/100;
 shi=tt%100/10;
 ge=tt%10;
}

void display(uchar one,uchar two,uchar three)
{
       dula=1;
  P0=table[one];   //send segment data
  dula=0;
  P0=0xff;   //turn off all displays before sending bit data to prevent the segment select data from passing through the bit select latch after the bit select latch is turned on
  wela=1;
  P0=0xfe;
  wela=0;
  delay(1);

  dula=1;
  P0=table[two];
  dula=0;
  P0=0xff;
  wela=1;
  P0=0xfd;
  wela=0;
  delay(1);

  dula=1;
  P0=table[three];
  dula=0;
  P0=0xff;
  wela=1;
  P0=0xfb;
  wela=0;
  delay(1);
}

void delay(uint z)       //delay sub-function
{
 uint x,y;
 for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}

 [page]

 

Procedure 3

Using dynamic scanning and timer 1, the digital tube displays a number
starting from 765432 and decreasing at a speed of 1/10 second
until 765398 and keeps displaying this number. At the same time, using
timer 0 at a speed of 500MS, the running lights move from top to
bottom. When the number on the digital tube decreases to a stop,
the running lights on the experimental board also stop and then all start flashing. After 3 seconds
(using T0 timing), all the running lights turn off and the digital tube
displays "HELLO". Keep it here.

#include  //52 MCU header file
#include //Library containing left and right circular shift sub-functions
#define uint unsigned int    //Macro definition
#define uchar unsigned char  //Macro definition
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={   //Display data encoding
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x76,0x79,0x38,0x3f,0};
uchar temp,t0,t1,bai,shi,ge,flag,flag1;
uint shu;
void init();              //Function declaration
void display(uchar aa,uchar bb,uchar cc,uchar bai,uchar shi,uchar ge);
void delay(uint z)     //delay sub-function
{
 uint x,y;
 for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}
void main()          //main function
{
 init();
 while(1)
 {
  if(flag1!=1)   //If flagi is no longer equal to 1, display data
   display(7,6,5,bai,shi,ge);
  else
   display(16,17,18,18,19,20);   //Otherwise display hello
 }
}

void init()          //initialization function
{
 shu=432;
 temp=0xfe;
 P1=temp;
 TMOD=0x11;
 TH0=(65536-50000)/256;  //timer initialization
 TL0=(65536-50000)%256;
 TH1=(65536-50000)/256;
 TL1=(65536-50000)%256;
 EA=1;
 ET0=1;
 ET1=1;
 TR0=1;
 TR1=1;
}

void timer0() interrupt 1   //Timer 0 interrupt
{
 TH0=(65536-50000)/256;
 TL0=(65536-50000)%256;
 t0++;
 if(flag!=1)          //When flag is not equal to 1, turn on the running light
  {
  if(t0==10)
   {
    t0=1;
    temp=_crol_(temp,1);
    P1=temp;
   }
  }
 else              //Otherwise, the light flashes
  {
   if(t0%4==0)  //The small light changes every 200 milliseconds
    P1=~P1;
   if(t0==60)
   {
    TR0=0;   //After 3 seconds, turn off timer 0, turn off the light, and set flag=1
    P1=0xff;
    flag1=1;
   }
  
}

void timer1() interrupt 3  //Timer 1 interrupt function
{
 TH1=(65536-50000)/256;
 TL1=(65536-50000)%256;
 t1++;
 if(t1==2)
  {
   t1=0;
   shu--;
   bai=shu/100;
   shi=shu%100/10;
   ge=shu%10;
   if(shu==398)    //When the number reaches 398, clear the original number in T0, re-add the initial value to make the small light flash
    {
     TR0=0;
     TH0=(65536-50000)/256;
     TL0=(65536-50000)%256;
     TR0=1;
     flag=1;
     t0=0;
     P1=0xff;
     TR1=0;
    
  
}

void display(uchar aa,uchar bb,uchar cc,uchar bai,uchar shi,uchar ge) // display 子function
{
       dula=1;
  P0=table[aa];
  dula=0;
  P0=0xff;
  wela=1;
  P0=0xfe;
  wela=0;
  delay(1);

        dula=1;
  P0=table[bb];
  dula=0;
  P0=0xff;
  wela=1;
  P0=0xfd;
  wela=0;
  delay(1);

      game=1;
  P0=table[cc];
  game=0;
  P0=0xff;
  wela=1;
  P0=0xfb;
  wela=0;
  delay(1);
       game=1;
  P0=table[bai];
  game=0;
  P0=0xff;
  wela=1;
  P0=0xf7;
  wela=0;
  delay(1);

  dula=1;
  P0=table[shi];
  dula=0;
  P0=0xff;
  wela=1;
  P0=0xef;
  wela=0;
  delay(1);

  game=1;
  P0=table[ge];
  game=0;
  P0=0xff;
  wela=1;
  P0=0xdf;
  wela=0;
  delay(1);
}

Reference address:51 MCU C language program (III) Digital tube

Previous article:51 MCU running light C language source program
Next article:51 MCU C language program (II) Timer/Counter interrupt

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号