Timer application example source code

Publisher:渤海湾Latest update time:2017-11-30 Source: eefocusKeywords:Timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include "stdio.h"
#include "reg51.h"

// 0 - 9 encoding
unsigned char code table[] = { 0x11,0xb7,0x29,0x23,0x87,0x43,0x41,0x37,0x01,0x03,0x00 } ;
unsigned int count2 ;

//第1-6 位的片选线
unsigned char code dig[] = { 0x7f,0xfd,0xdf,0xf7,0xef,0xfb } ;
unsigned char buffer[8],val[6] ;
unsigned char count1,scan,i,ch,KeyVal,modify,OverTime ;
bit fKey,fSec,Key1,Key2,Key3,OldKey1,OldKey2,OldKey3,blink,fTimer,fSTOP ;
unsigned long int cn,tmp ;

void init(void) ; 
void display(void) ;
void Cal_cn(void) ;
void Cal_val(void) ;

main()
{
heat() ;


while(1) {


if(fSec) { // Is the second flag set?
fSec = 0 ; // Clear the second flagif
(!fSTOP && modify==0) {
if(cn!=0) cn-- ; else { fTimer = 1 ; fSTOP = 1 ; }
}
display() ;
}


if(fKey) { // Is a key pressed?
fKey = 0 ; // Clear the key flag and process the key

if(KeyVal==1) { // Is it an adjustment button?
fSTOP = 0 ; modify++ ; // Enter the corresponding state and reset the timeout timer
if(modify==7) { modify = 0 ; OverTime = 0 ; } else OverTime = 10 ;
}

if(KeyVal==2 && modify!=0) { // +1 button, corresponding bit +1
Cal_val() ;
if(val[modify-1]<9) val[modify-1]++ ;
Cal_cn() ;
display() ; OverTime = 10 ; // Refresh the display and reset the timeout timer
}

if(KeyVal==3 && modify!=0) { // -1 button, corresponding bit -1
Cal_val() ;
if(val[modify-1]>0) val[modify-1]-- ;
Cal_cn() ;
display() ; OverTime = 10 ; // Refresh the display and reset the timeout timer
}

}

}
}

void init(void) {

count1 = count2 = 0 ; scan = 0 ; KeyVal = 0 ;
cn = 0 ; for(i=0 ;i<6 ;i++) val = 0 ;
fKey = 0 ; modify = 0 ; blink = 0 ; OverTime = 0 ; fTimer = 0 ; fSTOP = 0 ;
Key1 = Key2 = Key3 = 0 ;
OldKey1 = OldKey2 = OldKey3 = 1 ;

for(i=0 ;i<6 ;i++) buffer = 0 ;

TCON &= 0xCF ; // Initialize Timer0
TMOD &= 0xF0 ; 
TMOD |= 0x01 ; 
TH0 = TL0 = 0 ; 
EA = 1 ;
ET0 = 1 ;
TR0 = 1 ;

}


timerint () interrupt 1 {

TR0 = 0 ;
TL0 = 0x12 ;
TH0 = 0xf7 ; // 0xf712 for 2.5ms
TR0 = 1 ;


if(scan==1 && !fKey) { // Scan adjustment button
Key1 = P30 ;
if(!Key1 && OldKey1) { fKey = 1 ; KeyVal = 1 ; }
}


if(scan==3 && !fKey) { // Scan +1 button
Key2 = P30 ;
if(!Key2 && OldKey2) { fKey = 1 ; KeyVal = 2 ; }
}

if(scan==4 && !fKey) { // Scan -1 button
Key3 = P30 ;
if(!Key3 && OldKey3) { fKey = 1 ; KeyVal = 3 ; }
}

OldKey1 = Key1 ; OldKey2 = Key2 ; OldKey3 = Key3 ;


P1 = 0xff ; P3 = dig[scan] ;
ch = buffer[scan] ; // Display 6 digits in sequence

if(modify==0) {
if(fTimer) P1 = table[ch] & 0xfe ; // 倒计时结束,启动P10(P10低电平)
else P1 = table[ch] ;
}
else if(modify==1 && blink && scan==0) P1 = 0xff ;
else if(modify==2 && blink && scan==1) P1 = 0xff ;
else if(modify==3 && blink && scan==2) P1 = 0xff ; 
else if(modify==4 && blink && scan==3) P1 = 0xff ; 
else if(modify==5 && blink && scan==4) P1 = 0xff ;
else if(modify==6 && blink && scan==5) P1 = 0xff ;
else P1 = table[ch] ;

scan++ ; if(scan==6) scan = 0 ;

count1++ ;
if(count1==200) { count1 = 0 ; blink = ~blink ; } // 0.5s blinking timer

count2++ ; 
if(count2==400) {
count2 = 0 ; fSec = 1 ; fTimer = 0 ; // Set the second pulse flag and turn off P10 output

if(OverTime) { OverTime-- ; if(OverTime==0) modify = 0 ; } // 10s timeout timer

}

}

void Cal_cn(void) {

cn = 0 ;
for(i=0 ;i<6 ;i++) {
cn *= 10 ; cn += val ;
}

}

void Cal_val(void) {

tmp = cn ;
for(i=0 ;i<6 ;i++) {
val[5-i] =(unsigned char) ( tmp % 10 ) ;
tmp = tmp / 10 ;
}

}

void display(void) {

tmp = cn ;
for(i=0 ;i<6 ;i++) {
buffer[5-i] =(unsigned char) ( tmp % 10 ) ;
tmp = tmp / 10 ;
}

}


Keywords:Timer Reference address:Timer application example source code

Previous article:Micro printer C51 control program
Next article:Detailed explanation of the indirect addressing method of 51 base address offset

Recommended ReadingLatest update time:2024-11-23 07:28

ARM learning timer Timer0 experiment
/* Project: Timer0 experiment   Writer: SHOW         Time: 2011/10/16    Hareware: Hardware platform: mini2440, J-link.   Function: Use timer 0 to make LED1 flash at 1s intervals.   Direction: The main knowledge of this experiment is the setting of timer and timer clock source. The whole experiment idea is as fol
[Microcontroller]
PIC Timer (TIMER0)
    For example, if the internal instruction cycle clock is set as the clock source and the frequency division is 1:4 for a 4M crystal oscillator, the timer increment is 1us (but the PIC18F microcontroller is Three-stage pipeline, one instruction cycle = 1/4 clock/crystal cycle. For example: 8M crystal
[Microcontroller]
PIC Timer (TIMER0)
EasyARM-iMX257_U-Boot source code transplantation analysis
I-mx257 u-boot-2009.08/cpu/arm926ejs u-boot-2009.08/board/freescale u-boot-2009.08/board/freescale/mx25_3stack (1) Analyze Makefile First, we analyze the Makefile and easily find that in lines 3210-3214, the following is newly added: mx25_3stack_config    :    unconf
[Microcontroller]
EasyARM-iMX257_U-Boot source code transplantation analysis
LabVIEW Helps Optimize Software Development Practices and Source Code Control
As a system design software, LabVIEW can provide all the engineering elements needed to build the most advanced and technically challenging systems. As engineering applications become more challenging and complex, the concept of software engineering has been incorporated into system development. Software engine
[Test Measurement]
LabVIEW Helps Optimize Software Development Practices and Source Code Control
STM32 one timer outputs 4 PWMs with different frequencies and adjustable duty cycles
main.c /*********************************************     Title: Exercise on operating USART     Software platform: MDK-ARM Standard Version4.70     Hardware platform: stm32f4-discovery     Main frequency: 168M Periph_Driver_version: V1.0.0          Description: Use one timer (TIM3) to implement four PWMs with dif
[Microcontroller]
PIC16F873A MCU Illuminance Measurement Project Schematic and Source Code
Illuminance measurement C language code based on PIC16F873A. Labview can be used to communicate with PC via RS232 interface and obtain real-time data   The microcontroller source program is as follows: #include pic.h //Call the header file of PIC16F87XA microcontroller __CONFIG(HS&WDTDIS&LVPDIS); #define U5A RC
[Microcontroller]
PIC16F873A MCU Illuminance Measurement Project Schematic and Source Code
C51 reads and writes AT24C04 source code and AT24C04 test program
1. C51 reads and writes AT24C04 source code /*=============================================*/ /*;***********************************/ /*;Start 24C01 timing*/ void Start() { SCL=1; SDA=1; SDA=0; SCL=0; } /*;************************************/ /*;Stop 24C01 timing*/ void Stop() { SDA=0; SCL=1; SDA=1; } /*;********
[Microcontroller]
Latest Microcontroller Articles
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号