This article is only for beginners, experts please skip it!
1. Experimental purpose
Use the IO port of the microcontroller to simulate the SPI bus timing logic, control 24 LEDs through 74HC595, and learn how to use 74HC595 serial control.
2. Experimental content
Write a program to light up all 24 LEDs first, and then turn them off one by one from left to right at intervals of one second.
3. Hardware Principle
The hardware schematic diagram of 74HC595 serial control LED lamp is shown in the figure below. Use a flat cable to connect to terminal P3, then the 0-2 pins of the corresponding port of the microcontroller are connected to the CS_595_1, CLK_595_1, and Data_595_1 pins of terminal P3 (for example, use a flat cable to connect P9 to terminal P3, then the PC0 pin is connected to CS_595_1, the PC1 pin is connected to CLK_595_1, and the PC2 pin is connected to Data_595_1). CS_595_1 is the chip select pin of 74HC595, which is used to control the latch between the internal shift register and the pin of 74HC595. CLK_595_1 is the clock pin of 74HC595. Every cycle change of this pin will input 1 bit of data into the internal shift register of 74HC595. Data_595_1 is the data pin, and its high and low values will be sent to the internal shift register of 74HC595 within a cycle of CLK_595_1 change.
For a 74HC595, the working process is to use CS_595_1 to close the latch between the internal shift register and the pin of the 74HC595, prepare data on the Data_595_1 pin, operate CLK_595_1 to change it for 1 cycle, and the corresponding high and low values of Data_595_1 are sent to the 0th bit of the internal shift register of the 74HC595. This cycle is repeated 8 times, and 8 bits of data are input to the internal shift register of the 74HC595. The first input is at the 7th bit of the internal shift register of the 74HC595, and the last input is at the 0th bit of the internal shift register of the 74HC595. After the data is input, operate CS_595_1 to open the latch between the internal shift register and the pin of the 74HC595, so that the value of the internal shift register of the 74HC595 is presented on the pin, and then close the latch to continue the subsequent operation.
For the control of multiple 74HC595, the three pins CS_595_1, CLK_595_1, and Data_595_1 are still used. From the schematic diagram, we can see that the Q7 pin of the first 74HC595 is connected to the Data pin of the second 74HC595, and the Q7 pin of the second 74HC595 is connected to the Data pin of the third 74HC595. This connection can control multiple 74HC595. Take three 74HC595 as an example, prepare 3 bytes of data before operation, and send the 3 bytes of data to the first 74HC595 in three times according to the method of operating one 74HC595. Then the first byte will be transmitted to the third 74HC595, and the last byte will be transmitted to the first 74HC595. After the data is transferred, as in the above method, operate CS_595_1 to open the latch between the internal shift register of 74HC595 and the pin, so that the value of the internal shift register of 74HC595 is displayed on the pin, and then close the latch to continue the subsequent operation.
4. Experimental reference procedures and annotations
Change the mapping layer 0_logiclayer_config.h file. The following program enables HC595 so that its driver can be added to the compilation process and prompts the user to configure 595 in the corresponding config file of HC595.
#define EXTERNAL_MODULE_HC595_MODE 1 //0: HC595 chip is not used
//1: Use the HC595 chip and set its pin configuration in the corresponding config file
Change the mapping layer 3_ExternalModulelayer_HC595_config.h file. The following program changes should be completed according to the schematic diagram. When P9 and P3 terminals are connected, PORTC port is used to control HC595, so the corresponding HC595_PORT and HC595_DDR are defined as PORTC and DDRC respectively. HC595_CLOCK, HC595_CS and HC595_MOSI are specific pin numbers. Just check the schematic diagram and write them.
#define HC595_PORT PORTC
#define HC595_DDR DDRC
#define HC595_CLOCK BIT1
#define HC595_CS BIT0
#define HC595_MOSI BIT2
Modify the logic layer 0_logiclayer_main.c file
//Main program
int main(void)
{
// Target board initialization, this function will automatically initialize the corresponding peripheral files
TARGET_Init();
//Background main loop
while(1)
{
//Complete your own project logic here
HC595_WriteByte(0x00); //Write 1 byte of data to HC595. After the whole operation is completed, the data is written to the third HC595
HC595_WriteByte(0x00); //Write 1 byte of data to HC595. After the whole operation is completed, the data is written to the second HC595
HC595_WriteByte(0x00); //Write 1 byte of data to HC595. After the whole operation is completed, the data is written to the first HC595
HC595_SELECT; //Open the latch between the HC595 internal shift register and the pin
// Make the value of the 74HC595 internal shift register appear on the pin, and then close the latch
TARGET_Delayms(1000,1);
HC595_WriteByte(0x01); // Column 1 is off, the binary of 0x01 is 00000001, please note that the LED in the circuit is lit at low level
HC595_WriteByte(0x01);
HC595_WriteByte(0x01);
HC595_SELECT;
TARGET_Delayms(1000,1);
HC595_WriteByte(0x03); // Columns 1-2 are off, the binary value of 0x03 is 00000011, please note that the LED in the circuit is lit at low level
HC595_WriteByte(0x03);
HC595_WriteByte(0x03);
HC595_SELECT;
TARGET_Delayms(1000,1);
HC595_WriteByte(0x07); // Columns 1-3 are off, the binary value of 0x07 is 00000111, please note that the LED in the circuit is lit at low level
HC595_WriteByte(0x07);
HC595_WriteByte(0x07);
HC595_SELECT;
TARGET_Delayms(1000,1);
HC595_WriteByte(0x0f); // Columns 1-4 are off, the binary value of 0x0f is 00001111, please note that the LED in the circuit is lit at low level
HC595_WriteByte(0x0f);
HC595_WriteByte(0x0f);
HC595_SELECT;
TARGET_Delayms(1000,1);
HC595_WriteByte(0x1f); // Columns 1-5 are off, the binary value of 0x1f is 00011111, please note that the LED in the circuit is lit at low level
HC595_WriteByte(0x1f);
HC595_WriteByte(0x1f);
HC595_SELECT;
TARGET_Delayms(1000,1);
HC595_WriteByte(0x3f); // Columns 1-6 are off, the binary value of 0x3f is 00111111, please note that the LED in the circuit is lit at low level
HC595_WriteByte(0x3f);
HC595_WriteByte(0x3f);
HC595_SELECT;
TARGET_Delayms(1000,1);
HC595_WriteByte(0x7f); // Columns 1-7 are off, the binary value of 0x7f is 01111111, please note that the LED in the circuit is lit at low level
HC595_WriteByte(0x7f);
HC595_WriteByte(0x7f);
HC595_SELECT;
TARGET_Delayms(1000,1);
HC595_WriteByte(0xff); // Columns 1-8 are off, the binary value of 0xff is 11111111, please note that the LED in the circuit is lit at low level
HC595_WriteByte(0xff);
HC595_WriteByte(0xff);
HC595_SELECT;
TARGET_Delayms(1000,1);
/*
********************************************************************************
Feed the dog statement, most projects should not remove it
********************************************************************************
*/
#if WDT_MODE!=0
TARGET_WatchDogReset();
#endif
}
return 0; //never execute
}
Previous article:AVRWARE++ Development Notes 8: Atmel Studio removes spell check
Next article:AVRWARE++ Development Notes 6: 8-way direct I/O port control LED light experiment
Recommended ReadingLatest update time:2024-11-16 17:42
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- ATmega16 MCU C language programming classic example (Chen Zhongping)
- STC MCU Principles and Applications: Analysis and Design from Devices, Assembly, C to Operating Systems: A Three-Dimensional Tutorial (He Bin)
- Arduino Development Guide: STM32
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- RA2L1 MCU download program JLink Info: T-bit of XPSR is 0 but should be 1.
- Silicon carbide industry chain science video: substrate and epitaxy
- Senior foreigner explains the misunderstandings of high-speed PCB design and testing (video-Chinese)
- After zigbee sends data to nb-iot, nb-iot sends data to the cloud, but the data displayed on the cloud is completely wrong. How to solve it?
- Notice: The new markdown editor is now online!!
- Design of password lock using AT89S52 single chip microcomputer
- [Review of Arteli Development Board AT32F421] + Have a good understanding of the power supply voltage
- 【Development and application based on NUCLEO-F746ZG motor】4. Share information
- encryption
- Motor control basics - timer basics and PWM output principles