Advertisement
ripred

ArduinoShiftRegisters.ino

Oct 11th, 2023 (edited)
975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | Source Code | 0 0
  1. // ArduinoShiftRegisters.ino
  2. //
  3. // for use at http://falstad.com/circuit/avr8js/
  4. //
  5. //
  6. #define  DUR       50LU
  7. #define  dataPin   8
  8. #define  clockPin  9
  9. #define  ActiveHi  true
  10.  
  11. int pass;
  12.  
  13. inline void clk() {
  14.   digitalWrite(clockPin, ActiveHi ? HIGH : LOW);
  15.   delayMicroseconds(DUR);
  16.   digitalWrite(clockPin, (!ActiveHi) ? HIGH : LOW);
  17.   delayMicroseconds(DUR);
  18. }
  19.  
  20. inline void shift_bit(bool const val) {
  21.   digitalWrite(dataPin, val ? HIGH : LOW);
  22.   delayMicroseconds(DUR);
  23.  clk();
  24. }
  25.  
  26. inline void shift_byte(uint8_t const val) {
  27.   for (register int8_t i=0; i < 8; i++) {
  28.     uint8_t bit = (val >> i) & 0x01;
  29.     shift_bit(bit);
  30.   }
  31. }
  32.  
  33. inline void clear() {
  34.   Serial.print("clearing..");
  35.   for (int i=0; i < 7; i++) {
  36.     shift_byte(0);
  37.   }
  38.   Serial.println("finished");
  39. }
  40.  
  41. void setup() {
  42.   Serial.begin(115200);
  43.  
  44.   // add external voltage (Javascript) with name "pin n" to access output pins
  45.   pinMode(clockPin, OUTPUT);
  46.   pinMode(dataPin, OUTPUT);
  47.  
  48.   digitalWrite(clockPin, (!ActiveHi) ? HIGH : LOW);
  49.   delayMicroseconds(DUR);
  50.  
  51.   clear();
  52. }
  53.  
  54. void loop() {
  55.   Serial.print("Pass ");
  56.   Serial.println(++pass);
  57.  
  58.   shift_byte(0x3A);   // 'o'
  59.   shift_byte(0x2A);   // 'n'
  60.   shift_byte(0x20);   // 'i'
  61.   shift_byte(0x38);   // 'u'
  62.   shift_byte(0x7A);   // 'd'
  63.   shift_byte(0x0A);   // 'r'
  64.   shift_byte(0xEF);   // 'A'
  65.  
  66.   delay(10);
  67.   clear();
  68.   // delay(50);
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement