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

📄 stm32f10x_spi.c

📁 CAN 程序使用MCP2515 由STM32做主芯片控制
💻 C
字号:
/*******************************************************************************
* File Name          : spi.c
* Author             : 
* Date First Issued  : 03/31/2008
* Description        : Main program body
********************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "..\header\stm32f10x_reg.h"
#include "..\header\type.h"
#include "..\header\hardware.h"
#include "..\header\spi.h"
/***----------------------------------------------****/
//-----------------Function Prototype--------------------//
//----------------------------------------------------//
/*---------------------------------------------------------------
NAME       : spi_initialize
ENTRY  TYPE: unsigned int
RETURN TYPE: int
PURPOSE    : ---
VERSION    : 0.0.0
DESCRIPTION:
CREATE DATE: ---
LOGS       : 1.first create;

-----------------------------------------------------------------*/

//-----------------------------------------------------//
u16 spi_initialize(void)
{
      //pa5,pa6,pa7 set as the spi mode;
  // GPIOA->CRL.B.CNF4  =2;
  // GPIOA->CRL.B.MODE4 =3 ;
   GPIOA->CRL.B.CNF5  =0   ;  //CLK
   GPIOA->CRL.B.MODE5 =1  ;				
    GPIOA->CRL.B.CNF6 =1  ;	  //MISO
   GPIOA->CRL.B.MODE6 =0  ;
    GPIOA->CRL.B.CNF7 =0 ;	  //MOSI
   GPIOA->CRL.B.MODE7 =1 ;
  // GPIOA->BSRR.B.SETIO4=1;
  	/*
   SPI1->CR1.W=0x0000;
   SPI1->CR2.W=0x0000;
   SPI1->SR.W=0x0002;
   SPI1->CR1.B.SPE=0;   // spi=1,enable the spi;
   SPI1->CR1.B.CPHA=0;	//从第一个时钟开始采样;
   SPI1->CR1.B.CPOL=0;	//SCK低电平有效;   
   SPI1->CR1.B.BR=3;	//set the baud rate, Fclk/2;
   SPI1->CR1.B.LSBFIRST=0;//=1,LSB first;
   SPI1->CR1.B.DFF=0;//dff=1,16bit send and receive the data;
   SPI1->CR1.B.BIDIMODE=0;// SPI1->CR1.B.BIDIOE=1;//bidioe=1,only send data,bidioe=0,only receive data;
  // SPI1->CR1.B.SSM=1;
  // SPI1->CR1.B.SSI=1;
  // SPI1->CR2.B.SSOE=1;//Eable the NSS;
   //SET PIN IN OUTPUT MODE;
   SPI1->CR1.B.MSTR=1;  //choice the master device; mstr=1 set as the master;
  // SPI1->CRCPR=7;
   SPI1->CR1.B.SPE=1;   // spi=1,enable the spi;
   	  */
   return 0;
}  
void delay(u32 n)
{
   int i,j;
   for(i=0;i<n;i++)
   for(j=0;j<10;j++)
   ;
}
/*---------------------------------------------------------------
NAME       : write_data
ENTRY  TYPE: unsigned int
RETURN TYPE: int
PURPOSE    : ---
VERSION    : 0.0.0
DESCRIPTION:
CREATE DATE: ---
LOGS       : 1.first create;

-----------------------------------------------------------------*/
 /*
void write_data(u16 data)
{   
   while(SPI1->SR.B.BSY==1);			 
   SPI1->DR=data 	;
    while(SPI1->SR.B.TXE==0);
   //return 0;

} */
unsigned short write_data(unsigned short data)
{
	unsigned short i;

	for(i=0;i<8;i++)
	{ 	
	    
	 if(data & 0x80) MOSI_H;
	    
		else  MOSI_L;

 	    CLK_ON; 
	    delay(1);
       CLK_OFF;
		delay(1); 
	  
	   	data<<=1; 
	 }  
	 return 0;
} 
	
/*---------------------------------------------------------------
NAME       : read_data
ENTRY  TYPE: unsigned int
RETURN TYPE: int
PURPOSE    : ---
VERSION    : 0.0.0
DESCRIPTION:
CREATE DATE: ---
LOGS       : 1.first create;

-----------------------------------------------------------------*/
 /*
u16 read_data(void)
{
 // u16 data;
 // while(SPI1->SR.B.RXNE==0); //wait for RXNE being setted.
  // while(SPI1->SR.B.BSY==1);
   SPI1->DR=0;
   while(SPI1->SR.B.TXE==0);
   //delay_ms(10);  
   //SPI1->DR=0xf0;
  // delay(100); 
   //CS_ON;
   return (SPI1->DR);	  
} */
unsigned short read_data(void)
{
   unsigned short i,data;
   data=0x00;

   for(i=0;i<8;i++)
   {  
        data<<=1;
	
	 if(MISO==1) data++;
	 
	   delay(1);
	   CLK_ON;
	  
	   delay(1);
	  CLK_OFF;
	  
	 
	
	 
   }
   return data;

}  

⌨️ 快捷键说明

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