Hey,
So im working on a attiny based boost converter, and basically the boost converter needs a bit more frequency than the standard *PWM (edit)frequency at 9.6MHz. I googled a bunch and found no definitive answer/way that shows me how to do this. Some forum posts refer that it can be done by setting clock divider value, on the code, Others say that i need to mess with boards.txt, all in all this has me very confused. I am a novice so the simplest route should be the best given im not too picky on what frequency it outputs as long as its higher than the 20KHz auditory range.
My code
//Voltage Divider on input,Vcc-2100 ohm--970 ohm--gnd , measured the resistance with multimeter. actually 2k-1k resistor
//3V=193: 12V=777: 4.3V=278: 10V=647: 13V=839
//the boost conv has 2140-980 resistors.
//Duty Cycle = 1 - (Vin/Vout)
//pwm value = DutyCycle% x 255
//minimum input = 3V,maximum input = 4.2V fixed output 12V, Minimum duty cycle with no load 65%, max 75%
//*** configured so the boost converter can reach a stable state even in the case of sudden voltage spike
//that might be caused by load discontinution or connection, also while protecting the output from unexpected spikes.
const int groundControl = 3; //A low side mosfet switch that controlls all ground to the battery. Bjt push pull mosfet driver
// , On when LOW
const int voltageReadInput = A2; //Reads input voltage
const int voltageReadOutput = A1;//Reads output Voltage
const int boostConverterPin = 0; //Drives the boost converter, Also a bjt push-pull, Off when HIGH
const int chargePumpPin = 1;// for around 13V for the mosfet gates.
int pwmValue = 166; // (65/100)*255
void setup()
{
pinMode(groundControl, OUTPUT);
pinMode(voltageReadInput, INPUT);
pinMode(voltageReadOutput, INPUT);
pinMode(boostConverterPin, OUTPUT);
pinMode(chargePumpPin, OUTPUT);
}
void loop()
{
analogWrite(chargePumpPin, 130); //basically runs all the time, might be replaced by a boost converter upon observation.
if (analogRead(voltageReadOutput) >= 920) // basically greater than 13V, somethings wrong, disconnect ground ***
{
digitalWrite(groundControl, LOW);
}
if (analogRead(voltageReadInput) > 193) //***see note above
{
digitalWrite(groundControl, HIGH);
if (analogRead(voltageReadInput) > 278) //higher than max possible voltage for li ion cells, power source is connected, no need to draw power from battery.
{
digitalWrite(boostConverterPin, LOW);
}
if (analogRead(voltageReadInput) < 278) // running on battery, boosting needed
{
analogWrite(boostConverterPin, pwmValue);
if (analogRead(voltageReadOutput < 720))//this SHOULD output between 720 to 820 adc val, which means from 11.2V to 12.7V, pretty acceptable in my case.
{
pwmValue++;
}
if (analogRead(voltageReadOutput > 820))
{
pwmValue--;
}
}
}
}
Thanks.
TamjidK
Aren't you slowing things down with all those analogReads?
TheMemberFormerlyKnownAsAWOL:
Aren't you slowing things down with all those analogReads?
Yes i will set a variable and then calculate from there, first iteration of this code, i will probably change a few more things here and there
TheMemberFormerlyKnownAsAWOL:
Aren't you slowing things down with all those analogReads?
//Voltage Divider on input,Vcc-2100 ohm--970 ohm--gnd , measured the resistance with multimeter. actually 2k-1k resistor
//3V=193: 12V=777: 4.3V=278: 10V=647: 13V=839
//the boost conv has 2140-980 resistors.
//Duty Cycle = 1 - (Vin/Vout)
//pwm value = DutyCycle% x 255
//minimum input = 3V,maximum input = 4.2V fixed output 12V, Minimum duty cycle with no load 65%, max 75%
//*** configured so the boost converter can reach a stable state even in the case of sudden voltage spike
//that might be caused by load discontinution or connection, also while protecting the output from unexpected spikes.
const int groundControl = 3; //A low side mosfet switch that controlls all ground to the battery.
const int voltageReadInput = A2; //Reads input voltage
const int voltageReadOutput = A1;//Reads output Voltage
const int boostConverterPin = 0; //Drives the boost converter.
const int chargePumpPin = 1;// for around 13V for the mosfet gates.
int inputVoltage;
int outputVoltage;
int pwmValue = 166; // (65/100)*255
void setup()
{
pinMode(groundControl, OUTPUT);
pinMode(voltageReadInput, INPUT);
pinMode(voltageReadOutput, INPUT);
pinMode(boostConverterPin, OUTPUT);
pinMode(chargePumpPin, OUTPUT);
}
void loop()
{
inputVoltage = analogRead(voltageReadInput);
outputVoltage = analogRead(voltageReadOutput);
analogWrite(chargePumpPin, 130); //basically runs all the time, might be replaced by a boost converter upon observation.
if (outputVoltage >= 920) // basically greater than 13V, somethings wrong, disconnect ground ***
{
digitalWrite(groundControl, LOW);
}
if (inputVoltage > 193) //***see note above
{
digitalWrite(groundControl, HIGH);
if (inputVoltage > 278) //higher than max possible voltage for li ion cells, power source is connected, no need to draw power from battery.
{
digitalWrite(boostConverterPin, LOW);
}
if (inputVoltage < 278) // running on battery, boosting needed
{
analogWrite(boostConverterPin, pwmValue);
if (outputVoltage < 740)//this SHOULD output between 720 to 820 adc val, which means from 11.2V to 12.7V,
//pretty acceptable in my case, plan to set the closest value to 12V.
{
pwmValue++;
}
if (outputVoltage > 800)
{
pwmValue--;
}
}
}
}
better?
I actually planned at the beginning to do it this way, but wasnt sure how multiple analogReads affect speed vs how much space a variable takes. But this way seems better
analogWrite(chargePumpPin, 130); Why is this in loop()?
I don't see why inputVoltage and outputVoltage are globals.
TheMemberFormerlyKnownAsAWOL:
analogWrite(chargePumpPin, 130); Why is this in loop()?
I don't see why inputVoltage and outputVoltage are globals.
im not sure about the global part... should i put them in setup? does it make a difference?
I wasnt sure if you could put write/read functions in setup, so i put it in the loop.
Is there a particular reason im not getting any help?
kaseftamjid:
Is there a particular reason im not getting any help?
Because you haven't asked for any recently..?
Because you haven't told us your most recent problem?
Or shown us your most recent code or oscilloscope traces?
TheMemberFormerlyKnownAsAWOL:
Because you haven't asked for any recently..?
Because you haven't told us your most recent problem?
Or shown us your most recent code or oscilloscope traces?
The question persisted from the begining. I even changed the title to make more sense.
The only problem now and from the beginning is that the attiny13A has a pwm frequency of somewhere near 1khz according to default. There is a way to change it, but google searches results are vague and dont specify what effects it might have on other functions.
I believe i attached my code with comments on the very first post, And sadly i dont have an oscilloscope...