|
|
View previous topic :: View next topic |
Author |
Message |
delifadzli
Joined: 12 Apr 2010 Posts: 27
|
Counter code |
Posted: Sat Sep 25, 2010 1:24 pm |
|
|
Code: |
#include <16F877A.H>
#device ADC=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
#include "C:\Users\deli\Desktop\COde\maximum\brum\Flexlcd2.c"
//==========================
#define LED1 PIN_D7
#define LED2 PIN_D6
#define LED3 PIN_C4
#define EXIT PIN_C7
#define EXIT2 PIN_C6
#define ZERO PIN_C5
void main(void)
{
int16 temp_adc=0,counter1;
int inout_counter=0,pir1,pir2,in,out;
int temp;
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(PIN_A1);
set_adc_channel(1); //read analog input from channel 1
lcd_init();
while(1)
{
pir1=0;
pir2=0;
in=0;
out=0;
temp_adc = read_adc();
temp=5.00*temp_adc*100.00/1023.00;
lcd_gotoxy(1,1);
printf(lcd_putc,"count:%d",inout_counter);
lcd_gotoxy(1,2);
printf(lcd_putc,"Temp:%d",temp);
{
pir1 = input(PIN_D5);
pir2 = input(PIN_D4);
if(pir1==1)
{
output_high(EXIT);
for(counter1=10000;counter1>0;counter1--)
{
pir2 = input(PIN_B7);
delay_ms(1);
if(pir2==1)
in=1;
}
if(in==1)
inout_counter++;
}
else if(pir2==1)
{
for(counter1=10000;counter1>0;counter1--)
{
output_high(EXIT2);
pir1 = input(PIN_B6);
delay_ms(1);
if(pir1==1)
out=1;
}
if(out==1)
inout_counter--;
}
if(inout_counter==0)
{
//output_high(LED1); //desired output
//output_low(LED2);
//output_low(EXIT);
//output_low(EXIT2);
output_low(LED2);
output_low(LED3);
output_low(LED1);
output_high(ZERO);
}
else
{
if((temp<20))
{
output_high(LED1);
output_low(LED2);
output_low(LED3);
}
else if ((temp>= 20) && (temp <30))
{
output_high(LED2);
output_low(LED1);
output_low(LED3);
}
else
{
output_high(LED3);
output_low(LED1);
output_low(LED2);
}
}}
}}
|
Here is the code that I had managed to compile without error.
Here I'm using 2 PIR sensor
The situation that I had achieve regarding counter aspect is
if PIR 1 on , then PIR 2 on the counter will increment.
The situation is , the count only can increase, if the PIR 1 firstly off, before checking the second sensor(PIR2)
Here i want to ask, how to make it more reliable like;
When checking PIR2 condition, it will ignore the condition of PIR1 either on or off....and if PIR2 on-- it will make the count increase,...
The same goes for decrement section.
Or can anyone suggest any suitable counter approach like:
- isn't needed any IC counter?
- new way on counter-- isn't more suitable to use interrupts
*Actually I don't quite familiar with interrupts routine.
sorry for my weakness... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 26, 2010 5:44 pm |
|
|
Quote: | setup_adc_ports(PIN_A1); |
I didn't try to solve your problem, but I noticed the line above is wrong.
That function does not use pin numbers as the parameter.
It works in your program, but it's because you're lucky. The number
represented by PIN_A1 just happens to enable pin A1 as an analog pin.
To find the correct value to use as the parameter, look in this section
of the 16F877a.h file:
Quote: |
// Constants used in SETUP_ADC_PORTS() are:
|
|
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Sep 27, 2010 1:59 am |
|
|
Your problem lies here:-
Code: |
if(pir1==1)
{
output_high(EXIT);
for(counter1=10000;counter1>0;counter1--)
{
pir2 = input(PIN_B7);
delay_ms(1);
if(pir2==1)
in=1;
}
if(in==1)
inout_counter++;
}
else if(pir2==1) // <<<<<<<<<<------------------
|
The else forces it to only check if the previous (pir1==1) condition is false.
Remove the else and the check on pir2 will be independent of pir1. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|