PROGRAM
//feed back by 10k and 1k
//adc value —0.75 for 8
//adc=2733/4.88 ==560
//to get adc value =v/.11
//for 8v—-150
//7.5–139
//7–132
//a5=current sensor
//o/p feed back
int c=0;
int buck=9,boost=10,a;
int buckduty=0,boostduty=0;
int power,pp,cp;
#include <LiquidCrystal.h>
LiquidCrystal lcd(3, 4, 5, 6, 7, 8);//LCD RS-3,En-4,D4-5,D5- 6,D6-7,D7-8
int x,y,cv;//cuurnet,past volt,cv
void setup()
{
x= analogRead(A5);//current sensor
delay(1000);
setPwmFrequency(buck,1);
setPwmFrequency(boost,1);
analogWrite(buck,buckduty);
analogWrite(boost,boostduty);
lcdstart();
Serial.begin(9600);
delay(1000);
}
void loop()
{
int y=analogRead(4);
delay(1);
int i = (analogRead(A5)-x);
delay(1);
pp=y*i;
Serial.print(“volt=”);Serial.println(y);
if(y>150)
{
buckduty=buckduty-1;if(buckduty<1)buckduty=0;
analogWrite(buck,buckduty);
}
if (y<132)
{
buckduty=buckduty+3;
if(buckduty>254)buckduty=255;
analogWrite(buck,buckduty);
delay(50);
int cv=analogRead(4);
delay(1);
int i = (analogRead(A5)-x);
delay(1);
cp=cv*i;
if(cp<pp)
{
if(buckduty<255)buckduty=buckduty-8;
// else {buckduty=buckduty-};
if(buckduty<1)buckduty=0;
analogWrite(buck,buckduty);
if(buckduty>253)
{
boostduty=boostduty+3;
if(boostduty>200)boostduty=200;
analogWrite(boost,boostduty);
delay(100);
int cv=analogRead(4);
delay(1);
int i = (analogRead(A5)-x);
delay(1);
cp=cv*i;
if(cp<pp)
{
boostduty=boostduty-8;
if(boostduty<1)boostduty=0;
analogWrite(boost,boostduty);
}
}
}
}
c++;
if(c>3)
{
c=0;
int i = (analogRead(A5)-x)*36;//*12.56=45//0.045
delay(1);if(i<0)i=0;
float v = y;
v=v*10/185;
float w=(i*v)/1000;
//delay(100);
lcd.clear();
lcd.setCursor(0, 0); lcd.print(i); lcd.print(“mA”);Serial.println(w);
lcd.setCursor(8, 0); lcd.print(v); lcd.print(“v”);// Print a message to the LCD.
lcd.setCursor(0, 1); lcd.print(w); lcd.print(“W”);// Print a message to the LCD.
c=0;
}
}
void powermeasure (void)
{
int a=analogRead(2);
delay(1);
int i = (analogRead(A0)-0);//*12.56=45//0.045 –*36
delay(1);if(i<0)i=0;
Serial.println(i);
power=a*i;
//Serial.println(power);
}
void lcdstart(void)
{
lcd.begin(16, 2);// set up the LCD’s number of columns and rows:
lcd.setCursor(0, 0);
lcd.print(“buck boost charger”);// Print a message to the LCD.
//delay(500);
//lcd.clear();
}
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
/**
* Divides a given PWM pin frequency by a divisor.
*
* The resulting frequency is equal to the base frequency divided by
* the given divisor:
* – Base frequencies:
* o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
* o The base frequency for pins 5 and 6 is 62500 Hz.
* – Divisors:
* o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
* 256, and 1024.
* o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
* 128, 256, and 1024.
*
* PWM frequencies are tied together in pairs of pins. If one in a
* pair is changed, the other is also changed to match:
* – Pins 5 and 6 are paired on timer0
* – Pins 9 and 10 are paired on timer1
* – Pins 3 and 11 are paired on timer2
*
* Note that this function will have side effects on anything else
* that uses timers:
* – Changes on pins 3, 5, 6, or 11 may cause the delay() and
* millis() functions to stop working. Other timing-related
* functions may also be affected.
* – Changes on pins 9 or 10 will cause the Servo library to function
* incorrectly.
*
* Thanks to macegr of the Arduino forums for his documentation of the
* PWM frequency divisors. His post can be viewed at:
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
*
*/