Using timer to realize simple clock function

Publisher:名字太长了吗Latest update time:2018-05-13 Source: eefocusKeywords:Timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include


#define PORTLEN P0


sbit bit_select = P2^0;
sbit seg_select = P2^1;


unsigned char src[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f ,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char qrc [8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; unsigned
char second1[8];
shi = 0;
unsigned char fen = 0;
unsigned char miao = 0;


void timer0_init(void)
{
 EA = 1;
 TMOD |= 0x01;
 TH0 = (65536 - 1000) / 256;
 TL0 = (65536 - 1000) % 256; //Set the initial value of timer T0, generate an interrupt once every 1ms
 ET0 = 1; //Open Timer T0 interrupt
 TR0 = 1; //Turn on timer T0 and start adding 1 every 1ms



void timer1_init(void)
{
 EA = 1;
 TMOD |= 0x10;
 TH1 = (65536 - 1000) / 256;
 TL1 = ( 65536 - 1000) % 256; //Set the initial value of timer T1, generate an interrupt every 1ms
 ET1 = 1; //Turn on timer T1 interrupt
 TR1 = 1; //Turn on timer T1, start adding 1 every 1ms
}


void main()
{
        timer0_init();
 imer1_init();
 while(1);
}


void display()//Dynamic display clock
{
         static unsigned char i = 0;
 PORTLEN = 0x0; // Eliminate ghosting
 seg_select = 1;
 seg_select = 0;

 PORTLEN = 0xff;
 bit_select = 1;
 bit_select = 0;

 PORTLEN = second1[i];
 seg_select = 1;
 seg_select = 0;

 PORTLEN = qrc[i];
 bit_select = 1;
 bit_select = 0;
 
 i++;
 if(8 == i)
{
 i = 0;
}

}



void timer0_isr() interrupt 1
{
 static unsigned char i = 0;
          TH0 = (65536 - 20000) / 256;
 TL0 = (65536 - 20000) % 256; //Set the initial value of the timer to generate an interrupt every 20ms

 i++;
 if(50 == i) //Count down to 1s
  {
 i = 0;
                          miao++;
 if(60 == miao)
  {   
  miao = 0;
           fen++;
   if (60 == fen)
    {
    fen = 0;
    shi++;
    if(24 == shi)
     {
    shi = 0;
      }
   }
}
         }
second1[0] = src[shi / 10]; // Assign values ​​to each digit of the digital tube
second1[ 1] = src[shi % 10];
second1[2] = 0x40;
second1[3] = src[fen / 10];
second1[4] = src[fen % 10];
second1[5] = 0x40;
second1[ 6] = src[miao / 10];
second1[7] = src[miao % 10];
}


void timer1_isr() interrupt 3
{
          TH1 = (65536 - 1000) / 256; re-initialize
 TL1 = (65536 - 1000) % 256;
 display();
}

Keywords:Timer Reference address:Using timer to realize simple clock function

Previous article:Realizing a Rectangular Wave Generator Using a Single Chip Microcomputer
Next article:Microcontroller Experiment - Using Timer to Generate Square Wave

Recommended ReadingLatest update time:2024-11-16 11:27

S3C2440 Timer Initialization Method
S3C2410 provides 5 16-bit Timers (Timer0~Timer4), of which Timer0~Timer3 support Pulse Width Modulation - PWM (Pulse Width Modulation). Timer4 is an internal timer (internal timer), PCLK is the signal source of the Timer, we convert PCLK into an input clock signal by setting the corresponding Prescaler and Clock Divid
[Microcontroller]
S3C2440 Timer Initialization Method
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号