CODE | Shift Register 8-Bit Binary Counter - Arduino Project 017

RonWang2 years ago (2023-10-07)电子编程 COD2

In this project, you’re going to use additional ICs (Integrated Circuits) in the form of shift registers in order to drive LEDs to count in binary (I will explain what binary is soon). Specifically, you will drive eight LEDs independently using just three output pins from the Arduino.

Project 17 Shift Register 8-Bit Binary Counter

/* Coding Ron Wang
   Sep.10th 2024
   Autaba support for coding hardware
   Project 17 Shift Register 8-Bit Binary Counter
 */
int latchPin = 8; //Pin connected to Pin 12 of 74HC595 (Latch)
int clockPin = 12; //Pin connected to Pin 11 of 74HC595 (Clock)
int dataPin = 11; //Pin connected to Pin 14 of 74HC595 (Data)
void setup() {
 //set pins to output
 pinMode(latchPin, OUTPUT);
 pinMode(clockPin, OUTPUT);
 pinMode(dataPin, OUTPUT);
}
void loop() {
 //count from 0 to 255
 for (int i = 0; i < 256; i++) {
 //set latchPin low to allow data flow
 digitalWrite(latchPin, LOW);
 shiftOut(i);
 //set latchPin to high to lock and send data
 digitalWrite(latchPin, HIGH);
 delay(1000);
 }
}
void shiftOut(byte dataOut) {
 // Shift out 8 bits LSB first, on rising edge of clock
 boolean pinState;
 digitalWrite(dataPin, LOW); //clear shift register ready for sending data
 digitalWrite(clockPin, LOW);
 for (int i=0; i<=7; i++) { // for each bit in dataOut send out a bit
 digitalWrite(clockPin, LOW); //set clockPin to LOW prior to sending bit
 // if the value of DataOut and (logical AND) a bitmask
 // are true, set pinState to 1 (HIGH)
 if ( dataOut & (1<<i) ) {
 pinState = HIGH;
 }
 else {
 pinState = LOW;
 }
 //sets dataPin to HIGH or LOW depending on pinState
 digitalWrite(dataPin, pinState); //send bit out on rising edge of clock
 digitalWrite(clockPin, HIGH);
 }
 digitalWrite(clockPin, LOW); //stop shifting out data
}

Arduino Shift Register 8-Bit Binary Counter Circuit

Arduino Shift Register 8-Bit Binary Counter Schematic

Share with Friends:

Related Articles

CODE | Variables - Arduino Programming Basic

CODE | Variables - Arduino Programming Basic

Arduino 程序基础,介绍Arduino程序的基本组成,第一部分编写了10个例子,关于变量及变量名称,串口监视器,if循环,for循环,while循环等。第二部分介绍了函数,全局变量,局部变量和静…

CODE | SD Card Information Basic - Arduino Project 042

CODE | SD Card Information Basic - Arduino Project 042

Arduino Programming Basic -- Reading and Writing to an SD CardSD cards have non-volatile flash memor…

Setting Up an LNMP Environment on CentOS 7.9 64-bit

Setting Up an LNMP Environment on CentOS 7.9 64-bit

CentOS 7.9 64位 搭建 LNMP环境What is a LAMPLAMP is a free and open source software suite combines four po…

CODE | IR Remote Control Light - Arduino Project 050

CODE | IR Remote Control Light - Arduino Project 050

An IR remote and receiver communicate with each other by transmitting and decoding a signal in the f…

C03 |  Program Structure

C03 | Program Structure

C语言教程03:C语言程序结构一个 C 语言程序由一个固定名称为 main 的主函数和若干个其他函数(可没有)组成。下面通 过几个例题,总结出 C 语言程序的结构特点。在 Dev C++环境下编写的第…

CODE | Human Body Infrared Detector and Relay Light - Arduino Project 048

CODE | Human Body Infrared Detector and Relay Light - Arduino Project 048

Project 48 Human Body Infrared Detector and Relay LightThis Infrared Human Sensor referenc…

Post a Comment

Anonymous

Feel free to share your thoughts and opinions here.