nickey 0 Posted April 12, 2011 Share Posted April 12, 2011 Ok, thanks to all of the great help I've had on this forum, I have this working on my breadboard and would love to hear any comments or criticism that could help me turn it into a better circuit or better coding. Warn you in advance that my circuit layout and C++ structure is far from professional. //*************************************************************************************** // Fish tank aerator // final version 1.0 // April 2011 // Built with Code Composer Studio v4 //*************************************************************************************** #include #define freq_10 BIT0 //Port 1 bit 0 #define freq_30 BIT1 //Port 1 bit 1 #define freq_60 BIT2 //Port 1 bit 2 #define dur_5 BIT3 //Port 1 bit 3 #define dur_10 BIT4 //Port 1 bit 4 #define freq_LED BIT5 //Port 1 bit 5 -- LED indicating frequency timer is active #define dur_LED BIT6 //Port 1 bit 6 -- LED indicating duration timer (pump) is on #define pump_relay BIT7 //Port 1 bit 7 void clear_states(void); //function to reset states void clear_timers(void); //function to reset all timers and interrupts and setup Ports void set_freq_timer(void); //function to setup frequency timer int freq_state = 0; int dur_state = 0; int timer_state = 0; int freq_timer_active = 0; int minutes_counter = 0; int half_seconds_counter = 0; int seconds_counter = 0; int timer_goal = 0; void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer clear_timers(); // reset timers clear_states(); // reset states //*************************************************************** // Poll switches loop //*************************************************************** for (; { freq_state = (P1IN & (freq_10 + freq_30 + freq_60)); if (freq_state == 00) { clear_timers(); //if frequency switch is off clear_states(); // reset states } else { dur_state = (P1IN & (dur_5 + dur_10)); //get pump duration from switch if (timer_state != (freq_state + dur_state)) //if switches have changed since last time { clear_timers(); //reset any old timers set_freq_timer(); //setup new frequency timer } } } } //*************************************************************** // Clear Timers and Interrupts and Initialize Ports //*************************************************************** void clear_timers(void) { TACCTL0 = 0; //disable interrrupts for CCR0 freq_timer_active = 0; //clear active timers minutes_counter = 0; //clear the minutes counter half_seconds_counter = 0; //clear the half/seconds counter seconds_counter = 0; //reset seconds counter timer_goal = 0; //clear timer goal number P1IE = 0; //disable all ints port 1 P2IE = 0; //disable all ints port 2 P1OUT = 0; //clear all pins on port 1 P1DIR = 0; //set all port 1 pins to "input" P1DIR |= dur_LED + freq_LED + pump_relay; //set port 1 LED and PUMP pins to "output" } //*************************************************************** // Clear States //*************************************************************** void clear_states(void) { freq_state = 0; dur_state = 0; //reset states timer_state = 0; freq_timer_active = 0; } //*************************************************************** // Setup Frequency Timer //*************************************************************** void set_freq_timer(void) { timer_state = (freq_state + dur_state); freq_timer_active = 1; //indicate frequency timer active minutes_counter = 0; //clear the minutes counter switch (freq_state) { case freq_10: timer_goal = 10; //set timer goal to 10 minutes break; case freq_30: timer_goal = 30; //set timer goal to 30 minutes break; case freq_60: timer_goal = 60; //set timer goal to 60 minutes } P1OUT |= freq_LED; //set frequency LED on P1OUT &= ~dur_LED; //set duration (pump) LED off P1OUT &= ~pump_relay; //turn pump off TACCR0 = 1000; //0 through 62499 cycles TACTL = TASSEL_2 + ID_3 + MC_1 + TACLR; //smclk,divider8,upmode,clrTAR TACCTL0 = CCIE; //enable interrupts for CCR0 _BIS_SR(GIE); //enable interrupts } //*************************************************************** //Timer Interrupt Service Routine // entered every .007 seconds //*************************************************************** #pragma vector = TIMERA0_VECTOR __interrupt void CCR0_ISR (void) { if (++half_seconds_counter >= 73) //if one/half second is up { half_seconds_counter = 0; //clear the half/seconds counter P1OUT ^= freq_LED; //toggle the freq led if (freq_timer_active == 0) //if the duration timer is active { P1OUT ^= dur_LED; //toggle the duration led } if (++seconds_counter >= 120) //if one minute is up { seconds_counter = 0; //reset seconds counter if (++minutes_counter >= timer_goal) //and if timer is complete { minutes_counter = 0; if (freq_timer_active == 1) //and it was the frequency timer { freq_timer_active = 0; P1OUT |= pump_relay; //turn on the pump switch (dur_state) { case dur_5: timer_goal = 5; //set timer goal to 5 minutes break; case dur_10: timer_goal = 10; //set timer goal to 10 minutes } } else //must have been duration/pump timer is now complete { set_freq_timer(); //start frequency timer again } } } } } water pump circuit.doc water pump code.doc Quote Link to post Share on other sites
Mac 67 Posted April 12, 2011 Share Posted April 12, 2011 Hi nickey, I think your design looks very nice. If you don't mind another perspective -- I did something similar (below) that uses a jumper to select either a 15 or 30 minute period and a pot to select 0 to 10 minutes of pump on time or always on (green) during each period. Cheerful regards, Mike Quote Link to post Share on other sites
nickey 0 Posted April 12, 2011 Author Share Posted April 12, 2011 hi Mike, wow thats just what I was looking for.. My only experience with micro controllers is the TI MSP430 and so I have no knowledge of the PIC systems.. Did it take much programming on your part? Also did that chip allow you to drive a relay directly from one of the pins? My relay has a coil taking about 200ma to activate it so I had to use a transistor. Quote Link to post Share on other sites
Mac 67 Posted April 12, 2011 Share Posted April 12, 2011 The PIC program is relatively simple. You should be able to something similar on the MSP430. If your relay coil takes 200-ma then you should definitely use a driver transistor. Cheerful regards, Mike Quote Link to post Share on other sites
infrared 3 Posted April 15, 2011 Share Posted April 15, 2011 Great Job Nickey Do you have any data on the duty cycle (how much power consumed over time) for your application? It would be great to see a low power fail safe mode to keep the 12 volt system from dropping below a certain level and allowing the starter to be able to start the motor once again for recharging. Quote Link to post Share on other sites
nickey 0 Posted April 15, 2011 Author Share Posted April 15, 2011 Thanks for the comments, no I haven't looked at overall power consumption yet.. good idea though. My current thoughts are that since I control the on and off times that I can keep the battery drain to negligible. Might look at your suggestion in the future though. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.