📄 spi_master.c
字号:
/***************************************************************************
Author : Neil Zhao - CAST
Date : Nov 20th, 2008
File : SPI_Master.c
Hardware : ADuC7026 and AD9910
Description : Use the GPIO to simulate the SPI communication of AD9910
***************************************************************************/
#include<ADuC7026.h>
#include<Common.h>
void delay (signed int length)
{
while (length >0)
length--;
}
//---------------------------------
//WriteToAD9910ViaSpi();
//---------------------------------
//Function that writes to the AD9910 via the SPI port. It sends first the control
//word that includes the start address and then the data to write.
//--------------------------------------------------------------------------------
void WriteToAD9910ViaSpi(unsigned char RegisterAddress, unsigned char NumberofRegisters, unsigned int *RegisterData)
{
unsigned char ControlValue = 0;
unsigned int ValueToWrite = 0;
signed char RegisterIndex = 0;
unsigned char i = 0;
//Create the 8-bit header
ControlValue = RegisterAddress;
SET_SCL();
delay(1);
SET_CS();
delay(1);
CLR_CS(); //bring CS low
delay(1);
SDA_OUT(); //Make SDIO an output
//Write out the control word
for(i=0; i<8; i++)
{
CLR_SCL();
if(0x80 == (ControlValue & 0x80))
{
SET_SDIO(); //Send one to SDIO pin
}
else
{
CLR_SDIO(); //Send zero to SDIO pin
}
delay(1);
SET_SCL();
delay(1);
ControlValue <<= 1; //Rotate data
}
delay(2);
//And then the data
for (RegisterIndex=NumberofRegisters; RegisterIndex>0; RegisterIndex--)
{
ValueToWrite = *(RegisterData + RegisterIndex - 1);
for (i=0; i<32; i++) {
CLR_SCL();
if(0x80000000 == (ValueToWrite & 0x80000000))
{
SET_SDIO(); //Send one to SDIO pin
}
else
{
CLR_SDIO(); //Send zero to SDIO pin
}
delay(1);
SET_SCL();
delay(1);
ValueToWrite <<= 1; //Rotate data
}
}
delay(1);
SET_CS(); //bring CS high again
IO_Update();
}
//---------------------------------
//ReadFromAD9910ViaSpi();
//---------------------------------
//Function that reads from the AD9910 via the SPI port. It first send the control word
//that includes the start address and then 32 clocks for each register to read.
//--------------------------------------------------------------------------------
void ReadFromAD9910ViaSpi(unsigned char RegisterAddress, unsigned char NumberofRegisters, unsigned int *RegisterData)
{
unsigned char ControlValue = 0;
signed char RegisterIndex = 0;
unsigned int ReceiveData = 0;
unsigned char i = 0;
unsigned int iTemp = 0;
//Create the 8-bit header
ControlValue = RegisterAddress;
SET_SCL();
delay(1);
SET_CS();
delay(1);
CLR_CS(); //bring CS low
delay(1);
SDA_OUT(); //Make SDIO an output
//Write out the control word
for(i=0; i<8; i++)
{
CLR_SCL();
if(0x80 == (ControlValue & 0x80))
{
SET_SDIO(); //Send one to SDIO pin
}
else
{
CLR_SDIO(); //Send zero to SDIO pin
}
delay(1);
SET_SCL();
delay(1);
ControlValue <<= 1; //Rotate data
}
SDA_IN(); //Make SDA an input
//Read data in
for (RegisterIndex=NumberofRegisters; RegisterIndex>0; RegisterIndex--)
{
for(i=0; i<32; i++)
{
CLR_SCL();
delay(1);
SET_SCL();
ReceiveData <<= 1; //Rotate data
iTemp = GP1DAT; //Read SDIO of AD9910
if(0x00000008 == (iTemp & 0x00000008))
{
ReceiveData |= 1;
}
delay(1);
}
*(RegisterData + RegisterIndex - 1) = ReceiveData;
}
delay(1);
SET_CS(); //bring CS high again
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -