Help pic16f877a with 74HC595 problem

Discussion in 'Embedded Systems and Microcontrollers' started by Veto, Sep 13, 2012.

  1. Veto

    Thread Starter New Member

    Jul 26, 2012
    2
    0
    hi all

    I have written the code blew in mikroBasic Pro to drive 4 seven segment
    & it works well on Proteus the purpose of code is to make a counter from 0 to 9 on the 4 seven segment
    but when i apply it on the real world it does not give me the expected result
    also the seven segment are flickering all the time
    note : My hardware is connected well & fully tested

    is that code right ?

    Code ( (Unknown Language)):
    1.  
    2.  
    3. program HC595
    4.  
    5. ' Declarations section
    6.                                   '( 0  , 1  , 2  , 3  , 4  , 5  , 6  , 7  , 8  , 9  )
    7.   const AnodeNumbers as byte[10] = (0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90)
    8.  
    9.   dim Datax as sbit at RB0_bit    'Data Input
    10.   dim Clk as sbit at RB1_bit    'clock
    11.   dim Latch as sbit at RB2_bit    'Latch
    12.   dim iLoop as byte
    13.  
    14. sub procedure ClockPuls()
    15.     Clk = 1
    16.     delay_us(5)
    17.     Clk = 0
    18. end sub
    19.  
    20. sub procedure LatchPuls()
    21.     Latch = 1
    22.     delay_us(5)
    23.     Latch = 0
    24. end sub
    25.  
    26. sub procedure ShiftOut(dim dgt as byte)
    27.  
    28.   dim i as byte
    29.   dim RegCnt as byte
    30.  
    31.  for RegCnt = 0 to 3'shift registers count (4 shift registers)
    32.  
    33. '-----------------------------------------------
    34.     for i = 0 to 7
    35.  
    36.       if ((dgt<<i) AND 0x80)  then
    37.           Datax = 1
    38.       else
    39.           Datax = 0
    40.       end if
    41.  
    42.       ClockPuls
    43.  
    44.    next i
    45.  
    46.    LatchPuls
    47.    
    48. '-----------------------------------------------
    49.  next RegCnt
    50.  
    51. end sub
    52.  
    53. main:
    54. '   Main program
    55.   TRISB = 0
    56.   portb = 0
    57.  
    58.   while 1
    59.  
    60.        ShiftOut(%11111111)
    61.        Delay_Ms(5000)
    62.  
    63.        for iLoop = 0 to 9
    64.             ShiftOut(AnodeNumbers[iLoop])
    65.             Delay_Ms(5000)
    66.        next iLoop
    67.  
    68.   wend
    69.  
    70. end.
    71.  
    72.  
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    One would need the schematic to compare the program to the hardware.

    How did you determine "My hardware is connected well & fully tested"? What code did you test the hardware against?
     
  3. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    I don't use PIC BASIC, but from what I understand your code is incorrect.

    You don't update 4 registers each time in the loop for sure.
    Do you use 9 Anode rails?
    Then you need 9 different patterns.
    And 5000 msec. delay is grossly wrong for LED multiplex.
     
  4. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I don't think there is any need to send the latch pulse until all 4 displays have been shifted.
    If they are flickering while it should be in a 5 second delay and not doing anything, then maybe the PIC is being reset. Try a 1 second delay before any shifting code and you should be able to tell if it is resetting.
     
  5. Veto

    Thread Starter New Member

    Jul 26, 2012
    2
    0
    Okay thanks for all
    My hardware is working very well & tested with another program but unfortunately it's a hex file & i don't have the source code
    okay anyway i have exceeded this point by rewriting the code in mikroC & it works well
    But i have another problem the program works some times well & gives me the expected results but also some times it gives me unexpected results
    i think there's (data loss on the serial bus) AND i don't what can i do & now this's my new code in mikroC

    NOTE: the AnodeNumbers array depends on my new connection with the 7 segment & it's also tested & works well

    Code ( (Unknown Language)):
    1.  
    2.  
    3. const unsigned char AnodeNumbers[10] = {0x4,0x1F,0x88,0xA,0x13,0x22,0x20,0xF,0x0,0x2};  //Mohd Oraby Serial Seven Segment Anode Numbers For 1" B-C     4 Digits
    4.  
    5. sbit latch at RB1_bit;
    6. sbit clock at RB0_bit;
    7. sbit data_bit at RB3_bit;
    8. sbit latch_Direction at TRISB1_bit;
    9. sbit clock_Direction at TRISB0_bit;
    10. sbit data_bit_Direction at TRISB3_bit;
    11.  
    12. /*-----------------------------------------------------------------------------*/
    13.  
    14. void send_data(unsigned char Digit);
    15. void ZeroLatches(unsigned char LatchesCount);
    16.  
    17. /*-----------------------------------------------------------------------------*/
    18. void main()
    19. {
    20.      unsigned char bLoop;
    21.      latch_Direction=0;
    22.      clock_Direction=0;
    23.      data_bit_Direction=0;
    24.  
    25.      while(1)
    26.      {
    27.          ZeroLatches(4);
    28.          Delay_ms(5000);
    29.  
    30.          for(bLoop=0;bLoop<10;bLoop++)
    31.          {
    32.            send_data(AnodeNumbers[bLoop]);
    33.            Delay_ms(3000);
    34.          }
    35.  
    36.      }
    37.          
    38. }
    39. /*-----------------------------------------------------------------------------*/
    40. void send_data(unsigned char Digit)
    41. {
    42.    unsigned char i;
    43.    unsigned char cr;//shift registers count
    44.    unsigned char db;
    45.  
    46.    for (cr=0;cr<4;cr++)
    47.    {
    48.  
    49.      latch=0;
    50.      clock=0;
    51.      
    52.      for(i=0;i<8;i++)
    53.      {
    54.  
    55.        db=Digit>>i;
    56.        
    57.        if(db&1)
    58.            data_bit=1;
    59.        else
    60.            data_bit=0;
    61.  
    62.        asm nop;
    63.        
    64.        clock=1;
    65.        clock=0;
    66.  
    67.      }
    68.  
    69.         latch=1;
    70.    }
    71.    
    72. }
    73. /*-----------------------------------------------------------------------------*/
    74. void ZeroLatches(unsigned char LatchesCount)
    75. {
    76.      unsigned char i;
    77.      unsigned char j;
    78.      
    79.      for(i=0;i<LatchesCount;i++)
    80.      {
    81.          latch=0;
    82.          for(j=0;j<8;j++)
    83.          {
    84.               data_bit=1;
    85.               clock=1;
    86.               clock=0;
    87.          }
    88.          latch=1;
    89.      }
    90. }
    91. /*-----------------------------------------------------------------------------*/
    92.  
     
Loading...