|
楼主 |
发表于 2005-6-1 11:54:43
|
显示全部楼层
如果需要源程序,如下
#include <avr/io.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/delay.h>
#include <avr/pgmspace.h>
#define Xtal 8000000 // system clock frequency
#define prescaler 1 // timer1 prescaler
#define N_samples 128 // Number of samples in lookup table
#define Fck Xtal/prescaler // Timer1 working frequency
#define delaycyc 10 // port B setup delay cycles
#define CPU_CRYSTAL (8.0000)
#define wait_us(num)\
_delay_loop_2((int16_t)((num)*CPU_CRYSTAL/4))
//************************** SIN TABLE *************************************
// Samples table : one period sampled on 128 samples and
// quantized on 7 bit
//**************************************************************************
const prog_uchar 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};
//*************************** x_SW ***************************************
//Table of x_SW (excess 8): x_SW = ROUND(8*N_samples*f*510/Fck)
//**************************************************************************
//high frequency (coloun)
//1209hz ---> x_SW = 79
//1336hz ---> x_SW = 87
//1477hz ---> x_SW = 96
//1633hz ---> x_SW = 107
const unsigned char auc_frequencyH [4] = {
107,96,
87,79};
//low frequency (row)
//697hz ---> x_SW = 46
//770hz ---> x_SW = 50
//852hz ---> x_SW = 56
//941hz ---> x_SW = 61
const unsigned char auc_frequencyL [4] = {
61,56,
50,46};
//************************** global variables ****************************
unsigned char x_SWa = 0x00; // step width of high frequency
unsigned char x_SWb = 0x00; // step width of low frequency
unsigned int i_CurSinValA = 0; // position freq. A in LUT (extended format)
unsigned int i_CurSinValB = 0; // position freq. B in LUT (extended format)
unsigned int i_TmpSinValA; // position freq. A in LUT (actual position)
unsigned int i_TmpSinValB; // position freq. B in LUT (actual position)
//**************************************************************************
// Timer overflow interrupt service routine
//**************************************************************************
SIGNAL(SIG_OVERFOLW1)
{
// move Pointer about step width aheaed
i_CurSinValA += x_SWa;
i_CurSinValB += x_SWb;
// normalize Temp-Pointer
i_TmpSinValA = (uint16_t)(((i_CurSinValA+4) >> 3)&(0x007F));
i_TmpSinValB = (uint16_t)(((i_CurSinValB+4) >> 3)&(0x007F));
// calculate PWM value: high frequency value + 3/4 low frequency value
OCR1A = (auc_SinParam[i_TmpSinValA] + (auc_SinParam[i_TmpSinValB]-(auc_SinParam[i_TmpSinValB]>>2)));
}
//**************************************************************************
// Initialization
//**************************************************************************
void init (void)
{
TIMSK = (1<<TOIE1); // Int T1 Overflow enabled
TCCR1A = (1<<COM1A1)|(1<<WGM10); // non inverting / 8Bit PWM
TCCR1B = (1<<CS10); // CLK/1
DDRD = (1 << PD5); // PD5 (OC1A) as output
sei(); // Interrupts enabled
}
int main(void)
{
init();
while(1)
{
int8_t i;
i=0;
x_SWa = auc_frequencyH[2];
x_SWb = auc_frequencyL[2];
for(i=0;i<200;i++)
{
wait_us(1000);
}
}
} |
|