Thunderhorse312
Member level 1
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
i only wrote the first option but there would be 11 of them at the end.. is my code correct?
thanks!
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!