計數(shù)器PWM設(shè)計方案解析
程序中用的ATmega128的8位T/C0工作模式為相位調(diào)整PWM模式輸出,系統(tǒng)時鐘為8MHz,分頻系數(shù)為1,其可以產(chǎn)生最高PWM頻率為:
8000000Hz / 510 =
15686Hz。每16次輸出構(gòu)成一個周期正弦波,正弦波的頻率為980.4Hz。PWM由OC0(PB4)引腳輸出。參考程序如下(ICCAVR)。
//ICC-AVR application builder : 2004-08
// Target : M128
// Crystal: 8.0000Mhz
#include 《iom128v.h》
#include 《macros.h》
#pragma data:code
// 128點正弦波樣本表
const unsigned char auc_SinParam[128] = {
64,67,70,73,76,79,82,85,88,91,94,96,99,102,104,106,109,111,113,115,117,118,120,121,
123,124,125,126,126,127,127,127,127,127,127,127,126,126,125,124,123,121,120,118,
117,115,113,111,109,106,104,102,99,96,94,91,88,85,82,79,76,73,70,67,64,60,57,54,51,48,
45,42,39,36,33,31,28,25,23,21,18,16,14,12,10,9,7,6,4,3,2,1,1,0,0,0,0,0,0,0,1,1,2,3,4,6,
7,9,10,12,14,16,18,21,23,25,28,31,33,36,39,42,45,48,51,54,57,60};
#pragma data:data
unsigned char x_SW = 8,X_LUT = 0;
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
X_LUT += x_SW; // 新樣點指針
if (X_LUT> 127) X_LUT -= 128; // 樣點指針調(diào)整
OCR0 = auc_SinParam[X_LUT]; // 取樣點指針到比較匹配寄存器
}
pwm相關(guān)文章:pwm原理
塵埃粒子計數(shù)器相關(guān)文章:塵埃粒子計數(shù)器原理
評論