Modular programming of single-chip microcomputer control timer or counter

Source: Internet
Author: User
Tags volatile

Learning SCM has been a while, but many programs are missing the idea of modularity, previously thought as long as the single function encapsulated in a function is modular, but in the company after more than 10 days of internship, only really have a modular programming ideas, Here will I write 51 single-chip microcomputer control timer interrupt program sharing, one is hope that no modular programming idea of the later look, the second is to hope that the predecessors to give treatise

(add: The following code only needs to modify the text of the document containing the words "select" The comment section, you can achieve the effect of reuse, focus on the Chinese part)


/*############### #timer. h start################* /

#ifndef __timer_h__ #define __TIMER_H__ #ifndef high_level #define HIGH_LEVEL 1 #endif #ifndef low_level #define Low_l   Evel 0 #endif extern volatile unsigned int timerdelaytick;/* selected to be processed in the TIMER0 or IMER1 Interrupt Service program */* TMOD (SFR 0x89) Sbit 7  6 5 4 3 2 1 0 |  |  |  |  |  |  |_ |_ _m1 M0 (Timer0 operation mode) |  |  |  |  |  		|  0 0 13-bit Timer/counter |  |  |  |  |        |  0 1 16-bit Timer/counter |  |  |  |  |  		|  1 0 8-bit Auto-load timer/counter |  |  |  |  |  		|  1 1 (Onlu for Timer0), TL0 is a 8-bit Counter by standard |  |  |  |  |  				| Timer0 control bits.  But,th0 is a 8-bit Counter and is |  |  |  |  |  				|  Control by Timer0 Control bits |  |  |  |  |  |_ _ _ c/t |  |  |  |  			|  0 (Timer), input form internal clock |  |  |  |  			|  1 (Counter), input form external pin |  |  |  | 
	|  |  |  |  |  |_ _ _ _ _gate |  |  | 
	|  |  |  |
	|  |  |  | 
	|  |  | |_ |_ _ _ _ _ _ _ M1 M0 (Timer1 operation mode)
	|  |  				|  0 0 13-bit Timer/counter |  |     		    |  0 1 16-bit Timer/counter |  |   				|  1 0 8-bit Auto-load timer/counter |  |  		
	|  |  |  	                     |_ _ _ _ _ _ _c/t |               		 0 (Timer), input form internal clock |  
	1 (Counter), input form external pin | |_ _ _ _ _ _ _ _ _ _ _gate *///TIMER0 #define Timer0_16bit_timer 0x01 #define Timer0_8bit_timer 0x02 #define Timer0_ 		16bit_counter 0X05 #define Timer0_8bit_counter 0x06//timer1 #define Timer1_16bit_timer 0x10 #define Timer1_8bit_timer 0x20 #define Timer1_16bit_counter 0x50 #define Timer1_8bit_counter 0x60//timer0 and Timer1 mode select #define Tmod_t IMER0 timer0_16bit_timer/* SELECT */#define TMOD_TIMER1 timer1_16bit_timer/* as needed */#define TMOD_SETUP (Tmod_timer0 | TMOD_TIMER1)/* Select the Count value of */#define Timer0_tick 50000/*50ms,50000us.@12.000mzh*//*timer0 as needed, select */#define Timer1_ Tick the count value of 50000/*50ms,50000us.@12.000mzh*//*timer1, select */* as needed-----------Unit conversion------------*/#ifndef s_to_ms #define S_to_ms #endif #ifndef ms_to_us #define MS_TO_US #endif #ifndef

Us_to_ns #define US_TO_NS #endif/*------------------------------*/* Timer initialization */extern void timerinit (void);


/* Timer Delay MS, timed wait until time to a specific time */extern void timerdelayms (unsigned long period);
 /* The interrupt program does not need to be declared here */#endif/*__timer_h__*/

/*############### #timer. h start################* /

/*############### #timer. C Start################* /

/*----------------------------------------------------------------------------filename:timer.c Author:yangrui date:20130206 versiion:1.0 (20130206_15_13)--------------------------------------------------------------------- --------

----------------------------------------------------------------------------
*************************** * Modified 0002 2014-02-07-15_44 **************************** added #if (Timer0_tick > 65536) within function timerinit () #error--INV Aild Timer0_tick. When TIMER0 is in 16-bit mode.
	Timer0_tick Max is 65536. #endif and #if (Timer0_tick >) #error--invaild timer0_tick. When Timer1 is in 8-bit mode.
		Timer0_tick Max is 256. #endif and corresponding: #if (Timer1_Tick > 65536) #error--invaild timer1_tick.when timer1 work in 16-bit mode.
	Timer1_Tick Max is 65536. #endif and #if (Timer1_Tick >) #error--invaild timer1_tick. When Timer1 is in 8-bit mode.
	Timer1_Tick Max is 256.
#endif these four sentences, mainly to prevent users in the choice of operation mode and the process of assigning initial value is not careful, do not match these two, resulting in a timing error,	Such errors are sometimes not directly observable.  Modify 0001 2014-02-07-13_24 **************************** the function void timer1delayms (unsigned int Period)---failure modified to void timer1delayms (unsigned long period)---successful-----------------------------------------------------  -----------------------*/#include <reg52.h> #include "timer.h" volatile unsigned long g_timerdelaytick = 0;
/*for timerdelayms (unsigned long period) *//****************************************************************** Funcationname:timerinit input:n/a output:n/a return:n/a note:timer initialization/***********************

	/void Timerinit (void) {tmod = Tmod_setup; 0 = = Timer0_16bit_timer)) {/*timer0 mode:16bit timer/counter*/#if (Timer0_tick > 65536) #error--invaild time R0_tick. When TIMER0 is in 16-bit mode. timer0_TICK Max is 65536.
	#endif TH0 = (65535-timer0_tick+1)/256;/*full is 65535,overflow when 65536*/TL0 = (65535-timer0_tick+1)%256; } #elif ((Tmod_timer0 = = timer0_8bit_counter) | | \ tmod_timer0 = timer0_8bit_timer) {/*timer0 mode:8bit Auto-load timer/counter*//* Here is to prevent user error, although can be assigned to >256, but the final phenomenon is wrong, so for security purposes, add this sentence */#if (Timer0_tick >) #error--INV Aild Timer0_tick. When Timer1 is in 8-bit mode.
		Timer0_tick Max is 256.
	#endif TH0 = 255-timer0_tick+1;/*full is 255,overflow shen 256*/TL0 = 255-timer0_tick+1; } #else {#error--invaild Tmod_timer0.
	Please select correct TIMER0 mode! } #endif TR0 = High_level;/*timer0 start running*/ET0 = high_level;/*enable Timer0 Overflow interrupt*//*::::::::: ::::::::::::: TIMER1::-:::-::::::::::,:*/#if (tmod_timer1 = = timer1_16bit_counter) | | \ (Tmod_timer1 = = time R1_16bit_timer) {/*timer1 mode:16bit timer/counter*/#if (Timer1_Tick > 65536) #error--invaild timeR1_tick.when timer1 work in 16-bit mode.
		Timer1_Tick Max is 65536.
	#endif TH1 = (65535-timer1_tick+1)/256;/*full is 65535,overflow when 65536*/TL1 = (65535-timer1_tick+1)%256;
			} #elif ((Tmod_timer1 = = timer1_8bit_counter) | | \ (tmod_timer1 = = Timer1_8bit_timer)) {#if (Timer1_Tick > 256) #error--invaild Timer1_Tick. When Timer1 is in 8-bit mode.
		Timer1_Tick Max is 256.
		#endif th1= 255-timer1_tick+1;
	Tl1= 255-timer1_tick+1; } #else {#error--invaild tmod_timer1.
	Please select correct Timer1 mode! } #endif TR1 = high_level;/*timer1 start running*/ET1 = high_level;/*enable Timer1 Overflow interruput*//*:::::::: :::::::::::: TIMER2:::::-----.:*///* ... ..... ... .... ... ..... ..... ..... ..... ..... ..... ..... ..... .......-.....-........
*/EA = High_level;
}/****************************************************************** Funcationname:timer0interruptserverroutine input:n/a output:n/a return:n/a NOTE:TIMER0 Interrupt SerVer routine/******************************************************************/void timer0interruptserverroutine
		(void) Interrupt 1 using 1 {#if (Tmod_timer0 = = timer0_16bit_counter) | | \ (TMOD_TIMER0 = = Timer0_16bit_timer)) {
		TH0 = (65535-timer0_tick+1)/256;
	TL0 = (65535-timer0_tick+1)%256;
		   } #elif ((Tmod_timer0 = = Timer0_8bit_counter) | |
	(Tmod_timer0 = = Timer0_8bit_timer)) {//auto-load} #else #error--invaild Tmod_timer0.
	Please select correct TIMER0 mode! #endif/*--------need to work on the processing of interrupts, waiting to be replenished. Start-------*/*--------need to be replenished in the work of the interrupt processing. End-------*/}/****************************************************************** Funcationname: Timer1interruptserverroutine input:n/a output:n/a return:n/a note:timer1 Interrupt Server routine/******** /void Timer1interruptserverroutine (void) Interrupt 3 {# if (Tmod_timer1 = = timer1_16bit_counter) | | \ (tmod_timer1== timer1_16bit_timer)) {TH1 = (65535-timer1_tick+1)/256;
	tl1= (65535-timer1_tick+1)%256;
		   } #elif ((Tmod_timer1 = = Timer1_8bit_counter) | |
	(Tmod_timer1 = = Timer1_8bit_timer)) {//auto-load} #else #error--invaild tmod_timer1.
	Please select correct Timer1 mode! #endif/*--------need to work on the processing of interrupts, waiting to be replenished.
	Start-------*//*for timer1delayms () */if (G_timerdelaytick) {g_timerdelaytick--; }/*--------need to work on the interrupt processing, wait for the supplement. End-------*/}/****************************************************************** funcationname:timerinit Input: Period (server ms) output:n/a return:n/a note:timer Delay Server MS, wait until time reach/**************** /void timerdelayms (unsigned long period) {/* Ms to us*/period *= M
	S_to_us;

	G_timerdelaytick = Period/timer1_tick;
	
	TR0 = High_level;
 while (G_timerdelaytick) {;//wait}}

/*############### #timer. C Start################* /


Test procedure:

Step 0001: Add code to TIMER.C

volatile unsigned int g_aa = 0;

Step 0002: Add code to Timer.h

extern volatile unsigned int g_aa;			/* Choose to process in TIMER0 or IMER1 Interrupt Service program */

Step 0003: Functions in the TIMER.C

void Timer0interruptserverroutine (void) Interrupt 1 using 1

Add the code to the following effect:

	/*--------need to work on interruptions in the process, waiting to be replenished. Start-------*/
	g_aa++;

	/*--------need to work on interruptions in the process, waiting to be replenished. End-------*/

Step 0004: Test the main program for:

 #include <reg52.h> #include "timer.h" void Main (void) {P0 = 0x00;

	Timerinit ();
			/* Beta version One */while (1) {if (G_aa = =) {G_aa = 0;
		P0=~p0;
		}}/* Beta version two */while (1) {timerdelayms (1000);
	P0 = ~p0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.