Hi everybody,
I would like to drive a MOSFET halfbridge with arduino nano. The thing is, I'm brand new to Arduino, and obviously I'm missing something.
I wrote a program using Timer1, see below.
What it is suppose to do is:
either drive the Highside mosfet with pin 9 in buck converter mode, or to drive the low side Mosfet with pin 10 in boost converter mode.
The mode is selected through switch.
At this point, it should get in buck converter mode when the programm begins, because of the initial values of TimerSet and IsCharging. But nothing happens at either output 9 or 10. (I checked with an osco)
If I put the ISR as comment, then both output pins are high, so I think I missed something about the timer, but I cannot figure out what exactly. I have looked at tutorials and read the AtMega168 datasheet part about timers but I've become quite confused, so I am turning to you.
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
const int ModeSwitchHigh = 2;
const int ModeSwitchLow = 3;
const int HighSideMos = 9;
const int LowSideMos = 10;
volatile boolean IsCharging = true; //True -> Buck converter mode, using only High-side Mosfet. False -> Boost converter mode, using only Low-Side Mosfet
volatile boolean TimerSet = false;
void setup() {
pinMode(ModeSwitchHigh,INPUT_PULLUP);
pinMode(ModeSwitchLow,INPUT_PULLUP);
pinMode(HighSideMos, OUTPUT);
pinMode(LowSideMos, OUTPUT);
digitalWrite(HighSideMos, HIGH);
digitalWrite(LowSideMos, HIGH); //Vielleicht nicht nötig wegen main Loop
attachInterrupt(digitalPinToInterrupt(ModeSwitchHigh), Buckmode, FALLING); //Buckconverter mode, Battery loading, High-Side Mosfet
attachInterrupt(digitalPinToInterrupt(ModeSwitchLow), Boostmode, FALLING); //Boostconverter mode, Battery unloading, Low-Side Mosfet
//InitTimer1
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
ICR1 = 159; //Set Top Value, -> frequenz 100 kHz 16 MHz/100kHz-1, 16 MHz internal clock
OCR1A = 80; // init Wert 5 mikro sek
TCCR1B |= (1 << CS10); // no prescaling, internal clock
TCCR1B |= (1 << WGM13); // turn on fast PWM mode
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
TCCR1A |= (1 << COM1A1); //nötig zum nichtinvertiertes PWM-Signal setzen
}
void loop() {
//Buckmode
if ((IsCharging == true)&&(TimerSet==false)) {
OCR1A = 71; //44,44% of 160. 4V output with 9V input
TimerSet=true;
digitalWrite(HighSideMos, HIGH);
digitalWrite(LowSideMos, LOW);
}
//Boostmode
else if ((IsCharging == false)&&(TimerSet==false)){
OCR1A = 96; //T_E = 96, 60 % Period.
TimerSet = true;
digitalWrite(HighSideMos, LOW);
digitalWrite(LowSideMos, HIGH);
}
}
ISR(TIMER1_COMPA_vect){
if (IsCharging == true) //TSS, PWM Aus High-Side Mosfet, Pin 9
{
digitalWrite(HighSideMos,LOW);
}
else if (IsCharging == false) {
digitalWrite(LowSideMos,LOW);
}
}
void Buckmode(){
IsCharging = true;
TimerSet = false;
}
void Boostmode(){
IsCharging = false;
TimerSet = false;
}
Thanks in advance for your time
Murmeltier