hi..i'm a student..
and i am really new with c programming..
i had a project to build a security system...this is the 1st draft of my coding..but it seems to not working after i program it into my microcontroller. can u guys tell me if something wrong with this coding...pleasee...
//---------------------------------------------
Code:
#include <16F877A.h>
#device ADC=8
#fuses XT, NOWDT, NOPROTECT, NOPUT, NOBROWNOUT, NOLVP
#use delay (clock=4000000)
#include <LCD.C>
#include <keyy_padd.c>
#include <string.h>
temp();
smoke();
magnetic();
char condition;
unsigned int16 Smoke1, Magnetic1, Temp1;
char lcd_buffer[5];
char k;
float adcValue, currTemp, voltage;
void do_states(void)
{
if(condition == 1) //condition 1..it will assigned the program to go to temp() function
{
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Temperature");
temp();
}
else if(condition == 2) //condition 2..it will assigned the program to go to smoke() function
{
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Smoke");
smoke();
}
else if(condition == 3) //condition 3..it will assigned the program to go to magnetic() function
{
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Magnetic");
magnetic();
}
else if(condition == 4)
{
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Security Running"); //condition 4..it will displayed security running.
}
}
void main()
{
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_tris_c(0b00000000);
while(1) //will this while continue to loop and checking..the circuit...? regardless of the keypad action and lcd?
{
set_adc_channel(0); // set the adc channel to port A0
delay_us(50);
adcValue=read_adc();
delay_us(50);
voltage=5.000*adcValue/255.000; //the calculation for temperature circuit
currTemp = voltage/0.01;
if(adcValue >= 20)
output_high(pin_A5);
else
output_low(pin_A5);
delay_us(250);
set_adc_channel(1); // change the adc channel to port A1 (smoke detector)circuit..can i really change it like this?
delay_us(50);
adcValue=read_adc();
if (input(PIN_A1))
output_high(pin_A5);
else
output_low(pin_A5);
delay_us(250);
set_adc_channel(2); // change the adc channel to port A1 (magnetic detector)circuit..can i really change it like this?
delay_us(50);
adcValue=read_adc();
if (input(PIN_A2))
output_high(pin_A5);
else
output_low(pin_A5);
}
{
lcd_init(); //start the LCD...
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Choose 1.Temp 2.Smoke 3.Magnetic");
k=get_key(); //this suppose to make...when we press keypad 1...it will go to condition 1
if(k=='1')
{
while(1)
{
condition = 1;
}
}
if(k=='2')
{
while(1)
{
condition = 2; //this suppose to make...when we press keypad 2...it will go to condition 2
}
}
if(k=='3')
{
while(1)
{
condition = 3; //this suppose to make...when we press keypad 3...it will go to condition 3
}
}
else
while(1) //when other key is pressed then it will go to condition 4
{
condition = 4;
}
lcd_gotoxy(1,1); //indicating that the keypad had been press and waited to be executed
lcd_putc('\f');
lcd_putc("Security Starting");
delay_ms(3000);
if(k=='1')
{
do_states(); //execute the function
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Next Menu"); //displayed after the function had been executed...what will the program do after this...how am i suppose to make it ask me to
(choose 1.Temp 2.Smoke 3.Magnetic) again?
delay_ms(1000);
}
if(k=='2')
{
do_states();
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Next Menu");
delay_ms(1000);
}
if(k=='3')
{
do_states();
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Next Menu");
delay_ms(1000);
}
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Done");
}
}
temp() //it will executed the function for every condition
{
if (output_low(pin_A5))
{
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("curr temp: ");
printf(lcd_buffer,"%3i",currTemp); //this will display the current temperature...will it?
lcd_gotoxy(1,1);
lcd_putc(lcd_buffer);
}
else if (output_high(pin_A5))
{
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Warning...FIRE!");
}
delay_ms(500);
}
smoke()
{
if (output_low(pin_A5))
{
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("No FIre");
}
else if (output_high(pin_A5))
{
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Warning...FIRE!");
}
delay_ms(500);
}
magnetic()
{
if (output_low(pin_A5))
{
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Door Lock");
}
else if (output_high(pin_A5))
{
lcd_gotoxy(1,1);
lcd_putc('\f');
lcd_putc("Warning...BREAK IN!");
}
delay_ms(500);
}
//-----------------------------------------------
thats all...can u guys please help me...please...please...
i wanted to make...after the program asked me to (choose 1.temp 2.smoke and 3.magnetic)
for example i choose keypad 1..
and after the condition 1 had been executed...i would want the program to asked me again to choose