AVR T/C 使用技巧----硬件的50%方波产生
给一个用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);
} 呀,没人顶,我顶一下! 谢谢! ICCAVR的程序向导的确非常好用。有机会真的要好好整理一下。 首先不是使用向导的问题,而是你是否踏踏实实的去学习掌握和灵活使用AVR的结构和特点。如果你不了解T/C2的功能,使用向导也无济于事的。向导是按你的配置生成的,只是帮你节省时间。 向导切实有用!!!
但是最好还是自己动手编写比较好 不在于自己动手编写还是用向导生成。重要的是你知道利用T/C2的比较匹配工作方式就可以非常简单的完成一个50%方波的发生器。使用标准51不会这么方便的。
让大家体会AVR的T/C比51的功能强的多。尝试如何用AVR的T/C的捕捉功能去精确测频率和周期? ji
页:
[1]