ATMEGA16采用T/C0硬件定时器的时钟系统

代码:

/*******************************************************
This program was created by the
CodeWizardAVR V3.12 Advanced
Automatic Program Generator
?Copyright 1998-2014 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 2024/6/7
Author  : 
Company : 
Comments: 


Chip type               : ATmega16
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*******************************************************/

#include <mega16.h>

// Declare your global variables here
typedef unsigned char uchar;
typedef unsigned int uint;

flash uchar led_7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,
	  			0x7d,0x07,0x7f,0x6f};
flash uchar position[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar time[3];
uchar dis_buff[6];
uint time_counter;
uchar posit;
bit point_on,time_1s_ok;

void display(void)
{
 
 
    PORTD=0xff;
  PORTC=led_7[dis_buff[posit]];
  if(point_on && (posit==2 || posit==4))
  			  PORTC|=0X80;
  PORTD=position[posit];
  if(++posit>=6) posit=0;
 
}
interrupt [TIM0_COMP] void timer0_comp_isr(void)
{
    display();
    if(++time_counter>=500)
    {
        time_counter=0;
        time_1s_ok=1;
    }
}
void time_to_disbuffer(void)
{
 	 uchar i,j=0;
	 for(i=0;i<=2;i++)
	 {
	  				  dis_buff[j++]=time[i]%10;
					  dis_buff[j++]=time[i]/10;
	 }
}

void main(void)
{

 PORTC=0;
 DDRC=0XFF;
 PORTD=0X3F;
 DDRD=0X3F;
 TCCR0=0X0B;
 TCNT0=0;
 OCR0=0X7C;
 TIMSK=2;
 
 time[2]=23;time[1]=58;time[0]=55;
 posit=0;
 time_to_disbuffer();    
 #asm("sei");
 while(1)
 {
  
  if(time_1s_ok)
  {
   time_1s_ok=0;
   point_on=~point_on;
   if(++time[0]>=60)
   	{
	 	time[0]=0;
		if(++time[1]>=60)
		{
			time[1]=0;
			if(++time[2]>=24)time[2]=0;
			
		}
	}				  
	time_to_disbuffer();
  }
  
 }
}

仿真效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值