// // // hello.H-bridge.44.DCmod.c // // Christian, modified from Neil Gershenfeld // 11/18/12 // #include <avr/io.h> #include <util/delay.h> #define on_delay() _delay_us(10) // PWM on time - making argument variable takes 100% of memory #define off_delay1() _delay_us(1) // fast #define off_delay2() _delay_us(2) // medium #define off_delay3() _delay_us(3) // slow #define PWM_count 10000 // number of PWM cycles // // main // int main(void) { // program during the solar-day - starts with power up! // declarations static uint16_t count; // unsigned 16 bit uint8_t full,empty; // upper main tank full, empty uint16_t HOHunit; // H2O units (in %) pumped // // set clock divider to /1 // CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // initialize H-bridge pins // SET attyne44 ports as outputs DDRA |= _BV(0); //PA0 = output for supply power (e.g. 5V) DDRA |= _BV(1); //PA1 = output for supply power (e.g. 24V) DDRA |= _BV(2); //PA2 = output (in for 4953) DDRA |= _BV(3); //PA3 = output (in for 4953) // CLEAR attyne44 ports using AND and invert PORTA &= ~(_BV(0)); PORTA &= ~(_BV(1)); PORTA &= ~(_BV(2)); PORTA &= ~(_BV(3)); // SET dc-driver direction (see 4953 documentation) // forward: IN1(4953)=0, IN2(4953)=1; reverse: IN1(4953)=1, IN2(4953)=0 PORTA |= _BV(2); //Set PA2(=IN2/4953) HI ("forward") // // initialize // // clear PB0,PB1 as inputs DDRB &= ~(1<<PB0); //upper tank "full" indicator (e.g. HI > 90%) DDRB &= ~(1<<PB1); //Upper tank "low""indicator (e.g. LOW < 10%) // clear H2O pumped unit counter HOHunit = 0; while (1) { // // do this loop until the day ends (or a break ivent occors) // // CLEAR power outputs PORTA &= ~(_BV(0)); PORTA &= ~(_BV(1)); // // Get upper tank info, PB0=HI: tank is full (full=1); PB1=0: tank is empty (empty = 1) // if(PINB & (1 << PB0)){ full = 1; empty = 0;} else { full = 0; if(PINB & (1 << PB1)){ empty = 0;} else { empty = 1;} } // test full = 0; empty = 1; // // Fill the tank, supply power // if (full == 1) { // high tank is full, stop pump and supply max PV-energy PORTA &= ~(_BV(2)); // CLEAR PA2(=IN2/4953) Low - STOP, dont forget to set to high Impedance Z!! PORTA |= _BV(0); // SET PA0 HI for PowerSupply 1 ON (5V) PORTA |= _BV(1); // SET PA1 HI for PowerSupply 2 ON (24V) } else { // fill tank until full, use different speeds via PWM (=power requirements) for different stages for (count = 0; count < PWM_count; ++count) { PORTA |= _BV(2); // Set PA2(=IN2/4953) HI on_delay(); PORTA &= ~(_BV(2)); // CLEAR PA2(=IN2/4953) Low if (HOHunit < 50) { off_delay1(); // fast fill } else { if (HOHunit < 75) { off_delay2(); // medium fill PORTA |= _BV(0); // + power: SET PA0 HI for PowerSupply 1 ON (5V) } else { off_delay3(); // slow fill + PORTA |= _BV(0); // power: SET PA0 HI for PowerSupply 1 ON (5V) PORTA |= _BV(1); // power: SET PA1 HI for PowerSupply 2 ON (24V) } } } // for count ++HOHunit; // don't for get to weigt with set pump-power!! } // if full else } //while 1 } //main