'**************************************************************** '* Name : Time Countdown * '* Author : Arava * '* Date : 4/24/2007 * '* Version : 1.0 * '* Notes : PIC16F819 * '**************************************************************** 'BUZZER CONNECTED TO PortB.4 'START BUTTON CONNECTED TO PortB.5 'RESET BUTTON CONNECTED TO PortB.0 'FOOD BUTTONS CONNECTED TO Ports B.1/ B.2 / B.3 'TIMER BUTTON CONNECTED TO PortA.2 'LCD CONNECTED TO PortB.6 'POTENTIOMETER CONNECTED TO PortA.0 OSCCON = %01100100 'set the internal oscillator to 4MHz TRISA = %11111111 'make portA all input ADCON1 = %10001110 'make portA.0 analog , all others Digital output portB.6 ' output to lcd OUTPUT portB.4 ' output to buzzer input portB.5 ' input start button input portB.0 ' input reset button input portB.1 ' chicken button input input portB.2 ' meat button input input portB.3 ' egg button input include "modedefs.bas" DEFINE ADC_BITS 10 ' 10 bit adc conversion DEFINE ADC_CLOCK 3 'These difine ADCIN parameters DEFINE ADC_SAMPLEUS 20 '******************** variables*********************** counting var bit ' 0=not counting. 1=counting in progress. secCount var word ' counting the seconds wile cooking secLoop60 var word secRemain var word ' the time for the countdown minRemain var word TimeLoop var byte cookingTime var word ' the time that takes for the food to cook DeBounce con 10 start_buttonState var bit 'start button reset_buttonState var bit c_buttonstate var bit m_buttonstate var bit e_buttonstate var bit timer_buttonstate var bit weight var byte ' between 1-8 pounds PotNoise con 2 'potentiometer noise PotSignal var word 'potentiometer signal (0-1012) PotSignala var word LCD var PortB.6 BUZZER var PortB.4 chic_button var portB.1 meat_button var portB.2 egg_button var portB.3 start_button var portB.5 reset_button var portB.0 timer_button Var portA.2 '***************************setup************************************* Setup: PAUSE 100 serout2 LCD,396,[12] 'clear LCD screen pause 10 serout2 LCD,396,[17] 'turn on the LCD backlight pause 10 serout2 LCD,396,["select food type"] ' prints first instruction PAUSE 500 ' wait for LCD to initiate counting = 0 cookingTime = 0 secCount = 0 secRemain = 0 minRemain = 0 TimeLoop = 0 low BUZZER Goto main '***************************main************************************* ' if counting - keep counting. ' if not - feel for food buttons, if it's not pressed don't do anything. ' any other button pressed now - nothing happens Main: if counting = 1 then ' if cooking Goto Timer ' keep counting endif c_buttonstate = chic_button ' feelFoodButton - feel 4 buttons m_buttonstate = meat_button e_buttonstate = egg_button timer_buttonstate = timer_button if c_buttonstate = 1 then 'if chick button changes **** pause 10 'debounce time c_buttonstate = chic_button 'feel the button if c_buttonstate = 1 then 'if the input is real serout2 LCD,396,[12] 'clear LCD screen for countdown pause 5 serout2 LCD,396,["chicken", 13, "weight:"] pause 5 Goto CalculateChicken endif endif if m_buttonstate = 1 then 'if meat button changes **** pause 10 'debounce time m_buttonstate = meat_button 'feel the button if m_buttonstate = 1 then 'if the input is real serout2 LCD,396,[12] 'clear LCD screen for countdown pause 5 serout2 LCD,396,["meat", 13, "weight:"] pause 5 Goto CalculateMeat endif endif if e_buttonstate = 1 then 'if egg button changes **** pause 10 'debounce time e_buttonstate = egg_button 'feel the button if e_buttonstate = 1 then 'if the input is real serout2 LCD,396,[12] 'clear LCD screen for countdown pause 5 serout2 LCD,396,["egg", 13, "level: "] pause 5 Goto CalculateEgg endif endif if timer_buttonstate = 1 then 'if timer button changes **** pause 10 'debounce time timer_buttonstate = timer_button 'feel the button if timer_buttonstate = 1 then 'if the input is real serout2 LCD,396,[12] 'clear LCD screen for countdown pause 5 serout2 LCD,396,["timer"] pause 5 Goto CalculateTimer endif endif gosub FeelResetButton Goto main '***************************CalculateTimer cooking time************************************* CalculateTimer: 'feel potentiometer + calculate var weight adcin 0,PotSignal 'take two readings and compare them pause 10 'with Noise as a measure adcin 0,PotSignala 'a simple way to clean up your singnal if PotSignala < PotSignal - PotNoise then cookingTime = (PotSignala/4)*60 serout2 LCD,396,[148,"Time: ", dec cookingTime /60 ,"min"] 'courser to second line pos 0 print weight. endif if PotSignala > PotSignal + PotNoise then cookingTime = (PotSignala/4)*60 serout2 LCD,396,[148,"Time: ", dec cookingTime /60 ,"min"] endif '***special Feel Start Button timer time start_buttonState = start_button if start_buttonState = 1 then 'if egg button changes **** pause 10 'debounce time start_buttonState = start_button 'feel the button if start_buttonState = 1 then 'if the input is real pause 5 serout2 LCD,396,[148, "Time "] 'courser to line1 position 0 pause 5 counting = 1 'change to counting state goto Timer 'and start counting endif endif gosub FeelResetButton Goto CalculateTimer '***************************CalculateMeat cooking time************************************* CalculateMeat: 'feel potentiometer + calculate var weight adcin 0,PotSignal 'take two readings and compare them pause 10 'with Noise as a measure adcin 0,PotSignala 'a simple way to clean up your singnal if PotSignala < PotSignal - PotNoise then weight = PotSignala/126 serout2 LCD,396,[156, dec weight , "p "] 'courser to second line pos 8 print weight. endif if PotSignala > PotSignal + PotNoise then weight = PotSignala/126 serout2 LCD,396,[156, dec weight , "p "] 'courser to second line pos 8 print weight. endif if weight < 2 then cookingTime = 35*60 else cookingTime = (45+((weight-2)*10))*60 endif gosub FeelStartButton 'go to FeelStartButton gosub FeelResetButton Goto CalculateMeat '***************************CalculateChicken cooking time************************************* CalculateChicken: 'feel potentiometer + calculate var weight adcin 0,PotSignal 'take two readings and compare them pause 10 'with Noise as a measure adcin 0,PotSignala 'a simple way to clean up your singnal if PotSignala < PotSignal - PotNoise then weight = PotSignala/126 serout2 LCD,396,[156, dec weight , "p "] 'courser to second line pos 8 print weight. endif if PotSignala > PotSignal + PotNoise then weight = PotSignala/126 serout2 LCD,396,[156, dec weight , "p "] 'courser to second line pos 8 print weight. endif if weight < 2 then cookingTime = 40*60 else cookingTime = (60+((weight-2)*10))*60 endif gosub FeelStartButton 'go to FeelStartButton gosub FeelResetButton Goto CalculateChicken '***************************CalculateEgg cooking time************************************* CalculateEgg: 'feel potentiometer + calculate var weight adcin 0,PotSignal 'take two readings and compare them pause 10 'with Noise as a measure adcin 0,PotSignala 'a simple way to clean up your singnal if PotSignala < PotSignal - PotNoise then weight = PotSignala/126 if weight <=4 then serout2 LCD,396,[156, "soft"] 'courser to second line pos 8 print weight. else serout2 LCD,396,[156, "hard"] endif endif if PotSignala > PotSignal + PotNoise then weight = PotSignala/126 if weight <=4 then serout2 LCD,396,[156, "soft"] 'courser to second line pos 8 print weight. else serout2 LCD,396,[156, "hard"] endif endif if weight <=4 then cookingTime = 5*60 else cookingTime = 11*60 endif 'special FeelStartButton for egg state start_buttonState = start_button if start_buttonState = 1 then 'if egg button changes **** pause 10 'debounce time start_buttonState = start_button 'feel the button if start_buttonState = 1 then 'if the input is real pause 5 if weight <=4 then serout2 LCD,396,[134, "soft"] ' courser to line0 position 6 else serout2 LCD,396,[134, "hard"] endif serout2 LCD,396,[148, "Time "] 'courser to line1 position 0 pause 5 counting = 1 'change to counting state goto Timer 'and start counting endif endif gosub FeelResetButton Goto CalculateEgg '***************************Feel Start Button************************************* FeelStartButton: start_buttonState = start_button if start_buttonState = 1 then 'if egg button changes **** pause 10 'debounce time start_buttonState = start_button 'feel the button if start_buttonState = 1 then 'if the input is real pause 5 serout2 LCD,396,[137, dec weight, "p "] 'courser to line0 position 9 serout2 LCD,396,[148, "Time "] 'courser to line1 position 0 pause 5 counting = 1 'change to counting state goto Timer 'and start counting endif endif return '***************************Feel Reset Button************************************* 'feel reset button FeelResetButton: 'reset_buttonState = reset_button ' if reset_buttonState = 1 then 'if egg button changes **** ' pause 5 'debounce time reset_buttonState = reset_button 'feel the button if reset_buttonState = 1 then 'if the input is real low BUZZER ' pause 5 goto setup 'and start counting endif 'endif return '**********************************timer******************************************** Timer: secLoop60 = 0 while secLoop60 <= 59 TimeLoop = 0 while TimeLoop <= 99 'counts 100 times TimeLoop = TimeLoop +1 ' 1 miliSec gosub FeelResetButton pause 10 wend secCount = secCount +1 ' add one sec to count secRemain = cookingTime - secCount ' calculates the time remains to cook minRemain = secRemain/60 if secLoop60 = 0 then serout2 LCD,396,[155,dec minRemain,":00"] else if secLoop60 > 50 then serout2 LCD,396,[155,dec minRemain,":0", dec 60-secLoop60] else serout2 LCD,396,[155,dec minRemain,":", dec 60-secLoop60] endif endif secLoop60 = secLoop60 + 1 if secCount = 900 then ' after 15 min high buzzer ' buzz 5 sec pause 5000 secCount = secCount - 5 ' reduce 5 sec from count low buzzer endif if secCount = cookingTime then ' if the food is ready while 1=1 HIGH BUZZER ' make noise with BUZZER pause 5000 low BUZZER wend counting = 0 ' change counting to off goto Setup ' go back to start endif wend goto Timer