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
RB0=70
RB1=63
RB2=56
RB3=49
RB4=42
RB5=35
RA2=28
RA3=21
RA6=14
RA7=7
so for example if the voltage is greater than 28 then RA7,RA6,RA3,RA2 would light up
Code:
//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
13
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
//*** INSERT YOUR PROGRAM CODE HERE TO ILLUMINATE THE LEDs***
//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); }