Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Switch high / Switch Low (was Pic microcontroller)

Status
Not open for further replies.

naim

New Member
Someone can give me an example of switch active low..and active high...used for pic 16f877a..
ex:when we use active high circuit,so what about the program???example would help..thanks.
 
Someone can give me an example of switch active low..and active high...used for pic 16f877a..
ex:when we use active high circuit,so what about the program???example would help..thanks.

The only difference is using either BTFSS or BTFSC accordingly - my tutorials show how to read switches.
 
can u give me an example of program using "counter"..
your tutorial very useful..thanks.
i want to make program use counter.
while(1)
ex.--->input->portb.c7--->>output-->portc=0b0000001... led on 5 sc
but for 2nd time
same port input..same port output but i want led on for 10 sec.
please..
 
Last edited:
Naim.. As I explained in the other thread.. You really don't need a counter for this kind of application you need to toggle a byte value for subsequent reads of the alarm switch.

ie..

x = 0
if switch then x = 1
if switch then x = 2
if timeout then x = 0
if x then process LED

I need to know exactly what you are trying to do..

Cheers Ian
 
Last edited:
u know this Wednesday i must to present my final year project.but i think can't solve this problem.:-(
i dont now how to program it.
void main()
{
int i;

trisc=1; // set port as a input
trisb=0; //set port b as output


while(1) {
if (portc.f5) //read portc.f5 and switch as input button

{
delay_ms(2000); //two second delay
for(i=0;i<=10;i++) //use for to read funtion for ten times ex;if led it will blink ten time
{
portb=0b0000001; //port bo on -siren or buzzer is on
delay_ms(100); //one second delay
portb=0; //all of port b is off-alarm or buzzer is stop after sound for ten time.
delay_ms(1000) ; //one second delay
}}
else if (portc.f4) //read port.f4 and switch as input button

{
delay_ms(2000); //two second delay
for(i=0;i<=5;i++) // use for to read funtion for five times ex;if led it will blink five time
{
portb=0b0000001; //port bo is on -siren or buzzer will sound wiiuw..wiiuuww...
delay_ms(1000); //one second delay
portb=0; //all port b is turn off,buzzer and siren will stop sound
delay_ms(1000) ; //one second delay
}
}
else if (portc.f3) // read portc.f3 and switch as input button

{
delay_ms(2000); //two second delay for next operation
for(i=0;i<=10;i++) //use for to read funtion for ten times ex;if led it will blink ten time
{
portb=0b0000001; //port bo on -siren or buzzer is on
delay_ms(100); //one second delay
portb=0; //all of port b is off-alarm or buzzer is stop after sound for ten time.
delay_ms(1000) ;


}}
else if (portc.f2) // read portc.f2 and sensor as a switch

{
delay_ms(2000); //two second delay for next operation
for(i=0;i<=100;i++) //use for to read funtion for ten times ex;if led it will blink ten time
{
portb=0b1111111; //port bo on -siren or buzzer is on
delay_ms(100); //one second delay
portb=0; //all of port b is off-alarm or buzzer is stop after sound for ten time.
delay_ms(1000) ;
} } }}
above are my program.but for input portc.f2..i want a function like i said before.can u edit to me??
ok2.i can xplain it briefly...
1st...if portc.f2 got input..then the output is led blink 5sc
2nd time..the same port got input...but the i want the different output.mybe led blink 10sc
 
You will need to juggle this a bit, but this is how I would start

Code:
void main()
	{
	int i;
	unsigned char state, oldstate;

	trisc=1; // set port as a input
	trisb=0; //set port b as output


	while(1) 
		{
		state = (portb & 0x2C) >> 2; // we need  2,3,4,5 so put them in low nibble
		old state <<= 4; 			// added to oldstate
		oldstate += state;			// gives us 8 possible states needed
		// other states can be implemented as you need them
		
		switch(oldstate)
			{
			case 0x01:					// switch on f2
				output(1,5);
				break;
			case 0x02:					// switch on f3
				output(1,5);
				break;
			case 0x04:					// switch on f4
				output(1,5);
				break;
			case 0x08:					// switch on f5
				output(255,5);
				break;
			case 0x11:					// switch on f2 ( again )
				output(1,10);
				break;
			case 0x22:					// switch on f3 ( again )
				output(1,10);
				break;
			case 0x44:					// switch on f4 ( again )
				output(1,10);
				break;
			case 0x88:					// switch on f5 ( again )
				output(255,10);
				break;
				
			}
		}
	}
	
void output(char portval, char duration)
	{
	int i;	
	
	delay_ms(2000); 			//two second delay for next operation
	for(i=0;i <= duration;i++) 	//use for to read funtion for duration ex;if led it will blink duration
		{
		portb = portval;		//port bo on -siren or buzzer is on or LED's
		delay_ms(500);			//half second delay
		portb = 0; 				//all of port b is off-alarm or buzzer is stop after sound for ten time.
		delay_ms(500);			//half secon delay 
		}
		
	}

I havent checked this through but you should get the idea... it's the basis of a state machine.

Cheers Ian
 
Use the switch case statement it should give a idea

Code:
switch (expression) {
  case constant-expression_1 : statement_1;
    .
    .
    .
  case constant-expression_n : statement_n;
  [default : statement;]
}
 
Last edited:
output(1,10);------->
output(255,10);------>how to convert to be ex: portb=0b0000001

how to declare "old state"..
i can't compile it.....

.hehe---sorie for trubling u.
 
Last edited:
Ian Rogers how you post a switch case before me I sure didn't see it when I posted but thats what naim needs to get it going

Nice work Ian

Code:
unsigned char state, oldstate;
 
Last edited:
Naim! No the code should compile with both... The portb should be initialized to 0 just before the trisb..

In the function output it takes two arguments portb val and duration you can call it with a statement like this

Code:
output(%0000001, 10);

this will work for you.

I am going to run this on isis to make sure its ok.

Cheers Ian
 
I have just compiled this with Hi-tec in MPLAB.. I had to change the delay call as mine is slightly different.

Code:
void output(char portval, char duration);

void main()
	{
	int i;
	unsigned char state, oldstate;

	TRISC=255; // set port as a input
	TRISB=0; //set port b as output


	while(1) 
		{
		state = (PORTB & 0x2C) >> 2; // we need  2,3,4,5 so put them in low nibble
		oldstate <<= 4; 			// added to oldstate
		oldstate += state;			// gives us 8 possible states needed
		// other states can be implemented as you need them
		
		switch(oldstate)
			{
			case 0x01:					// switch on f2
				output(1,5);
				break;
			case 0x02:					// switch on f3
				output(1,5);
				break;
			case 0x04:					// switch on f4
				output(1,5);
				break;
			case 0x08:					// switch on f5
				output(255,5);
				break;
			case 0x11:					// switch on f2 ( again )
				output(1,10);
				break;
			case 0x22:					// switch on f3 ( again )
				output(1,10);
				break;
			case 0x44:					// switch on f4 ( again )
				output(1,10);
				break;
			case 0x88:					// switch on f5 ( again )
				output(255,10);
				break;
				
			}
		}
	}
	
void output(char portval, char duration)
	{
	int i;	
	
	DelayMs(2000); 			//two second delay for next operation
	for(i=0;i <= duration;i++) 	//use for to read funtion for duration ex;if led it will blink duration
		{
		PORTB = portval;		//port bo on -siren or buzzer is on or LED's
		DelayMs(500);			//half second delay
		PORTB = 0; 				//all of port b is off-alarm or buzzer is stop after sound for ten time.
		DelayMs(500);			//half secon delay 
		}
		
}

Cheers Ian (I had a space in the identifier oldstate, oops).
 
i have to compile it using mpLab??
DelayMs(500);-----can i make it like this----delay_ms(500)

is it same either we compile in micro c or Mplab??
is it can become a same thing??
 
Last edited:
I used DelayMs(500); because I haven't got a copy of your delay_ms() routine. This code is as close to ansi C as possible so it should compile on mikroC (the only differences will be PORTB "on hitec" and portb on mikroC),, either case you will see it in the compile errors.

Cheers Ian
 
Status
Not open for further replies.

Latest threads

Back
Top