发布网友
共2个回答
热心网友
你是问 ATmega8 单片机 吗?
那是用来测频率的。
具体方法看下面例子。
正弦波先要整形成方波再送入M8进行中断,不然容易出错
例子;给你参考一下吧 外部中断来了 你就在外部中断函数里面写你中断以后你要执行的程序就可以了,就这么简单 PWM占空比,周期 都可以通过定时器寄存器初值来设定
//ICC-AVR application builder : 2009-09-11 19:02:05
// 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;
}
//TIMER1 initialize - prescale:
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1mSec
// actual value: 1.000mSec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x83;
OCR1AH = 0x00;
OCR1AL = 0x7D;
OCR1BH = 0x00;
OCR1BL = 0x7D;
ICR1H = 0x00;
ICR1L = 0x7D;
TCCR1A = 0x00;
TCCR1B = 0x03; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0xFF; //reload counter high value
TCNT1L = 0x83; //reload counter low value
}
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)
{
//external interupt on INT0
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
MCUCR = 0x00;
GICR = 0x40;
TIMSK = 0x04; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
//insert your functional code here...
}
补充:
建议你仔细看M16的资料 关于定时器及PWM那块 这是别人无法代替的
输出比较寄存器1A - OCR1AH 与 OCR1AL
输出比较寄存器1B - OCR1BH 与 OCR1BL
16 位定时器/ 计数器寄存器的说明 T/C1 控制寄存器A - TCCR1A
T/C1 控制寄存器B - TCCR1B
TCCR1A 用来对模式进行选择 详情还得看资料 内容太多
TCCR1B 分频选择
如TCCR1A:
7 6 5 4 3 2 1 0
COM1A1 COM1A0 COM1B1 COM1B0 FOC1A FOC1B WGM11 WGM10
热心网友
对不起,那是个什么文件了?