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

📄 tc0.c

📁 在Codevision环境下
💻 C
字号:
/***********************************************************
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为待显示的数组名
{   
   PORTB=0xfe;
   PORTD=table[p[5]];
   delay_ms(5);
   PORTB=0xfd;
   PORTD=table[p[4]];
   delay_ms(5);
   PORTB=0xfb;
   PORTD=table[p[3]];
   delay_ms(5);
   PORTB=0xf7;
   PORTD=table[p[2]];
   delay_ms(5);
   PORTB=0xef;
   PORTD=table[p[1]];
   delay_ms(5);
   PORTB=0xdf;
   PORTD=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口
{
   DDRD=0xff;             //设置A口为推挽1输出
   PORTD=0xff;
   DDRB=0x3f;             //设置C口为推挽1输出        
   PORTB=0x3f;
}

void main(void)
{  
   Init_IO();             //初始化I/O口
   PORTA=0xff;             //点亮以测试所有的数码管
   PORTD=0x00;             
   delay_ms(1000);           //延时
   PORTD=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;    
      } 
   }   
}

⌨️ 快捷键说明

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