using adc to light multiple led

Thread Starter

damilarem

Joined Aug 5, 2017
41
i want to use adc of 16f877a to on led from the begining to the end .... b1-c1
this is my program help will be appreciated


C:
//[cc lang=”C”]
unsigned int adc =0; // Variable to save ADC value
const unsigned short VREF = 5;
int val;
void main() // Start of Main function
{
CMCON = 0x07; // Disable Comparators
ADCON1 = 0x80; // For ADC Module configuration
TRISA = 0xFF; // PORTA is input (ADC Input)
TRISB0_bit = 0x00;
TRISB1_bit = 0x00; // PORTB is output
TRISB2_bit = 0x00;
TRISB3_bit = 0x00;
TRISB4_bit = 0x00;
TRISB5_bit = 0x00;
TRISB6_bit = 0x00;
TRISB7_bit = 0x00;

TRISC0_bit = 0x00; // PORTC is Output
TRISC1_bit = 0x00;
do
{
adc = ADC_Read(1); // Get 10-bit results of AD conversion of channel 1
  val =(adc * VREF )/1024;
TRISB0_bit= val =1 ; // Send lower 8 bits to PORTB
TRISC0_bit = val =2; // PORTC is Output
TRISC1_bit =val =3; // Send 2 most significant bits to RC7, RC6
TRISB1_bit =val =4; // PORTB is output
TRISB2_bit = val =5;
TRISB3_bit = val =6;
TRISB4_bit = val =7;
TRISB5_bit = val =8;
TRISB6_bit = val =9;
TRISB7_bit = val =10;

}
while(1);
}
 
Last edited by a moderator:

Thread Starter

damilarem

Joined Aug 5, 2017
41
I used potentiometer ..... connect it to adc an0 and d other pins to positive and negative part...... the output is to on led sequentially
 

ebeowulf17

Joined Aug 12, 2014
3,307
I used potentiometer ..... connect it to adc an0 and d other pins to positive and negative part...... the output is to on led sequentially
You've asked for help, but you haven't described a problem. Have you tried your code yet? Is something not working?

If you haven't tried it yet, is it because there are portions of code you don't know how to write? If so, tell us what you're struggling with.

If there's any chance at all that you're having problems related to the circuit, not the code, then you should post a schematic showing what you've got so far.
 

philba

Joined Aug 17, 2017
959
Here are questions to ask and answer yourself - What is the value of val at the bottom of the loop? And how do you set a bit to high in a PIC port? My PIC knowledge is a bit rusty but I'm sure answering those questions will get you closer.
 

Thread Starter

damilarem

Joined Aug 5, 2017
41
my volatge is 5v.... i want the 10 leds to on sequentially when the voltage is 0.5, the portc.f1 should come on when it is 1, the portc.f0 should come on without turning on the first one then when 1.5volt, portb.f7 should come on without turning of the rest of the light and so on and so forth.... but when reducing the voltage through the pot.. it will go off from the top to the botttom also.. here is my diagram... the code is not working for it.. i have tried it, though am not good with codes
 

Attachments

Last edited:

Thread Starter

damilarem

Joined Aug 5, 2017
41
sure no problem... when it was got working, it just increase current as the resistance is increased and it switches on the leds one after the other


i have sent the connections..
here is the code

/*this code was written by damilarem just to make increase in resistance turn on led sequencially
from one port to the other ..
using pic 16f877a..
the adc is 5v.. but if to use 2.5v we use voltage divider

*/
unsigned int adc; // declearing the variable
/******************************************************************************************************
main program
*****************************************************************************************************/
void main()
{

CMCON=0X07;
ADCON1 = 0x80;
TRISA = 0xFF; // PORTA is input
ADRESH=0X80;
TRISC = 0x00; // Pins RC7, RC6 are outputs
TRISB = 0; // PORTB is output
do
{
adc = ADC_Read(0); // Get 10-bit results of AD conversion
//of channel 1
PORTB.F0 = (adc >100); // make a single ports to come on after comparision
PORTB.F1 = (adc > 200); //
PORTB.F2 = (adc > 300) ;
PORTB.F3 = (adc > 400) ;
PORTB.F4 = (adc > 500) ;
PORTB.F5 = (adc > 600) ;
PORTB.F6 = (adc > 700) ;
PORTB.F7 = (adc > 800) ;
PORTC.F0 = (adc > 900) ;
PORTC.F1 = (adc > 1000) ;
} while(1);
}
 
Top