Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with mikroC program for light saving sensor

Status
Not open for further replies.

wazzaw

Newbie level 4
Joined
Apr 13, 2010
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,339
hi i am new to microcontrollers.
i have a project to do which is a light saving sensor that is during the day all light should be off, and during night only when there is people in the room,it will light.

to detect someone entering and someone leaving i have used 2 motion sensors( 2 photocells with laser beam) but the problem is in the mikroC program
the 2 sensors are connected to PORTC.F4 and PORTC.F5 output PORTB, i am using pic16F877A the program i wrote is:
int COUNTER=0;
void main() {
ADCON1=0 ;
TRISB=0b00000000;
TRISC=0b11111111;
PORTB=0;
while (1){
if(PORTC.F4==0 && PORTC.F5==0)
PORTB=0;
if (PORTC.F4==1){
delay_ms(10000);
if (PORTC.F5==1){
COUNTER=COUNTER+1;}
}
else if (PORTC.F5==1){
delay_ms(10000);
if (PORTC.F4==1){
COUNTER=COUNTER-1;}
}
if (COUNTER>=1)
PORTB=1;
}
}

thanks for help in advance.
 

Re: need help in mikroC

This expression will not work like you expect it to?

if(PORTC.F4==0 && PORTC.F5==0)

This expression compares F4 to zero, then logically ands the result to F5, then compares the result of the logical and to zero.

You need more brackets.

if((PORTC.F4 == 0) && (PORTC.F5 == 0))
 

Re: need help in mikroC

thanx for th reply first. but i still have the same problem
my major problm in mikroC is how to implement if this port is 1 bfore the other port become 1 .
in this example if sensor 1 is on then after a while sensor 2 is on means someone entered the room so add one to counter if sensor 2 is on and after a while sensor 1 is on means that someone left the room decrement counter, whenever counter>0 keep light on
 

Re: need help in mikroC

Hello,

In my humble opinion, you must do something like following :

1. test inputs
if (PORTC.4 ==1)
{
test_bit =1 ;
}
else
{ test_bit = 0 ; }

if (PORTC.5 == 1 )
{test_bit_1 = 1 ; }
else
{test_bit_1 = 0 }
2. check if someone enter
if (test_bit)
{
delay_ms (2000) / wait 2 sec
if (test_bit_1)
{
counter++;
}
}

3. check if someone lives
if (test_bit_1)
{
delay_ms(2000) // wait 2 sec
if (test_bit)
{
counter --;
}
}

4. make decision
if (counter > 0 )
{PORTB =1;} // set the light on
else
{PORTB = 0 ; } // set the light off



Of course that the code can be optimised.
The main idea is that the sensors must not be on in the same time or you will get the counter out of control.
You can add some extra conditions to checks ... so if sensor 1 is on AND sensor 2 is OFF that means that is possible that someone to enter and go to test this... also if sensor 2 is ON AND sensor 1 is OFF is possible that someone to leave the room and go to test it. ..
Hope it help you to make an idea.


Regards Sica.
 

Re: need help in mikroC

I think your best approach is to make use of the Port B interrupt on change feature.
You would have to move your inputs to RB4, RB5.

The interrupt will detect the change sequence much more reliably than polling input pins.
 

Re: need help in mikroC

thanx for the solution, which i think it is true but i dont know why it didnot works , i also write antoher one and didnot work
while (1){
if (PORTB.F7 ==1){ // SENSOR 1 IS ON

while (PORTB.F6 ==0){
;
}
if (PORTB.F6 ==1)
counter++;
}
if (PORTB.F6 ==1){ // SENSOR 2 IS ON

while (PORTB.F7 ==0){
;
}
if (PORTB.F7==1)
counter--;
}
if (counter>0)
PORTC=0b11111111 ;
if (counter==0)
PORTC=0b00000000 ;
}
}
 

Re: need help in mikroC

You have to be careful with counters, if you decrement it when it is zero, it will become 255!
you should do a check first: if(counter > 0) counter--;
The solution to a problem is to break it up into smaller problems.
You could try something like this.


Code:
for{;;)
  {
  while((PORTB.F6 == 0) && (PORTB.F7 == 0)){
    ;
    }

  if((PORTB.F6 == 0) && (PORTB.F7 == 1)){
    enter();
    }
  else if{PORTB.F7 == 0) && (PORTB.F6 == 1)){
    leave();
    }

  while((PORTB.F6 == 1) || (PORTB.F7 == 1)){
    ;
    }
  
  if(counter > 0){
    PORTC = 0xff;
    }
  else{
    PORTC = 0x00;
    }
  }

/*--- function enter ---*/

void enter(void)
  {
  while(PORTB.F6 == 0)){  /* Time out function needed? */
   ;
   }

  if((PORTB.F6 == 1) && (PORTB.F7 == 1)){
    counter++;
    }     
  }

/*--- function leave ---*/

void leave(void)
  {
  while(PORTB.F7 == 0){  /* Time out function needed? */
   ;
   }

  if((PORTB.F7 == 1) &&  (PORTB.F6 == 1)){
    if(counter > 0){ 
      counter--;
      }
    }     
  }

/*--- End of File ---*/

PORTB pins 6 and 7 support interrupt on change.
That would be the best and most reliable way to go!
 

Re: need help in mikroC

i tried the code u gave it to me but it didnot work i dont know if the problem is in the pic but i tried many pics.now i am also having problem with simple code like:

if(PORTB.F7 == 0){
PORTC.F0=0;
}
if (PORTB.F7 ==1)
PORTC.F0=1;
am i doing something wrong? is there something i still dont know about pic?
thanx in advance
 

Re: need help in mikroC

The code looks straightforward enough.
If the config fuses have been set up ok and the data direction registers set, it should work.
One thing to watch out for is the watchdog timer. During development it's best to disable it.
Post your complete code of the program so we can take a look.
Also make use of mplab sim to step through the code. That always helps.
 

Re: need help in mikroC

this is the complete program
int counter=0;
void main() {
TRISB=0b11111111;
TRISC=0b00001111;
TRISD=0b00000000;
PORTB=0;
while (1){
if (PORTB.F7 !=0){ // SENSOR 1 IS ON
while (PORTB.F6 ==0){
;
}
if (PORTB.F6 !=0)
counter++;
}
if (PORTB.F6 !=0){ // SENSOR 2 IS ON

while (PORTB.F7 ==0){
;
}
if (PORTB.F7!=0){
if (counter>0)
counter--;
}
}
if (counter>0)
PORTD==0xff ;
if (counter==0)
PORTD=0x00 ;
/**********************************************/

thanx for help
 

Re: need help in mikroC

Still cant see your config register settings?
This is normally the first thing to do that sets up oscillator, watchdog timer, power up timer, brown out detect, Lowvoltage programming etc.

In Hi-Tech it is done something like this:

Code:
/*--- Config fuse settings ---*/

	__CONFIG(XT & WDTDIS & PWRTEN & BOREN & LVPDIS);

Also cant see any include files. You must have one for the compiler to know the register names?
 

Re: need help in mikroC

before writing the program i use include default
 

hex file limitation in demo version

I have problem with volume limitation in hex file in demo versions. :cry:
can you help me to free download a fuul version off mikroc.:?:
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top