|
|
View previous topic :: View next topic |
Author |
Message |
ycho87
Joined: 11 Mar 2011 Posts: 7
|
Checking sequence using if statement |
Posted: Mon Mar 21, 2011 12:21 pm |
|
|
Hi,
I am currently doing Automatic room light controller.
There are 2 pairs of transmitters receivers.
Transmitter and receiver will be place at each side of the door.
Lets assume first pair of the transmitter and receiver as S1 and another pair as S2.
PS: the output of the receiver will always in high stage, if people pass through(no IR) = low stage.
here the case for people in:
S1 = 0 S2 = 1
S1 = 0 S2 = 0
S1 = 1 S2 = 0
For people out:
S1 = 1 S2 = 0
S1 = 0 S2 = 0
S1 = 0 S2 = 1
My problem is S1 = 1 S2 =0 and S1 = 0 S2 = 1 will be occur twice no matter people in or out.
I wrote the program that if S1 = 0 S2=1, the program will start the rest of the sequence checking, follow by S1 = 0 S2 =0 and finally S1 = 1 S2 = 0. Below is the program i wrote.
Code: |
#include <16f877a.h>
#use delay(clock=20000000)
#fuses hs,noprotect,nowdt,nolvp
#define use_portb_lcd TRUE
#include <lcd.c>
#define PWR_LED Pin_A5
#define IR1 Pin_A0
#define PWR_LED1 PIN_E0
#define IR2 Pin_A1
void main()
{
int interrupt = 0;
int interrupt1 = 0;
set_tris_a(0b00000011);
lcd_init();
lcd_putc("\fAutomatic Room");
lcd_putc("\nLight Controller");
delay_ms(5000);
lcd_putc("\f");
lcd_gotoxy(1,1);
lcd_putc("People in = 0\n");
lcd_putc("People out = 0\n");
do{
if (!IR1 & IR2) the initial stage of IR1 and IR2 will always high
{
while(IR1 ^ IR2);
if (!IR1 & !IR2)
{
while(!(IR1 & IR2));
if (IR1 & !IR2)
{
interrupt += 1;
lcd_gotoxy(14,1);
printf(lcd_putc,"%3u",interrupt);
}
while(IR1 ^ IR2);
}
}
}while(1);
} |
The above program shows no respond at all, no counting show on the LCD. Please help |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 21, 2011 12:50 pm |
|
|
Quote: | #define IR1 Pin_A0
if (!IR1 & IR2)
|
That is not the correct method to read and test a pin.
Download the CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Go to this section:
Quote: | BUILT-IN-FUNCTIONS |
Then look in this sub-section:
You will see a list of all functions for doing i/o operations on PIC pins.
To read a pin, use the input() function. Look in the CCS manual before
doing anything. There will be a function specifically for it.
There is one more problem (in that area of your code). You are using
"bitwise" operators. If you want to do logical testing, then you should use
logical operators. |
|
|
ycho87
Joined: 11 Mar 2011 Posts: 7
|
|
Posted: Tue Mar 22, 2011 3:44 am |
|
|
Hi PCM Programmer,
Thanks for your reply
Below is the modified code.
Code: |
#include <16f877a.h>
#use delay(clock=20000000)
#fuses hs,noprotect,nowdt,nolvp
#define use_portb_lcd TRUE
#include <lcd.c>
#define PWR_LED Pin_A5
#define IR1 Pin_A0
#define PWR_LED1 PIN_E0
#define IR2 Pin_A1
void main()
{
int interrupt = 0;
int interrupt1 = 0;
set_tris_a(0b00000011);
lcd_init();
lcd_putc("\fAutomatic Room");
lcd_putc("\nLight Controller");
delay_ms(5000);
lcd_putc("\f");
lcd_gotoxy(1,1);
lcd_putc("People in = 0\n");
lcd_putc("People out = 0\n");
do{
if (input(IR1)==0 && input(IR2==1))
{
while(input(IR1)==1 ^ input(IR2)==1);
if (input(IR1)==0 && input(IR2)==0)
{
while(input(IR1)==0 && input(IR2)==0);
if (input(IR1)==1 & input(IR2)==0)
{
interrupt += 1;
lcd_gotoxy(14,1);
printf(lcd_putc,"%3u",interrupt);
}
while(input(IR1)==1 ^ input(IR2)==1);
}
}
}while(1);
}
; |
The LCD show the counting now, but it only can count up to 1.
What is the problem?
thank you |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Mar 23, 2011 10:28 am |
|
|
As a suggestion: You will get better and quicker responses when you make sure the program you post is well formatted. The indentation in your program is now jumping left and right which makes it very difficult to read.
Code: | if (input(IR1)==0 && input(IR2==1)) | I am surprised this line did compile without warning or error code. Fix the bad placed ')' character.
Code: | if (input(IR1)==1 & input(IR2)==0) | Here, again, you should replace the 'binary AND' by a 'logical AND' . Personally I like to add a few more braces to clarify the operators belonging together: Code: | if ((input(IR1)==1) && (input(IR2)==0)) |
|
|
|
|
|
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
|