Hi All,
I am doing a control for power electronics for an LC converter with two signal 50 % duty and in anti-phase. Basically I would like to change the frequency based on the power request.
#include <avr/io.h>
int Frequency = 200;
int DeadTime = 10;
int DutyCycle_A = (Frequency+DeadTime)/2;
int DutyCycle_B = (Frequency-DeadTime)/2;
int ledOut = 13;
volatile int state = LOW;
int voltage_pin = A0;
int current_pin = A1;
int temp_pin = A2;
int val_voltage = 0;
int val_current = 0;
int val_temperature = 0;
int acquisition_inverval = 126;
int j = 0;
int j_max =4;
int i=0;
int i_max=100;
int zerocrossing = 12;
int realPower= 0;
int Power_req = 2000;
void InitPortAnalog(void){
pinMode(voltage_pin,INPUT);
pinMode(current_pin,INPUT);
pinMode(temp_pin,INPUT);
pinMode(zerocrossing,INPUT);
}
void Interrupt(void){ // Set up the digital pin 2 to an Interrupt and Pin 4 to an Output
pinMode(ledOut, OUTPUT);
SREG |= 0x80; // Enable the interrupt SPI Control Register
EICRA = (1 << ISC00) | (1 << ISC10); // Any change INT0, INT1
EIMSK = (1 << INT0) | (1 << INT1); // External Interrupt Mask Register
EIFR |= (1 << INTF0) | (1 << INTF1);
}
void InitTimer0(void){
TCCR0A|= (1 << WGM01); // This set the timer zero in CTC
OCR0A=acquisition_inverval; //set CTC value - Top of Timer0- Here is stored the time interval between each acquisition
TIMSK0 |= (1<<OCIE0A); // This generates the interrupt and the TOP
}
void InitPort(void)
{
DDRB|=(1<<PORTB1)|(1<<PORTB2); //Init PB1/OC1A and PB2/OC1B pins as output
}
void InitTimer1(void){
TCNT1=0; //Set Initial Timer value
//set non inverted PWM on OC1A pin and inverted on OC1B
TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<COM1B0);
//set top value to ICR1
//set corrcet phase and frequency PWM mode
TCCR1B|=(1<<WGM13);
//set compare values
OCR1A=DutyCycle_B; //OCR1A=0x0064; half of ICR1=0x00FF
OCR1B=DutyCycle_A; // OCR1B=0x0096; half of ICR1=0x00FF
}
void StartTimer0(void){ //Start timer0 without no prescaller
TCCR0B |= (1 << CS02); // this set the prescal at clock/8
}
void StartTimer1(void){ //Start timer1 without no prescaller
TCCR1B|=(1<<CS10);
}
int main(void){
InitPortAnalog();
Interrupt();
InitPort();
InitTimer1();
InitTimer0();
StartTimer1();
StartTimer0();
while(1)
{
ICR1=Frequency; //0x00FF equivale a 255
realPower=0;
for( j=0;j<j_max;j++){ // This for is meant in order to avg the power along 4 semicycle
if (digitalRead(zerocrossing)==1){
int val_voltage_act=0;
int val_current_act=0;
for( i=0;i<i_max;i++){ // Using for loop to calculate average power
val_voltage_act = (analogRead(voltage_pin)/2.56); // 2.56 is the ratio between the real value and the acquired one
val_voltage += val_voltage_act*val_voltage_act;
val_current_act = (analogRead(current_pin)/29.25); // 29.25 is the ration between the real value and the acquired one
val_current += val_current_act*val_current_act;
delayMicroseconds(100);
}
realPower = 0.1*sqrt(val_voltage/i_max)*sqrt(val_current/i_max);
}
realPower += realPower;
}
realPower = .25*realPower;
if (analogRead(realPower >Power_req)){
Frequency++ ;
} else if (realPower <Power_req){
Frequency-- ;
}
}
}
ISR(TIMER0_COMPA_vect){
state = !state;
digitalWrite(ledOut, state);
}
ISR(INT0_vect)
{ //syncronization of the analog reading with the zero crossing
OCR0A=0;
TIFR0=0;
}
ISR(INT1_vect)
{ //If the Interrupt on the PIN 1 is trigger all the Control signals go to 0
digitalWrite(10, LOW);
digitalWrite(9, LOW);
}
but I checked the analogRead and the control does not acquire anything. Someone can check the code?
Thank you very much
Regards,
Vittorio