#include <avr/pgmspace.h>
#include <avr/io.h>
PROGMEM const uint16_t sine512[] = {250,256,262,269,275,281,288,294,301,308,314,319,326,332,339,344,350,356,362,367,374,379,384,389,395,400,404,410,414,419,423,428,433,436,440,445,449,452,455,458,463,466,468,471,474,476,478,481,483,485,487,488,490,491,493,494,495,496,496,498,499,499,499,500,500,499,499,499,498,498,496,496,495,494,493,493,492,491,489,488,487,486,484,483,481,480,478,476,474,473,471,470,468,466,465,463,460,459,457,455,453,452,450,449,447,446,445,443,442,440,439,438,436,435,434,433,432,432,431,430,430,429,428,428,428,428,427,427,427,427,427,428,428,428,428,429,430,430,431,432,432,433,434,435,436,438,439,440,442,443,445,446,447,449,450,452,453,455,457,459,460,463,465,466,468,470,471,473,474,476,478,480,481,483,484,486,487,488,489,491,492,493,493,494,495,496,496,498,498,499,499,499,500,500,499,499,499,498,496,496,495,494,493,491,490,488,487,485,483,481,478,476,474,471,468,466,463,458,455,452,449,445,440,436,433,428,423,419,414,410,404,400,395,389,384,379,374,367,362,356,350,344,339,332,326,319,314,308,301,294,288,281,275,269,262,256,250,244,238,231,225,219,212,206,199,192,186,181,174,168,161,156,150,144,138,133,126,121,116,111,105,100,96,90,86,81,77,72,67,64,60,55,51,48,45,42,37,34,32,29,26,24,22,19,17,15,13,12,10,9,7,6,5,4,4,2,1,1,1,0,0,1,1,1,2,2,4,4,5,6,7,7,8,9,11,12,13,14,16,17,19,20,22,24,26,27,29,30,32,34,35,37,40,41,43,45,47,48,50,51,53,54,55,57,58,60,61,62,64,65,66,67,68,68,69,70,70,71,72,72,72,72,73,73,73,73,73,72,72,72,72,71,70,70,69,68,68,67,66,65,64,62,61,60,58,57,55,54,53,51,50,48,47,45,43,41,40,37,35,34,32,30,29,27,26,24,22,20,19,17,16,14,13,12,11,9,8,7,7,6,5,4,4,2,2,1,1,1,0,0,1,1,1,2,4,4,5,6,7,9,10,12,13,15,17,19,22,24,26,29,32,34,37,42,45,48,51,55,60,64,67,72,77,81,86,90,96,100,105,111,116,121,126,133,138,144,150,156,161,168,174,181,186,192,199,206,212,219,225,231,238,244};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
//vyssi refclk == vyssi frekout const unsigned long refclk=1562960;
const unsigned long refclk=781480;
volatile int current_count;
volatile unsigned long phase_accumulator;
volatile unsigned long tword_m;
volatile unsigned long stepm;
volatile word count = 0;
volatile byte DT = 10;
volatile byte ampl = 13;
volatile word offset_1 = 171;
volatile word offset_2 = 341;
word fnom = 5000;
word fzad = 0;
word boost = 600;
word readpot;
word calc;
boolean ISR_exec_time = 1;
boolean RUN = 0;
byte bufclr = 0;
void setup()
{
pinMode(6, OUTPUT); //OC4A
pinMode(7, OUTPUT); //OC4B
pinMode(8, OUTPUT); //OC4C
pinMode(11, OUTPUT); //OC1A
pinMode(12, OUTPUT); //OC1B
pinMode(13, OUTPUT); //OC1C
//pinMode(5, OUTPUT); //OC3A
//pinMode(2, OUTPUT); //OC3B
//pinMode(3, OUTPUT); //OC3C
pinMode(52, OUTPUT);
pinMode(53, OUTPUT);
pinMode(22, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP);
sbi (GTCCR, TSM);
sbi (GTCCR, PSRASY);
sbi (GTCCR, PSRSYNC);
Setup_timer1();
Setup_timer2();
Setup_timer4();
//Setup_timer3();
//Setup_timer5();
TCNT1 = 0;
//TCNT3 = 0;
TCNT4 = 0;
//TCNT5 = 0;
GTCCR = 0;
//Disable Timer 0
cbi (TIMSK0,TOIE0);
tword_m=pow(2,32)*fzad/refclk;
sbi (TIMSK4,TOIE4);
Serial.begin(9600);
}
void loop()
{
while(1)
{
readpot = 0;
for (int i=0; i <= 9; i++)
{
readpot = readpot + analogRead(0);
}
if (digitalRead(22)==0 && fzad==0)
{
offset_1 = 341;
offset_2 = 171;
}
if (digitalRead(22)==1 && fzad==0 )
{
offset_1 = 171;
offset_2 = 341;
}
//===============================================
if (digitalRead(23)==1 && fzad!=readpot)
{
//================================================
if (fzad>readpot && TCNT2 > 10)
{
fzad = fzad-1;
TCNT2=0;
}
if (fzad<readpot && TCNT2 > 10)
{
fzad = fzad+1;
TCNT2=0;
}
//================================================
stepm = pow(2,32)*fzad/refclk;
calc = ((1280/(fnom/100))*(fzad/100))/10;
if (fzad > fnom) //*500
{
calc = 128;
}
if (fzad < boost)
{
calc = 14;
}
cbi (TIMSK4,TOIE4);
ampl = calc; //*5
tword_m = stepm;
sbi (TIMSK4,TOIE4);
}
//===============================================
/* */
if (count > 7000)
{
bufclr = bufclr+1;
count = 0;
Serial.println(bufclr);
Serial.println(fzad);
Serial.println(ampl);
}
}
}
ISR(TIMER4_OVF_vect)
{
sbi(PORTB,ISR_exec_time);
phase_accumulator=phase_accumulator+tword_m;
current_count=phase_accumulator >> 23;
word offs;
word offs1;
word offs2;
offs= current_count + offset_1;
offs2 = current_count + offset_2;
if (offs > 511)
{
offs = offs - 512;
}
if (offs2 > 511)
{
offs2 = offs2 - 512;
}
offs = (pgm_read_word_near(sine512 + offs)*ampl) >> 6;
offs1 = (pgm_read_word_near(sine512 + current_count)*ampl) >> 6;
offs2 = (pgm_read_word_near(sine512 + offs2)*ampl) >> 6;
if (tword_m > 0)
{
OCR4B = offs + DT;
OCR1B = offs;
OCR4A = offs1 + DT;
OCR1A = offs1;
OCR4C = offs2 + DT;
OCR1C = offs2;
}
else
{
OCR4B = 1023;
OCR1B = 0;
OCR4A = 1023;
OCR1A = 0;
OCR4C = 1023;
OCR1C = 0;
}
cbi(PORTB,ISR_exec_time);
count = count+1;
/* */
}
void Setup_timer1(void)
{
//Clock Prescaler : 1
sbi (TCCR1B, CS10);
cbi (TCCR1B, CS11);
cbi (TCCR1B, CS12);
// Timer1 Phase Correct PWM
sbi (TCCR1A, COM1A0);
sbi (TCCR1A, COM1A1);
sbi (TCCR1A, COM1B0);
sbi (TCCR1A, COM1B1);
sbi (TCCR1A, COM1C0); //tretireg
sbi (TCCR1A, COM1C1); //tretireg
// Mode 1 / Phase Correct PWM
sbi (TCCR1A, WGM10);
sbi (TCCR1A, WGM11);
cbi (TCCR1B, WGM12);
cbi (TCCR1B, WGM13);
}
void Setup_timer4(void)
{
sbi (TCCR4B, CS40);
cbi (TCCR4B, CS41);
cbi (TCCR4B, CS42);
cbi (TCCR4A, COM4A0);
sbi (TCCR4A, COM4A1);
cbi (TCCR4A, COM4B0);
sbi (TCCR4A, COM4B1);
cbi (TCCR4A, COM4C0);
sbi (TCCR4A, COM4C1);
sbi (TCCR4A, WGM40);
sbi (TCCR4A, WGM41);
cbi (TCCR4B, WGM42);
cbi (TCCR4B, WGM43);
}
void Setup_timer2(void)
{
//Prescaler : 1
cbi (TCCR2B, CS20);
cbi (TCCR2B, CS21);
sbi (TCCR2B, CS22);
// Phase Correct PWM
cbi (TCCR2A, COM2A0); // clear Compare Match
cbi (TCCR2A, COM2A1);
cbi (TCCR2A, COM2B0);
cbi (TCCR2A, COM2B1);
//Phase Correct PWM
cbi (TCCR2A, WGM20);
cbi (TCCR2A, WGM21);
cbi (TCCR2B, WGM22);
}
/*
void Setup_timer3(void)
{
sbi (TCCR3B, CS30);
cbi (TCCR3B, CS31);
cbi (TCCR3B, CS32);
cbi (TCCR3A, COM3A0);
sbi (TCCR3A, COM3A1);
cbi (TCCR3A, COM3B0);
sbi (TCCR3A, COM3B1);
cbi (TCCR3A, COM3C0);
sbi (TCCR3A, COM3C1);
sbi (TCCR3A, WGM30);
sbi (TCCR3A, WGM31);
cbi (TCCR3B, WGM32);
cbi (TCCR3B, WGM33);
}
void Setup_timer5(void)
{
sbi (TCCR5B, CS50);
cbi (TCCR5B, CS51);
cbi (TCCR5B, CS52);
cbi (TCCR5A, COM5A0);
sbi (TCCR5A, COM5A1);
cbi (TCCR5A, COM5B0);
sbi (TCCR5A, COM5B1);
cbi (TCCR5A, COM5C0);
sbi (TCCR5A, COM5C1);
sbi (TCCR5A, WGM50);
sbi (TCCR5A, WGM51);
cbi (TCCR5B, WGM52);
cbi (TCCR5B, WGM53);
}
Setup_timer2();
void Setup_timer2()
{
//Prescaler : 1
sbi (TCCR2B, CS20);
cbi (TCCR2B, CS21);
cbi (TCCR2B, CS22);
// Phase Correct PWM
cbi (TCCR2A, COM2A0); // clear Compare Match
cbi (TCCR2A, COM2A1);
cbi (TCCR2A, COM2B0);
cbi (TCCR2A, COM2B1);
//Phase Correct PWM
sbi (TCCR2A, WGM20);
cbi (TCCR2A, WGM21);
cbi (TCCR2B, WGM22);
}
volatile byte ms4_delay;
volatile byte c4ms;
if (c4ms > 25)
{
c4ms=0;
offset_3=analogRead(0)/4;
dfreq=50.0;
cbi (TIMSK2,TOIE2);
tword_m=pow(2,32)*dfreq/refclk;
sbi (TIMSK2,TOIE2);
}
*/
/*
if(ms4_delay++ == 125)
{
c4ms++;
ms4_delay=0; //reset count
}
coffs = pgm_read_word_near(sine512 + coffs2 - 1);
OCR1B = ((word)coffs*99)/100; // +1 korekce
*/
/*
*/
ไม่มีความคิดเห็น:
แสดงความคิดเห็น