SPI Program for AVR

Publisher:BlissfulMoonLatest update time:2016-01-07 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
An example of SPI, using SPI to realize two-machine communication, using interrupt mode to realize full-duplex communication.
 This example is implemented with two MEGA8515, and the connection is:
   MISO----MISO
   MOSI----MOSI
   SCK ----SCK
   /SS ----/SS
 The function fill_tx_buffer that loads the data to be sent into the send buffer and the function read_rx_buffer that reads the data from the receive buffer are not given, please complete them according to your own needs.

#define SPI_RX_BUFFER_SIZE 10
#define SPI_RX_BUFFER_MASK ( SPI_RX_BUFFER_SIZE - 1 )
#define SPI_TX_BUFFER_SIZE 10
#define SPI_TX_BUFFER_MASK ( SPI_TX_BUFFER_SIZE - 1 )
#define SET_SPI_MODE PORTB.4
#define SPI_MODE PINB.4

static unsigned char SPI_RxBuf[SPI_RX_BUFFER_SIZE];
static volatile unsigned char SPI_RxHead;
static unsigned char SPI_TxBuf[SPI_TX_BUFFER_SIZE];
static volatile unsigned char SPI_TxHead;
//******************************************
// SPI interrupt service routine
//******************************************
interrupt [SPI_STC] void spi_isr(void)
{
unsigned char data;
if(spi_m==0)                        //If spi_m is 0, it indicates receiving state
{
 data = SPDR;                      //Read received
   dataSPI_RxBuf[SPI_RxHead-1] = data;        //Store received data into receiving bufferif
   ( SPI_RxHead == SPI_RX_BUFFER_MASK )   //If it is the last data of receiving frame  
   {
     SPI_RxHead = 0;                //Restore received dataMSTR
     =1;                      //Receiving completed, set SPI back to main modespi_trans_com
     =1;                //Set receiving completed flag
   }
   else
   {  
     SPI_RxHead++;                  //Received data counter plus 1
   }
 
}
else                              //If spi_m is 1, it indicates sending state
 {
                         
   if ( SPI_TxHead <= SPI_TX_BUFFER_MASK)   //If the data to be sent has not been sent completely
   {  
     SPDR = SPI_TxBuf[SPI_TxHead];      //Get data from the transmit buffer and send
     SPI_TxHead++;                  //Sent data counter plus 1
   }
   else                        //If all the data to be sent have been sent
     {
     SPI_TxHead=0;                //Sent data counter restored
     DDRB.4=0;
     SET_SPI_MODE=1;              //Release the bus so that the receiver can enter the main transmission.
     spi_m=0;                    
     spi_sending=0;                //Clear the sending flag
     }
 
 }

}

//**********************************************
// SPI initialization
//**********************************************
void InitSPI(void)
{

SPCR=0x52;
SPI_RxHead = 0;
SPI_TxHead = 0;

}

//**********************************************
//Send data
//**********************************************
void spi_send(void)
{
if(spi_sending==0)    //The sending flag is 0, indicating that spi sending is idle
 {
 
 fill_tx_buffer();   //Call fill_tx_buffer function to load the data to be sent into the send bufferwhile
 (PINB.4==0)  //If PINB.4 is low, it indicates that the bus is occupied by the receiver. Wait until the receiver completes sending.
 {;}
 
 InitSPI();      //Initialize SPI to master mode
 DDRB.4=1;
 SET_SPI_MODE=0;   //Pull PORTB.4 low to force the receiver to enter slave mode
 spi_m=1;        //Set spi_m flag to indicate sending state
 delay_us(10);
 spi_sending=1;    //Set spi_sending flag to indicate sending in progress
 SPDR=0xFF;      //Start sending, the first data received by the receiver is 0xFF and should be ignored
 SPIE=1;        //Open SPI interrupt,
 SPI_TxHead = 0;   //Clear the sent data counter
 }
}

void main(void)
{

...
while(1)
{
 ...
 
 if(spi_trans_com==1) //If the receive completion flag is 1, it indicates that some data has been received
 {
   read_rx_buffer(); //Call read_rx_buffer function to read the received data from the receive buffer
   spi_trans_com=0; //Clear the receive completion flag after reading
 }
 ...

}

}
Keywords:AVR Reference address:SPI Program for AVR

Previous article:Design of Solar Controller Based on AVR Microcontroller
Next article:Solution to the problem of AVR chip deadlock caused by wrong fuse position

Latest Microcontroller Articles
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号