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.

Switch Statement for lighting up LED's connected to a PIC16F877A

Status
Not open for further replies.

Thunderhorse312

Member level 1
Joined
May 7, 2012
Messages
32
Helped
0
Reputation
0
Reaction score
1
Trophy points
1,286
Activity points
1,526
Hi, I'm trying to light LED's using pic microcontroller and i wanted to use the switch statement to do so. My program starts with an analog input voltage coming from RA0 which i would use as an ADC terminal. I wanted that for some voltage inputs the LED's connected to RB0 - RB6 would light up. However, when i simulated the program using proteus there were no output. I used switch instead of if-else because of the break.

this is my code for the program
Code:
#include <htc.h>
#include <pic16f877a.h>
#include "delay.c"
#define _XTAL_FREQ 20000000
__CONFIG(0x3FFE);


unsigned int ADCRead(unsigned char channel)
{
	
		

	unsigned char l_byte, h_byte;		//Variables in exchange for ADRESL and ADRESH
	unsigned int ADR;					//stores the Analog to Digital input value

	
	
	ADCON0 = 0x81;						//sets what channel to be used for analog input
	DelayUs(50);						//acquistion time delay
	GO_DONE = 1;						//start the A/D conversion

	while(GO_DONE == 1); 				//wait for the bit to be cleared

	l_byte = ADRESL;					//set l_byte as ADRESL
	h_byte = ADRESH;					//set h_byte as ADRESH

	ADR = (h_byte << 8)|l_byte;			//shift ADRESH 8 places to the left and be ORed with ADRESL


	return ADR;

}


//unsigned int ReadADC()
//{
//	unsigned char l_byte, h_byte;
//	unsigned int ADR;
//	l_byte = ADRESL;
//	h_byte = ADRESH;
//	ADR = (h_byte <<8)|l_byte;
//	return ADR;
//}	


	void main(void)
		{
			#define Weight25 469
			#define Weight30 474
			#define Weight31 475
			#define Weight35 479
			#define Weight36 480
			#define Weight40 484
			#define Weight41 485
			#define Weight46 490
			#define Weight47 491
			#define Weight52 496
			#define Weight53 497
			#define Weight58 502
			#define Weight59 503
			#define Weight64 508
		    #define Weight65 509
			#define Weight70 514
			#define Weight71 515
			#define Weight76 520
			#define Weight77 521
			#define Weight82 526
			#define Weight83 527
			#define Weight88 532
			#define Weight89 533
			#define Weight94 538
		//	#define Weight95 539
		//	#define Weight100 544
			unsigned int Ch0,i;		//initialize variable for Analog input voltage

			CMCON = 7;				//disable comparator
			PORTD = 0;
			TRISD = 0;
			PORTB = 0;
			TRISB = 0;

			ADCON0 = 0x81;
			ADCON1 = 0xC0;
		
		while(1)
			{
			Ch0 = ADCRead(0);
		
		switch (Ch0)
				{
				
		case 1: 
		if ( Ch0 >= Weight25 )
		{
			if ( Ch0 <= Weight30)
			{
				RB0 = 1;
			}
			else
			{
				RB0 = 0;
			}
		
		}
				else
		{
			RB0 = 0;
		}

		break;	


}
}
return;

}

i only wrote the first option but there would be 11 of them at the end.. is my code correct?

thanks!
 

No!

The 'case' must equate to a constant and is the value used to decide if that part of code is to be executed. Yours executes only if the value of Ch0 is 1 so any further checking of the vakue is pointless. You might be able to use:

case Weightxx: where xx is one of your declared values because they are constants.

Brian.
 
As Brian says, this is incorrect use of the case statement. It makes no sense to have an 'if' statement (that is trying to check for
a large range) embeddded in the case statement that can only check one value! You might as well delete the case statement.

An alternative method that would work with case statements, but might be a little restrictive (it won't work with the
particular weight values you have defined), is to switch on just a few of the higher bits of the variable containing the ADC value,
so that you're ignoring the least significant bits.
e.g. 'switch (ch & 0xf000)' or something..
Given the weight numbers you are using, I suspect this method is ruled out for you.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top