|
给一个用M8的T/C2,使用比较匹配工作方式,在PB3输出50Hz的方波。只需要初始化设置,不用其它程序,可以认为是硬件产生的。使用ICCAVR的程序向导生成。道理自己体会。
此时,T/C2还可以用于产生秒定时,一石两中。
系统时钟1M,产生49.776Hz ,50%(0.5%)的方波。
//ICC-AVR application builder : 2005-03-15 22:48:29
// Target : M8
// Crystal: 1.0000Mhz
#include <iom8v.h>
#include <macros.h>
void port_init(void)
{
PORTB = 0x08;
DDRB = 0x08;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER2 initialize - prescale:64
// WGM: CTC
// desired value: 100Hz
// actual value: 99.522Hz (0.5%)
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x64; //setup
OCR2 = 0x9C;
TCCR2 = 0x1C; //start
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer2_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
//insert your functional code here...
while(1);
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
如果想吃一顿饺子,就得从冰箱里取出肉,剁馅儿,倒面粉、揉面、醒面,擀成皮儿,下锅……
一整个繁琐流程,就是为了出锅时那一嘴滚烫流油的热饺子。
如果这个过程,禁不住饿,零食下肚了,饺子出锅时也就不香了……《非诚勿扰3》
|