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.
Previous article:AVR microcontroller electronic clock C program
Next article:ATmega128 parallel control with font library 12864 program
- 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
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- EPLAN P8 Advanced Tutorial (Chinese)
- [ESP32-Audio-Kit Audio Development Board Review] Part 3 play_mp3_control
- PCB manufacturing experience summary (2)
- FPGA Design Process
- Power supply obstacles + power supply SBC selection and pitfalls
- Sharing of key points on the use of ADI passive components
- Four design considerations for telematics hardware in connected cars
- Wide voltage input USB-C, 5V/3A output adapter reference design
- When the machine is turned off, the LDO output is normally 3V. Why does the voltage reach 3.5V after the machine is turned on? What is the reason?
- Ginkgo USB-SPI host mode four data transmission mode timing diagram