Thread: PIC microcontroller LED display sound level meter

  1. #1
    Registered User
    Join Date
    Nov 2017

    PIC microcontroller LED display sound level meter

    okay so I have this code so far, how do I program it so it lights up LEDS depending on the voltage level? if;

    5v=1024 bits
    0.34v=70 bits

    so for example if the voltage is greater than 28 then RA7,RA6,RA3,RA2 would light up

    //16F819 PIC software to drive a 10-LED bargraph display from a dc level on ADC input 0 (AN0).
     //Port A and B used to drive LEDs. 
    //Pin Configuration:
     //Pin Configuration:
     //1-RA2 (LED4)   18-NC 
    //2-RA3 (LED3)   17-AN0 (DC Sound Level input)
     //3-NC     16-RA7 (LED1)
     //4-MCLR    15-RA6 (LED2) 
    //5-VSS    14-VDD
     //6-RB0 (LED10)   13-RB7 (PGD)
     //7-RB1 (LED9)   12-RB6 (PGC)
     //8-RB2 (LED8)   11-RB5 (LED5)
     //9-RB3 (LED7)   10-RB4 (LED6) 
    #include <xc.h>               //header file for device
     #include <stdint.h>         //header file for standard types e.g uint8_t 
    //fuse settings to configure device 
    //i.e. NOWDT - No Watchdog Timer, INTOSCIO - Internal clock used, pins available for I/O #pragma config MCLRE = ON, CP = OFF, CPD = OFF, BOREN = OFF, WDTE = OFF #pragma config PWRTE = OFF, FOSC = INTOSCIO, LVP = OFF, DEBUG = ON 
    void main() {     //*** INSERT ANY VARIABLE DECLARATIONS HERE ***     //uint8_t = 8-bit unsigned number     //unint16_t = 16-bit unsigned number     uint16_t value; 
        //*** The following code initializes the PIC ***     OSCCONbits.IRCF = 0b111;    
        //use internal 8MHz clock (FOSC=8MHz) 
        TRISB = 0b00000000;             //Port B all outputs     TRISA = 0b00110011;             //Port A B6/B7/B3/B2 outputs 
        ADCON1bits.ADFM = 1;       
         //A/D Result Format Right Justified     ADCON1bits.ADCS2 = 1;        
       //A/D clock Source divided by 2     ADCON1bits.PCFG = 0b1110;     
      //Enable AN0 input for sound level ADC 
     ADCON0bits.ADCS = 0b01;         //set ADC clock, should be between 1.6us and 6.4us (1/8MHz x 16 = 2us)   
    ADCON0bits.CHS = 0b000;         //select AN0 for input    
     ADCON0bits.ADON = 1;            //A/D Converter is operating     
        do     {     //The code below will read the digital value from the 10-bit analogue to digital converter.  
                       //The range of the return value will be between 0 and 1023. Where 0V = 0 and 5V = 1023.   
                       //For example 2.5V on the ADC will return 511 to the variable value below.    
     ADCON0bits.GO_nDONE = 1;        //start A/D conversion    
     while(ADCON0bits.GO_nDONE == 1);    //wait for A/D conversion to complete  
       value = ADRESH;                 //read MSB of ADC result   
      value = value << 8;             //shift left 8 bits     value = value + ADRESL;               
                                                //read LSB of ADC result. value now contains a 10-bit ADC number 
        //The PIC PORTA and PORTB registers should be used to output a value to the Port pins   
      //PORTA=0b00001111; will output a binary number to port A. Bits 7,6,5,4=0 and Bits 3,2,1,0=1.    
     //Alternatively PORTA=15; for decimal equivalent.    
     //The parameter may also be a variable instead of a constant e.g. PORTA=value; 
       } while (1); }
    Last edited by Santac; 12-05-2017 at 04:27 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    I suggest you do something on your own. If you want free code this is not a good site to get it.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jun 2011
    I would recommend a two step process. Starting with a value of zero:

    1. Find the highest bit that needs to be set (using basic algebra)
    2. Set all lower bits to one

    Since your output pins seem not to be sequential, you will need to do a bit of additional work to map the result to the physical pins.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sound meter
    By capuman in forum C Programming
    Replies: 3
    Last Post: 07-30-2013, 06:21 AM
  2. Replies: 6
    Last Post: 06-18-2013, 12:31 AM
  3. Replies: 3
    Last Post: 02-23-2012, 02:31 PM
  4. HTTP traffic meter
    By Carlos in forum Windows Programming
    Replies: 3
    Last Post: 10-02-2003, 04:27 PM
  5. Progress meter color
    By Echidna in forum Windows Programming
    Replies: 3
    Last Post: 11-29-2002, 03:30 AM

Tags for this Thread