PIC 16F88 project for birthday gift

Discussion in 'Embedded Systems and Microcontrollers' started by absf, Jun 25, 2018.

  1. absf

    Thread Starter Senior Member

    Dec 29, 2010
    1,850
    504
    I am doing this simple project to display some message on 2x 7 segment LED. But before I did that I decided to use a pic16f88 to display some counting values from 00 to FF in Hex. Once that works I'd continue to modify it to display text.

    The schematic looks as follows:
    my birthday project.PNG

    The software is attached below

    Code (C):
    1. #include <pic.h>
    2. /* c2x7seg.c
    3. roll through 16 digit on a dual 7 segment LED display
    4. Display HEX 16 digits on 2x CA LED display.
    5.  
    6. RB6    -    seg a
    7. RB5 -    seg b
    8. RB4 -    seg c
    9. RB3 -    seg d
    10. RB2 -    seg e
    11. RB1 -    seg f
    12. RB0 -    seg g
    13. RA0 -    Right 7 seg disp
    14. RA1 -    Left  7 seg disp
    15. */
    16. __CONFIG (WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & CCPRB3 & LVPDIS);
    17. //LVPDIS is needed so that RB3 will not be used by Low voltage programming to use it as I/O
    18. //pin and also be used by CCP1 control.**
    19. //see CONFIG word on datasheet.
    20.  
    21. unsigned int i,j;
    22. unsigned int disp_value, disp_LED;
    23.  
    24. const char LEDdigit []=
    25. {
    26. //abcdefg  -    LED segments
    27. 0b0000001,        //0
    28. 0b1001111,        //1
    29. 0b0010010,        //2
    30. 0b0000110,        //3
    31. 0b1001100,        //4
    32. 0b0100100,        //5
    33. 0b0100000,        //6
    34. 0b0001111,        //7
    35. 0b0000000,        //8
    36. 0b0000100,        //9
    37. 0b0001000,        //"A"
    38. 0b1100000,        //"b"
    39. 0b0110001,        //"C"
    40. 0b1000010,        //"d"
    41. 0b0110000,        //"E"
    42. 0b0111000};     //"F"
    43.  
    44. void main()
    45. {
    46. PORTA=0;
    47. PORTB=0;
    48. CMCON=7;        //TURN OFF COMPARATOR
    49. CCP1CON=0;        //disable CCP1 functions ON RB0 & RB3
    50. ANSEL=0;        //TURN OFF ADC
    51. TRISA=0b111110;
    52. TRISB=0b00000000;
    53. disp_value = 0x00;    //start disp at 0x00
    54. disp_LED = 0;        //disp the first digit
    55.  
    56. while (1==1)
    57. {
    58.     if (disp_LED == 0)
    59.     {
    60.     PORTB=LEDdigit[disp_value & 0x0F] & 0x7F;
    61.     }
    62.     else
    63.     {
    64.     PORTB=LEDdigit[(disp_value >> 4) & 0x0F] & 0x7F;
    65.     }    //fi
    66.  
    67.     TRISA = TRISA ^ 0b000011;    //XOR RA0 and RA1
    68.     PORTA = PORTA & 0b111100;  
    69.     disp_LED = disp_LED ^ 1;    //alternate btw first and second digit
    70.  
    71. NOP();        // subroutine for 10 ms delay
    72. for (i=0; i<660; i++);    //10ms delay
    73.  
    74. NOP();
    75.     j=j+1;
    76.     if (25 == j)    //display for 25 times
    77.     {
    78.     disp_value++;    //increment display value
    79.     j = 0;
    80.     }    //fi
    81. }        //end while
    82. }        //end c2x7seg
    83.  
    The problem is that the contents of both digits are the same all the time. It was supposed to display HEX from "00" to "FF"

    Any idea what's wrong with the program?

    Allen
     
  2. MaxHeadRoom

    Expert

    Jul 18, 2013
    15,646
    4,555
    I Don't C but if you showing a changing sequence are you updating too fast maybe?
    Max.
     
    absf likes this.
  3. hexreader

    Active Member

    Apr 16, 2011
    310
    131
    Why are you messing around with TRISA so much? Set TRISA = 0bxxxxxx00; at start of code then leave TRISA bits 0 and 1 alone. This makes RA0 and RA1 as outputs and leaves them that way.

    For one digit, PORTA = 0bxxxxxx01; for the other digit PORTA = 0bxxxxxx10;

    Your logic looks totally illogical to me.

    ...oh .. and as MaxHeadRoom says, add a delay after display of each individual digit to give your eye time to see each digit.

    100 Ohms seems like a very low value for base resistor.

    On the plus side.... Thank you for commenting the code well. Not many programmers bother. You get 9/10 just for the good commenting - in my book.
     
    Last edited: Jun 25, 2018
    JohnInTX and absf like this.
  4. MrChips

    Moderator

    Oct 2, 2009
    16,710
    5,124
    Are you correctly enabling RA0 and RA1 to be active LOW in the proper sequence as required for multiplexing?
    It doesn't seem so with lines 67 and 68.
    Besides, lines 67 and 68 are in the wrong place in the code.
     
    absf likes this.
  5. absf

    Thread Starter Senior Member

    Dec 29, 2010
    1,850
    504
    @hexreader
    At first glance, TRISA seems weird. But the scope trace confirmed that it works. How should I do it if I freeze the TRISA and just manipulate the RA0/1?

    The resistor value of 100R was too low? Would 1K be ok?

    Thanks for your compliments on comments I made.

    @MrChips-
    From the scope trace on RA0 and RA1, I think they are properly lined up or else both of them wouldn't be complementing.

    Can you believe that it was actually copied from the book "123 PIC microcontroller experiments for the evil genius" on page 123?

    The original PIC used was 16F684 with segment a on RA5 and segment b-g on portC RC5-0. I replaced it with 16F88 so I dont have to split the 7 segments onto 2 ports thus simplifying the HW and SW.

    Allen
     
  6. absf

    Thread Starter Senior Member

    Dec 29, 2010
    1,850
    504
    From the scope trace, each digit was displayed for 10mS for 25 times before it switches to the next value for displaying.

    10mS * 25 *2 = 500mS for each number. Would that be too fast ?

    Allen
     
  7. hexreader

    Active Member

    Apr 16, 2011
    310
    131
    Scope trace says that all is OK, but your eyes seem to show a problem.

    I can well believe that messing with TRIS will acheive required outputs in a perverse way, but I would need to spend a lot of time trying to work out WHY it works. My brain is simple and wonders why you (or the author) want to do it the hard way.

    How are you testing this? Proteus only (good luck with that!) or real hardware, or both?

    What is it that you would like from us now?

    Might be willing to re-write code (if you tell me which compiler you use) but don't know how soon. Not willing to try to fix what was provided in that book. Too much like trying to polish a turd :)

    1K sounds like a good choice of base resistor to me, and is what I would choose, but I am a hobbyist, not an expert.
     
    Last edited: Jun 25, 2018
    absf likes this.
  8. MaxHeadRoom

    Expert

    Jul 18, 2013
    15,646
    4,555
    As I mentioned I don't use C but Usually you have to ensure you turn off the previous digit before displaying the next otherwise you get ghosting of digits.
    Max.
     
    absf likes this.
  9. jpanhalt

    Expert

    Jan 18, 2008
    6,263
    1,157
    absf likes this.
  10. hexreader

    Active Member

    Apr 16, 2011
    310
    131
    I am fully aware that there are times when manipulating TRIS bits is a sensible thing to do. I just don't see that this is one of those occasions. I am willing to provide simpler solution given time (probably). Seems to me that the PIC is sinking transistor base current, not LED current in this case.

    Too lazy to follow those links - so willing to admit to being wrong.
     
    Last edited: Jun 25, 2018
    absf likes this.
  11. Ian Rogers

    Active Member

    Dec 12, 2012
    390
    95
    Allen.... I had to put pulldown resistors on that 7 seg module anode connections... Must have a fault in the sim module.

    BUT!! You need to rewrite and blank the digits, as its looks crazy!!
     
    absf likes this.
  12. MMcLaren

    Distinguished Member

    Feb 14, 2010
    836
    158
    I agree... blank the display by either turning off the active-low digit driver lines or the active-low segment driver lines at the beginning of each digit refresh cycle. example;
    Code (C):
    1.    void main()
    2.    { static int bumptimer = 0;      // 1-second interval timer
    3.      CMCON=7;                       // TURN OFF COMPARATOR
    4.      CCP1CON=0;                     // disable CCP1 functions ON RB0 & RB3
    5.      ANSEL=0;                       // TURN OFF ADC
    6.      PORTB=0b01111111;              // LED segments 'off'
    7.      PORTA=0b00000001;              // RA1 (tens) digit on (0)
    8.      TRISA=0b11111100;              // RA1-RA0 outputs
    9.      TRISB=0b00000000;              //
    10.      disp_value = 0x00;             // start disp at 0x00
    11.  
    12.      while(1)                       //
    13.      { PORTB |= 0b01111111;         // blank the display (segments off)
    14.        PORTA ^= 0b00000011;         // toggle digit select lines
    15.        if(PORTA & 1)                // if 'tens' digit selected
    16.          PORTB = LEDdigit[disp_value/16];
    17.        else                         // if 'ones' digit selected
    18.          PORTB = LEDdigit[disp_value%16];
    19.        __delay_ms(2);               // 2-ms on time (250-Hz refresh rate)
    20.        if(bumptimer++ == 499)       // if ~1-second interval
    21.        { bumptimer = 0;             // reset interval timer and
    22.          disp_value++;              // bump display value, 0x00..0xFF
    23.        }                            //
    24.      }                              //
    25.    }                                //
     
    Ian Rogers and absf like this.
  13. Ian Rogers

    Active Member

    Dec 12, 2012
    390
    95
    Spot on Mike.... Steady as a rock!!
     
  14. absf

    Thread Starter Senior Member

    Dec 29, 2010
    1,850
    504
    Jogging with my friends now. Will change the codes according to Mike's this afternoon.

    Will try to breadboard it too. Report back tonight if working.

    Thanks all who helped.

    Allen
     
  15. absf

    Thread Starter Senior Member

    Dec 29, 2010
    1,850
    504
    @Ian Rogers,
    Yes after putting in the pull-down resistors, proteus seems to work better with 2 differnt digits being displayed. Though I cannot make up what they are.:(:D

    @MMcLaren,
    After replacing the faulty part of the display routine with yours, adding "#define _XTAL_FREQ 4000000", the simualtor finally worked like a star just as described by Ian.

    Here is the sim in proteus. The hardware is under construction and should be ready by tomorrow.
    Thank you all for helping. I'll start phase 2 as soon as the hardware is working.

    iris project 16F88 #2.PNG

    Allen
     
  16. absf

    Thread Starter Senior Member

    Dec 29, 2010
    1,850
    504
    I am actually a beginner in C. The next project I'd do is to make 8x8 matrix LED version of the same project for my wife whose birthday is 2 months away.

    This time I would like to use Atmel 40C51 20 pin scalled down 8051 chip and I'd use assembly for this one. Hope 2 months is not to short for me to complete this project.

    I also have another traffic light project on TTL chips with count-down timer for my grandson and it was just completed.

    Allen
     
  17. Ian Rogers

    Active Member

    Dec 12, 2012
    390
    95
    @absf I have code for the pic in C for matrices (plural ) I'll dig it out and you can port it to 8051...I have done a bit on the intel platform (as you know)..
     
    absf likes this.
  18. absf

    Thread Starter Senior Member

    Dec 29, 2010
    1,850
    504
    I have both 5x7 large matrix as well as the 8x8 small matrix with MAX7219 chip on a PCB.

    Which one does your program support?

    Allen

    20180627_151712.jpg
     
  19. Ian Rogers

    Active Member

    Dec 12, 2012
    390
    95
    Any amount...

    I use three pins as a serial connection to as many 74hc595 latches for column drivers... Then use a port for common row drivers..

    I have managed 32 x 64 pixels without issue...
     
    absf likes this.
  20. Ian Rogers

    Active Member

    Dec 12, 2012
    390
    95
    Here's one with 8 matrices and 6 7 seg LED's
     
    absf likes this.
Loading...