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

📄 50%wave.c

📁 利用Atmega16单片机的定时器T0设计的方波发生器
💻 C
字号:
//ICC-AVR application builder : 2006-12-9 10:11:46
// Target : M8
// Crystal: 4.0000Mhz

#include <iom8v.h>
#include <macros.h>
long Ddummy;
char Settings_PerDotGroupNum,Settings_PrintPhase;
char DAOutPutNow;

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

//TIMER2 initialisation - prescale:1
// WGM: CTC
// desired value: 62.5KHz
// actual value:  63.39KHz (3.1%)
void timer2_init(void)
{
 TCCR2 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT2 = 0x00; //setup
 OCR2  = 0x20; //计数上限值
 TCCR2 = 0x19; //0x11:一般模式, 0x19:比较模式,计数比较成功后PB3电平取反 
}

#pragma interrupt_handler timer2_comp_isr:4
void timer2_comp_isr(void)  //compare occured TCNT2=OCR2
{
    Ddummy++;
    if(Ddummy==1) PORTB = PORTB & (~1);   //PB0脚置低电平
	if(Ddummy >=Settings_PerDotGroupNum){
	   Ddummy =0; //恢复计数初值
       PORTB = (PORTB & (~1)) + 1;   //PB0脚置高电平
	}  
}

/*
#pragma interrupt_handler timer2_ovf_isr:5
void timer2_ovf_isr(void)   //在比较模式下计数溢出中断不会发生
{
  TCNT2 = 0xF0;    //计数初值
  if(PORTB & 2){   //PB1脚是高电平,改变PB1脚的电平使其形成振荡
     PORTB -= 2;   //则将PB1脚电平变低
     return;
  }
  PORTB += 2;      //如果PB1脚电平是低,则加2使其变高
}
*/

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

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

void main()
{
     init_devices();
	 DAOutPutNow = 0;
	 Ddummy = 0;                     //临时变量,赋初值
	 Settings_PerDotGroupNum =10;    //每10个墨滴选一个进行打印
	 Settings_PrintPhase =10;	 
}

⌨️ 快捷键说明

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