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

📄 oc2上输出矩形波._c

📁 ICC mega8例子
💻 _C
字号:
//ICC-AVR application builder : 2009-3-11 13:07:26
// Target : M8
// Crystal: 8.0000Mhz

#include <iom8v.h>
#include <macros.h>

void port_init(void)
{
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

/************************************
用    途:TC2用CTC模式输出频率可调矩形波
Taget   :mega8
crystal :8M
介    绍:初始化以后就可以在OC2上输出矩形波了,
	  	 但OC2必须设为输出(PB3)
入口参数:p可用来调节频率,为0-255中的一个
出口参数:
*************************************/
void init_timer2_CTC(unsigned char p)
{
 DDRB|=(1<<PB3);
 TCCR2=0x0;//TC2工作于定时方式,先停止TC2
 TCNT2=0x00;//计数器初始值设为0
 OCR2=p;//设置比较参考值
 TIMSK|=(1<<OCIE2);//打开TOV2的比较区配中断	
 //TC2工作于定时方式CTC(WGM2[1:0]=2)
 //比较匹配时触发OC2取反(COM2[1:0]=1)
 //系统时钟256分频(CS2[2:0]=0xC)
 TCCR2=(1<<WGM21)|(1<<COM20)|(1<<CS22)|(1<<CS21);
}
//改变频率
void timer2_CTC(unsigned char x)
{
 OCR2=x;
}

void main()
{
 port_init();
 init_devices();
 init_timer2_CTC(20);
 
 while(1)
 {
  ;
 }
}

⌨️ 快捷键说明

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