SPI display program of AVR microcontroller and 74HC595

Publisher:Xiaohan521Latest update time:2015-07-21 Source: eechina Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Using SPI interface to control 74HC595 LED display

The full name of SPI interface is "Serial Peripheral Interface". SPI interface is mainly used in EEPROM, FLASH, real-time clock, AD converter, and between digital signal processor and digital signal decoder.

  The SPI interface is a synchronous serial data transmission between the CPU and peripheral low-speed devices. Under the shift pulse of the master device, data is transmitted bit by bit. It is full-duplex communication. The data transmission speed is generally faster than the I2C bus and can reach several Mbps.

  The SPI interface works in master-slave mode. This mode usually has one master device and one or more slave devices. Its interface includes the following four signals:

    (1) MOSI – Master data output, slave data input 
    (2) MISO – Master data input, slave data output 
    (3) SCLK – Clock signal, generated by the master device 
    (4) /SS – Slave enable signal, controlled by the master device

  In BASCOM, statements for the software-implemented SPI interface are provided. BASCOM also has statements that support hardware SPI. 
  The following three examples will illustrate how to use the SPI interface in BASCOM.

  Before introducing the example, let's first understand the hardware connection diagram, as shown in Figure 1.

   There are three 74HC595 chips in total in the figure, which control three digital tubes respectively. The three 74HC595 chips are cascaded through the Q7' pin. The DS pin of the first 74HC595 is connected to the MOSI pin of ATmega88, while the SH_CP pin is connected to the SCK pin of ATmega88, and the ST_CP pin is connected to the PB1 pin of ATmega88.

           1. Use the hardware SPI interface to control 74HC595
            '------------------------------------------------ -----------------------
            'Chip used: ATmega88,
            'Crystal oscillator: internal 8MHz crystal oscillator
            'email: support@avrbascom.com
            'Software version: Bascom-AVR 1.11.8.3 Full version
            '------------------------------------------------ -----------------------

                $regfile = "m88def.dat" 'The chip is ATmega88
                $crystal = 8000000 '8M crystal frequency

                $hwstack = 32 'Set the stack size
                $swstack = 10
                $framesize = 40

                Dim I As Byte
                Dim A(3) As Byte

                Latch Alias ​​Portb.1

                Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes ,
                Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 0
                                        'Define to use the hardware SPI of the microcontroller, SPI interrupt is not enabled, when data is sent, the high bit is sent first.
                                        'Set as SPI master, clock divided by 4
                Spiinit 'SPI initialization
                Restore Segtab 'Get the content of the data to be displayed
                    For I = 1 To 3
                          Read A(i)
                    Next I

                Spiout A(1) , 3 'Output the content to be displayed to 74HC595, output A(1), A(2), A(3) respectively

                Latch = 1? 'Enable display
                Wait 10
                Latch = 0

                Do

                Loop

                End
          '************************************************ ****************************
          '--------------------------Specialized glyph table------------------------------------------
          Segtab:
               Data &H77 , &H14 , &HE6 , &HB6 , &H95 , &HB3 , &HF3 , &H16 , &HF7 , &HB7
               '0 1 2 3 4 5 6 7 8 9

 

       2. Use software SPI interface to control 74HC595
          '------------------------------------------------ -----------------------
          'Chip used: ATmega88,
          'Crystal oscillator: internal 8MHz crystal oscillator
          'email: support@avrbascom.com
          'Software version: Bascom-AVR 1.11.8.3 Full version
          '------------------------------------------------ -----------------------


               $regfile = "m88def.dat" 'The chip is ATmega88
               $crystal = 8000000 '8M crystal frequency

               $hwstack = 32 'Set the stack size
               $swstack = 10
               $framesize = 40


               Const On = 1
               Const Off = 0

               Dim I As Byte
               Dim A(3) As Byte

               Latch Alias ​​Portb.1

               Config Spi = Soft , Dout = Portb.3 , Ss = None , Clock = Portb.5
                                                       'Define the use of software SPI, pb3 as the data output port, pb5 as the clock pin

               Spiinit 'SPI initialization
               Restore Segtab 'Get the content of the data to be displayed
                   For I = 1 To 3
                         Read A(i)
                   Next I

               Spiout A(1) ,3
                                       'Output the content to be displayed to 74HC595, output A(1), A(2), A(3) respectively

               Latch = Off 'Enable display
               Wait 10
               Latch = On

               Do

               Loop

               End
         '************************************************ *******************
         '--------------------------Specialized glyph table------------------------------------------
          Segtab:
               Data &H77 , &H14 , &HE6 , &HB6 , &H95 , &HB3 , &HF3 , &H16 , &HF7 , &HB7
               '0 1 2 3 4 5 6 7 8 9

 

       3. Use software SPI interface to control 74HC595
         '------------------------------------------------ -----------------------
         'Chip used: ATmega88,
         'Crystal oscillator: internal 8MHz crystal oscillator
         'email: support@avrbascom.com
         'Software version: Bascom-AVR 1.11.8.3 Full version
         '------------------------------------------------ -----------------------

 

               $regfile = "m88def.dat" 'The chip is ATmega88
               $crystal = 8000000 '8M crystal frequency

               $hwstack = 32 'Set the stack size
               $swstack = 10
               $framesize = 40

               Const On = 1
               Const Off = 0

               Ledata Alias ​​Portb.3 'Data output port
               Ledclk Alias ​​Portb.5 'clock output port
               Latch Alias ​​Portb.1

               Dim I As Byte
               Dim A(3) As Byte

 

 

               Restore Segtab 'Get the content of the data to be displayed
               For I = 1 To 3
                    Read A(i)
               Next I

 

               For I = 3 To 1 Step -1
                     Shiftout Ledata , Ledclk , A(i) , 1
                 'Output the content to be displayed to 74HC595
               Next I

               Latch = Off 'Enable display
               Wait 10
               Latch = On

               Do

               Loop

               End
         '************************************************ *******************
         '--------------------------Specialized glyph table------------------------------------------
         Segtab:
              Data &H77 , &H14 , &HE6 , &HB6 , &H95 , &HB3 , &HF3 , &H16 , &HF7 , &HB7
             '0 1 2 3 4 5 6 7 8 9

 

[page]

 

//******************************************

// 74HC595/165 Driver

// (165,595 are both two pieces)

//******************************************

unsigned int LED;

unsigned int KEY_DATA;

void Driver_595(unsigned char Bit, unsigned char State)

{

    if(State)

       LED|=(0x00001<>8));

    set_bit(1,0);

    clr_bit(1,0);

    SPI_W_R((int)LED);

    set_bit(1,0);

    clr_bit(1,0);

}

void Port_Init(void)

{

    DDRB=0xff;

PORTB=0x00;

}

//************************************************ ****************

void Driver_165(void) //Bite takes the bit 0~15, State is the state of the changed bit 0, 1

{

    unsigned char a1;

    unsigned char a2;

    clr_bit(1,3);  

set_bit(1,3);

    a1=SPI_W_R(0x00); //74HC165 input enable       

a2=SPI_W_R(0x00); //Read key value

KEY_DATA=a1;

KEY_DATA<<=8;

KEY_DATA+=a2;

return;

}

//************************************************ *******************

void set_bit(unsigned char port,unsigned char n) //port is the port number, 0~3 represents A~D, n is the pin number of the port 0~7

{

switch(port)

{

 case 0x00:

 PORTA|=(0x01<>7)==0x00); //Wait for data to be sent

return SPDR; //Return received data

}

//************************OK****************************** ***************

 


    Summary: The above are examples of using the SPI interface to control 74HC595 by AVR microcontrollers. These examples can be applied to other chips with SPI after simple modifications, such as the AT45DB series Flash memory chips with SPI interface, the DS1302 clock chip with SPI interface, the AD or DA chip with SPI interface, etc.

    AVR mega series microcontrollers generally have hardware SPI interfaces. It is recommended that customers use hardware SPI interfaces, which can occupy less hardware and software resources. However, software implementation of SPI interfaces also has certain advantages, such as the IO port it uses is not fixed and can be specified as needed, and the data it sends at a time can be specified as needed instead of 8 bits.

      Shiftout VS Spiout

These two statements are SPI output statements, corresponding to them are shiftin and spiin.
    Shiftout statements can be used in hardware spi or software spi interface, determined by config statement: 
          Config Spi = Hard/soft
    Shiftout statement is used in software spi interface.

Reference address:SPI display program of AVR microcontroller and 74HC595

Previous article:AVR microcontroller electronic clock C program
Next article:ATmega128 parallel control with font library 12864 program

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号