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

📄 spi.cpp

📁 这个是在LPC213X上实现的模拟SPI总线的读写功能;
💻 CPP
字号:
#include "SPI.H"
//#include "..\ArmBIOS.h"
#include "..\..\Lib\ArmOs\ArmLib.h"

//这个是模拟SPI
//----------------------------------------------------------------------
/*
void ____simulate_spi_______()
*/
//----------------------------------------------------------------------
/*
gain: 1,2,4,5,8,10,16,20 V/V
SCLK_1_CPU	P0.29
D_IN_1_CPU	P0.30
D_OUT_1_CPU	P1.16
CS_1_CPU	  P1.31

CS_2_CPU    P1.21
D_IN_2_CPU  P0.18
D_OUT_2_CPU P0.19
SCLK_2_CPU  P0.17

DI_IN       P0.16
CS_DA_CPU   P0.15
*/
//SPI for ADS7871, channel 1

#define SPI_SCK_HIGH_1  SetPinOn(0,29)  //  PinSetLev( PIN0, 29, LEVEL_HIGH )
#define SPI_SCK_LOW_1   SetPinOff(0,29) //PinSetLev( PIN0, 29, LEVEL_LOW )

#define SPI_MOSI_HIGH_1 SetPinOn(0,30)  //PinSetLev( PIN0, 30, LEVEL_HIGH )
#define SPI_MOSI_LOW_1  SetPinOff(0,30) //PinSetLev( PIN0, 30, LEVEL_LOW )

#define SPI_GET_MISO_1  GetPinState(1,16) //PinGetLev( PIN1, 16 )
//SPI for ADS7871, channel 2

#define SPI_SCK_HIGH_2  SetPinOn(0,17) //PinSetLev( PIN0, 17, LEVEL_HIGH )
#define SPI_SCK_LOW_2   SetPinOff(0,17) //PinSetLev( PIN0, 17, LEVEL_LOW )

#define SPI_MOSI_HIGH_2 SetPinOn(0,18) //PinSetLev( PIN0, 18, LEVEL_HIGH )
#define SPI_MOSI_LOW_2  SetPinOff(0,18) //PinSetLev( PIN0, 18, LEVEL_LOW )

#define SPI_GET_MISO_2  GetPinState(0,19) //PinGetLev( PIN0, 19 )
/*
void _______siut2chips_______()
*/
//--------------------------------------------------------------------------------------------
enum{LOW,HIGH};
void SetSIState( unsigned char sel_num, unsigned char set_state )
{
	if( sel_num && set_state ){//2通道高电平
		SPI_MOSI_HIGH_2;
	}else if( sel_num ){//2通道低电平
		SPI_MOSI_LOW_2;
	}else if( set_state ){//1通道高电平
		SPI_MOSI_HIGH_1;
	}else{//1通道低电平
		SPI_MOSI_LOW_1;
	}
}
//--------------------------------------------------------------------------------------------
void SetSCKState( unsigned char sel_num, unsigned char set_state )
{
	if( sel_num && set_state ){//2通道高电平
		SPI_SCK_HIGH_2;
	}else if( sel_num ){//2通道低电平
		SPI_SCK_LOW_2;
	}else if( set_state ){//1通道高电平
		SPI_SCK_HIGH_1;
	}else{//1通道低电平
		SPI_SCK_LOW_1;
	}
}
//--------------------------------------------------------------------------------------------
unsigned long GetSOState( unsigned char sel_num )
{
	if(sel_num){
		return SPI_GET_MISO_2;
	}else{
		return SPI_GET_MISO_1;
	}
}
//--------------------------------------------------------------------------------------------
void SendByte( unsigned char sel_num, unsigned char send_data )//上升沿有效, msb first
{
	unsigned char i;
	for( i = 0; i < 8; i ++ )
	{		
		if( send_data & 0x80 ){
			SetSIState( sel_num, HIGH );			
		}else{ 
			SetSIState( sel_num, LOW );	
		}
		SetSCKState( sel_num, HIGH );
		send_data <<= 1;
		SetSCKState( sel_num, LOW );		
	}
}
//--------------------------------------------------------------------------------------------
unsigned char ReadByte( unsigned char sel_num )
{
	unsigned char read_data;
	//unsigned char count = 8;
	unsigned char i;
	unsigned char Data_Mark = 0x80;
	unsigned long temp = 0;
	read_data = 0; 		
	for( i = 0; i < 8; i ++ )
	{
		SetSCKState( sel_num, HIGH );
		temp = GetSOState( sel_num );
		if( temp ) read_data |= Data_Mark;
		Data_Mark >>= 1;
		SetSCKState( sel_num, LOW );
	}
	return read_data;
}
//----------------------------------------------------------------------
int SPI_SendBuf( unsigned char *from_buf, unsigned int attemp_len, unsigned char sel_num )
{
	unsigned char i;
	for( i = 0; i < attemp_len; i ++ )
		SendByte( sel_num, from_buf[i] );
		
	return SPI_ERR_OK;
}
//----------------------------------------------------------------------
int SPI_RcvBuf( unsigned char *to_buf, unsigned int attemp_len, unsigned char sel_num )
{
	unsigned char i;
	for( i = 0; i < attemp_len; i ++ )
		to_buf[i] = ReadByte( sel_num );
		
	return SPI_ERR_OK;
}
//----------------------------------------------------------------------

⌨️ 快捷键说明

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