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

📄 spi.c

📁 MSP4250单芯片电子称,采样数据无线发送给主机,编译环境IAR for MSP430
💻 C
字号:
/*
*********************************************************************************************************
*                                              MSP430
*                                          SPI functions
*
*
* File    : SPI.C
* Data	  : April 12, 2007
*********************************************************************************************************
*/

#include "hal.h"

/*
*********************************************************************************************************
*                                         CC_SPISETUP
*
* Description      :
* Arguments        :
* Returned Values  :
* Note(s)/Warnings :
*********************************************************************************************************
*/
void CC_SpiSetup(void)
{
	
	//CSN
  	CC_CSn_PxOUT |= CC_CSn_PIN;
  	CC_CSn_PxDIR |= CC_CSn_PIN;
	//GDO2
	CC_GDO2_PxDIR &= ~CC_GDO2_PIN;

  	// MOSI, SCK
  	CC_SPI_BITBANG_PxOUT |= CC_SPI_BITBANG_MOSI | CC_SPI_BITBANG_SCK;
  	CC_SPI_BITBANG_PxDIR |= CC_SPI_BITBANG_MOSI | CC_SPI_BITBANG_SCK;
	
	//MISO
	CC_SPI_BITBANG_PxOUT &= CC_SPI_BITBANG_MISO;
}

/*
*********************************************************************************************************
*                                         CC_SPI_bitbang_out
*
* Description      : Output eight-bit value using selected bit-bang pins
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void CC_SPI_bitbang_out(char value)
{
  	char i;

  	for(i=8; i; i--){
    	if(value & 0x80) SET_MOSI;
    	else CLR_MOSI;
    	value = value << 1;                                  // Rotate bits
    	SET_SCK;
    	CLR_SCK;
  	}
}

/*
*********************************************************************************************************
*                                         CC_SPI_bitbang_in
*
* Description      : Input eight-bit value using selected bit-bang pins
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
char CC_SPI_bitbang_in(void)
{
  	char x, i;

  	for(i = 8; i ; i--){
    	SET_SCK;
		x = x << 1;                             // Make room for next bit
		if(READ_MISO) x |= 1;
    	CLR_SCK;
  	}                                         // Store next bit
  	return(x);
}

/*
*********************************************************************************************************
*                                         SpiWriteReg
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void SpiWriteReg(char addr, char value)
{
	
	CLR_CSN;
    while (READ_MISO); 					 // Wait CCxxxx ready
    CC_SPI_bitbang_out(addr);            // Send address
    CC_SPI_bitbang_out(value);           // Send data
    SET_CSN;
}

/*
*********************************************************************************************************
*                                         SpiWriteBurstReg
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void SpiWriteBurstReg(char addr, char *buffer, char count)
{
    char i;

	CLR_CSN;
    while (READ_MISO); 								  // Wait CCxxxx ready
    CC_SPI_bitbang_out(addr | CCxxx0_WRITE_BURST);   // Send address
    for (i = 0; i < count; i++)
      CC_SPI_bitbang_out(buffer[i]);    			 // Send data
    SET_CSN;
}

/*
*********************************************************************************************************
*                                         SpiReadReg
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
char SpiReadReg(char addr)
{
  	char x;

  	CLR_CSN;
  	CC_SPI_bitbang_out(addr | CCxxx0_READ_SINGLE);//Send address
  	x = CC_SPI_bitbang_in();               // Read data
  	SET_CSN;
  	return x;
}

/*
*********************************************************************************************************
*                                         SpiReadBurstReg
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void SpiReadBurstReg(char addr, char *buffer, char count)
{
  	char i;

  	CLR_CSN;
  	while (READ_MISO); 								 	// Wait CCxxxx ready
  	CC_SPI_bitbang_out(addr | CCxxx0_READ_BURST);    	// Send address
  	for (i = 0; i < count; i++)
    	buffer[i] = CC_SPI_bitbang_in();     			// Read data
  	SET_CSN;
}

/*
*********************************************************************************************************
*                                        SpiReadStatus
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
char SpiReadStatus(char addr)
{
  	char x;

  	CLR_CSN;
  	while (READ_MISO); 									// Wait CCxxxx ready
  	CC_SPI_bitbang_out(addr | CCxxx0_READ_BURST);      	// Send address
  	x = CC_SPI_bitbang_in();               				// Read data
  	SET_CSN;

  	return x;
}

/*
*********************************************************************************************************
*                                         SpiStrobe
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void SpiStrobe(char strobe)
{
	
  	CLR_CSN;
  	while (READ_MISO); 						// Wait for CCxxxx ready
  	CC_SPI_bitbang_out(strobe);            	// Send strobe
  	SET_CSN;
}

/*
*********************************************************************************************************
*                                         SpiWriteCd
*
* Description      : write data to cd4094
* Arguments        :
*				ch,  plate channel 0,1...4
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void SpiWriteCd(char value, char ch)
{
	char i;

  	for(i=8; i; i--){
    	if(value & 0x80) SET_MOSI;
    	else CLR_MOSI;
    	value = value << 1;                                  // Rotate bits
    	SET_SCK;
    	CLR_SCK;
  	}
	
	i = BIT3 <<ch;
	P2IE  &= ~i; 											//disable interrupt
	P2DIR |=  i; 											//pin out
	P2OUT |=  i;											//set
	__no_operation();
	__no_operation();
	P2OUT &= ~i; 											//clear
	P2DIR &= ~i; 											//pin input
	P2IE  |=  i;											//enable interrupt
}

⌨️ 快捷键说明

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