PDA

View Full Version : Newbie 74hc595 question



manjero
- 22nd January 2008, 00:37
Hie
I'm a newbie in pic programming and i'm trying to build a night lamp with 30 leds. at start they are all supposed to be lit and then after every minute one of them should turn off.
I built the circuit using 4 74hc595 cascaded and with a pic16f684 i had laying around with a 4 MHZ xtal.
my question is about the code - if i understand correctly, the code it should look like this:

Include "modedefs.bas" ' Include shift modes

D1PIN var PORTA.0 ' Shift data pin 1
C1PIN var PORTA.1 ' Shift clock pin 1

bvar var BYTE

bvar = 255
' Shift out 8 bits of data onto other pins
Shiftout D1PIN, C1PIN, MSBFIRST,[bvar]
Shiftout D1PIN, C1PIN, MSBFIRST,[bvar]
Shiftout D1PIN, C1PIN, MSBFIRST,[bvar]
Shiftout D1PIN, C1PIN, MSBFIRST,[bvar]

Main:
sleep 1000
Shiftout D1PIN, C1PIN, MSBFIRST,[0]
goto Main
end

Is this ok? im getting the programmer ready now and i just wanted to get some input before i actually program it because its my first project.

Thanks in advance,
Shahar Zrihen.

Darrel Taylor
- 22nd January 2008, 01:15
The 74HC595 also needs a "Latch" signal. (3 pins, clk, data, latch)

I think it's called RCLK in the datasheets.
<br>

manjero
- 22nd January 2008, 01:38
First of all, Thanks darren for the quick reply.
so i need to put an extra pin - can i use porta.2?
and just take it low and high before and after shifting right?
Thanks again,
Shahar

Darrel Taylor
- 22nd January 2008, 05:21
First of all, Thanks darren for the quick reply.
No problem Shaharn. :)

>> so i need to put an extra pin - can i use porta.2?

Yes!
But before you can use any of the PORTA pins, including the ones you have already used, you need to turn off the Analog functions.

CMCON0 = 7
ANSEL = 0

>> and just take it low and high before and after shifting right?

A simple HIGH/LOW toggle of the Latch after shifting will do it. The 595 is a very fast chip.

But there are more problems too.

Main:
sleep 1000
Shiftout D1PIN, C1PIN, MSBFIRST,[0]
goto Main
end

sleep 1000 will put the PIC in low power mode for somewhere around 1000 seconds. (16 minutes, 40 seconds)

sleep 60 would be closer to 1-minute, but it'll only be "somewhere around" a minute, since it uses the Watch Dog Timer..

PAUSE 60000 ; would be a better approximation, without resorting to Timers.<hr>
>> Shiftout D1PIN, C1PIN, MSBFIRST,[0]
That's going to shift out 8-bits. So 8 of the LED's will turn off at the same time.

The way you're doing it, you don't really need the shiftout statement in the Main loop.

First, set the Data pin to 0 before going into the loop.
Then after each minute toggle the Clock pin once, to shift in a 0.
Then toggle the Latch Pin, to move the data to the 595's output's.

<hr>Also, the first ShiftOut's can be combined...
Shiftout D1PIN, C1PIN, MSBFIRST,[bvar, bvar, bvar, bvar]

<br>

manjero
- 22nd January 2008, 07:59
Hey DT.
Thanks again - that was very informative!
I changed my code to this:

Include "modedefs.bas" ' Include shift modes

CMCON0 = 7
ANSEL = 0
DPIN var PORTA.0 ' Shift data pin 1
CPIN var PORTA.1 ' Shift clock pin 1
LPIN var PORTA.2 ' Latch pin 1


bvar var BYTE

bvar = 255
' Shift out 8 bits of data onto other pins
Shiftout DPIN, CPIN, MSBFIRST,[bvar, bvar, bvar, bvar]
Low DPIN

Main:
pause 5980
High CPIN
pause 10
'Shiftout D1PIN, C1PIN, MSBFIRST,[0]
LOW CPIN
Pulsout LPIN,10
goto Main
end

Compiles OK so lets hope :-)

scarroll01
- 22nd January 2008, 13:59
Hello manjero, I think that Darrel missed the fact that you're using an 'F84. You can drop the:-

CMCON0 = 7
ANSEL = 0

The 'F84 doesn't have any analogue inputs to turn off or configure as digital IO. They're always digital IO. In fact, I have 16F84(A) selected, the compiler, (v 2.44), doesn't even recognise CMCON0.

... Steve (Yep, another one)

skimask
- 22nd January 2008, 14:50
In fact, I have 16F84(A) selected, the compiler, (v 2.44), doesn't even recognise CMCON0.
... Steve (Yep, another one)

The upgrade covers that too, only $25.
Get your now...hurry hurry hurry.

bbarney
- 22nd January 2008, 15:31
the 16F684 does

[/QUOTE]Hello manjero, I think that Darrel missed the fact that you're using an 'F84. You can drop the:-

CMCON0 = 7
ANSEL = 0

The 'F84 doesn't have any analogue inputs to turn off or configure as digital IO. They're always digital IO. In fact, I have 16F84(A) selected, the compiler, (v 2.44), doesn't even recognise CMCON0.
[/QUOTE]


I built the circuit using 4 74hc595 cascaded and with a pic16f684 i had laying around with a 4 MHZ xtal.

scarroll01
- 22nd January 2008, 16:05
the 16F684 does

Hello manjero, I think that Darrel missed the fact that you're using an 'F84. You can drop the:-

CMCON0 = 7
ANSEL = 0

The 'F84 doesn't have any analogue inputs to turn off or configure as digital IO. They're always digital IO. In fact, I have 16F84(A) selected, the compiler, (v 2.44), doesn't even recognise CMCON0.
[/QUOTE][/QUOTE]

Sorry, it's my failing eyesight. I didn't see the "6" and thought it was a PIC16F84.

... Steve

bbarney
- 22nd January 2008, 19:32
I only seen the 6 cause I know Darrel doesn't miss anything :)

Darrel Taylor
- 22nd January 2008, 22:22
I only seen the 6 cause I know Darrel doesn't miss anything :)
Uht-Oh, I'm getting a reputation. :o



I changed my code to this: ...

That's getting closer, but Wait, there's More.

The Latch Pin should start out LOW.
Then after the SHIFTOUT, the data gets latched by toggling it High/Low.

The pause only adds up to about 6000 (6 seconds).
For 60 Seconds, you need 60,000.

And the 595 is a really fast chip. pause 10 and Pulsout aren't needed.


bvar = 255

LOW LPIN ; Latch Idles LOW

' Shift out 32 - 1's to 74HC595's
Shiftout DPIN, CPIN, MSBFIRST,[bvar, bvar, bvar, bvar]

HIGH LPIN ; Latch data to 595's outputs
LOW LPIN

Low DPIN ; Set Data to 0

Main:
pause 60000 ; Wait 1 minute

HIGH CPIN ; clock in a 0
LOW CPIN

HIGH LPIN ; Latch data to 595's outputs
LOW LPIN
GOTO Main

hth,