# need help

Status
Not open for further replies.

#### jukkaz

##### New Member
hi this is my first post XD

I have a problem regarding PIC programming using 16F877A. I followed this light control system project but i have no idea on how to program from the given flowchart. here it is:

I plan to use mikroC. can anyone guide me through this one?

thanx

Last edited:

#### Gayan Soyza

##### Active Member
Looks likes a finite state system.Are you making a room light controller?

#### jukkaz

##### New Member
yup...at least that was what i planned...lol. Ive already prepared and constructed the circuit an stuffs, but im stuck here at the microcontroller programming. Actually i followed the project tutorial from Electronics.lab.com. They only give the algorithm for the code but not the actual code itself.so thats my problem.thanx for the reply btw.

#### be80be

##### Well-Known Member
Can you post the circuit

#### Gayan Soyza

##### Active Member
You need to compare the previous state & the new state. Its bit tricky.

*Read A & B save it to the “previous state”
*If any change read port & save it to the “new state” register.
*Read A & B again if its in the same state wait until change occurs.
* If any change read port, backup the “new state” register to the “previous state” register. & update the “new state” with the current port read.

If you compare the “state” registers with some known values you can take a decision what to do.

#### Torben

##### Well-Known Member
yup...at least that was what i planned...lol. Ive already prepared and constructed the circuit an stuffs, but im stuck here at the microcontroller programming. Actually i followed the project tutorial from Electronics.lab.com. They only give the algorithm for the code but not the actual code itself.so thats my problem.thanx for the reply btw.
Hi jukkaz, and welcome to the board!

First off, it would be helpful to know what experience you have programming in C. Once we know that it will be easier to know where to start. Do you already know C and just need to know how to access the inputs and outputs on the PIC, or have you already written the program and are just having trouble actually writing it to the chip, or is the problem that you don't know how to translate the flowchart into C to start with? Knowing that will help us help you.

Regards,

Torben

#### jukkaz

##### New Member
Hi jukkaz, and welcome to the board!

First off, it would be helpful to know what experience you have programming in C. Once we know that it will be easier to know where to start. Do you already know C and just need to know how to access the inputs and outputs on the PIC, or have you already written the program and are just having trouble actually writing it to the chip, or is the problem that you don't know how to translate the flowchart into C to start with? Knowing that will help us help you.

Regards,

Torben

first of all here's the circuit diagram:

yup im already familiar with C but my problem is to know how to access the inputs and output of the PIC (16F877A in particular). The problem is i dont even know how to start writing a program for PIC.do we have to initialize the ports etc and stuffs....how to read inputs for the ports etc...

##### Banned
There is so much available code on the net for PIC's you should easily be able to PIC 16F877A C examples - Google Search and find someone elses code, keep hunting and eventually you'll find one that's well commented. Play around with it, modify it for your needs and if you're still stuck come back with some code and questions.

Last edited:

#### jukkaz

##### New Member
hi guys...we came up with this code...could anyone verify/comment bout this one please. Thanx...all your help will be much appreciated...XD

Code:
#include <16f877A.h>	//requirement
#fuses hs,nowdt, noprotect, brownout, nolvp //requirement
#use delay(clock=20000000)              //requirement
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, parity=N) //whats this for?

void main()
{
int reg=0;        // initialize register
set_tris_a(255);  // set port a as input
set_tris_b(0);    // set pin b as output

do
{
start:
if(input(PIN_A0)==0 && input(PIN_A1)==1)          //check for A=0 B=1
{  delay_ms(5);                                   //if A=0 B=1 then delay
if(input(PIN_A0)==0 && input(PIN_A1)==1)       //if no change goto start
{  goto start; }
else goto a0b0;                                //goto a0b0

a0b0:                                          //check for A=0 B=0
if(input(PIN_A0)==0 && input(PIN_A1)==0)
{ delay_ms(5);                                 //if A=0 B=0 then delay
if(input(PIN_A0)==0 && input(PIN_A1)==0)     //if no change goto a0b0
{ goto start;}
else goto a1b0;
}
else if(input(PIN_A0)==0 && input(PIN_A1)==1)
{goto start;}
else goto a0b0;

a1b0:
if(input(PIN_A0)==1 && input(PIN_A1)==0)
{ delay_ms(5);
if(input(PIN_A0)==1 && input(PIN_A1)==0)
{ goto start;}
else goto a1b1;
}
else if(input(PIN_A0)==0 && input(PIN_A1)==0)
{goto start;}
else goto a1b0;

a1b1:
if(input(PIN_A0)==1 && input(PIN_A1)==1)
{ reg++;                                  //increment register
if(reg>0)                               //check register
{output_bit(PIN_D5, 1);}                //if register is >0 Output=HIGH
else
{ output_bit(PIN_D5, 0);}
delay_ms(1000);
goto start;

}
else if(input(PIN_A0)==1 && input(PIN_A1)==0)
{goto start;}
else goto a1b1;

}
else
{goto start2;}

start2:
if(input(PIN_A0)==1 && input(PIN_A1)==0)
{  delay_ms(5);
if(input(PIN_A0)==1 && input(PIN_A1)==0)
{  goto start; }
else goto a20b0;

a20b0:
if(input(PIN_A0)==0 && input(PIN_A1)==0)
{ delay_ms(5);
if(input(PIN_A0)==0 && input(PIN_A1)==0)
{ goto start;}
else goto a0b1;
}
else if(input(PIN_A0)==1 && input(PIN_A1)==0)
{goto start;}
else goto a20b0;

a0b1:
if(input(PIN_A0)==0 && input(PIN_A1)==1)
{ delay_ms(5);
if(input(PIN_A0)==0 && input(PIN_A1)==1)
{ goto start;}
else goto a21b1;
}
else if(input(PIN_A0)==0 && input(PIN_A1)==0)
{goto start;}
else goto a0b1;

a21b1:
if(input(PIN_A0)==1 && input(PIN_A1)==1)
{ reg--;                                  //decrement register
if(reg>0)                               //check register
{output_bit(PIN_D5, 1);}                //if register is >0 Output=HIGH
else
{ output_bit(PIN_D5, 0);}
delay_ms(1000);
goto start;

}
else if(input(PIN_A0)==0 && input(PIN_A1)==1)
{goto start;}
else goto a21b1;

}
else goto start;

}while(1);
}

Last edited:

#### felis

##### New Member
1. Use CODE tags when posting code. Without them the code is pretty much unreadable.
2. Don't configure the whole port as input when using only one pin as input. It will bite you.
3. I'd rather use switch statement instead of ifs.

#### jukkaz

##### New Member
1. Use CODE tags when posting code. Without them the code is pretty much unreadable.
2. Don't configure the whole port as input when using only one pin as input. It will bite you.
3. I'd rather use switch statement instead of ifs.
1)thanx, i already wrapped them with code tags

2)how to configure only pin a0 and a1 as inputs?? issit trisa.f0=1 and trisa.f1=1

3)i already tried using switch statement but i get problems when i use switch like this

case (A==0 && B==1): it gives error....i have no idea why.

i mean can we use more than 1 condition for each case?? example:

case (A && B && C): etc...

#### felis

##### New Member
2)how to configure only pin a0 and a1 as inputs?? issit trisa.f0=1 and trisa.f1=1
This will work. Also, you can do it in one instruction:

Code:
TRISA |= 0x03;
Switch cases should be static. You have four states of PORTA inputs - 0, 1, 2, 3. Declare a variable, say, tmp. Then:

Code:
tmp = PORTA & 0xfc; //pins 0,1, the rest is zero
switch( tmp ) {
case( 0 ):
...
case( 1 ):
...
or something like that.

Status
Not open for further replies.