2016年6月18日 星期六

關於 74HC595 Shift Register


  74HC595 為一個位移暫存器: 8bit serial in, parallel out。主要可以透過控制 SER、RCLK, SRCLK 3個輸入接腳控制8個輸出(QA~QH), 這如同是一個3-to-8的Decoder。
  74HC595 位移暫存器可以作為I/O Expansion, 用3根GPIO獲得到8個輸出


  • Vcc
    Up to 6V (needs to be the same voltage as your microcontroller) – Usually 3.3 / 5v
  • QA to QH
    Shift Register Outputs.==> Register 輸出pin
  • SER.
    (Serial) Input for the next pin that gets shifted in.==> Register 輸入pin
  • SRCLK
    (Serial Clock) pulled high, 時 SER pin 狀態會丟入Register
  • RCLK
    (Register Clock) Needs to be pulled high to set the output to the new shift register values,  pulled high, 時 Register 內部8bit值會輸出到QA~QH. This must be pulled high directly after SRCLK has gone LOW again.
  • /SRCLR
    (Serial Clear) Will empty the whole Shift Register if pulled LOW, must be pulled High to enable.清除8bit register內容
  • /OE
    (Output Enable) This pin enables the output when tied to GND, & disabled when HIGH.=>控制輸出與否
  • OH' (pin 9) : You know how the last register just dumps its value when it is shifted over? 每次Shift Out 的值 (即bit 9的內容)





int main()
{
DDRB = 0xFF;
PORTB = 0x00;
char counter = 0;

while(1){
counter++; // Counter used for displaying a number in binary via the shift register
shift(PB1, PB2, PB3, counter); // PB1 = SERCLK ,PB2 = RCLK ,PB3 = SER
_delay_ms(500); 
shift(PB1, PB2, PB3, 0x00); // Set all pins to off
_delay_ms(500);
}
return 0;
}

//一次把要Latch 的8bit data 寫入,經由 SER pin及SCL的LOW-->HIGH8次, 再一次輸出 (RCLK LOW -->HIGH)
void shift(int SRCLK_Pin, int RCLK_Pin, int SER_Pin, unsigned long data)
{
PORTB &= ~(1 << RCLK_Pin); // Set the register-clock pin low

for (int i = 0; i < (8 * number_of_74hc595s); i++){ // Now we are entering the loop to shift out 8+ bits

        PORTB &= ~(1 << SRCLK_Pin); // Set the serial-clock pin low

        PORTB |= (((data&(0x01<<i))>>i) << SER_Pin ); // Go through each bit of data and output it

        PORTB |= (1 << SRCLK_Pin); // Set the serial-clock pin high

        PORTB &= ~(((data&(0x01<<i))>>i) << SER_Pin ); // Set the datapin low again 
}

PORTB |= (1 << RCLK_Pin); // Set the register-clock pin high to update the output of the shift-register
}







沒有留言 :

張貼留言