Shift Registers - Tutorial 3, Using Timers
A tutorial to explore the use of the ez_SIPO8_lib timer functions and capabilities.
Components and supplies
8
Resistor 220 ohm
1
Breadboard (generic)
1
Arduino UNO
1
Shift Register- Serial to Parallel
8
LED (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Using ez_SIPO8_lib Timers
c_cpp
Using timers to demonstrate how we may control multiple SIPO ports
1// 2// Tutorial 3 - use of ez_SIPO8 library, 3// 1x physical SIPO, and use of SIPO8 timers to control SIPO outputs with time 4// 5// Ron D Bentley, Stafford, UK 6// April 2021 7// 8// This example and code is in the public domain and 9// may be used without restriction and without warranty. 10// 11 12#include <ez_SIPO8_lib.h> 13 14#define Max_SIPOs 1 // one virtual SIPO for this tutorial 15#define Max_timers 8 // 8 timers required to map a complete 8bit SIPO 16 17// initiate the class for Max SIPOs/timers required 18SIPO8 my_SIPOs(Max_SIPOs, Max_timers); 19 20int bank0_id; // used to keep the SIPO bank id 21// 22// setup pin/LED flash data 23uint8_t timer; 24uint32_t timer_intervals[Max_timers] = { 25 300, 400, 500, 600, 700, 800, 900, 1000 // millisecond elapse timer values 26}; 27uint8_t timer_pins[Max_timers] = { 28 0, 1, 2, 3, 4, 5, 6, 7 // SIPO output pin absolute addresses 29}; 30 31void setup() { 32 Serial.begin(9600); 33 // create bank, params are: 34 // data pin, clock pin, latch pin, number of SIPOs this bank 35 bank0_id = my_SIPOs.create_bank(8, 10, 9, 1); 36 if (bank0_id == create_bank_failure) { 37 Serial.println(F("\ 38failed to create bank")); 39 Serial.flush(); 40 exit(0); 41 } 42 // print the bank data for confirmation/inspection 43 my_SIPOs.print_SIPO_data(); 44} 45 46void loop() { 47 // start by setting all SIPO outputs to low (off) 48 my_SIPOs.set_all_array_pins(LOW);// set all declared virtual output pins LOW/off 49 my_SIPOs.xfer_array(LSBFIRST); // move virtual pin statuses to real SIPO output pins 50 // 51 // start all timers 52 for (timer = 0; timer < Max_timers; timer++) { 53 my_SIPOs.SIPO8_start_timer(timer); 54 } 55 timer = 0; // start checking at first timer 56 do { 57 // check each timer for elapse and, if elapsed, invert the timer's output pin 58 // and reset the timer 59 if (my_SIPOs.SIPO8_timer_elapsed(timer, timer_intervals[timer]) == elapsed) { 60 my_SIPOs.invert_array_pin(timer_pins[timer]); // invert the pin associated with this timer 61 my_SIPOs.xfer_array(MSBFIRST); // update physical SIPO outputs 62 my_SIPOs.SIPO8_start_timer(timer); // reset/restart the current timer 63 } 64 timer++; // move on to next timer 65 if (timer >= Max_timers) timer = 0; // wrap around to beginning 66 } while (true); 67} 68
Using ez_SIPO8_lib Timers
c_cpp
Using timers to demonstrate how we may control multiple SIPO ports
1// 2// Tutorial 3 - use of ez_SIPO8 library, 3// 1x physical SIPO, 4 and use of SIPO8 timers to control SIPO outputs with time 5// 6// Ron D Bentley, 7 Stafford, UK 8// April 2021 9// 10// This example and code is in the public 11 domain and 12// may be used without restriction and without warranty. 13// 14 15#include 16 <ez_SIPO8_lib.h> 17 18#define Max_SIPOs 1 // one virtual SIPO for this tutorial 19#define 20 Max_timers 8 // 8 timers required to map a complete 8bit SIPO 21 22// initiate 23 the class for Max SIPOs/timers required 24SIPO8 my_SIPOs(Max_SIPOs, Max_timers); 25 26int 27 bank0_id; // used to keep the SIPO bank id 28// 29// setup pin/LED flash data 30uint8_t 31 timer; 32uint32_t timer_intervals[Max_timers] = { 33 300, 400, 500, 600, 700, 34 800, 900, 1000 // millisecond elapse timer values 35}; 36uint8_t timer_pins[Max_timers] 37 = { 38 0, 1, 2, 3, 4, 5, 6, 7 // SIPO output pin absolute addresses 39}; 40 41void 42 setup() { 43 Serial.begin(9600); 44 // create bank, params are: 45 // data 46 pin, clock pin, latch pin, number of SIPOs this bank 47 bank0_id = my_SIPOs.create_bank(8, 48 10, 9, 1); 49 if (bank0_id == create_bank_failure) { 50 Serial.println(F("\ 51failed 52 to create bank")); 53 Serial.flush(); 54 exit(0); 55 } 56 // print 57 the bank data for confirmation/inspection 58 my_SIPOs.print_SIPO_data(); 59} 60 61void 62 loop() { 63 // start by setting all SIPO outputs to low (off) 64 my_SIPOs.set_all_array_pins(LOW);// 65 set all declared virtual output pins LOW/off 66 my_SIPOs.xfer_array(LSBFIRST); 67 // move virtual pin statuses to real SIPO output pins 68 // 69 // start all 70 timers 71 for (timer = 0; timer < Max_timers; timer++) { 72 my_SIPOs.SIPO8_start_timer(timer); 73 74 } 75 timer = 0; // start checking at first timer 76 do { 77 // check each 78 timer for elapse and, if elapsed, invert the timer's output pin 79 // and reset 80 the timer 81 if (my_SIPOs.SIPO8_timer_elapsed(timer, timer_intervals[timer]) 82 == elapsed) { 83 my_SIPOs.invert_array_pin(timer_pins[timer]); // invert the 84 pin associated with this timer 85 my_SIPOs.xfer_array(MSBFIRST); // 86 update physical SIPO outputs 87 my_SIPOs.SIPO8_start_timer(timer); // 88 reset/restart the current timer 89 } 90 timer++; // move on to next timer 91 92 if (timer >= Max_timers) timer = 0; // wrap around to beginning 93 } while 94 (true); 95} 96
Downloadable files
Single SIPO Shift Register, wiring diagram
Used throughout the tutorials
Single SIPO Shift Register, wiring diagram

Comments
Only logged in users can leave comments