# Help pic16f877a with 74HC595 problem

#### Veto

Joined Jul 26, 2012
2
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 ?

Rich (BB code):
program HC595

' Declarations section
'( 0  , 1  , 2  , 3  , 4  , 5  , 6  , 7  , 8  , 9  )
const AnodeNumbers as byte[10] = (0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90)

dim Datax as sbit at RB0_bit    'Data Input
dim Clk as sbit at RB1_bit    'clock
dim Latch as sbit at RB2_bit    'Latch
dim iLoop as byte

sub procedure ClockPuls()
Clk = 1
delay_us(5)
Clk = 0
end sub

sub procedure LatchPuls()
Latch = 1
delay_us(5)
Latch = 0
end sub

sub procedure ShiftOut(dim dgt as byte)

dim i as byte
dim RegCnt as byte

for RegCnt = 0 to 3'shift registers count (4 shift registers)

'-----------------------------------------------
for i = 0 to 7

if ((dgt<<i) AND 0x80)  then
Datax = 1
else
Datax = 0
end if

ClockPuls

next i

LatchPuls

'-----------------------------------------------
next RegCnt

end sub

main:
'   Main program
TRISB = 0
portb = 0

while 1

ShiftOut(%11111111)
Delay_Ms(5000)

for iLoop = 0 to 9
ShiftOut(AnodeNumbers[iLoop])
Delay_Ms(5000)
next iLoop

wend

end.

#### ErnieM

Joined Apr 24, 2011
8,006
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?

#### takao21203

Joined Apr 28, 2012
3,696
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.

#### Markd77

Joined Sep 7, 2009
2,806
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.

#### Veto

Joined Jul 26, 2012
2
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

Rich (BB code):
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

sbit latch at RB1_bit;
sbit clock at RB0_bit;
sbit data_bit at RB3_bit;
sbit latch_Direction at TRISB1_bit;
sbit clock_Direction at TRISB0_bit;
sbit data_bit_Direction at TRISB3_bit;

/*-----------------------------------------------------------------------------*/

void send_data(unsigned char Digit);
void ZeroLatches(unsigned char LatchesCount);

/*-----------------------------------------------------------------------------*/
void main()
{
unsigned char bLoop;
latch_Direction=0;
clock_Direction=0;
data_bit_Direction=0;

while(1)
{
ZeroLatches(4);
Delay_ms(5000);

for(bLoop=0;bLoop<10;bLoop++)
{
send_data(AnodeNumbers[bLoop]);
Delay_ms(3000);
}

}

}
/*-----------------------------------------------------------------------------*/
void send_data(unsigned char Digit)
{
unsigned char i;
unsigned char cr;//shift registers count
unsigned char db;

for (cr=0;cr<4;cr++)
{

latch=0;
clock=0;

for(i=0;i<8;i++)
{

db=Digit>>i;

if(db&1)
data_bit=1;
else
data_bit=0;

asm nop;

clock=1;
clock=0;

}

latch=1;
}

}
/*-----------------------------------------------------------------------------*/
void ZeroLatches(unsigned char LatchesCount)
{
unsigned char i;
unsigned char j;

for(i=0;i<LatchesCount;i++)
{
latch=0;
for(j=0;j<8;j++)
{
data_bit=1;
clock=1;
clock=0;
}
latch=1;
}
}
/*-----------------------------------------------------------------------------*/