Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(2, 4, 5, 6, 7, 8);
- #define ADJUST_PIN A0
- #define BUCK_PIN 3
- #define BOOST_PIN 11
- #define VIN 12.0
- #define BOOST_EN 13
- #define BUCK_EN 12
- #define VOUT_PIN A1
- int hystrsis = 5;
- void setup() {
- pinMode(BUCK_PIN, OUTPUT);
- pinMode(BOOST_PIN, OUTPUT);
- pinMode(BOOST_EN, OUTPUT);
- pinMode(BUCK_EN, OUTPUT);
- TCCR2B = TCCR2B & B11111000 | B00000010; // for PWM frequency of 3921.16 Hz
- Serial.begin(9600);
- lcd.begin(16, 2);
- }
- void loop() {
- float Vout = volt_adjust();
- if (Vout >= 12.0) {
- boost_regulating(Vout);
- }
- else if (Vout < 12.0) {
- buck_regulating(Vout);
- }
- }
- void boost_regulating(float Vout) {
- digitalWrite(BUCK_EN, HIGH);
- digitalWrite(BOOST_EN, LOW);
- float Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0; //calculate v feedback
- float duty = 1.0 - (VIN / Vout);
- float analog_output = duty * 255.0;
- analog_output = constrain(analog_output, 0, 255);
- analogWrite(BOOST_PIN, analog_output);
- while ((Vout - Vout_feedback) > (Vout * 0.15)) {
- analog_output++;
- analog_output = constrain(analog_output, 0, 255);
- analogWrite(BOOST_PIN, analog_output);
- Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0;
- Serial.println(Vout_feedback);
- }
- while ((Vout_feedback - Vout) > (Vout * 0.15)) {
- analog_output--;
- analog_output = constrain(analog_output, 0, 255);
- analogWrite(BOOST_PIN, analog_output);
- Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0;
- Serial.println(Vout_feedback);
- }
- }
- void buck_regulating(float Vout) {
- digitalWrite(BOOST_EN, HIGH);
- digitalWrite(BUCK_EN, LOW);
- float Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0; //calculate v feedback
- float duty = (Vout / VIN);
- float analog_output = duty * 255.0;
- analog_output = constrain(analog_output, 0, 255);
- analogWrite(BUCK_PIN, analog_output);
- while ((Vout - Vout_feedback) > (Vout * 0.15)) {
- analog_output++;
- analog_output = constrain(analog_output, 0, 255);
- analogWrite(BUCK_PIN, analog_output);
- Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0;
- Serial.println(analog_output);
- }
- while ((Vout_feedback - Vout) > (Vout * 0.15)) {
- analog_output--;
- analog_output = constrain(analog_output, 0, 255);
- analogWrite(BUCK_PIN, analog_output);
- Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0;
- Serial.println(analog_output);
- }
- }
- float volt_adjust() {
- delay(50);
- int pot_read = analogRead(ADJUST_PIN);
- pot_read = map(pot_read, 0, 1023, 0, 120);
- pot_read = constrain(pot_read, 0, 120);
- lcd.clear();
- lcd.setCursor(2, 0);
- lcd.print("OUTPUT VOLT");
- lcd.setCursor(5, 1);
- lcd.print(pot_read);
- lcd.print(" V");
- return pot_read;
- //return 6.0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement