AVR Learning Experience (Part 3)

Publisher:独行于世Latest update time:2016-07-18 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
 AVR mega128 SPI control HC595

      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<

  •    SPI control register SPCR:

           SPCR=(1<

  •     SPI data register SPDR:

            SPDR = Data; /* Start data transfer */

  •     SPI status register SPSR:

            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 SCK 1
#define MOSI 2
#define MISO 3

#define SS_H() PORTB|=(1< #define SS_L() PORTB&=~(1<

Send data from HC595 to storage register

void SPI_MasterTransmit(char Data)
{
   /* Start data transmission*/
   SPDR = Data;
   /* Wait for transmission to end*/
   while(!(SPSR & (1<    ;
}

Data is output from the parallel port of 595

void HC_595_OUT(unsigned char data) 
{
   SS_L(); //Pull down the storage register clock
   SPI_MasterTransmit(data); //Transmit data to HC595
   SS_H(); //Pull up the storage register clock
}

 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
#define SS_H() PORTB|=(1< #define SS_L() PORTB&=~(1< //Shift register clock input
#define SCK_H() PORTB|=(1< #define SCK_L() PORTB&=~(1< //Serial data input
#define MOSI_H() PORTB|=(1< #define MOSI_L() PORTB&=~(1<

Send data from HC595 to storage register

void HC_595_input(unsigned char data)
{
   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;  
   }
}

Data is output from the parallel port of 595

 void HC_595_output(unsigned char data)
{
   SS_L(); //Storage register clock pulls down the clock  
   HC_595_input(data);
   SS_H(); //Storage register clock pulls up the clock
}

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<

}

Keywords:AVR Reference address:AVR Learning Experience (Part 3)

Previous article:AVR Timer Usage Example
Next article:Serial Design Based on SPI of AVR Microcontroller

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号