[AVR单片机] 产生40Kz波形出现的问题?

[复制链接]
2455|5
 楼主| jack_zhang0077 发表于 2010-5-17 09:03 | 显示全部楼层 |阅读模式
TI, ni, tc, ST, timer
// Target : ATMEGA8
// Crystal: 2.0000Mhz
#include <iom8v.h>
#include <macros.h>
void port_init(void)
{
PORTB = 0x00;
DDRB  = 0xFF;
PORTC = 0x00; //m103 output only
DDRC  = 0x00;
PORTD = 0x00;
DDRD  = 0x00;
}
//TIMER1 initialize - prescale:1
// WGM: 15) PWM fast, TOP=OCRn
// desired value: 40KHz
// actual value: 40.000KHz
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0xCF;
OCR1AH = 0x00;
OCR1AL = 0x31;
OCR1BH = 0x00;
OCR1BL = 0x31;
ICR1H  = 0x00;
ICR1L  = 0x31;
TCCR1A = 0x53;
TCCR1B = 0x19; //start Timer
}
//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  = 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);
}
用的快速PWM,波形产生模式方式15,COM1A1,COM1A0=01,为什么只有20KZ?按公式foc1apwm=Fclc_i/o除以N(1+TOP)算出来TOP等于0X31啊?
ershisi 发表于 2010-5-18 21:37 | 显示全部楼层
用文字描述下计算的过程,看的有点糊涂
电子尘埃 发表于 2010-5-18 22:04 | 显示全部楼层
那么贵还用呀
xiaohuai 发表于 2010-5-18 22:37 | 显示全部楼层
OCRn不是OCR2用的多吗
lpydidi 发表于 2010-5-18 23:03 | 显示全部楼层
32446975 发表于 2010-6-2 20:45 | 显示全部楼层
产生40Kz波形,计算时当然要用80KHz进行计算啦.
计算时应该用2倍频率的值去计算.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

12

主题

29

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部