shift register

Dependents:   Enrico_newproject_copy Enrico_copy Enrico_copy2 Project_ORBE

Fork of Shifter by marco valli

Committer:
billycorgan123
Date:
Wed May 30 14:57:03 2018 +0000
Revision:
3:4511084b5546
Parent:
1:b6a66e7e1947
changed timing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
billycorgan123 0:7af9b977e6e9 1 // Include the standard types
billycorgan123 0:7af9b977e6e9 2 #include "Shifter.h"
billycorgan123 0:7af9b977e6e9 3
billycorgan123 0:7af9b977e6e9 4 #define LOW 0
billycorgan123 0:7af9b977e6e9 5 #define HIGH 1
billycorgan123 1:b6a66e7e1947 6 DigitalOut SER_Pin(D4); //pin 14 su 595
billycorgan123 3:4511084b5546 7 DigitalOut RCLK_Pin(D3); //pin 11 su 595
billycorgan123 3:4511084b5546 8 DigitalOut SRCLK_Pin(D2); //pin 12 su 595
billycorgan123 0:7af9b977e6e9 9 int Number_of_Registers=2;
billycorgan123 0:7af9b977e6e9 10
billycorgan123 0:7af9b977e6e9 11 void Shifter::write()
billycorgan123 0:7af9b977e6e9 12 {
billycorgan123 0:7af9b977e6e9 13 //Set and display registers
billycorgan123 0:7af9b977e6e9 14 //Only call AFTER all values are set how you would like (slow otherwise)
billycorgan123 3:4511084b5546 15 RCLK_Pin.write(LOW);
billycorgan123 3:4511084b5546 16 SRCLK_Pin.write(LOW);
billycorgan123 3:4511084b5546 17 wait_us(10);
billycorgan123 3:4511084b5546 18 //iterate through the registers
billycorgan123 3:4511084b5546 19 for(int i = Number_of_Registers - 1; i >= 0; i--) {
billycorgan123 3:4511084b5546 20 //iterate through the bits in each registers
billycorgan123 3:4511084b5546 21 for(int j = 8 - 1; j >= 0; j--) {
billycorgan123 3:4511084b5546 22 SRCLK_Pin.write(LOW);
billycorgan123 3:4511084b5546 23 wait_us(4);
billycorgan123 3:4511084b5546 24 int val = shiftRegisters[i] & (1 << j);
billycorgan123 3:4511084b5546 25 SER_Pin.write(val);
billycorgan123 3:4511084b5546 26 wait_us(1); //wait_us(10);
billycorgan123 3:4511084b5546 27 SRCLK_Pin.write(HIGH);
billycorgan123 3:4511084b5546 28 wait_us(10); //wait_us(10);
billycorgan123 3:4511084b5546 29 SRCLK_Pin.write(LOW);
billycorgan123 3:4511084b5546 30 wait_us(6); //wait_us(10);
billycorgan123 3:4511084b5546 31 }
billycorgan123 3:4511084b5546 32 }
billycorgan123 3:4511084b5546 33 RCLK_Pin.write(HIGH);
billycorgan123 3:4511084b5546 34 wait_us(10);
billycorgan123 3:4511084b5546 35 RCLK_Pin.write(LOW);
billycorgan123 3:4511084b5546 36 wait_us(10);
billycorgan123 0:7af9b977e6e9 37 }
billycorgan123 3:4511084b5546 38
billycorgan123 0:7af9b977e6e9 39 void Shifter::setPin(int index, bool val)
billycorgan123 0:7af9b977e6e9 40 {
billycorgan123 0:7af9b977e6e9 41 int byteIndex = index/8;
billycorgan123 0:7af9b977e6e9 42 int bitIndex = index % 8;
billycorgan123 0:7af9b977e6e9 43 char current = shiftRegisters[byteIndex];
billycorgan123 0:7af9b977e6e9 44 current &= ~(1 << bitIndex); //clear the bit
billycorgan123 0:7af9b977e6e9 45 current |= val << bitIndex; //set the bit
billycorgan123 0:7af9b977e6e9 46 shiftRegisters[byteIndex] = current; //set the value
billycorgan123 0:7af9b977e6e9 47 }
billycorgan123 0:7af9b977e6e9 48
billycorgan123 0:7af9b977e6e9 49 void Shifter::setAll(bool val)
billycorgan123 0:7af9b977e6e9 50 {
billycorgan123 3:4511084b5546 51 //set all register pins to val
billycorgan123 3:4511084b5546 52 for(int i = Number_of_Registers * 8 - 1; i >= 0; i--) {
billycorgan123 3:4511084b5546 53 setPin(i, val);
billycorgan123 3:4511084b5546 54 }
billycorgan123 0:7af9b977e6e9 55 }
billycorgan123 0:7af9b977e6e9 56
billycorgan123 0:7af9b977e6e9 57 void Shifter::clear()
billycorgan123 0:7af9b977e6e9 58 {
billycorgan123 3:4511084b5546 59 //set all register pins to LOW
billycorgan123 3:4511084b5546 60 for(int i = Number_of_Registers * 8 - 1; i >= 0; i--) {
billycorgan123 3:4511084b5546 61 setPin(i, HIGH);
billycorgan123 3:4511084b5546 62 }
billycorgan123 0:7af9b977e6e9 63 }