sunfounder / Sunfounder_SuperKit_Python_code_for_RaspberryPi

Sunfounder Super Kit Python code for Raspberry Pi
60 stars 44 forks source link

Request for Clarification about How the Python code Logic of writing 0x80 to DSI/SDI pin 11 once in Sequence of 8 Loops to Light a Different one of 8 LEDs each Time in 10_74HC595_LED.py #3

Open rdjmsy opened 8 years ago

rdjmsy commented 8 years ago

I have a question regarding the 10_74HC595_LED,py script's line 26 where it seems to write 0x80 or 128 to the DS/SDI pin on the 74HC595 chip once every eight loops through the def_hc595_in(dat) function.

I understand that 0x80 is sent to the serial Data serial in pin on the 74HC595 chip for conversion to parallel output on the 8 pins driving the 8 LEDs, at a different position in the sequence of 0-7 (bin) for each of the eight data words in the WhichLeds list. I just cannot quite yet comprehend how that lights a different LED each time through. If you put in a 128, shift that with a positive transiton of SRCLCK/the SHCP pin via pin 13 on the GPIO, wouldn't that always light the second or the seventh LED over and over again instead of sequencing through lighting all eight LEDs, e.g. once every 8 loops, per data serial in word, in of 128 total loops. per call of the def loop() function?

The code in the 10_74HC595_LED.py function/script clearly works well and succeeds lighting each of the eight LEDs first left to right, then right to left, then repeat both over and over, How the program logic invoked with the the loop, hc595(dat) and hc595_out() functions in the 10_74HC595_LED.py script actually achieves this is still more than a bit mysterious to me.

I am hoping that anyone who understands how this code logic actually works could find the time in their busy schedule to break this bit of the program execution logic and how it interacts with the 74HC595 chip to achieve these operational results down and reply in kind with a bit of low-level clarification and detail.

rdjmsy commented 8 years ago

10_74hc595_led-dot-py

rdjmsy commented 8 years ago

sunfounder-super-kit_4_rpi-lession-10_74hc595_8led-ckt-fritz_diag-captr-1

sunfounder commented 8 years ago

First you need to know that 0x80 does not mean 128 in decimal, but 1000 0000 in binary. And you should understand the WhichLeds list. It contains 8 values (maps) in hexadecimal, and you should take them each an 8-bit binary number. Each bit of an individual number represents one of the 8 LEDs. For example, 0x01 is 0000 0001 in binary, indicating the first LED lights up and others stay dim. It's similar for the rest values. The def loop() function: In each for loop, it circulates the WhichLeds maps, one at each time. Then the map is sent to the 74HC595 in serial via hc595_in(), and then 74HC595 converts the serial data to parallel data to control the 8 LEDs.

rdjmsy commented 8 years ago

Thanks for the response. However, I am still confused because the contents of line 26 in this python script seem to indicate that the only serial data that is ever sent in on the DS or SDI (Serial Data In -I believe line) pin on the 74HC595 chip is the result of a bitwise and with 0x80 or 1000 0000. Unless I am missing something, this means that either a 1000 0000 or a 0000 0000 will be sent to the chip's DS pin for each of the eight passes of the hc595_in(dat) function, whenever the hc595_in(dat) function is called by the loop function. So for the 0x01 map/dat the eighth repetition of the for loop in the hc595_in(dat) function would result in a 1000 0000 being sent to the DS pin on the 74HC595 chip and all of the other repetitions of that loop would send a 0000 0000. For the 0x02 map/dat the seventh repetition of the for loop in the hc595_in(dat) function would result in a 1000 0000 being sent to the DS pin on the 74HC595 chip and all of the other repetitions of that loop would send a 0000 0000. How, exactly, does that not result in the same physical LED being turned on and off over and over. I would have to guess that it has something to do with the positive transitions of the shift register clock pin being achieved by taking SRCLK HIGH and then LOW once each of the eight times that same for loop in the same hc595_in(dat) function executes.

This is the piece of how the python program execution logic is actually interacting with the shift and storage registers on the 74HC595 chip that I don't quite yet understand. This bit, to be a bit more specific, is what I am hoping to be able to conceptualize much more clearly. Any detailed information outlining how this is actually working would be greatly appreciated.

rdjmsy commented 8 years ago

I found a helpful explanatory web page that explains the the input for each latch in the Shift Register is cascaded DS --> QA/Q1 --> QB/Q2 --> ....QG/Q6 --> QH/Q7 and that once 0x80 is loaded, you then shift that leading one, in 1000 0000 down, 0 to 7 times depending on how many for loop reps are left after you finally get an and-ed match between 0x80 and the bin (0-7) shifted map/dat word. Then the strobes/positive transitions of the SRCLK pin move that leading one down through the latches int he Shift register column. So the 0x80 word was selected as a data input to have a leading one that could be shifted through the latches in the input register before RCLK positive transition is triggered by loop function then calling the hc595 out function once to output that data on a particular QX pin / forward bias that particular LED for a tenth of a second before turning it off again.

I am unsure if I am allowed to post a link to the page that finally helped me understand this piece of how the overall logic being employed in the 10_74HC595_LED,py script actually interacts with the 74HC595 chip to achieve the results that it does, when lighting the LEDs from left to right, but I will take a chance and post that explanatory link here: http://www3.nd.edu/~lemmon/courses/ee224/web-manual/web-manual/lab9/node6.html.

Obviously the second half of the loop function in that python script does the same thing but in reverse order, lighting the LEDs, one at a time for a tenth of a second each - but from right to left / in reverse order.