Host side:
1 /*********************************
2 Code function: Implement SPI data transmission (host side) through the underlying AVR method
3. Creation time: 2016*10*17
4. Use resources:
5 Lower-level aTmega (AVR) library
6 SPI three registers SPCR (control register), SPCR (status register), SPDR (data register)
7 SPCR (SPI Control Register)
8 7bit SPIE Chip select enable, SPIE = 1; chip select is invalid, SPIE = 0 chip select is valid (the host does not need chip select) (stand-alone communication also does not need chip select)
9 6bit SPE
10 5bit DORD
11 4bit MSTR
12 3bit CPOL clock polarity, CPOL = 0 (clock idle is low level); CPOL = 1 (clock idle is high level)
13 2bit CPHA phase polarity, CPHA = 0 (indicates the first edge); CPHA = 1 (indicates the second edge); both represent data sampling and the time when the data is valid.
14 1bit SPR1
15 0bit SPR0
16
17
18
19
20 SPCR (SPI Status Register)
21 SPDR (SPI Control Register data register)
22
23 Author’s email: jikexianfeng@outlook.com
24 ********************************/
25 #define SCK_PIN 13 //Macro definition of clock port
26 #define MISO_PIN 12 //Macro definition of host output line
27 #define MOSI_PIN 11 //Macro definition of slave output line
28 #define SS_PIN 10 //Chip select
29 boolean SlaveDataFlag; //Data flag
30
31 void MasterInit(void) //Initialize SPI pin settings
32 {
33 pinMode(SCK_PIN,OUTPUT); //SCK_PIN is output mode (host mode)
34 pinMode(MOSI_PIN,OUTPUT); //Initialize host output bus
35 pinMode(MISO_PIN,INPUT); //Initialize slave output bus
36 pinMode(SS_PIN,OUTPUT); //Initialize chip select pin (high level for host, low level for slave)
37 //Start SPI
38 SPCR = B00000000;
39 SPCR = (1< 41 42 byte ReadByte(void) 43 { 44 while(!(SPSR&(1< 46 } 47 48 void WriteByte(byte value) 49 { 50 SPDR = value; 51 while(!(SPSR&(1< 53 } 54 55 void setup() 56 { 57 Serial.begin(115200); 58 MasterInit(); 59 Serial.println("jikexianfeng@outlook.com"); 60 digitalWrite(SS_PIN,HIGH); 61 62 SlaveDataFlag = true; 63 } 64 65 void loop() 66 { 67 byte rxData; 68 while(true) 69 { 70 if(SlaveDataFlag) 71 { 72 digitalWrite(SS_PIN,LOW); 73 WriteByte(17); 74 Serial.println("Done writing data..."); 75 Serial.println("Reading data from slave ..."); 76 rxData = ReadByte(); 77 digitalWrite(SS_PIN,HIGH); 78 Serial.println("Dome Reading data ..."); 79 Serial.print("From Slave :"); 80 Serial.println(rxData,DEC); 81 SlaveDataFlag = false; 82 } 83 } 84 } From the machine: 1 /********************************* 2 Code function: Implement SPI data transmission (host side) through the underlying AVR method 3. Creation time: 2016*10*17 4. Use resources: 5 Lower-level aTmega (AVR) library 6 Author’s email: jikexianfeng@outlook.com 7 ********************************/ 8 #define SCK_PIN 13 9 #define MISO_PIN 12 10 #define MOSI_PIN 11 11 #define SS_PIN 10 12 13 void SlaveInit(void) 14 { 15 pinMode(SCK_PIN,INPUT); 16 pinMode(MISO_PIN,INPUT); 17 pinMode(MISO_PIN,OUTPUT); 18 pinMode(SS_PIN,INPUT); 19 20 SPCR = B00000000; 21 SPCR = (1< 23 24 byte ReadByte(void) 25 { 26 while(!(SPSR&(1< 28 } 29 30 void writeByte(byte value) 31 { 32 SPDR = value; 33 while(!(SPSR&(1< 35 } 36 37 void setup() 38 { 39 Serial.begin(115200); 40 SlaveInit(); 41 } 42 43 void loop() 44 { 45 if(digitalRead(SS_PIN) == LOW) 46 { 47 writeByte(19); 48 byte rxData; 49 rxData = ReadByte(); 50 Serial.print("Command :"); 51 Serial.println(rxData); 52 } 53 }
Previous article:AVR microcontroller controls light emitting diodes
Next article:Old article backup: Analysis of AVR reading and writing EEPROM
- 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