Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to show adc value in seven segment display using mikroc and proteus

Status
Not open for further replies.

khatus

Member level 3
Joined
Oct 7, 2017
Messages
56
Helped
1
Reputation
2
Reaction score
0
Trophy points
6
Activity points
441
Hello, Guys, I wanted to show adc value in 7 segments dynamic display. I am using ADC channel 1 for this purpose. But while simulating in the proteus it shows the following error.Also, some random digits show in the display. I can't figure out the error. Please update my code if possible .........

moderator action: removed link to external file servers

Here is my full code

Code:
/*
* 7-Segment common anode display for showing ADC value read from channe-1 of analog pin
* Programmer: SRikrishna Jana
* Target PIC: PIC16F877
* Frequency 8 Mhz
* Date: 5th august 2021
* Display on 3-digit 7 SEGMENT display
*/
unsigned char SEGMENT=0;
sbit C1 at RD0_bit; // C1--->C4 are for turnig on the common anode display
sbit C2 at RD1_bit; // If C1=1 display is on if C1=0  display is off
sbit C3 at RD2_bit; //Same rule follows C2,C3,C4
sbit C4 at RD3_bit;
//unsigned int value;
const unsigned char DriveSegment[10] =  { 192, 249, 164, 176, 153, 146, 130, 248, 128, 152 }; //Digit code for 0 to 9


void UpdateDisplay(unsigned int value)
{
     unsigned int SendVal = value;
     unsigned char Digit;
        SEGMENT++;
        if (SEGMENT > 4)
         SEGMENT = 1;
        C1 = 0; C2 = 0; C3 = 0,C4 =0; //Initially Displays are off
switch (SEGMENT){
               case 1:
                    Digit = (unsigned char) (SendVal / 1000); // Suppose ADC value= 1023 This gives you 1.
                    C1=1; //turn on the display
                    delay_ms(10);
                    C1=0; //turn off the display

                    break;
               case 2:
                    Digit = (unsigned char) ((SendVal / 100) % 10);//This gives you 0.
                    C2=1; //turn on the display
                    delay_ms(10);
                    C2=0; //turn off the display
                    break;
               case 3:
                    Digit = (unsigned char) ((SendVal / 10) % 100);//This gives you 2.
                    C3=1; //turn on the display
                    delay_ms(10);
                    C3=0;  //turn off the display
                    break;
               case 4:
                    Digit = (unsigned char) (SendVal % 10);//This gives you 3.
                    C4=1; //turn on the display
                    delay_ms(10);
                    C4=0;//turn off the display
                    break;
        }

        SendVal = DriveSegment[Digit] & 0xFF;
        PORTB = SendVal;

}


unsigned int  ADC_reading(void )
{
    unsigned int value_adc;
    ADC_Init();
    value_adc = ADC_Read(1);
    delay_us(10);
    return(value_adc);
}






void main()
{
unsigned int value;// variable for storing adc value
TRISB=0x00;// B0 to B6 declared as a output
PORTB = 0x00;//Initially PORTB is OFF
TRISD=0x00; // D0 to D3 declared as a output
PORTD = 0x00;// Initially PORTD is OFF
TRISA.F1 =1;
ADCON0.F5=0; //*For channel 1
ADCON0.F4=0; //*For channel 1
ADCON0.F3=1; //*For channel 1

while(1)
{
value = ADC_reading(void) ;//call the ADC_reading function for reading analog channel 1
UpdateDisplay(value);// pass the valor to UpdateDisplay function for showing to seven segment display
}
}
 
Last edited by a moderator:

This should be your complete homework assignment:
C:
/*
* File:   main.c
* Author: dan1138
* Target: PIC16F877
* Compiler: XC8 v2.32
* IDE: MPLABX v5.50
*
* Created on August 7, 2021, 11:06 PM
*
*                         PIC16F877
*                 +----------:_:----------+
*       VPP ->  1 : MCLR/VPP      PGD/RB7 : 40 <> PGD/seg_dp
*  Volts IN ->  2 : RA0/AN0       PGC/RB6 : 39 <> PGC/seg_g
*           <>  3 : RA1               RB5 : 38 <>    /seg_f
*           <>  4 : RA2               RB4 : 37 <>    /seg_e
*           <>  5 : RA3           PGM/RB3 : 36 <>    /seg_d
*           <>  6 : RA4               RB2 : 35 <>    /seg_c
*           <>  7 : RA5               RB1 : 34 <>    /seg_b
*           <>  8 : RE0               RB0 : 33 <>    /seg_a
*           <>  9 : RE1               VDD : 32 <- 5v0
*           <> 10 : RE2               VSS : 31 <- GND
*       5v0 -> 11 : VDD               RD7 : 30 <>
*       GND -> 12 : VSS               RD6 : 29 <>
*      8MHz -> 13 : OSC1              RD5 : 28 <>
* 1    8MHz <- 14 : OSC2              RD4 : 27 <>
*           <> 15 : RC0/SOSCO   RX/DT/RC7 : 26 <>
*           <> 16 : RC1/SOSCI   TX/CK/RC6 : 25 <>
*           <> 17 : RC2/CCP1          RC5 : 24 <>
*           <> 18 : RC3/SCL       SDA/RC4 : 23 <>
*   digit_1 <> 19 : RD0               RD3 : 22 <> digit_4
*   digit_2 <> 20 : RD1               RD2 : 21 <> digit_3
*                 +-----------------------:
*                          DIP-40
*
* Description:
*  Drive 4-digit LED 7-segment display with a
*  three decimal value from ADC reading the
*  voltage at AN0.
*
*  The display range is from 000 to 500 in
*  digits 1 to 3, digit 1 is the hundreds,
*  digit 2 is the tens and digit 3 is ones.
*/

#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF        // Low Voltage In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = ON         // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)

#define _XTAL_FREQ (8000000UL)

#include <xc.h>

/* LED is common anode so zero is segment on */
const char LEDDigit[] =
{
  /* gfedcba         _   */
  0b11000000,   /*  | |  */
                /*  |_|  */
                /*       */
  0b11111001,   /*    |  */
                /*    |  */
                /*   _   */
  0b10100100,   /*   _|  */
                /*  |_   */
                /*   _   */
  0b10110000,   /*   _|  */
                /*   _|  */
                /*       */
  0b10011001,   /*  |_|  */
                /*    |  */
                /*   _   */
  0b10010010,   /*  |_   */
                /*   _|  */
                /*   _   */
  0b10000010,   /*  |_   */
                /*  |_|  */
                /*   _   */
  0b11111000,   /*    |  */
                /*    |  */
                /*   _   */
  0b10000000,   /*  |_|  */
                /*  |_|  */
                /*   _   */
  0b10011000,   /*  |_|  */
                /*    |  */
                /*       */
  0b11111111    /* blank */
                /*       */
};
/*
* Global RAM
*/
unsigned char DigitSegments[4];
unsigned char DigitOn;
/*
* Initialize this PIC
*/
void PIC_Init(void)
{
    /* disable all interrupt sources */
    INTCON = 0x00;
    PIE1 = 0x00;
    PIE2 = 0x00;

    ADCON0 = 0xC1;          /* turn on ADC module */
                            /* TAD = ADFRC */
                            /* Select AN0 as input */
    ADCON1 = 0x8E;          /* set PORTA AN0 for analog input all others for digital I/O */
                            /* ADC results are right justified */
   
    TRISB = 0x00;
    TRISD = 0xF0;
   
    PORTB = 0xFF;
    PORTD = 0xF0;
   
    DigitOn = 0;
    DigitSegments[0] = LEDDigit[10];
    DigitSegments[1] = LEDDigit[10];
    DigitSegments[2] = LEDDigit[10];
    DigitSegments[3] = LEDDigit[10];
}
/*
* Main application
*/
#define ADC_VREF_cV (500) /* VREF in centivolts (1/100ths of a volt) */
unsigned long ADC_Raw;

void main(void)
{
    PIC_Init();
    /*
     * Application process loop
     */
    for(;;)
    {
        /* Turn on next LED digit */
        PORTB = 0xFF;      /* Turn off all segments */
        PORTD &= 0xF0;
        PORTD |= 0x0F & DigitOn;
        if(DigitOn & 1) PORTB = DigitSegments[0];
        if(DigitOn & 2) PORTB = DigitSegments[1];
        if(DigitOn & 4) PORTB = DigitSegments[2];
        if(DigitOn & 8) PORTB = DigitSegments[3];
        DigitOn <<= 1;

        /* Check of all four LED digits have been refreshed */
        if(!(DigitOn & 0x0F))
        {
            DigitOn = 1;
       
            /* Read ADC */
            ADCON0bits.GO_nDONE = 1;
            while(ADCON0bits.GO_nDONE){}
            ADC_Raw = ((unsigned short)(ADRESH)<<8) | ADRESL;

            /* Convert to decimal value in range from 000 to 500 */
            ADC_Raw *= ADC_VREF_cV;
            ADC_Raw += (1<<9);
            ADC_Raw >>= 10;

            /* Convert ADC decimal value to 7-segments */
            DigitSegments[3] = LEDDigit[ADC_Raw % 10];
            ADC_Raw /= 10;
            DigitSegments[2] = LEDDigit[ADC_Raw % 10];
            ADC_Raw /= 10;
            DigitSegments[1] = LEDDigit[ADC_Raw % 10];
        }
       
        /* Make process loop take at least one millisecond */
        __delay_ms(1);
    }
}
 
  • Like
Reactions: khatus

    khatus

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top