一直在调DAC8760,输出范围4-20mA, 目前发现只能输出0mA 和 24mA,不知道什么原因,我附上程序,请帮我看看,是不是我配置的问题?
//库文件名: dac8760.h
#ifndef _dac8760_H
#define _dac8760_H
//***********************************************************
//以下是使用到的SPI通讯端口,需要根据实际连接情况进行配置
#define DAC8760_GPIO GPIOB
#define DAC8760_DIN_GPIO GPIOD
#define DAC8760_DIN GPIO_Pin_2
//写数据线
#define DAC8760_SCK GPIO_Pin_3
//时钟线
#define DAC8760_LAT GPIO_Pin_4
//使能线
#define DIN_1 GPIO_SetBits(DAC8760_DIN_GPIO, DAC8760_DIN)
#define DIN_0 GPIO_ResetBits(DAC8760_DIN_GPIO, DAC8760_DIN)
#define SCK_1 GPIO_SetBits(DAC8760_GPIO, DAC8760_SCK)
#define SCK_0 GPIO_ResetBits(DAC8760_GPIO, DAC8760_SCK)
#define LAT_1 GPIO_SetBits(DAC8760_GPIO, DAC8760_LAT)
#define LAT_0 GPIO_ResetBits(DAC8760_GPIO, DAC8760_LAT)
//**********************************************************
#define V_0_5 0x0000
//电压范围0-5V
#define V_0_10 0x0001
//电压范围0-10V
#define I_4_20 ((unsigned short)(1<<9))
//电流范围4-20mA
#define I_0_20 ((unsigned short)(2<<9))
//电流范围0-20mA
#define I_0_24 ((unsigned short)(3<<9))
//电流范围0-24mA
//**********************************************************
extern void DAC8760_Init(void);
extern void DAC8760_Out(unsigned short dat);
#endif
//文件名: dac8760.c
/****************************************************************************
DAC8760控制程序
****************************************************************************/
#include "common.h"
//**********************************************************
void DAC8760_Write(unsigned char add,unsigned short dat);
void DAC8760_Config(void);
void DAC8760_Init(void);
void DAC8760_Out(unsigned short dat);
void delay_ms(unsigned int cnt)
{
unsigned int i;
for(i = 0; i < cnt; i++)
{
__nop();
}
}
//写寄存器
void DAC8760_Write(unsigned char add,unsigned short dat)
{
unsigned char i; unsigned char h_8bit, l_8bit;
h_8bit=(dat>>8)&0x00FF;
//取高8位数据
l_8bit=dat&0x00FF;
//取低8位数据
LAT_0;
for(i=8;i>0;i--)//写8位地址
{
SCK_0;
if(add&0x80)
{
DIN_1;
}
else
{
DIN_0;
}
delay_ms(100);
SCK_1;
add<<=1;
}
for(i=8;i>0;i--)
//写高8位数据
{
SCK_0;
if(h_8bit&0x80)
{
DIN_1;
}
else
{
DIN_0;
}
delay_ms(100);
SCK_1;
h_8bit<<=1;
}
for(i=8;i>0;i--)
//写低8位数据
{
SCK_0;
if(l_8bit&0x80)
{
DIN_1;
}
else
{
DIN_0;
}
delay_ms(100);
SCK_1;
l_8bit<<=1;
}
delay_ms(100);
LAT_1;
DIN_1;
}
void DAC8760_Config(void)
{
DAC8760_Write(0x56,0x0001);
delay_ms(1000);
//复位所有寄存器;
DAC8760_Write(0x55,(1<<12)| V_0_5);
delay_ms(1000);
//控制寄存器设置; DB12 输出使能 | 电压输出范围0-5V
DAC8760_Write(0x57,(1<<8) | I_4_20);
delay_ms(1000);
//配置寄存器设置; DB8 双输出使能 | 电流输出范围4-20mA
DAC8760_Write(0x01,0);
delay_ms(1000);
//输出0
}
void DAC8760_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure DAC8760 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
DAC8760_Config();
}
void DAC8760_Out(unsigned short dat)
{
DAC8760_Write(0x01,dat);
}
|