Written by tahmid,
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 void interrupt() org IVT_ADDR_TIMER1_OVF{ TBL_POINTER_NEW = TBL_POINTER_NEW + SET_FREQ; //sending signal every 10 ms, if (Direction == 0){ MOSA = 0; MOSD = 0; MOSB = 1; MOSC = 1; Direction = 1; } else{ MOSB = 0; MOSC = 0; MOSA = 1; MOSD = 1; Direction = 0; } TBL_POINTER_SHIFT = TBL_POINTER_NEW >> ; DUTY_CYCLE = TBL_POINTER_SHIFT; TBL_POINTER_SHIFT = sin_table[DUTY_CYCLE]; OCR1AH = TBL_POINTER_SHIFT >> 8; OCR1AL = TBL_POINTER_SHIFT & 0x0F; }
//----------------------------------------------------------------------------------------
//Programmer: Syed Tahmid Mahbub
//Target Microcontroller: ATMEGA16
//Compiler: mikroC PRO for AVR (Can easily port to any other compiler)
//-----------------------------------------------------------------------------------------
[COLOR="#FF0000"]
unsigned int sin_table[128]={0, 24, 49, 73, 98, 122, 146, 171, 195, 219, 242, 266, 290, 313, 336, 359, 382, 404, 426, 448, 470, 491, 513, 533, 554, 574, 594, 613, 632, 651, 669, 687, 704, 721, 738, 754, 770, 785, 799, 814, 827, 841, 853, 865, 877, 888, 899, 909, 918, 927, 935, 943, 950, 957, 963, 968, 973, 977, 981, 984, 986, 988, 989, 990, 990, 989, 988, 986, 984, 981, 977, 973, 968, 963, 957, 950, 943, 935, 927, 918, 909, 899, 888, 877, 865, 853, 841, 827, 814, 799, 785, 770, 754, 738, 721, 704, 687, 669, 651, 632, 613, 594, 574, 554, 533, 513, 491, 470, 448, 426, 404, 382, 359, 336, 313, 290, 266, 242, 219, 195, 171, 146, 122, 98, 73, 49, 24,0};
[/COLOR] //sine table with 128 entries, Peak value as 999 and 127 entries, zero at the end makes it 128
#define MOSA PORTD0_bit
#define MOSB PORTD1_bit
#define MOSC PORTD2_bit
#define MOSD PORTD3_bit
unsigned char FlagReg;
#define Direction FlagReg.B0
//0 -> MOS A + D
//1 -> MOS B + C
unsigned int TBL_POINTER_NEW, TBL_POINTER_SHIFT, SET_FREQ;
unsigned int TBL_temp;
unsigned char DUTY_CYCLE;
void interrupt() org IVT_ADDR_TIMER1_OVF{
TBL_POINTER_NEW = TBL_POINTER_NEW + SET_FREQ;
//sending signal every 10 ms,
if (Direction == 0){
MOSA = 0;
MOSD = 0;
MOSB = 1;
MOSC = 1;
Direction = 1;
}
else{
MOSB = 0;
MOSC = 0;
MOSA = 1;
MOSD = 1;
Direction = 0;
}
[COLOR="#FF0000"] TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 9; [/COLOR] //TBL_POINTER_NEW is shifted 9 times instead of 11.
DUTY_CYCLE = TBL_POINTER_SHIFT;
TBL_POINTER_SHIFT = sin_table[DUTY_CYCLE];
OCR1AH = TBL_POINTER_SHIFT >> 8;
OCR1AL = TBL_POINTER_SHIFT & 0x0F;
}
void main() {
SET_FREQ = 410;
TBL_POINTER_SHIFT = 0;
TBL_POINTER_NEW = 0;
TBL_POINTER_OLD = 0;
DUTY_CYCLE = 0;
DDRC = 0xFF;
DDRD = 0XFF;
OCR1AH = 0;
OCR1AL = 0;
TCCR1A = 0x82;
ICR1H = 0x03;
ICR1L = 0xE7;
//ICR1 = 999 -> TOP -> 16kHz
TIMSK = 0x04;
TCCR1B = 0x19;
SREG_I_bit = 1;
while(1);
}
Why should it be more often , didn't able to understand it ???The remark says that timer1_ovf happens every 10ms, but it needs to be more often...
Hi,
To fetch a sine value from the table takes only a few instructions.
Calculating takes much longer. It simply increases processing load.
Why the values differ...
We don't see how you calculated it and how the results are.
Either we need a crystal ball or more information.
Klaus
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?