Shop OBEX P1 Docs P2 Docs Learn Events
Display voltage on 7 segment display — Parallax Forums

Display voltage on 7 segment display

Hello, I am trying to display the analog to digital voltage from an activity board on a seven segment display. All I have been able to do so far is display the voltage on the serial terminal. I haven't been able to get it to display on the seven segment display. I think the code I have is the problem but if anyone knows the correct wiring for the display that would be good to confirm as well.

Thanks for the replies.


#include "simpletools.h" // Include simpletools
#include "adcDCpropab.h" // Include adcDCpropab
int main() // main function
{
adc_init(21, 20, 19, 18); // CS=21, SCL=20, DO=19, DI=18
float v2, v3; // Voltage variables
while(1) // Loop repeats indefinitely
{
v2 = adc_volts(2); // Check A/D 2
set_directions(0, 3, v2); // A/D 0...A/D 3 -> output
putChar(HOME); // Cursor -> top-left "home"
print("A/D2 = %f V%c\n", v2, CLREOL); // Display volts
set_outputs(0, 3, v2); // 0 -> 7-segment display
set_outputs(0, 3, v2); // set v2 high
pause(100); // Wait 1/10 s
}
}

Comments

  • I just put a multimeter on the resistor going from the analog to digital pin and it doesn't have any voltage. What would be the code statement to have a pin on an Activity board output a voltage from the analog to digital pin?
  • I'm not sure if you are already working with some of the SimpleIDE learn examples for the Activity board. There are examples of reading the ADC and also working with 7 segment displays.

    The following link (7 segment Learn tutorial) will show you how to wire a 7 segment display and suggest how to use the ADC Measure Volts example to make a program display the voltage on 7 segment displays.



  • Yes, that is the example I am working from. The "your turn" part is where I am a little confused. Should the display be wired the same as the example they give? And what should my code look like to have the voltage be displayed? I have read all the examples they give and I just need some clarification about how to proceed.

    Thanks
  • The 7 segment display can be wired as shown in the 7 segment tutorial if you are using a common-cathode 7 segment display. You can test the 7 segment display using the code in the "Try this" section of the tutorial.

    In order to display the ADC voltage you will need to determine how you want to display the information. If you have only a single 7 segment display you can only display a single digit of the voltage (at a time). You could look into using the 'round()' function to take the ADC voltage and round it to the nearest integer.
    For example:
    set_outputs(15, 8, n[round(v3)])
    

    This will have the 7 segment display, display the integer part of the ADC voltage as a digit. If you don't want the voltage rounded, you will need to use something like 'int()' to obtain the integer value without rounding.

    If you want to display the other digits of the ADC voltage you will need to "parse" the ADC voltage value to obtain each digit you want to display. The "parsing" could be accomplished by multiplying the ADC voltage by 10 and then subtracting 10 to get the tenths digit. If you do that in a loop you could get the digit for each successive fractional value.
  • Hey, Francis, I am only using a single display so the round command will work. Do you have an example of a statement that would go before set_outputs(15, 8, n[round(v3)])? I have tried a couple of statement that very from the measured voltage page and haven't come up with something that works.

    Thanks
  • Here is what I have so far. I found the second display so ill use that without the round function. I think the program is telling just the second pin to go high based on what the display is doing. Please change as necessary.


    #include "simpletools.h" // Include simpletools
    #include "adcDCpropab.h" // Include adcDCpropab

    int main() // Main function
    {
    adc_init(21, 20, 19, 18); // CS=21, SCL=20, DO=19, DI=18

    float v2, v3; // Voltage variables

    while(1) // Loop repeats indefinitely
    {
    v2 = adc_volts(2); // Check A/D 2
    v3 = adc_volts(3); // Check A/D 3
    set_directions(15, 8, 0b11111111); // P15...P8 -> output
    set_outputs(15, 8, (v3)); // v3
    pause(500);
    }
    }
  • sokin,

    It turns out the round() function isn't completely implemented in the SimpleIDE environment.

    Here is my program that I created and tested on my Propeller Activity Board using a common cathode 7 segment LED display and potentiometer as shown in the two learn examples.

    My program will display all the significant digits of the measured voltage one digit at a time on the 7 segment LED display. The float2string function (simpletext.h) in the code is another possible way of dealing with numerical values by putting them into a string, which could then be used or displayed.

    Take a look at my code and download the attached zip file.

    NOTE: To get posted code to look like the following in the forum, use the "C" <code> format option that is in the editor toolbar.
    /*
      File: DisplayADCvoltageLED.c
      Modified: 04/21/2017
      Programmer: Francis E. Bauer
      
      Make voltmeter style measurements with the Propeller Activity Board and
      display digits on a 7 segment (common cathode) LED display.
      
      See the following pages for more information:
      
      http://learn.parallax.com/propeller-c-simple-circuits/measure-volts
      http://learn.parallax.com/propeller-c-simple-circuits/seven-segment-display 
    */
    #include "simpletools.h"                      // Include simple tools
    #include "simpletext.h"                       // Include simple text
    #include "adcDCpropab.h"                      // Include adcDCpropab
    
    void LED_init(void);
    void Blank_LED(void);
    void testLED(int,int);
    
    int n[] = {0b11100111, 0b10000100, 0b11010011,
               0b11010110, 0b10110100, 0b01110110,
               0b01110111, 0b11000100, 0b11110111,
               0b11110100};
               
     char *digits;
     char d[10];
     int FieldWidth, NumDigits;       
    
    int main()                                    // Main function
    {
      print("%c",CLS);                            // Clear Terminal screen
      testLED(1, 200);
      
      adc_init(21, 20, 19, 18);                   // CS=21, SCL=20, DO=19, DI=18
    
      float v1, v2, v3, v4;                       // Voltage variables
      int volt;
    
      while(1)                                    // Loop repeats indefinitely
      {
        v2 = adc_volts(2);                        // Check A/D 2                
        v3 = adc_volts(3);                        // Check A/D 3
        
        putChar(HOME);                            // Cursor -> top-left "home"
        print("A/D2    = %f V%c\n", v2, CLREOL);  // Display volts
        print("A/D3    = %f V%c\n", v3, CLREOL);  // Display volts
        print("int(D3) = %d\n",(int) v3);
        
        FieldWidth = 7;                           // String field width
        NumDigits = 6;                            // Number of digits to the right of decimal point
        
        // You can also use the float2string function to convert value to a string
        // and then parse/display the string.
        digits = float2string (v3, d, FieldWidth, NumDigits);
        print("String  = %s\n", digits);
        
        // Display each digit on 7 Segment LED display
        set_outputs(15, 8, n[(int) (v3)]);        // Display digit
        set_outputs(11, 11, 1);                   // Display decimal point
        pause(1000);
        v4 = v3;
        for (int i = 0; i <=5; ++i)               // Loop through each digit
        { 
          volt = (int) v4;                        // Get integer value
          v1 = (v4 - volt) * 10;                  // (Subtract integer value) * 10
          set_outputs(15, 8, n[(int) v1]);        // Display digit
          v4 = v1;                                // Keep remaining value
          pause(1000);   
          Blank_LED();                            // Blank LED display
          pause(100);
        }    
        pause(100);                               // Wait 1/10 second
      }  
    }
    
    void LED_init()
    { // Setup pins for LED display as outputs.
    
      set_directions(15, 8, 0b11111111);          // P15...P8 -> output
    }
    
    void Blank_LED()
    { // Turn-off all LED segments.
      set_outputs(15, 8, 0);
    }  
    
    void testLED(int Count, int PauseTime)
    { // Cycle through the 0 - 9 digits of the LED.
    
      LED_init();
      
      for (int Loop = 1; Loop <= Count; ++Loop)
      {
        print("Loop: #%d\n", Loop);
        for (int i = 9; i >= 0; --i)
        {
          set_outputs(15, 8, n[i]);
          print(" n[%d] displayed.\n", i);
          pause(PauseTime);
        }  
      }
      print("%c",CLS);                            // Clear Terminal screen
      Blank_LED();                                // Blank LED display   
    }
    
  • Francis,

    This helps me so much. Thank you for all of your input.
Sign In or Register to comment.