#include "DSP281x_Device.h"
#include "DSP281x_Spi.h"
#include "DSP281x_Delay.h"
#define CS GpioDataRegs.GPFDAT.bit.GPIOF3
void InitSPI(void)
{
EALLOW;
GpioMuxRegs.GPFMUX.all = 0x0007;
GpioMuxRegs.GPFDIR.all |= 0x0008; //把STEA设置成普通IO模式
EDIS;
//使用的是普通SPI模式,没用FIFO
SpiaRegs.SPICCR.bit.SPISWRESET=0; // Reset SPI
SpiaRegs.SPICCR.all=0x0007; // 8-bit character, Loopback mode disabled*数据是8位
SpiaRegs.SPICTL.all=0x0006; // SPI Interrupt disabled, Master mode, Master/slave transmit enabled 主模式
SpiaRegs.SPISTS.all=0x0000; //状态位清零
SpiaRegs.SPIBRR=127; //计算波特率 LSPCLK/(SPIBRR+1)=30/127+1=234
//SpiaRegs.SPIPRI.all=0x0040;
SpiaRegs.SPICCR.all=0x0087; // Enable SPI
}
/************************************************
0x058B A0/A1 +/-2.048 0x038B A0/A1 +/-4.096
0x458B A0/GND +/-2.048 0x438B A0/GND +/-4.096
0x558B A1/GND +/-2.048 0x538B A1/GND +/-4.096
0x658B A2/GND +/-2.048 0x638B A2/GND +/-4.096
0x758B A3/GND +/-2.048 0x738B A3/GND +/-4.096
**********************************************/
void ADS_Config(void)
{
unsigned int Config_Value;
Config_Value = 0x538B; // ADS1118 configuration AIN1, FS=+/-4.096, DR=128sps, PULLUP on DOUT
CS=0; // Set CS low
WriteSPI(Config_Value,0); // Write configuration to ADS1118
CS=1; // Set CS high
}
int ADS_Read(void)
{
unsigned int Data, Config_Value;
Config_Value = 0x538B;// ADS1118 configuration AIN1, FS=+/-4.096, DR=128sps, PULLUP on DOUT
CS=0; // Set CS low
Data = WriteSPI(Config_Value,1); // Read data from ADS1118
CS=1; // Set CS high
return Data;
}
/*
* Mode 0: Only write config register to ADS1118
* Mode 1: Write config register to ADS1118 as well as read data from ADS1118
*/
signed int WriteSPI(unsigned int config, unsigned char mode)
{
signed int msb,TEMP_dummy;
unsigned int temp;
volatile signed int dummy;
temp = config;
if (mode==1) temp = config | 0x8000;
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG==1); //判断是否发送完毕
SpiaRegs.SPITXBUF = (temp >> 8 ); // Write MSB of Config
while(SpiaRegs.SPISTS.bit.INT_FLAG ==0);
msb = SpiaRegs.SPIRXBUF; // Read MSB of Data
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG==1);
SpiaRegs.SPITXBUF = (temp & 0xff); // Write LSB of Config
while(SpiaRegs.SPISTS.bit.INT_FLAG ==0);
msb = (msb << 8) | SpiaRegs.SPIRXBUF; // Read LSB of Data
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG==1);
SpiaRegs.SPITXBUF = (temp >> 8 ); // Write MSB of Config
while(SpiaRegs.SPISTS.bit.INT_FLAG ==0);
dummy = SpiaRegs.SPIRXBUF; // Read MSB of Config
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG==1);
SpiaRegs.SPITXBUF= (temp & 0xff); // Write LSB of Config
while(SpiaRegs.SPISTS.bit.INT_FLAG ==0);
TEMP_dummy=dummy;
dummy = (TEMP_dummy <<8) | SpiaRegs.SPIRXBUF; // Read LSB of Config
delay_us(100);
return msb;
}
Eason Wang:
move to C2000 forum
#include "DSP281x_Device.h"
#include "DSP281x_Spi.h"
#include "DSP281x_Delay.h"
#define CS GpioDataRegs.GPFDAT.bit.GPIOF3
void InitSPI(void)
{
EALLOW;
GpioMuxRegs.GPFMUX.all = 0x0007;
GpioMuxRegs.GPFDIR.all |= 0x0008; //把STEA设置成普通IO模式
EDIS;
//使用的是普通SPI模式,没用FIFO
SpiaRegs.SPICCR.bit.SPISWRESET=0; // Reset SPI
SpiaRegs.SPICCR.all=0x0007; // 8-bit character, Loopback mode disabled*数据是8位
SpiaRegs.SPICTL.all=0x0006; // SPI Interrupt disabled, Master mode, Master/slave transmit enabled 主模式
SpiaRegs.SPISTS.all=0x0000; //状态位清零
SpiaRegs.SPIBRR=127; //计算波特率 LSPCLK/(SPIBRR+1)=30/127+1=234
//SpiaRegs.SPIPRI.all=0x0040;
SpiaRegs.SPICCR.all=0x0087; // Enable SPI
}
/************************************************
0x058B A0/A1 +/-2.048 0x038B A0/A1 +/-4.096
0x458B A0/GND +/-2.048 0x438B A0/GND +/-4.096
0x558B A1/GND +/-2.048 0x538B A1/GND +/-4.096
0x658B A2/GND +/-2.048 0x638B A2/GND +/-4.096
0x758B A3/GND +/-2.048 0x738B A3/GND +/-4.096
**********************************************/
void ADS_Config(void)
{
unsigned int Config_Value;
Config_Value = 0x538B; // ADS1118 configuration AIN1, FS=+/-4.096, DR=128sps, PULLUP on DOUT
CS=0; // Set CS low
WriteSPI(Config_Value,0); // Write configuration to ADS1118
CS=1; // Set CS high
}
int ADS_Read(void)
{
unsigned int Data, Config_Value;
Config_Value = 0x538B;// ADS1118 configuration AIN1, FS=+/-4.096, DR=128sps, PULLUP on DOUT
CS=0; // Set CS low
Data = WriteSPI(Config_Value,1); // Read data from ADS1118
CS=1; // Set CS high
return Data;
}
/*
* Mode 0: Only write config register to ADS1118
* Mode 1: Write config register to ADS1118 as well as read data from ADS1118
*/
signed int WriteSPI(unsigned int config, unsigned char mode)
{
signed int msb,TEMP_dummy;
unsigned int temp;
volatile signed int dummy;
temp = config;
if (mode==1) temp = config | 0x8000;
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG==1); //判断是否发送完毕
SpiaRegs.SPITXBUF = (temp >> 8 ); // Write MSB of Config
while(SpiaRegs.SPISTS.bit.INT_FLAG ==0);
msb = SpiaRegs.SPIRXBUF; // Read MSB of Data
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG==1);
SpiaRegs.SPITXBUF = (temp & 0xff); // Write LSB of Config
while(SpiaRegs.SPISTS.bit.INT_FLAG ==0);
msb = (msb << 8) | SpiaRegs.SPIRXBUF; // Read LSB of Data
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG==1);
SpiaRegs.SPITXBUF = (temp >> 8 ); // Write MSB of Config
while(SpiaRegs.SPISTS.bit.INT_FLAG ==0);
dummy = SpiaRegs.SPIRXBUF; // Read MSB of Config
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG==1);
SpiaRegs.SPITXBUF= (temp & 0xff); // Write LSB of Config
while(SpiaRegs.SPISTS.bit.INT_FLAG ==0);
TEMP_dummy=dummy;
dummy = (TEMP_dummy <<8) | SpiaRegs.SPIRXBUF; // Read LSB of Config
delay_us(100);
return msb;
}
dong wang8:
回复 Eason Wang:
can you write chinese