hi! so i've been working on sine wave inverter project wherein i plan to use 20*4 i2c lcd. i really need to use all the timers for functions such as spwm generation,fan pwm control etc. So all the timers are utilized and work without prescaler. here is a code below :
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE);
int i= 0;
int OK = 0;
int x = 0;
int duty_50khz = 0; // this value should not exceed 80
//float Var ;
float Vin ;
int temp;
const int shutdown_pin = 4;
const int trip_current = 1;
int sinPWM[]={1,2,5,7,10,12,15,17,19,22,24,27,30,32,34,37,39,42,
44,47,49,52,54,57,59,61,64,66,69,71,73,76,78,80,83,85,88,90,92,94,97,99,
101,103,106,108,110,113,115,117,119,121,124,126,128,130,132,134,136,138,140,142,144,146,
148,150,152,154,156,158,160,162,164,166,168,169,171,173,175,177,178,180,182,184,185,187,188,190,192,193,
195,196,198,199,201,202,204,205,207,208,209,211,212,213,215,216,217,219,220,221,222,223,224,225,226,227,
228,229,230,231,232,233,234,235,236,237,237,238,239,240,240,241,242,242,243,243,244,244,245,245,246,246,
247,247,247,248,248,248,248,249,249,249,249,249,255,255,255,255,249,249,249,249,249,248,
248,248,248,247,247,247,246,246,245,245,244,244,243,243,242,242,241,240,240,239,238,237,237,236,235,234,
233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,217,216,215,213,212,211,209,208,207,205,204,
202,201,199,198,196,195,193,192,190,188,187,185,184,182,180,178,177,175,173,171,169,168,166,164,162,160,
158,156,154,152,150,148,146,144,142,140,138,136,134,132,130,128,126,124,121,119,117,115,113,110,108,106,
103,101,99,97,94,92,90,88,85,83,80,78,76,73,71,69,66,64,61,59,57,54,52,49,47,44,42,39,37,34,32,30,
27,24,22,19,17,15,12,10,7,5,2,1};
const float calibration_multiplier = 1;
const float calibration_offset = 0.00;
const float calibration_offset2 = 0.3;
float rms_current ;
int current_read;
void initial()
{
for( int i=0; i<=19; i++)
{
lcd.setCursor(i,0);
lcd.write(byte(4));
lcd.setCursor(19-i,3);
lcd.write(byte(4));
delay(20);
}
lcd.setCursor(3,1);
for(int i=0 ; i <=7;i++)
{
lcd.print(text[i]);
delay(50);
}
lcd.setCursor(12,1);
for(int i = 0;i<=1; i++)
{
lcd.print(text2[i]);
}
delay(2000);
lcd.clear();
}
void setup()
{
lcd.begin(20,4);
initial();
lcd.createChar(0,degree_celcius);
digitalWrite(shutdown_pin,LOW);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
//TIMER 1 SETUP FOR 50kHZ
DDRB |= (1<<DDB1)|(1<<DDB2); // digital pin 9 and 10 output
TCCR1A = B00000000 ;
TCCR1B = B00000000 ;
TCCR1A |= (1<< COM1A1)| (1<<COM1B1); ; // pin 9 non inverted mode and pin 10 inverted mode
TCCR1A |= (1<<COM1B0) ; //
ICR1 = 160 ;
TCCR1B |= (1<<WGM13) | (1<<CS10); // phase and frequency corrected pwm with no prescaler
cli();
TCCR0A = 0;
TCCR0B = 0; // timer 0 for spwm generation
TCNT0 = 0; // timer 1 for 50khz pwm for driving ferrite transformer
TCCR0A = B10100001 ; // timer 2 for generating interrupts and fan pwm
TCCR0B = B00000001 ;
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
TCCR2A = B10100001 ;
TIMSK2 |= (1<<OCIE2B) ;
OCR2B = 0 ; // for interrupt
TCCR2B = B00000001 ;
sei();
OCR2A = 0 ; // to control fan pwm
}
ISR (TIMER2_COMPA_vect)
{
if(i>313 && OK==0)
{// final value from vector for pin 6
i=0;// go to first value of vector
OK=1;//enable pin 5
}
if(i>313 && OK==1)
{// final value from vector for pin 5
i=0;//go to firs value of vector
OK=0;//enable pin 6
}
x=sinPWM[i];// x take the value from vector corresponding to position i(i is zero indexed)
i=i+1;// go to the next position
if(OK==0)
{
OCR0B=0;//make pin 5 0
OCR0A=x;//enable pin 6 to corresponding duty cycle
}
if(OK==1)
{
OCR0A=0;//make pin 6 0
OCR0B=x;//enable pin 5 to corresponding duty cycle
}
}
void loop() {
// Var = voltage to arduino analog input
Vin = ((analogRead(A7)* 0.004882)/0.3197 ) ;
if(Vin<10)
{
digitalWrite(shutdown_pin,HIGH);
lcd.clear();
lcd.setCursor(5,1);
lcd.print("Battery Voltage low!");
}
if(Vin>10 && Vin<10.5)
duty_50khz = 78;
if(Vin>10.5 && Vin<11)
duty_50khz = 70;
if(Vin>11 && Vin<11.5)
duty_50khz = 65;
if(Vin>11.5 && Vin<12)
duty_50khz = 60;
if(Vin>12 && Vin<12.5)
duty_50khz = 55;
if(Vin>12.5 && Vin<13)
duty_50khz = 50;
OCR1A = duty_50khz;
OCR1B = 160-duty_50khz ;
rms_current = (((analogRead(A3)-410.487)/57.11))-calibration_offset;
if(rms_current >4.7)
rms_current+= 0.25;
if( rms_current > trip_current)
digitalWrite(shutdown_pin,HIGH);
temp = analogRead(A0)*0.512;
if( temp > 70)
OCR2A = 255 ;
if(temp< 70 && temp>60)
OCR2A = 200 ;
if(temp<60 && temp>40)
OCR2A = 150 ;
if(temp<40 && temp>30)
OCR2A = 100;
if(temp < 30)
OCR2A = 50 ;
lcd.setCursor(1,0);
lcd.print("I:");
lcd.print(rms_current);
lcd.print("A");
lcd.setCursor(0,1);
lcd.print("Vin:");
lcd.print(Vin);
lcd.print("volts");
lcd.setCursor(0,2);
lcd.print("Temp:");
lcd.print(temp);
lcd.write(byte(0));
}
so basically, the function " initial" works perfectly and display work till that point. But not after that because i guess i am changing the clock prescaler and using the interrupts. Is that the reason behind lcd not working? any solution to overcome this problem? appreciate the help
Thanks !