A simple example of counting with a timer

Publisher:不染尘埃Latest update time:2012-11-15 Source: 21ic Keywords:Timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*
Note: This program mainly shows how to use the timer to count, and reflects the modular sub-functions. It is a relatively simple program
*/

#include //header file
#define uchar unsigned char //macro definition
#define uint unsigned int
uchar count; //define global variables
void display_led() //led display sub-function
{
if(count==20) //changes every 1S (crystal oscillator: 12MHZ)
{
count=0; //count cleared for next count
P2=~P2; //P2 value inverted
P2=P2<<1; //P2 value shifted left
P2=~P2;
if(P2==0xff) //if the last LED is on,
P2=0xfe; //then the first LED is on
}
}
void init() //initialization sub-function
{
EA=1; //turn on total interrupt
ET0=1; //turn on timer interrupt
TR0=1; //start timer
TMOD=0x01; //T0 working mode 1, 16-bit counting
TH0=(65536-50000)/256; //Set the initial value of the timer
TL0=(65536-50000)%256;
P2=0xfe; //Assign initial value to P2 port
while(1)
{
display_led(); //Call LED display sub-function
}
}
void main() //Main function
{
init(); //Call initialization sub-function
}
void delay() interrupt 1 //Timed interrupt sub-function
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++; //Count variable plus 1
}

Keywords:Timer Reference address:A simple example of counting with a timer

Previous article:Interrupt method keyboard scanning c program
Next article:Simple 4*4 matrix keyboard scanning program

Recommended ReadingLatest update time:2024-11-16 14:53

PIC32MZ tutorial -- 32-bit Timer
  The microcontroller is PIC32MZ2048ECH144 on the PIC32MZ EC Starter Kit. This microcontroller has four 32-bit synchronous timers are available by combining Timer2 with Timer3, Timer4 with Timer5, Timer6 with Timer7, and Timer8 with Timer9. The 32-bit timers can operate in one of three modes: •Synchronous internal 3
[Microcontroller]
MSP430F149 TIMER_A (I)——16-bit timing counter
    TIMER_A has four optional clock sources. For convenience, ACLK and SMCLK are generally selected. A time base is generated through a frequency divider. Its structure diagram is as follows:   TIMER_A has three timing/counting modes: (1). Incremental counting mode: Counting cycle: TAR increases from 0 to TAC
[Microcontroller]
STM32 Timer PWM_Output
The pulse width modulation mode can generate a signal with a frequency determined by the TIMx_ARR register and a duty cycle determined by the TIMx_CCRx register. The following is an example of PWM mode 1. The PWM signal reference OCxREF is high when TIMx_CNT   Library function STM32F10x_StdPeriph_Lib_V3.3.0\Proje
[Microcontroller]
STM32 Timer PWM_Output
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号