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

📄 t0时钟.c

📁 iccavr下
💻 C
字号:
/* 
    Title:    icc-avr timer0 ctc timer
    Author:   dushibiao
    Date:     2007 10 18
    Purpose:  use timer0 ctc mode to generate a timer
	Frequency: internal 8M
    Software: icc-avr 
    Hardware: AVR mega16 BOARD
    Connect:  dushibiao@126.com
*/
//内部8M晶振,T0  CTC方式   DUSHIBIAO 2007 7 7

#include<iom16v.h>
#include<macros.h>
#include "shumaguan.h"
#define uint unsigned int 
#define uchar unsigned char

uint micsec;                      //毫秒
uchar second, minute;   

void initial(void);

void main()
{
 	 initial();
	
	 while(1)
	 {
	         unsigned int disvalue;
			 disvalue=(unsigned int)minute*100+second;
	  		 display(disvalue);
	 }
	 
}
/*-------------------------------------------------------------
fuction:  initialize ports  and timer0 register
--------------------------------------------------------------*/
void initial(void)
{
  	 PORTA=0XFF;   //A口初始化
	 DDRA=0XFF;    //out
	 PORTB=0XFF;   //B口初始化
	 DDRB=0XFF;
	 PORTC=0XFF;   //其它口上拉电阻有效
	 PORTD=0XFF;
	 _SEI();
	 OCR0=249;  //定时250us      ORC0+1							
	 TIMSK|=(1<<OCIE0);  //比较匹配中断允许
	 TCCR0|=(1<<WGM01)|(1<<CS01);  //CTC模式,8分频
	 /*---------------------------------------------------------
	 TCCR0
	 7:     FOC0--------Force Output Campare
	 6,3    WGM01:0-----Waveform Generator Mode
	        00----------Normal
			01----------PWM,Phase Correct
			10----------CTC
			11----------Fast PWM
	 5,4    COM11:0-----Compare Match Output Mode
	         ----------------------------------------
			 Non PWM mode
			 ----------------------------------------
	        00----------Normal Port Operation, OC0 disconnected
			01----------Toggle OC0 on compare match
			10----------Clear OC0 on compare match
			11----------Set OC0 on compare match
	 2--0   CS02:0------Clock Select
	        000---------None clock select
			001---------clk(io)
			010---------clk(io)/8
			011---------clk(io)/64
			100---------clk(io)/256
			101---------clk(io)/1024
			110---------T0 pin,falling edge
			111---------T0 pin,rising edge
	 ----------------------------------------------------------*/
}

/*------------------------------------------------------------
function:   interrupt service routine,it increases micsec,minute
            second , and the main loop routine display the current 
			time 
-------------------------------------------------------------*/

#pragma interrupt_handler  T0INT: iv_TIMER0_COMP
void T0INT(void)
{
 		micsec += 1;
		if(micsec==4000)
		{
			micsec=0;
			second +=1 ;
			if(second==60)
			{
			 		second = 0;
					minute += 1 ;
					if(minute==60)
					minute=0;
			}
		}
			
} 

⌨️ 快捷键说明

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