MAX1036是8位ADC,四通道单端输入,和差分输入
快速AD转换,而且功耗极低8μA at 10ksps,内置参考电压4.096V 可以选择
高速I2C接口,内部时钟和外部时钟选择,
8脚SOT23封装,体积超小!!!
推荐!!!
压缩包里面包含以下文件:
MAX1036+LED 二极管显示
MAX1036+MAX7221 8位数码管显示
MAX1036+LCD1602 液晶LCD1602显示
芯片可以到美信公司申请(完全免费!!!)
以下为头文件 I2C协议接口
/*--------------------------------------------------------------*/
//File: MAX1036.H
//Data: 16:51 2009-3-25
//Amend: 20:30 2009-3-27
//Version: Ver0.2
/*--------------------------------------------------------------*/
//防止被重复定义
#ifndef __MAX1036_H__
#define __MAX1036_H__
/*--------------------------------------------------------------*/
//包含头文件
#include <intrins.h>
/*--------------------------------------------------------------*/
//接口定义
sbit SCL = P1^6; //时钟
sbit SDA = P1^7; //数据
/*--------------------------------------------------------------*/
//器件地址读写控制
#define address_w 0xc8 //写控制
#define address_r 0xc9 //读控制
/*--------------------------------------------------------------*/
#define setup_byte 0xc0 //设置
#define bipolar 0xc0 //单端输入方式
#define unipolar 0xc4 //差分输入方式
/*---Setup Byte Format---*/
/*-------------------------------------------------------------------
BIT7(MSB) BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0(LSB)
REG SEL2 SEL1 SEL0 CLK BIP/UNI RST X
-------------------------------------------------------------------*/
//REG Register bit. 1 = Setup Byte, 0 = Configuration Byte
//CLK 1 = External clock, 0 = Internal clock. Defaulted to zero at power-up
//BIP/UNI 1 = Bipolar, 0 = Unipolar. Defaulted to zero at power-up
//RST 1 = No action, 0 = Resets the configuration register to default. Setup register remains unchanged.
//X Don’t care, can be set to 1 or 0.
/*---Reference Voltage and AIN_/REF Format---*/
/*-------------------------------------------------------------------
SEL2 SEL1 SEL0 REFERENCE VOLTAGE AIN_/REF INTERNAL REFERENCE STATE
0 0 X VDD Analog input Always Off
0 1 X External reference Reference input Always Off
1 0 0 Internal reference Analog input Auto Shutdown
1 0 1 Internal reference Analog input Always On
1 1 X Internal reference Reference output Always On
-------------------------------------------------------------------*/
/*--------------------------------------------------------------*/
#define config_byte 0x61 //配制
#define channel0_s 0x61 //选择通道0, 单端输入方式
#define channel1_s 0x63 //选择通道1, 单端输入方式
#define channel2_s 0x64 //选择通道2, 单端输入方式
#define channel3_s 0x67 //选择通道3, 单端输入方式
#define channel0_d 0x60 //选择通道0+/1-, 差分输入方式
#define channel1_d 0x62 //选择通道0-/1+, 差分输入方式
#define channel2_d 0x63 //选择通道2+/3-, 差分输入方式
#define channel3_d 0x66 //选择通道2-/3+, 差分输入方式
/*---Configuration Byte Format---*/
/*-------------------------------------------------------------------
BIT7(MSB) BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0(LSB)
REG SCAN1 SCAN0 CS3 CS2 CS1 CS0 SGL/DIF
-------------------------------------------------------------------*/
//REG Register bit. 1 = Setup Byte (Table 1), 0 = Configuration Byte.
//SGL/DIF 1 = single-ended, 0 = pseudo-differential. Default to 1 at power-up
/*---Channel Selection in Single-Ended Mode (SGL/DIF = 1)---*/
/*-------------------------------------------------------------------
CS3 CS2 CS1 CS0 AIN0 AIN1 AIN2 AIN3 GND
0 0 0 0 + -
0 0 0 1 + -
0 0 1 0 + -
0 0 1 1 + -
-------------------------------------------------------------------*/
/*---Channel Selection in Pseudo-Differential Mode (SGL/DIF = 0)---*/
/*-------------------------------------------------------------------
CS3 CS2 CS1 CS0 AIN0 AIN1 AIN2 AIN3
0 0 0 0 + -
0 0 0 1 - +
0 0 1 0 + -
0 0 1 1 - +
-------------------------------------------------------------------*/
/*---Scanning Configuration---*/
/*-------------------------------------------------------------------
SCAN1 SCAN0 SCANNING CONFIGURATION
0 0 Scans up from AIN0 to the input selected by CS3–CS0 (default setting).
0 1 Converts the input selected by CS3–CS0 eight times.
1 0 Scans up from AIN2 to the input selected by CS1 and CS0. When CS1 and CS0 are set for
AIN0–AIN2, the scanning stops at AIN2.
1 1 Converts the channel selected by CS3–CS0.
-------------------------------------------------------------------*/
/*--------------------------------------------------------------*/
//函数声明
void delay_10us(void); //延迟时间10us
void delay_1s(void); //延迟时间100ms
void start(void); //开始总线
void stop(void); //结束总线
void ack(bit ack); //发送确认位
bit check_ack(void); //检测确认位
void write_byte(unsigned char dat); //写入一个字节
unsigned char read_byte(void); //读出一个字节
void write_cmd(unsigned char dat); //写入一个指令
unsigned char read_dat(void); //读出数据
void init_MAX1036(void); //初始化从设备MAX1036
/*--------------------------------------------------------------*/
//延迟时间10us
void delay_10us(void)
{
_nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_();
}
/*--------------------------------------------------------------*/
//延迟时间1s
//延时1000000+1us 函数定义
void delay_1s(void)
{
unsigned char i,j,k;
for(i=167;i>0;i--)
for(j=171;j>0;j--)
for(k=16;k>0;k--);
}
/*--------------------------------------------------------------*/
//开始总线
void start(void)
{
SDA = 1; //在SCL为高时SDA由1变为0开始总线
SCL = 1;
SDA = 0;
SCL = 0; //钳住I2C总线,准备发送或接收数据
}
/*--------------------------------------------------------------*/
//结束总线
void stop(void)
{
SCL = 0;
SDA = 0; //在SCL为高时SDA由0变为1结束总线
SCL = 1;
SDA = 1;
}
/*--------------------------------------------------------------*/
//发送确认位
//ack = 1 不确认
//ack = 0 确认
void ack(bit ack)
{
SDA = ack;
SCL = 1;
SCL = 0;
}
/*--------------------------------------------------------------*/
//检测确认位
//返回值为0时,表示确认
//返回值为1时,表示确认错误
bit check_ack(void)
{
bit error_ack;
SDA = 1; //8位发送完后释放数据线,准备接收应答位
SCL = 1;
error_ack = SDA;
SCL = 0;
return(error_ack);
}
/*--------------------------------------------------------------*/
//写入一个字节
void write_byte(unsigned char dat)
{
unsigned char i;
for(i = 8; i != 0; i--)
{
SDA = (bit)(dat & 0x80); //先发送最高位
SCL = 1;
SCL = 0;
dat <<= 1;
}
}
/*--------------------------------------------------------------*/
//读出一个字节
unsigned char read_byte(void)
{
unsigned char i, dat;
dat = 0; //初始化变量
for(i = 8; i != 0; i--)
{
SCL = 1;
dat <<= 1;
dat |= (unsigned char)(SDA);//先收到最高位
SCL = 0;
}
return(dat);
}
/*--------------------------------------------------------------*/
//写入一个指令
void write_cmd(unsigned char dat)
{
start(); //开始总线
write_byte(address_w); //写入从设备地址,进行写入操作
check_ack(); //检测确认位
write_byte(dat); //写入指令字
check_ack(); //检测确认位
stop(); //结束总线
}
/*--------------------------------------------------------------*/
//读出数据
unsigned char read_dat(void)
{
unsigned char dat;
start(); //开始总线
write_byte(address_r); //写入从设备地址,进行读出操作
check_ack(); //检测确认位
delay_10us(); //延迟时间10us
dat = read_byte(); //读出数据
ack(1); //发送确认位,不确认
stop(); //结束总线
return(dat);
}
/*--------------------------------------------------------------*/
//初始化从设备MAX1036
void init_MAX1036(void)
{
write_cmd(setup_byte);
write_cmd(config_byte);
}
/*--------------------------------------------------------------*/
#endif
用户518079 2009-3-28 10:54