Implementing SPI data transfer via low-level AVR methods

Publisher:EnchantingEyesLatest update time:2020-07-25 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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<40 }

41 

42 byte ReadByte(void)

43 {

44   while(!(SPSR&(1<45   return SPDR;

46 }

47 

48 void WriteByte(byte value)

49 {

50   SPDR = value;

51   while(!(SPSR&(1<52   return ;

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<22 }

23 

24 byte ReadByte(void)

25 {

26   while(!(SPSR&(1<27   return SPDR;

28 }

29 

30 void writeByte(byte value)

31 {

32   SPDR = value;

33   while(!(SPSR&(1<34   return ;

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 }


Reference address:Implementing SPI data transfer via low-level AVR methods

Previous article:AVR microcontroller controls light emitting diodes
Next article:Old article backup: Analysis of AVR reading and writing EEPROM

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
Guess you like

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号