请问有没有AD7323 m128的示范程序,我按AD7321的示范程序改的,芯片总是采集不到数据!!!
头文件
#ifndef _AD7323_H_
#define _AD7323_H_
#define CONTROL_REGISTER 0x8000 //控制寄存器
#define RANGE_REGISTER 0xA000 //量程范围寄存器
#define SEQUENCE_REGISTER 0xE000 //定序寄存器
#define CONVEN
tiON 0x0000 //启动AD转换
/*-----------------reg0(CONTROL_REGISTER)----------------*/
//模拟通道选择
#define ADD10_VIN0 0x000 //VIN0通道
#define ADD10_VIN1 0x400 //VIN1通道
#define ADD10_VIN2 0x800 //VIN2通道
#define ADD10_VIN3 0xc00 //VIN3通道
//模拟输入配置选择
#define PSEUDO_DIFFERENTIAL_3 0x300 //3个伪差分输入
#define FULL_DIFFERENTIAL_2 0x200 //2个全差分输入
#define PSEUDO_DIFFERENTIAL_2 0x100 //2个伪差分输入
#define SINGLE_ENDED 0x000 //单端输入
//
电源模式
#define NORMAL_MODE 0x00 //正常模式
#define AUTOSTANDBY_MODE 0x40 //自动待机模式
#define AUTOSTANDDOWN_MODE 0x80 //自动关机模式
#define FULL_SHUTDOWN_MODE 0xC0 //全部关闭模式
//定序器选择
#define SEQUENCER_USED 0x08 //使用定序器
#define SEQUENCER_NOT_USED 0x00 //不使用定序器
//输出编码
#define TWOS_COMPLEMENT_CODING 0x00 //补码
#define STRAIGHT_BINARY 0x20 //原码
//参考电压
#define INTERNAL_REF 0x10 //外部
#define EXTERNAL_REF 0x00 //内部
/*-----------------reg1(RANGE_REGISTER)----------------*/
#define VIN0_10V 0x000
#define VIN0_5V 0x800
#define VIN0_2_5V 0x1000
#define VIN0_0_10V 0x1800
#define VIN1_10V 0x000
#define VIN1_5V 0x200
#define VIN1_2_5V 0x400
#define VIN1_0_10V 0x600
#define VIN2_10V 0x00
#define VIN2_5V 0x80
#define VIN2_2_5V 0x100
#define VIN2_0_10V 0x180
#define VIN3_10V 0x00
#define VIN3_5V 0x20
#define VIN3_2_5V 0x40
#define VIN3_0_10V 0x60
//M64管脚定义
#define AD7323_CS 4 //PE4
#define AD7323_DIN 5 //PE5
#define AD7323_SCLK 6 //PE6
#define AD7323_DOUT 7 //PE7
#define set_spi_cs() PORTE|= (1<
#define clr_spi_cs() PORTE&=~(1<
#define set_spi_clk() PORTE|= (1<
#define clr_spi_clk() PORTE&=~(1<
#define set_spi_dout() PORTE|= (1<
#define clr_spi_dout() PORTE&=~(1<
#define read_spi_di() PINE&(1<
#define read_spi_di_HIGH (1<
#define set_spi_mosi() set_spi_dout()
#define clr_spi_mosi() clr_spi_dout()
#define read_spi_miso() read_spi_di()
#define read_spi_miso_HIGH read_spi_di_HIGH
typedef union {struct {unsigned int high_byte;
unsigned int low_byte;
}int_byte;
int value;
}INT;
extern void init_ad7323(void);
extern unsigned int AD7323_read_write(unsigned int );
#endif //_AD7323_H_
源文件
#include "main.h"
#include "AD7323.h"
void init_ad7323(void)
{
DDRE|=(1<
DDRE&=~(1<
PORTE|=(1<
/*************Configure CONTROL_REGISTER*******************/
AD7323_read_write(CONTROL_REGISTER | ADD10_VIN0 | SINGLE_ENDED | NORMAL_MODE | SEQUENCER_NOT_USED | TWOS_COMPLEMENT_CODING | EXTERNAL_REF);
/*************Configure RANGE_REGISTER*******************/
AD7323_read_write(RANGE_REGISTER | VIN0_5V);
AD7323_read_write(RANGE_REGISTER | VIN2_5V);
}
/********************************************************************************
Function: AD7323_read_write
Parameter: unsigned char spi_mosiValue
Return value : unsigned char
Description : Configure the regisiters of AD7323, and read the convention data.
********************************************************************************/
unsigned int AD7323_read_write(unsigned int spi_mosiValue)
{
uchar i;
uint spi_misoValue=0;
set_spi_cs();
delay_us(2);
clr_spi_cs();
for(i=0;i<16;i++)
{
set_spi_clk();
spi_misoValue = (spi_misoValue << 1);
if((spi_mosiValue & 0x8000)==0x8000)
set_spi_mosi();
else
clr_spi_mosi();
if(read_spi_miso() == read_spi_miso_HIGH)
spi_misoValue |= 0x0001;
else
spi_misoValue &= ~0x0001;
clr_spi_clk();
spi_mosiValue = (spi_mosiValue << 1);
delay_us(2);
}
set_spi_clk();
set_spi_cs();
return (spi_misoValue & 0x0000FFFF);
}
原理图
附件
0