After many days of hard work, I finally have a comprehensive understanding of AVR controlling HC595 to drive digital tubes. I will write down my design experience of AVR controlling HC595 below:
There are two ways to control HC595 with AVR:
One: Use the SPI interface control that comes with AVR;
One method: Use the AVR port to simulate SPI control.
Schematic diagram:
illustrate:
1. When using the SPI bus interface of AVR, pay attention to the use of several SPI registers:
- Set the SPI pin status:
DDRB|=(1< SPCR=(1< SPDR = Data; /* Start data transfer */ while(!(SPSR & (1< For HC595, you only need to pay attention to the timing. For SPI interface, the timing of the shift register is automatically generated by AVR, so you don't need to worry about it. Only the timing of the storage register is controlled by software. #define SS 0 #define SS_H() PORTB|=(1< Send data from HC595 to storage register void SPI_MasterTransmit(char Data) Data is output from the parallel port of 595 void HC_595_OUT(unsigned char data) 2. AVR simulates SPI bus to control HC595: This method requires less knowledge of microcontrollers. Just set the ports connecting the AVR and HC595 to output, and then control the 595 according to the timing of the 595. //Storage register clock input Send data from HC595 to storage register void HC_595_input(unsigned char data) Data is output from the parallel port of 595 void HC_595_output(unsigned char data) Summarize: From the above functions, we can see that the only thing that needs to be changed in these two methods is the function of "sending data from HC595 to storage register". The following function of "outputting data from 595 parallel port" is exactly the same. The function of the first method is simpler, but the setting is more complicated. You need to understand the function and setting method of AVR register. The second method does not require too much setting. You can control without understanding the internal register of the microcontroller. The function is more complicated. But you only need to understand the timing of 595 to control it, and it is easy to transplant. /*SPI interface initialization*/ void SPI_MasterInit(void) { DDRB |= (1< PORTB=~((1< //Configure the pull resistor SPCR = (1< | (1< }
#define SCK 1
#define MOSI 2
#define MISO 3
{
/* Start data transmission*/
SPDR = Data;
/* Wait for transmission to end*/
while(!(SPSR & (1<
}
{
SS_L(); //Pull down the storage register clock
SPI_MasterTransmit(data); //Transmit data to HC595
SS_H(); //Pull up the storage register clock
}
#define SS_H() PORTB|=(1<
#define SCK_H() PORTB|=(1<
#define MOSI_H() PORTB|=(1<
{
unsigned char i;
for(i=0;i<8;i++)
{
SCK_L();//Shift register clock pulls down the clock
if(data&0x80) //Highest bit judgment
{
MOSI_H(); //If the highest bit is 1, write 1 to the DS port
}
else
{
MOSI_L(); //If the highest bit is 0, write 0 to the DS port
}
SCK_H();//Shift register clock pulls up the clock
delay_1us();
data<<=1;
}
}
{
SS_L(); //Storage register clock pulls down the clock
HC_595_input(data);
SS_H(); //Storage register clock pulls up the clock
}
Previous article:AVR Timer Usage Example
Next article:Serial Design Based on SPI of AVR Microcontroller
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- 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
- C language STM8 program help
- Will Wi-Fi 6 replace Wi-Fi 5 as the mainstream wireless standard?
- Considerations for Successive Approximation Register ADC Input
- Today's live broadcast: The new Infineon MOSFET based on Source-down technology effectively improves power density, which is visible to the naked eye
- DIY a WS2812 clock
- [NUCLEO-L552ZE Review] Unboxing and Onboard Resource Analysis
- [Xianji HPM6750 Review] PWM Control Buzzer Sound
- Interface ov5640_camera
- 51 MCU 16_16 dot matrix example
- [NXP Rapid IoT Review] + How to import the project downloaded from WEB IDE into MCUXpresso IDE and debug it?