⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dingshiqi__.c

📁 定时器。利用数码管制作的定时器。 结构简单
💻 C
字号:
/*****************************************************
Project : dingshiqi
Date    : 2008-3-30
Author  : Lang Junwei                          

Chip type           : ATmega16
Program type        : Application
Clock frequency     : 4.000000 MHz
*****************************************************/

#include <mega16.h>                             
flash unsigned char led[11]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x40};  
flash unsigned char position[4]={0xfe,0xfd,0xfb,0xf7};       

#define data_port     PORTA
#define postion_port  PORTC                 
#define key_mask 0b11110000      
#define change_key 0b01110000
#define add_key 0b10110000
#define ok_key 0b11010000
#define cancel_key 0b11100000
#define No_key 255;                           

char tempbuff[4];     
char min,sec;
bit point_on;
bit time_1s=0;
int counter=0;
char pos=0;   
unsigned char state=0;            
unsigned char	key_stime_counter;        
unsigned char x;           
unsigned char key_temp;
bit	key_stime_ok;               
bit voice_on=0;     
bit light=0;        
unsigned char t; 
unsigned char t1; 

void display()
{       
    postion_port=0XFF;             
    data_port=led[tempbuff[pos]];
    if(point_on)
    {
        if(pos==2)
        {
            data_port|=0X80;
        }
    }
    postion_port=position[pos];                
    if(++pos>=4)
    {
        pos=0;
    }   
}                                

interrupt [TIM0_COMP] void timer0_comp_isr(void)
{    
    display();                 
    if (++key_stime_counter >=5)
	{
		key_stime_counter = 0;
		key_stime_ok = 1;				// 10ms到
	}	
    if(++counter>=500)
        {
            counter=0;
            time_1s=1;
        }   
}

unsigned char read_key()
{	
	static unsigned char key_state = 0, key_value;
    unsigned char key_return = No_key;
	
	switch (key_state)
	{
		case 0:
            key_value = key_mask & PINB;		
		    if (key_value != key_mask)
			{
				key_state++;					// 有按键,停止扫描
			}		
			break;
		case 1:
			if (key_value == (key_mask & PINB))		
			{
				switch (key_value)		
				{								
					case change_key:
						key_return = 1;
						break;
					case add_key:
						key_return = 2;
						break;
 					case ok_key:
						key_return = 3;
						break;
 					case cancel_key:
						key_return = 4;
						break; 					
				}
				key_state++;				// 转入等待按键释放状态
			}
			else
				key_state--;				// 两次列电平不同返回状态0,(消抖处理)
			break;						
		case 2:							// 等待按键释放状态
			if ( (key_mask & PINB) == key_mask)
				key_state=0;				// 列线全部为高电平返回状态0
			break;
	}
	return key_return;
} 

void go_back()
{   
    unsigned char i;
    tempbuff[0] = 0;    
	for (i=0; i<4 ;i++)
	{tempbuff[i] = 0;}	 
    state=0;      
    PORTB.2=1;
    PORTB.3=1;
    x=0;       
    voice_on=0;    
    light=0;
}            

void key_to_time()
{   
    min=tempbuff[3]*10+tempbuff[2];
    sec=tempbuff[1]*10+tempbuff[0]; 
}                             

void time_to_buff()
{
        tempbuff[0]=sec%10;
        tempbuff[1]=sec/10;
        tempbuff[2]=min%10;
        tempbuff[3]=min/10;              
}

void main(void)
{
    PORTA=0x00;
    DDRA=0xFF;
    PORTC=0x00;
    DDRC=0xFF;    
    PORTB=0xFF;
    DDRB=0x0F;

    TCCR0=0x0B;
    TCNT0=0x00;
    OCR0=0x7C;      
    TIMSK=0x02;   
    
    go_back();
    time_to_buff(); 
 
    #asm("sei")

    while (1)
    {
        if (key_stime_ok)				
        {
            key_stime_ok = 0;				// 10ms到
	        key_temp = read_key();		    // 调用键盘接口函数读键盘		              
	        if ((key_temp==1 || key_temp==2 || key_temp==3) && (state==0 || state==1))   
	        {                                      
	            state=1;
	            if (key_temp == 1)
	            {  
	                if(tempbuff[x]==10) {t=t1;}              
	                else {t=tempbuff[x];}                 
                    if(x>=3) {x=0;}
	                else {x=x+1;}          
	                t1=tempbuff[x];
	                tempbuff[x]=10;    
	                if(x==0) {tempbuff[3]=t;}
	                else {tempbuff[x-1]=t;} 
	            }               
	            if (key_temp == 2)
	            {  
	                if(x==0 || x==2)
	                {
	                    if(tempbuff[x]>=9) {tempbuff[x]=0;}
	                    else {tempbuff[x]++;}  
	                } 
	                if(x==1)
	                {
	                    if(tempbuff[x]>=5) {tempbuff[x]=0;}
	                    else {tempbuff[x]++;}       
	                }
	                if(x==3)
	                {
	                    if(tempbuff[x]>=3) {tempbuff[x]=0;}
	                    else {tempbuff[x]++;}   
	                }
	            }    
	            if (key_temp == 3)
	            {   
	                key_to_time();                 
	                if (min>30 || sec>59) {go_back();}
	                else
	                {
	                    time_to_buff();   
	                    state=2;
	                }
	            }
	        } 
	        
	        if (key_temp == 4) 
	        {          
	            go_back();
	        }            
        }
                     
        if(time_1s)
        {
            time_1s=0;
            point_on=~point_on;         
            if (voice_on==1)
            {
                voice_on=0;  
                PORTB.3=1;
            }        
            
            if(state==2)
            {
                if(sec--<=0)
                {
                    sec=59;
                    if(min--<=0)
                    {
                        min=0;
                        sec=0;
                        PORTB.2=0;  
                        PORTB.3=0;        
                        voice_on=1;
                        state=3;
                    }
                } 
                time_to_buff(); 
            }  
        }
    };
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -