概述
#ifndef MCP3421_H
#define MCP3421_H
#include "common.h"
typedef enum mcp_div
{
mcp_div_12 = 0,
mcp_div_14,
mcp_div_16,
mcp_div_18,
}mcp_div_e;
typedef enum mcp_amp
{
mcp_amp_x1 = 0,
mcp_amp_2,
mcp_amp_4,
mcp_amp_8,
}mcp_amp_e;
//获取ADC转换结果
float mcp3421_get_val();
//int mcp3421_set(u8 div,u8 amp);
int mcp3421_set(mcp_div_e div,mcp_amp_e amp);
int mcp3421_wr_data(unsigned char data);
unsigned int mcp3421_rd_data();
u8 mcp3421_config_data();
int select_res(char sw,char num);
#endif
#include "mcp3421.h"
#include "myiic.h"
#include "main.h"
#define VOL_REF (2.048*2)
#define MAX_18BIT (0x3FFFF)
#define MAX_16BIT (0xFFFF)
#define MAX_14BIT (0x3FFF)
#define MAX_12BIT (0xFFF)
u8 mcp_busy = 0;
static mcp_div_e mcp_div = mcp_div_12;
static mcp_amp_e mcp_amp = mcp_amp_x1;
u32 mcp_data = 0;
//获取ADC转换为电压的结果
float mcp3421_get_val()
{
mcp_data = 0xFFFFFFFF;
do
{
mcp_data = mcp3421_rd_data();
}while(mcp_data == 0xFFFFFFFF);
// if(mcp_data != 0xFFFFFFFF)
switch(mcp_div)
{
case mcp_div_18:
return VOL_REF*mcp_data/MAX_18BIT;
break;
case mcp_div_16:
//mcp_data -= 100;
return VOL_REF*mcp_data/MAX_16BIT;
break;
case mcp_div_14:
return VOL_REF*mcp_data/MAX_14BIT;
break;
case mcp_div_12:
return VOL_REF*mcp_data/MAX_12BIT;
break;
default:
return -1;
break;
}
// else
// return -1;
}
float mcp3421_get_ad_avg(int times)
{
float ad_data=0;
while(times--)
{
ad_data = mcp3421_get_val();
}
return ad_data/times;
}
//设置分辨率和输入端放大倍数
/*
div:
0 :12bit 240 SPS
1 :14bit 60 SPS
2 :16bit 15 SPS
3 :18bit 3.75 SPS
amp:
0 :x1
1 :x2
2 :x4
3 :x8
*/
u8 reg = 0;
int mcp3421_set(mcp_div_e div,mcp_amp_e amp)
{
if(div < 4 && amp < 4)
{
reg=(div<<2)+0x90;
reg += amp&0x03;
mcp3421_wr_data(reg);
mcp_div = div;
mcp_amp = amp;
}
return 0;
}
//向MCP3421写入一字节数据
static int mcp3421_wr_data(unsigned char data)
{
int ack = 1;
IIC_Start(); //启动总线
IIC_Send_Byte(0xD0); //发送器件地址
ack = IIC_Wait_Ack();
IIC_Send_Byte(data); //发送器件分地址
ack = IIC_Wait_Ack();
IIC_Stop(); //释放总线
return ack;
}
//返回采样数据
//读取成功:返回ADC原始数值
//失败返回:0xFFFFFFFF
unsigned int mcp3421_rd_data()
{
u8 elec1, elec2, elec3;
IIC_Start(); //启动总线
IIC_Send_Byte(0xD1); //发送器件地址
IIC_Wait_Ack();
elec1 = IIC_Read_Byte(1);
elec2 = IIC_Read_Byte(1);
elec3 = IIC_Read_Byte(1);
mcp_busy = IIC_Read_Byte(0);
IIC_Stop(); //释放总线
// debug_info("MCP3241 data:rn");
// debug_info("B1:%xrn",elec1);
// debug_info("B2:%xrn",elec2);
// debug_info("B3:%xrn",elec3);
// debug_info("BUSY:%xrn",mcp_busy);
if(mcp_div == mcp_div_18)
{
if((mcp_busy & 0x80) == 0x80)//(mcp_busy & 0x80) == 0x80
{
return 0xFFFFFFFF;
}
}
else
{
if((elec3 & 0x80) == 0x80)//(mcp_busy & 0x80) == 0x80
{
return 0xFFFFFFFF;
}
}
switch(mcp_div)
{
case mcp_div_18:
return ((u32)((u32)elec1 << 16 | (u32)elec2 << 8 | elec3)) & 0x03FFFF;
break;
case mcp_div_16:
return ((u32)((u32)elec1 << 8 | (u32)elec2 )) & 0xFFFF;
break;
case mcp_div_14:
return ((u32)((u32)elec1 << 8 | (u32)elec2 )) & 0x3FFF;
break;
case mcp_div_12:
return ((u32)((u32)elec1 << 8 | (u32)elec2 )) & 0xFFF;
break;
default:
return 0xFFFFFFFF;
break;
}
}
u8 mcp3421_config_data()
{
mcp3421_rd_data();
return mcp_busy;
}
最后
以上就是感性店员为你收集整理的ADC芯片3421驱动程序的全部内容,希望文章能够帮你解决ADC芯片3421驱动程序所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复