原创 AVR 用TC/0设计的时钟

2009-4-4 13:53 2546 4 4 分类: MCU/ 嵌入式

/***********************************************************
Project   : AVR 用TC/0设计的时钟
Chip type : ATmega16
Frequency : 8M
Software  : Codevision 1.24.6
Author    : shishuwu
Date      : 09.02.23
*************************************************************/
#include<MEGA16.h> 
#include<delay.h>


const unsigned char table[10]=
{0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Data[6]={0,0,0,0,0,0};
unsigned int CNT="0";             //初始计数值:0
unsigned char Timer[3]={0x00,0x00,0x00};


void Display(unsigned char *p)       //动态显示函数,参数p为待显示的数组名
{  
   PORTC="0xfe";
   PORTA="table"[p[5]];
   delay_ms(5);
   PORTC="0xfd";
   PORTA="table"[p[4]];
   delay_ms(5);
   PORTC="0xfb";
   PORTA="table"[p[3]];
   delay_ms(5);
   PORTC="0xf7";
   PORTA="table"[p[2]];
   delay_ms(5);
   PORTC="0xef";
   PORTA="table"[p[1]];
   delay_ms(5);
   PORTC="0xdf";
   PORTA="table"[p[0]];
   delay_ms(5);
}


void Process(unsigned char *p1,unsigned char *p2)
{
   p2[0]=p1[0]/10;
   p2[1]=p1[0]-p2[0]*10;
   p2[2]=p1[1]/10;
   p2[3]=p1[1]-p2[2]*10;
   p2[4]=p1[2]/10;
   p2[5]=p1[2]-p2[4]*10;
}


void Init_IO(void)         //初始化I/O口
{
   DDRA="0xff";             //设置A口为推挽1输出
   PORTA="0xff";
   DDRC="0x3f";             //设置C口为推挽1输出       
   PORTC="0x3f";
}


void main(void)

   Init_IO();             //初始化I/O口
   PORTA="0xff";             //点亮以测试所有的数码管
   PORTC="0x00";            
   delay_ms(1000);           //延时
   PORTC="0xff";             //熄灭所有的数码管
   TCCR0=0x04;             //TC/0工作在定时方式,CLK/256
   TCNT0=0x06;             //计数初始值6
   TIMSK="0x01";             //Timer0下降沿触发
   #asm("sei")             //开全局中断
   while(1)
   {
     Process(Timer,Data);
     Display(Data);
   }


interrupt[TIM0_OVF] void timer0_ovf_isr(void)
{
   if(++CNT==125)         //中断次数累加,1S
   {
      CNT="0";
      Timer[2]++;
      if(Timer[2]==60)    //秒加一
      {
         Timer[2]=0;
         Timer[1]++;      //分进位
      } 
      if(Timer[1]==60)  
      {
         Timer[1]=0;
         Timer[0]++;       //时进位
      }
      if(Timer[0]==24)   
      {
         Timer[0]=0;   
      }
   }  
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
4
关闭 站长推荐上一条 /3 下一条