18M2 to 74HC595 to 8-LEDs

SolidWorksMagi

Senior Member
Hi,

I want to send specific data patterns to a 74HC595 chip to turn on specific LEDs and eventually, hold on that pattern ... but this do loop doesn't seem to get me any results ??? Can anybody tell me what's wrong with my little do loop?

setfreq m4 ; All M2 parts internal k31, k250, k500, m1, m2, m4, m8, m16, m32

symbol SER = C.0 ; SER (pin 14) is for data bit to SR.
symbol SDCK = C.1 ; SDCK (pin 11) data clock
symbol SDCLR = C.2 ; SDCLR (pin 10) clear data register on LOW.
symbol LATCH = C.6 ; LATCH (pin 12 RCK) HIGH latches SR data
symbol ENABLE= C.7 ; ENABLE output for latch data (pin 13 or G-NOT)

do
for j = 0 to 84 ;= 01010101
val = j
for i = 1 to 8
temp1 = val & %01010101
if temp1 = 1 then HIGH SER
else LOW SER
endif
pulsout SDCK,1
val = val / 2 ; shift bits one place right
next i
pulsout LATCH,1
; data retained until next cycle
next j
loop


This do loop does work but is not what I want;

do
for j = 0 to 255
val = j
for i = 1 to 8
temp1 = val & %00000001
if temp1 = 1 then HIGH SER
else LOW SER
endif
pulsout SDCK,1
val = val / 2 ; shift bits one place right
next i
pulsout LATCH,1
; data retained until next cycle
pause 500 ; Determines time between each count/display
next j
loop
 
Last edited:

tmfkam

Senior Member
I might be missing it, but I can't see any assignment of a value to "val" in the first example?
 

techElder

Well-known member
Your second DO/LOOP uses the lines

Code:
do
for j = 0 to 255
val = j
Your first DO/LOOP could need the following change

Code:
do
for i = 0 to 8
[B]val = i[/B]
 

AllyCat

Senior Member
Hi,

You are also confusing two different methods of selecting individual bits from a byte. In the second version you have "val = val / 2 ; shift bits one place right*", but haven't first initialised val as %10000000 to generate the sequence of numbers: 128, 64, 32 ... 1.

In the first version you are ANDing the values 0, 1, 2 ... 8 with the byte, which won't give the correct result. One "fix" is to use a LOOKUP i,(128,64,32,16,8,4,2,1),val . Also, note that the FOR loop should be 0 TO 7 , not 0 TO 8.

* When possible, it can be better to use Left-Shifts because val = val + val is faster than val = val / 2.

Cheers, Alan.
 

hippy

Technical Support
Staff member
for i = 0 to 8

That will send out 9 sets of data and pulses; 0, 1, 2, 3, 4, 5, 6, 7, 8

You need 'for i = 0 to 7' or 'for i = 1 to 8' to send out 8 sets of data and pulses.

Then you need to set the SER output per bit for 'val'. The second code does that but the first does not; any value of 'val' which is not 1 will output as if zero.
 

lbenson

Senior Member
This is a case where the use of the simulator (even though you can't connect a 74HC595 chip) would probably have shown you immediately that you were not getting the expected output because the input hadn't been initialized properly.
 

SolidWorksMagi

Senior Member
Hi,

Thanks guys ... I got it working pretty well ... the subrouting to display and halt the scanning through the LEDs ... BTW, I don't use the forum all that much so remembering things like
Code:
 is not on the top of my brain and not an icon on the icon bar above either to remind me.

[code]
PlayPattern:
for j = 1 to PatNum
  val = j
  for i = 1 to 8
    temp1 = val & %00000001
    if temp1 = 1 then HIGH SER
    else LOW SER
    endif
    pulsout SDCK,1 
    val = val / 2 ; shift bits one place right
IF i = PatNum then return
ENDIF
next i
pulsout LATCH,1 
; data retained until next cycle
;pause 500  ; Determines time between each count/display
next j
return
 

westaust55

Moderator
There are three ways to have the [code] tags inserted into you post:
1. Manually
2. From the PE under the edit button with the section/all to be posted selected, select the “copy to forum” and then paste into your post
3. Once you have opened a post, click the “go advanced” button at bottom right. Then you have additional icons on the menu bar. Again select the part of the post with your code and then click on the “code” = the hash (#) button.
 
Top