⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi_master.c

📁 用ICC编写的程序包括SPI
💻 C
字号:
#include "SPI_master.h"

unsigned char SPI_rx_buff[SIZE];  
unsigned char SPI_tx_buff[SIZE];  
unsigned char rx_wr_index,rx_rd_index,rx_counter,rx_buffer_overflow;  
unsigned char tx_wr_index,tx_rd_index,tx_counter,SPI_ok,SPI_datain,temp; 

#pragma interrupt_handler spi_stc_isr:11  
void spi_stc_isr(void)  
{  
  //PORTB|=BIT(SS);
    if (tx_counter)        //如果发送缓冲区中有待发的数据  
  {  
     --tx_counter;  
	 
	 SPDR = SPI_tx_buff[tx_rd_index]; //发送一个字节数据,并调整指针  
     SPI_ok=0;
	 if (++tx_rd_index == SIZE) tx_rd_index = 0;  
  }
  SPI_rx_buff[rx_wr_index] = SPDR;    //从ISP口读出收到的字节  
  
  SPI_datain=1;
  SPI_ok=1;
  if (++rx_wr_index == SIZE) rx_wr_index = 0;    //放入接收缓冲区,并调整队列指针  
  if (++rx_counter == SIZE)  
  {  
    rx_counter = 0;  
    rx_buffer_overflow = 1;  
  }  
   //PORTB&=~BIT(SS);
}  

unsigned char getSPIchar(void)  
{  
  unsigned char data;  
  while (rx_counter == 0);     //无接收数据,等待  
  data = SPI_rx_buff[rx_rd_index];    //从接收缓冲区取出一个SPI收到的数据  
  if (++rx_rd_index == SIZE) rx_rd_index = 0;    //调整指针  
  CLI();  
  --rx_counter;  
  SEI();  
  return data;  
}  

void putSPIchar(char c)  
{  
  while (tx_counter == SIZE);//发送缓冲区满,等待  
  CLI();  
  if (tx_counter || (SPI_ok==0))//发送缓冲区已中有待发数据  
  {                //或SPI正在发送数据时  
    SPI_tx_buff[tx_wr_index] = c;    //将数据放入发送缓冲区排队  
    if (++tx_wr_index == SIZE) tx_wr_index = 0;    //调整指针  
    ++tx_counter;  
  }  
  else  
    {
	//PORTB&=~BIT(SS);
	SPDR = c;        //发送缓冲区中空且SPI口空闲,直接放入SPDR由SIP口发送  
    temp=SPDR;
	SPI_ok=0;
	}
  SEI();  
}  

	   	   			 //主机模式
void spi_init(void)  
{    
  DDRB |= BIT(MOSI)|BIT(SCK)|BIT(SS);    //MISO=input and MOSI,SCK,SS = output 
  DDRB &=~BIT(MISO); 
  PORTB|= BIT(SCK)|BIT(MOSI)|BIT(SS);    //MISO上拉电阻有效   
  SPCR = 0xD4;    //SPI允许,主机模式,MSB,允许SPI中断,极性方式01,1/4系统时钟速率  
  SPSR = 0x00;  
  temp = SPSR;  
  temp = SPDR;    //清空SPI,和中断标志,使SPI空闲
  SPI_ok=1;
  SPI_datain=0;  
}  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -