-1

Can someone please help me with this? I have been researching and trying to get this working, but I'm out of luck. All the codes I found online were not working... The output, for now, is 00, 11, 22, 33, ... FF and lop back to 00. How do I separate the first and second digit displays? Like I want it to display from 0 to 255 (00, 01, 02...FF)?

Requirements:

When the circuit is first energized, the seven-segment LEDs will start counting at 0x00.

The duel segment LEDs will count up to 0xFF, increasing by 1 each time. The count MUST BE sequential. It is unacceptable to count 0 to F to 0 on digit 2, then increase digit 1 by 1. The count shall perform like a counter (0x00 to 0x0F then 0x10 etc.).

Once the count reaches 0xFF, the count will start over at 0x00.

The code will contain sufficient delay between incrementing the count so that the count can be visually confirmed that the circuit/code is operating as designed.

The above will occur infinitely, another word, in an endless loop until the device is powered down.


Source code:

#include

void PORTA_init(void)
{
  PORTA = 0; // All PORTA Pins are low
  CMCON0 = 7; // Turn off Comparators
  ANSEL = 0; // Turn off ADC
  //TRISA = 0b001111; // RA4 and 5 are outputs; RA0,1,2, and 3 are input
  return;
}
/******** END OF PORTA_init ****************************/
/********************************************************

* Notes:
*
* Delay was determined through trial and error
*
* Returns: None
* ********************************************************/

/** Function: main *************************************
*
* Notes:
*
* RA4 - Positive LED Connection for D0
* RA5 - Negative LED Connection for D0
*
* Returns: None -- This routine contains an infinite loop
*
*/
// CONFIG --- Configuration Word --- START
#pragma config FOSC = INTOSCIO
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = OFF
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = OFF
#pragma config IESO = OFF
#pragma config FCMEN = OFF
// CONFIG --- Configuration Word --- END
int i, j;
int DisplayValue, DisplayLED;

//PLACE LEDDigit ARRAY HERE
const char LEDDigit[] = {
0b0000001,           //   "0"
0b1001111,           //   "1"
0b0010010,           //   "2"
0b0000110,           //   "3"
0b1001100,           //   "4"
0b0100100,           //   "5"
0b0100000,           //   "6"
0b0001111,           //   "7"
0b0000000,           //   "8"
0b0000100,           //   "9"
0b0001000,           //   "A"
0b1100000,           //   "b"
0b0110001,           //   "C"
0b1000010,           //   "d"
0b0110000,           //   "E"
0b0111000};           //   "F"


main()
{

  PORTA = 0;
  PORTC = 0;
  CMCON0 = 7;           //   Turn off Comparators
  ANSEL = 0;               //   Turn off ADC
  TRISA = 0b011100;
  TRISC = 0b000000;

  DisplayValue = 0;           //   Start Displaying at 0x00
  DisplayLED = 0;           //   Display the 1s first


  while(1 == 1)    //   Loop Forever
  {
    if (0 == DisplayLED)       //    True, then display right digit
    {
      RA5 = LEDDigit[DisplayValue & 0x0F] >> 6;
      //   Clears display bits 4 - 7 of DisplayValue,
      //   then selects bit 7 of LEDDigit

      PORTC = LEDDigit[DisplayValue & 0x0F] & 0x03F;
      //   clears display bits 4 - 7 of DisplayValue,
      //   then selects bits 0 - 6 of LEDDigit
    }
    else
    {
      RA5 = LEDDigit[(DisplayValue >> 4) & 0x0F] >> 6;
      PORTC = LEDDigit[(DisplayValue >> 4) & 0x0F] & 0x03F;
    } //
    TRISA = TRISA ^ 0b011111;   //   Swap Left/Right
    PORTA = PORTA & 0b111100;   //   Make Sure Bits are Low
    DisplayLED = DisplayLED ^ 1;   //   Other Digit Next

    NOP();               //   Used for 10 ms Timing
    for (i = 0; i < 660; i++);   //   10 ms Delay Loop
    NOP();               //   Used for 10 ms Timing

    j = j + 1;           //   Increment the Counter?
    if (25 == j)           //   1/4 Second Passed?
    {
      DisplayValue++;       //   Increment the Counter

      j = 0;          //   Reset for another 1/4 Second
    }                 //
  }                   //
}                     //

7-Segment Circuit Schematic

slebetman
  • 109,858
  • 19
  • 140
  • 171
  • 1
    You find the data-sheet for the device you're using. They can be different from device to device, as can a number of other electrical parameters. It's a bit like asking how to give your wife what she wants when we know nothing of her other than her relationship to you. Without specifics, everybody's wasting their time. – enhzflep Jul 07 '19 at 04:54
  • 1) Get the data sheet for the display 2) Get the data sheet for your board, i.e. how is the display connected to your processor 3) Get the data sheet for your processor to find out how to control the pins connected to the display 4) Make a simple 256 entry translation table that translates numbers into display control codes 5) your homework is done – Support Ukraine Jul 07 '19 at 06:09
  • Please show the code for displaying "88" as something of a [mcve] (though there is no error yet. It is probably hat you have been given as a "HelloWorld" for your environment. Then describe what value you have to write for making the 7segs to show "02". Play with the values to find out. Once you spotted the pattern it is down to getting the required counter behaviour. If you show the code for making some sample values you might get more help here. – Yunnosch Jul 07 '19 at 06:41
  • You do not appear to modify `RA0` or `RA1`. If the text in blue means anything, you probably have to set these correctly to choose which half of the display is selected for modification. –  Jul 08 '19 at 00:34

1 Answers1

0

This is a possible answer to your homework assignment:

/*
 * File:   main.c
 * Author: dan1138
 * Target: PIC16F688
 * Compiler: XC8 v2.00
 *
 *                       PIC16F688
 *             +------------:_:------------+
 *    GND -> 1 : VDD                   VSS : 14 <- 5v0
 * SEG_An <> 2 : RA5/T1CKI     PGD/AN0/RA0 : 13 <> PGD DIGIT_1n
 *        <> 3 : RA4/AN3       PGC/AN1/RA1 : 12 <> PGC DIGIT_2n
 *    VPP -> 4 : RA3/VPP           AN2/RA2 : 11 <>
 * SEG_Bn <> 5 : RC5/RXD           AN4/RC0 : 10 <> SEG_Gn
 * SEG_Cn <> 6 : RC4/TXD           AN5/RC1 : 9  <> SEG_Fn
 * SEG_Dn <> 7 : RC3/AN7           AN6 RC2 : 8  <> SEG_En
 *             +---------------------------:
 *                        DIP-14
 *
 * Created on July 7, 2019, 6:56 PM
 */

#pragma config FOSC = INTOSCIO
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = OFF
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = OFF
#pragma config IESO = OFF
#pragma config FCMEN = OFF

#include <xc.h>
#include <stdint.h>

#define _XTAL_FREQ (8000000ul)

const char LEDDigit[] = 
{ 
  /* abcdefg         _   */
  0b00000001,   /*  | |  */
                /*  |_|  */
                /*       */
  0b01001111,   /*    |  */
                /*    |  */
                /*   _   */
  0b00010010,   /*   _|  */
                /*  |_   */
                /*   _   */
  0b00000110,   /*   _|  */
                /*   _|  */
                /*       */
  0b01001100,   /*  |_|  */
                /*    |  */
                /*   _   */
  0b00100100,   /*  |_   */
                /*   _|  */
                /*   _   */
  0b00100000,   /*  |_   */
                /*  |_|  */
                /*   _   */
  0b00001111,   /*    |  */
                /*    |  */
                /*   _   */
  0b00000000,   /*  |_|  */
                /*  |_|  */
                /*   _   */
  0b00001100,   /*  |_|  */
                /*    |  */
                /*   _   */
  0b00001000,   /*  |_|  */
                /*  | |  */
                /*       */
  0b01100000,   /*  |_   */
                /*  |_|  */
                /*   _   */
  0b00110001,   /*  |    */
                /*  |_   */
                /*       */
  0b01000010,   /*   _|  */
                /*  |_|  */
                /*   _   */
  0b00110000,   /*  |_   */
                /*  |_   */
                /*   _   */
  0b00111000,   /*  |_   */
                /*  |    */
                /*       */
  0b01111111,   /* blank */
                /*       */
};
volatile uint8_t Digit1Segments;
volatile uint8_t Digit2Segments;

volatile uint8_t Timer0Ticks;

void __interrupt() ISR_handler(void)
{
    if (TMR0IE && TMR0IF) {  /* TIMER0 asserts and interrupt every 1.024 milliseconds */
        TMR0IF=0;
        Timer0Ticks++;
        if ((Timer0Ticks & 0x0F) == 0) { /* every 16.384 drive a new digit */
            if ((TRISA & 3) == 2) {
                TRISA |= 3;     /* Turn off all digit drivers */
                PORTA = 0;
                if ((Digit2Segments & (1<<6)) != 0 ) {
                    PORTA = (1<<5);
                }
                PORTC = Digit2Segments;
                /* Drive digit 2 segments */
                TRISA &= ~2;
            }
            else { 
                TRISA |= 3;
                PORTA = 0;
                if ((Digit1Segments & (1<<6)) != 0 ) {
                    PORTA = (1<<5);
                }
                PORTC = Digit1Segments;
                /* Drive digit 1 segments */
                TRISA &= ~1;
            }
        }
    }
}

void main(void) {
    uint8_t HexCount;
    /*
     * Initialize this PIC
     */
    INTCON = 0;
    OSCCON = 0x70;      /* Select 8MHz system oscillator */
    __delay_ms(500);    /* Give ICSP device programming tool a chance to get the PICs attention */

    Digit1Segments = 0b01111111;
    Digit2Segments = 0b01111111;
    TRISA = 0xDF;   /* PORTA bit 5 needs to be an output */
    TRISC = 0x00;
    ANSEL  = 0;
    OPTION_REG = 0b11000010; /* TIMER0 clock = FOSC/4, prescale 1:8 */
    PORTA = 0;
    PORTC = 0;
    CMCON0 = 7;
    TMR0 = 0;
    TMR0IF = 0;
    TMR0IE = 1;
    GIE = 1;
    /*
     * This is the application loop.
     * 
     * It counts up one count about every second.
     */
    HexCount = 0;
    while(1)
    {
        Digit1Segments = LEDDigit[HexCount & 0x0F];
        Digit2Segments = LEDDigit[(HexCount>>4) & 0x0F];
        __delay_ms(1000);
        HexCount++;
    }
}

Only checked using the MPLABX simulator so it may not work in real hardware or in a Proteus simulator.

Warning: I have used concepts that may not have been covered, so if this code works and you hand it in your instructor will know you got it from the internet.

Dan1138
  • 1,150
  • 8
  • 11
  • Thank you! It's counting definitely, but the right digit's display is upside down. I will study your code and try to figure out how to fix it. – NotSoTechnical Jul 08 '19 at 04:36
  • That seems strange. I would expect both digits to behave the same. Can you post the complete part number of the 7-segment display module you are using? – Dan1138 Jul 08 '19 at 04:45
  • https://www.elexp.com/ProductDetails.aspx?item_no=08TIE524&CatId=8dff089f-cef4-487e-85fd-3caedcbd00c7 – NotSoTechnical Jul 08 '19 at 05:29
  • I fixed a bug in my code where PORTA bit 5 was not setup as an output. You may have wired the segments of the right digit differently than the left digit. Perhaps if you post a photo of what you are seeing I could guess at what may be wrong, – Dan1138 Jul 08 '19 at 05:48
  • Sorry. I realized it wasn't upside down, but two segments are off. It was able to display 88, so it shouldn't be a hardware issue. it should be 03 but it displays this instead: Photo:https://photos.app.goo.gl/nGqYXhwCNyciMhpJ7 – NotSoTechnical Jul 08 '19 at 06:16
  • That is the bug in my code with PORTA bit 5. See line 137 and change it to be: TRISA = 0xDF; – Dan1138 Jul 08 '19 at 06:31
  • Hi Dan. I have been trying to study your code, but it's too hard. Here is what I'm trying to do, but I really don't know what I should fix anymore. Could you take a look at it? https://drive.google.com/file/d/1mBBzu2C_zeTY0ai8Is71zNM0T2hZo1Xn/view?usp=sharing – NotSoTechnical Jul 09 '19 at 07:25
  • I have looked at your code and it seems to me that what you need to fix is your experience level. How much time have you been really spending on trying to understand the concepts? The code shows that your comprehension is weak. What you need to know is elementary but difficult for you to learn. So I ask you do you want to get good at this kind of coding or is this an assignment you need to complete to move on to the next class? I can fix your code for you but you will not really learn anything. Just tell me what you want. – Dan1138 Jul 09 '19 at 16:51
  • By the way, I'm in electrical engineering major for 1.5 years. I experienced more with circuit design and power engineering in the early years. I'm very new to MPLab – NotSoTechnical Jul 09 '19 at 17:37
  • There are at least 3 bugs in your code that are so critical that any one will cause the application to fail to run. There are no simple one or two line changes that will fix your code. This code needs to be rewritten using a detailed implementation specification. An implementation specification is a document that the developer that writes the code creates in English (or their native language) that fully describes each functional state of the application. It is vital that this document is not written in computer code. We used to call this a flow chart. – Dan1138 Jul 09 '19 at 19:35
  • So as an exercise please write the implementation specification that describes how the code you posted actually works. Doing this will give you the experience you need to not only userstand your code but also comprehend it. We can do this together if you like. – Dan1138 Jul 09 '19 at 19:35
  • I tried to comment out some lines of the source code to see what each line/condition does to the output. The output always displayed 00 on the seven segments before Displayvalue++. So I thought maybe I could work from there. – NotSoTechnical Jul 09 '19 at 20:18
  • With the utmost respect I suggest that you stop hacking and start designing. So take a giant step back and write, using simple sentences in English, how this needs to be done. This is boring tedious work but it makes the coding so much easier. This is an experience you need to learn how to do. It will help you solve many other kinds of problems beyond writing code. – Dan1138 Jul 09 '19 at 21:23
  • so professor just posted two minor modifications to the source code, and it's working... Would you like to see it? – NotSoTechnical Jul 10 '19 at 16:31
  • Yes I would like to see what changed. – Dan1138 Jul 10 '19 at 16:39
  • TRISA = 0b011101; and TRISA = TRISA ^ 0b000011; I would never figure this out... https://drive.google.com/file/d/1kgvBW3Y44v_UQI5kFUpxsQUoGBECXSmh/view?usp=sharing – NotSoTechnical Jul 10 '19 at 20:45
  • Yes, your instructor is being very tricky. There is a subtle flaw in this implementation. The method used will display what called "segment ghosting". Segment ghosting occurs when the segment drives are changed for the next digit while the digit driver for the last digit is active. This is more visible when using high brightness LEDs. Maybe you could ask your instructor what to do about segment ghosting? The solution is to turn off all digit drivers before changing the segment drivers. – Dan1138 Jul 10 '19 at 22:11
  • There is another fault that your instructors code can have. If any glitch happens that caused both TRIS bits for the digit driver GPIO pins to be the same value then both LED digits will show the same segments. – Dan1138 Jul 10 '19 at 22:17