View previous topic :: View next topic |
Author |
Message |
lgeorge123
Joined: 05 Dec 2004 Posts: 31
|
cascade three 74hc595 with pic16f877a |
Posted: Wed Oct 03, 2018 8:00 pm |
|
|
I have three 74hc595d cascade with pic16f877a. The circuit part with 74hc595 is test with Arduino and is ok. The code is as follows:
Code: |
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=16000000)
void shiftOut(unsigned char val)
{
char j;
for(j = 0x80; j > 0; j = j >> 1)
{
if(val & j)
output_high(pin_e0); // data pin
else
output_low(pin_e0);
}
output_high(pin_e2); //clock pin
output_low(pin_e2);
}
void main() {
int8 h;
int8 p;
int8 u;
int32 d=70000;
h=(char)((0xff0000 & d)>>16);
p=(char)((0x00ff00&d)>>8);
u=(char)(0x0000ff &d);
output_low(pin_e1); // latch pin
shiftout(h);
shiftout(p);
shiftout(u);
output_high(pin_e1);
while(1);
}
|
The problem is the result is not as expected. Can someone help me !! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu Oct 04, 2018 1:15 am |
|
|
Why not just use the supplied 74595 driver. Just define your pins, tell it you have three 595's (#define NUMBER_OF_74594 3), and the work is all done.
Your maths for the values to output seems 'odd'.
Why % 70000?.
FF0000 % 70000 will give 0xc9E0. Shifting this right 16, gives 0.
If you just want the bytes from the values, why not just use make8, or a union?. |
|
|
lgeorge123
Joined: 05 Dec 2004 Posts: 31
|
cascade three 74hc595 with pic16f877a |
Posted: Thu Oct 04, 2018 2:58 am |
|
|
I change the code as follows:
Code: | #include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=16000000)
#include <74595.c>
#define EXP_OUT_ENABLE PIN_E1
#define EXP_OUT_CLOCK PIN_E2
#define EXP_OUT_DO PIN_E0
#define NUMBER_OF_74595 3
void main() {
int8 h;
int8 p;
int8 u;
int32 d=70000;
set_tris_e(0x00);
h=make8(d,0);
p=make8(d,1);
u=make8(d,2);
write_expanded_outputs(&h);
write_expanded_outputs(&p);
write_expanded_outputs(&u);
while(1);
}
the result is still not as expected ......... |
|
|
|
lgeorge123
Joined: 05 Dec 2004 Posts: 31
|
cascade three 74hc595 with pic16f877a |
Posted: Thu Oct 04, 2018 3:12 am |
|
|
What I want to do is show 70000 as binary digit in three 74hc595 IC |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu Oct 04, 2018 3:17 am |
|
|
He is not showing 70000, but I repeat my question of why he is using this value to get a remainder from the bytes he passes. It means he will get screwy values and I suspect this is the real reason the values aren't as expected....
For his first value it is almost always going to give 0, since only a number between 65536, and 70000 can actually give a bit in the 3rd byte, and this is only ever going to give a value of 0 or 1 to this chip.
Looks mathematically completely wrong.... |
|
|
lgeorge123
Joined: 05 Dec 2004 Posts: 31
|
cascade three 74hc595 with pic16f877a |
Posted: Thu Oct 04, 2018 3:34 am |
|
|
Sorry , my d value in ccs c language is int32; |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Thu Oct 04, 2018 5:33 am |
|
|
shouldn't the defines for the 74595 vars BEFORE the driver gets loaded ?
I'm thinking (with a 24hr headache) that the data is being sent to the pins the driver defaults to B0,B1,B2 not the E0,E1,E2 ?? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu Oct 04, 2018 7:42 am |
|
|
You'd be expecting 0b00000001 on the top chip, 0b00010001 on the second, and 0b0111000 on the third.
And Temtronc is right you _must_ define the settings before you load the driver. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu Oct 04, 2018 7:54 am |
|
|
How it needs to be done:
Code: |
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=16000000)
#define EXP_OUT_ENABLE PIN_E1
#define EXP_OUT_CLOCK PIN_E2
#define EXP_OUT_DO PIN_E0
#define NUMBER_OF_74595 3
#include <74595.c>
void main()
{
int32 d=70000;
set_tris_e(0x00);
write_expanded_outputs((int8 *)&d);
//This automatically sends three bytes from d.
//Since value is stored LSB first, this sends the low three bytes
while(1)
;
}
|
The single call updates all three chips. |
|
|
|