引脚要根据原理图另行设定
以下是ad9826.c中的内容:
/*
* 2009-12
* email:
* author: cczy
*/
/*技术参数:
* 频率:Freq_Max_SCLK = 10MHz
* Freq_ADCCLK = Freq_CDSCLK2 / 3
* OEB引脚是输出使能控制,所以只对 D1 ~ D8有效
*
*/
#include
#include // STM32F10x Library Definitions
#include "ad9826.h"
#include "stm32f10x_type.h"
#include "uartio.h"
#include "gpio.h"
#define SCLK_LOW() (GPIO_ResetBits(GPIOC, GPIO_Pin_2))
#define SCLK_HIGH() (GPIO_SetBits(GPIOC, GPIO_Pin_2))
#define SLOAD_LOW() (GPIO_ResetBits(GPIOC, GPIO_Pin_3))
#define SLOAD_HIGH() (GPIO_SetBits(GPIOC, GPIO_Pin_3))
#define SDATA_LOW() (GPIO_ResetBits(GPIOC, GPIO_Pin_1))
#define SDATA_HIGH() (GPIO_SetBits(GPIOC, GPIO_Pin_1))
//#define SDATA_Write(dat) (GPIO_WriteBit(GPIOC, GPIO_Pin_1,(BitAction)dat))
#define AD9826_Disable() (GPIO_SetBits(GPIOB, GPIO_Pin_0))
#define AD9826_Enable() (GPIO_ResetBits(GPIOB, GPIO_Pin_0))
#define AD9826_Get_State() GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_0)
/*
#define AD9826_Set_State(u8 state) \
if(state==0) \
AD9826_DISABLE();\
else \
AD9826_Enable();
*/
#define CDSCLK2 GPIO_Pin_7
#define ADCCLK GPIO_Pin_8
#define CDSCLK1 GPIO_Pin_6
/*
* paramter:
* -ns delay time
* notes:
* speed is 50MHZ=>> 0.2ns/I
*/
static void Delay(u32 val)
{
for(; val > 0; val--) ;
}
/*
* Param:
state: 1=enable
0=disable
* return: 1=error
0=ok
*/
u8 AD9826_Set_State(u8 state)
{
if(state==1)
AD9826_Enable();
else if(state==0)
AD9826_Disable();
else
return 1;
return 0;
}
/*
static u8 AD9826_Get_State()
{
return GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_0);
}
*/
/* Function : Write value into register of AD9826
* Parameter:
-addr : register addreess.
-dat : value,only low 8bit is valid.
* 3-SHA模式:
* CDSCLK2的下将沿开始采样。
* ADC在ADCCLK 的下将沿输出16Bit的采样数据,分别在ADCCLK
的下将和上升沿以8Bit的长度输出。
* CDSCLK2应该在ADCCLK变高前变低;在上一个ADCCLK变低之前,
CDSCLK2不应该变高。
* 输出数据要占用三个ADCCLK周期。
*/
static void AD9826_Write_Reg(u8 addr,u16 dat)
{
s8 i;
u8 tmp;
printf("addr=0x%x,dat=0x%x\r\n",addr,dat);
GPIO_PIN_Cfg(GPIOC,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3,\
GPIO_Mode_Out_PP,GPIO_Speed_10MHz);
SLOAD_HIGH(); //Disable Operation
//Write Flag
SDATA_LOW();
SCLK_LOW();
Delay(256); //?????
SLOAD_LOW();
Delay(256); // SLOAD to SCLK Set-Up Time >= 10ns (Min)
SCLK_HIGH();//Indicate Write operation
for(i=2;i>=0;i--)
{//Write Address
tmp=addr & (1< GPIO_WriteBit(GPIOC,GPIO_Pin_1,(BitAction)(tmp>>i));
SCLK_LOW();//Write ont bit
Delay(256);
SCLK_HIGH();
Delay(256); //Hold 10ns
}
for(i=8;i>=0;i--)
{//Write Data
tmp=dat & (1< GPIO_WriteBit(GPIOC, GPIO_Pin_1,(BitAction)(tmp>>i));
SCLK_LOW();
Delay(256);
SCLK_HIGH();
Delay(256);
}
SLOAD_HIGH();
}
u16 AD9826_Read_Reg(u8 addr)
{
s8 i;
u16 tmp;
printf("addr=0x%x\r\n",addr);
GPIO_PIN_Cfg(GPIOC,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3,\
GPIO_Mode_Out_PP,GPIO_Speed_2MHz);//Change Pin mode
SLOAD_HIGH();
GPIO_WriteBit(GPIOC, GPIO_Pin_1,(BitAction)(1));
SCLK_LOW();
SLOAD_LOW();
Delay(20000);
SCLK_HIGH();
Delay(20000);
for(i=2;i>=0;i--)
{//Write Address
tmp=addr & (1< GPIO_WriteBit(GPIOC, GPIO_Pin_1,(BitAction)(tmp>>i));
SCLK_LOW();//Write ont bit
Delay(20000);
SCLK_HIGH();
Delay(20000); //Hold 10ns
}
for(i=0;i<=3;i++)
{
GPIO_WriteBit(GPIOC, GPIO_Pin_1,(BitAction)(0));
SCLK_LOW();//Write ont bit
Delay(20000);
SCLK_HIGH();
Delay(20000); //Hold 10ns
}
tmp=0;
GPIO_PIN_Cfg(GPIOC,GPIO_Pin_1,GPIO_Mode_IPD,GPIO_Speed_2MHz); //Change Pin mode
Delay(20000);
for(i=8;i>=0;i--)
{//Read Data
SCLK_LOW();
Delay(20000);
SCLK_HIGH();
tmp<<=1;
tmp|=GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1);
//printf("i=%d,tmp=0x%x\r\n",i,tmp);
Delay(20000);
}
tmp=tmp>>1;//BUG,目前暂时用这种方式修正
SLOAD_HIGH();
printf("tmp=0x%x\r\n",tmp);
return tmp;
}
void Set_Default_Config_Reg(void)
{
AD9826_Write_Reg(ADDR_CFG,CFG_FOR_COLOR);
}
void Set_Default_Mux_Reg()
{
AD9826_Write_Reg(ADDR_MUX,MUX_FOR_COLOR);
}
/*
* parameter:
* state: 0=low,1=high
*/
static void Set_ADCCLK(u8 state)
{
if(state==0x00)
{
GPIO_ResetBits(GPIOC,ADCCLK);
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
}
else if(state==0x01)
{
GPIO_SetBits(GPIOC,ADCCLK);
GPIO_SetBits(GPIOB,GPIO_Pin_1);
}
}
/*
* parameter:
* state: 0=low,1=high
*/
static void Set_CDSCLK1(u8 state)
{
if(state==0x00)
{
GPIO_ResetBits(GPIOC, CDSCLK1);
GPIO_ResetBits(GPIOB, GPIO_Pin_11);
}
else if(state==0x01)
{
GPIO_SetBits(GPIOC, CDSCLK1);
GPIO_SetBits(GPIOB, GPIO_Pin_11);
}
}
/*
* parameter:
* state: 0=low,1=high
*/
static void Set_CDSCLK2(u8 state)
{
if(state==0x00)
{
GPIO_ResetBits(GPIOC, CDSCLK2);
GPIO_ResetBits(GPIOB, GPIO_Pin_10);
}
else if(state==0x01)
{
GPIO_SetBits(GPIOC, CDSCLK2);
GPIO_SetBits(GPIOB, GPIO_Pin_10);
}
}
static u8 AD9826_Read_Byte()
{
u16 dat;
dat=GPIO_ReadInputData(GPIOA);
return (u8)(dat & 0xff);
}
void Sha3_Sample_Rgb(u16 rgb[3])
{
//u16 rgb[3];//{red,green,blue}
u8 i;
GPIO_PIN_Cfg(GPIOA,0xFF,GPIO_Mode_IPD,GPIO_Speed_2MHz);
AD9826_Disable();
Set_ADCCLK(0);
Set_CDSCLK1(0);
//Set_CDSCLK2(1);
Set_CDSCLK2(0);
AD9826_Enable();
Set_ADCCLK(0);
Set_CDSCLK2(0);
Delay(256); //>=5ns
Set_CDSCLK2(1);
Delay(1536); //>=5ns
Set_CDSCLK2(0);
Delay(256); //>=5ns
for(i=0;i<=2;i++)
{
Set_ADCCLK(1); //ADCCLk Pulsewidth >= 30ns
Delay(256);//>=6ns
rgb[i]=AD9826_Read_Byte();
Set_ADCCLK(0);
Delay(256);//>=6ns
rgb[i]=(rgb[i]<<8) | AD9826_Read_Byte();
}
Delay(256);//>=10ns
AD9826_Disable();
}
void Test_Bus(void)
{
u8 ch;
//AD9826_Enable();
GPIO_ResetBits(GPIOB, GPIO_Pin_13);//on,,LED1_Yellow
Delay(2000000);
ch=getchar();
if(ch=='0')
{
puts("\r\nPress Any Key to break\r\nIn Test_SCLK\r\n");
while(1)
{
SCLK_HIGH();
//GPIO_SetBits(GPIOC, GPIO_Pin_2);
Delay(5000); //Hold 10ns
SCLK_LOW();//Write ont bit
//GPIO_ResetBits(GPIOC, GPIO_Pin_2);
Delay(5000);
}
}
else if(ch=='1')
{
GetKey();
puts("\r\nPress Any Key to break\r\nIn Test_SLOAD\r\n");
while(1)
{
SLOAD_HIGH();
//GPIO_SetBits(GPIOC, GPIO_Pin_3);
Delay(2000); //Hold 10ns
SLOAD_LOW();
//GPIO_ResetBits(GPIOC, GPIO_Pin_3);
Delay(2000);
}
}
else if(ch=='2')
{
GetKey();
puts("\r\nPress Any Key to break\r\nIn Test_SDATA\r\n");
while(1)
{
GPIO_SetBits(GPIOC, GPIO_Pin_1);
Delay(1000);
GPIO_ResetBits(GPIOC, GPIO_Pin_1);
Delay(1000);
}
}
GPIO_SetBits(GPIOB, GPIO_Pin_13); //off,LED1_Yellow
Delay(2000000);
puts("Out Test_Bus\r\n");
//AD9826_Disable();
}
以下是ad9826.h中的内容:
#ifndef __FLASH_h
#define __FLASH_h
#define INPUT_RANGE_4V (1<<7)
#define ENABLE_INTERNAL_VOL_REF (1<<6)
#define CHANNEL_3 (1<<5)
#define CDS_MODEL (1<<4)
#define DC_BIAS_LEV (1<<3) //should high for 4v clamp bias
#define SLEEP_MODE (1<<2)
#define SINGLE_BYTE (1<<0)
#define MULT_BYTE (0<<0)
//for colorrimeter
#define CFG_FOR_COLOR ((1<<7)|(1<<6)|(1<<5)|(1<<3)|(1<<2))
#define MUX_FOR_COLOR ((1<<7))
#define PGA_GAIN ()
#define OFFSET ()
#define ADDR_CFG 0x00
#define ADDR_MUX 0x01
#define ADDR_REG_PGA 0x02
#define ADDR_GREEN_PGA 0x03
#define ADDR_BLUE_PGA 0x04
#define ADDR_RED_OFFSET 0x05
#define ADDR_GREEN_OFFSET 0x06
#define ADDR_BLUE_OFFSET 0x07
u16 AD9826_Read_Reg(u8 addr);
void Set_Default_Config_Reg(void);
void Set_Default_Mux_Reg(void);
void Sha3_Sample_Rgb(u16 rgb[3]);
void Test_Bus(void);
#endif