This Analog data is read and the number 1-5 is shown on the 7 segment common anode display with 7447, but I can’t set this number for analog value because I can’t show analog values or output I wrote this code in MPLAB X IDE.
#include <xc.h>
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // GP3/MCLR pin function select (GP3/MCLR pin function is MCLR)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF
#define _XTAL_FREQ 4000000
#define HIGH 1
#define LOW 0
unsigned int adc_value=0;
void main(void) {
TRISIObits.TRISIO0 = 1;
TRISIObits.TRISIO1 = 0;
TRISIObits.TRISIO2 = 0;
TRISIObits.TRISIO4 = 0;
TRISIObits.TRISIO5 = 0;
ANSELbits.ADCS2 = 0;
ANSELbits.ADCS1 = 0;
ANSELbits.ADCS0 = 0;
ANSELbits.ANS0 = 1;
ADCON0bits.CHS0 = 0;
ADCON0bits.CHS1 = 0;
ADCON0bits.ADON = 1;
ADCON0bits.GO = 1;
ADCON0bits.ADFM = 1;
while(1) {
ADON = 1;
GO_DONE = 1;
while(GO_DONE);
adc_value = (((unsigned int) ADRESH << 8) + ADRESL);
ADON = 0;
if (adc_value >= 0 && adc_value <= 200) {
GP4 = 0;
GP5 = 0;
GP2 = 0;
GP1 = 0;
} else if (adc_value > 200 && adc_value <= 400) {
GP4 = 0;
GP5 = 0;
GP2 = 0;
GP1 = 1;
} else if (adc_value > 400 && adc_value <= 600) {
TRISIObits.TRISIO4 = 0;
TRISIObits.TRISIO5 = 0;
TRISIObits.TRISIO2 = 1;
TRISIObits.TRISIO1 = 0;
} else if (adc_value > 600 && adc_value <= 800) {
TRISIObits.TRISIO4 = 0;
TRISIObits.TRISIO5 = 0;
TRISIObits.TRISIO2 = 1;
TRISIObits.TRISIO1 = 1;
} else if (adc_value > 800 && adc_value <= 1023) {
TRISIObits.TRISIO4 = 0;
TRISIObits.TRISIO5 = 1;
TRISIObits.TRISIO2 = 0;
TRISIObits.TRISIO1 = 0;
} else {
TRISIObits.TRISIO4 = 1;
TRISIObits.TRISIO5 = 1;
TRISIObits.TRISIO2 = 1;
TRISIObits.TRISIO1 = 1;
}
__delay_ms(100);
}
return;
}
my aim in this project is to print the number between 0-5 on the 7 segment common anode display according to the analog data between 0-1023, but I could not solve this problem.