关于51单片机调试ADS1118芯片
时间:10-02
整理:3721RD
点击:
程序已经调通,串口上也能观察到现象。但是当更换 模拟SPI串口的引脚时,发现就不能显示了。程序如下:
主函数:
main.c
#include"reg52.h"
#include "ads1118.h"
void Init(void) //此函数方便串口调试
{
TMOD = 0X20;
TH1 = 0XFd; //2400baud
TL1 = 0XFd;
TR1 = 1;
SM1 = 1;
SM0 = 0;
REN = 1;
TI = 1;
}
float Volt_Value;
void main(void)
{
Init();
EA=1;
ADS1118_Init();
Write_ADS1118(CONFIG_VALUE,0);
while(1)
{
uint value;
delay_ms(3000);
value = ADS_SEL_Read(0,1); //0.512V
Volt_Value=value /16*100+value /16*10+value /16;
printf("value = %d\n\r",Volt_Value);
Volt_Value = value*4.096/32768; //第二次转换时读出的是前一次的电压
printf("Volt_Value %f\n\r\n\r",Volt_Value);
}
}
ADS1118.C
#include "ads1118.h"
void delay_ms(uint z)
{
uint x;
for(;z>0;z--)
for(x=120;x>0;x--);
}
void ADS1118_Init(void)
{
CS_H;
SCLK_H;
MOSI_L;
}
union aa
{
uint dat1[2];
ulong dat2;
};
uint Write_ADS1118(uint dat,uchar mode)
{
// uint temp;
union aa temp;
union aa temp1;
// ulong Value;
uchar i;
if(mode == 1) dat |= 0x8000; //开始转换
temp.dat1[1] = dat;
temp.dat1[0] = dat;
SCLK_L;
CS_L;
delay_ms(1);
for(i=0;i<32;i++)
{
if(temp.dat2 & 0x80000000)MOSI_H;
else MOSI_L;
temp.dat2 <<= 1;
_nop_();
SCLK_H;
_nop_();
SCLK_L;
_nop_();
temp1.dat2 <<= 1;
if(READ_MISO)temp1.dat2 |= 0x01;
_nop_();
}
CS_H;
SCLK_L;
MOSI_L;
MISO_L;
printf("temp %d\n\r",temp1.dat1[0]);
printf("temp %#X\n\r",temp1.dat1[1]);
return temp1.dat1[0];
}
/*******************************************************************************
//函数名称:ADS_SEL_Read()
//函数功能:读取各路电压,通过两个switch选择读取不同的通道
//输 入:road:增益放大器两端的电压选择,并选择测几路电压
// Ref: 选择参考电压,有6种选择
//输 出:dat:16位ad转换数据
//备 注:这一次读出的转换数据是上一次的转换数据,不要混淆.这里选择的是单次
转换电压值,当然,也可以选择多次转换,通过寄存器的第8位可以设置
//日 期:2013.6.8
*******************************************************************************/
int ADS_SEL_Read(uchar road,uchar Ref) //测几路电压
{
int dat = 0;
uint Config_Value = 0x008b; //默认低8位,DOUT带上拉电阻
switch(road)
{
case 0: Config_Value += 0x0000;break; //AINP = AIN0 and AINN = AIN1 (default)
case 1: Config_Value += 0x1000;break; //AINP = AIN0 and AINN = AIN3
case 2: Config_Value += 0x2000;break; //AINP = AIN1 and AINN = AIN3
case 3: Config_Value += 0x3000;break; //AINP = AIN2 and AINN = AIN3
case 4: Config_Value += 0x4000;break; //AINP = AIN0 and AINN = GND
case 5: Config_Value += 0x5000;break; //AINP = AIN1 and AINN = GND
case 6: Config_Value += 0x6000;break; //AINP = AIN2 and AINN = GND
case 7: Config_Value += 0x7000;break; //AINP = AIN3 and AINN = GND
default : break;
}
switch(Ref)
{
case 0: Config_Value += 0x0000;break; //000 : FS = ±6.144V(1)
case 1: Config_Value += 0x0200;break; //001 : FS = ±4.096V(1)
case 2: Config_Value += 0x0400;break; //002 : FS = ±2.048V(1)
case 3: Config_Value += 0x0600;break; //003 : FS = ±1.024V(1)
case 4: Config_Value += 0x0800;break; //004 : FS = ±0.512V(1)
case 5: case 6: case 7: Config_Value += 0x0a00;break; //005 : FS = ±0.256V(1)
default : break;
}
CS_L;
dat = Write_ADS1118(Config_Value,1);
_nop_();
CS_H;
return dat;
}
头文件:
ADS1118.H
#ifndef __ADS1118_H__
#define __ADS1118_H__
#include <reg52.h>
#include <stdio.h>
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
#define CONFIG_VALUE 0X038B //AIN0-AIN1 4.096 128sps pull on DOUT
sbit SCLK = P2^0;
sbit MOSI = P2^2;
sbit MISO = P2^3;
sbit CS = P2^1;
#define SCLK_H SCLK = 1
#define SCLK_L SCLK = 0
#define MOSI_H MOSI = 1
#define MOSI_L MOSI = 0
#define MISO_H MISO = 1
#define MISO_L MISO = 0
#define CS_H CS = 1
#define CS_L CS = 0
#define READ_MISO P2 & 0X8
void ADS1118_Init(void);
uint Write_ADS1118(uint dat,uchar mode);
void delay_ms(uint z);
int ADS_SEL_Read(uchar road,uchar Ref);
#endif
当我把MISO 和 CS 个引脚更换一下时,就没输出了。 这是为什么?
主函数:
main.c
#include"reg52.h"
#include "ads1118.h"
void Init(void) //此函数方便串口调试
{
TMOD = 0X20;
TH1 = 0XFd; //2400baud
TL1 = 0XFd;
TR1 = 1;
SM1 = 1;
SM0 = 0;
REN = 1;
TI = 1;
}
float Volt_Value;
void main(void)
{
Init();
EA=1;
ADS1118_Init();
Write_ADS1118(CONFIG_VALUE,0);
while(1)
{
uint value;
delay_ms(3000);
value = ADS_SEL_Read(0,1); //0.512V
Volt_Value=value /16*100+value /16*10+value /16;
printf("value = %d\n\r",Volt_Value);
Volt_Value = value*4.096/32768; //第二次转换时读出的是前一次的电压
printf("Volt_Value %f\n\r\n\r",Volt_Value);
}
}
ADS1118.C
#include "ads1118.h"
void delay_ms(uint z)
{
uint x;
for(;z>0;z--)
for(x=120;x>0;x--);
}
void ADS1118_Init(void)
{
CS_H;
SCLK_H;
MOSI_L;
}
union aa
{
uint dat1[2];
ulong dat2;
};
uint Write_ADS1118(uint dat,uchar mode)
{
// uint temp;
union aa temp;
union aa temp1;
// ulong Value;
uchar i;
if(mode == 1) dat |= 0x8000; //开始转换
temp.dat1[1] = dat;
temp.dat1[0] = dat;
SCLK_L;
CS_L;
delay_ms(1);
for(i=0;i<32;i++)
{
if(temp.dat2 & 0x80000000)MOSI_H;
else MOSI_L;
temp.dat2 <<= 1;
_nop_();
SCLK_H;
_nop_();
SCLK_L;
_nop_();
temp1.dat2 <<= 1;
if(READ_MISO)temp1.dat2 |= 0x01;
_nop_();
}
CS_H;
SCLK_L;
MOSI_L;
MISO_L;
printf("temp %d\n\r",temp1.dat1[0]);
printf("temp %#X\n\r",temp1.dat1[1]);
return temp1.dat1[0];
}
/*******************************************************************************
//函数名称:ADS_SEL_Read()
//函数功能:读取各路电压,通过两个switch选择读取不同的通道
//输 入:road:增益放大器两端的电压选择,并选择测几路电压
// Ref: 选择参考电压,有6种选择
//输 出:dat:16位ad转换数据
//备 注:这一次读出的转换数据是上一次的转换数据,不要混淆.这里选择的是单次
转换电压值,当然,也可以选择多次转换,通过寄存器的第8位可以设置
//日 期:2013.6.8
*******************************************************************************/
int ADS_SEL_Read(uchar road,uchar Ref) //测几路电压
{
int dat = 0;
uint Config_Value = 0x008b; //默认低8位,DOUT带上拉电阻
switch(road)
{
case 0: Config_Value += 0x0000;break; //AINP = AIN0 and AINN = AIN1 (default)
case 1: Config_Value += 0x1000;break; //AINP = AIN0 and AINN = AIN3
case 2: Config_Value += 0x2000;break; //AINP = AIN1 and AINN = AIN3
case 3: Config_Value += 0x3000;break; //AINP = AIN2 and AINN = AIN3
case 4: Config_Value += 0x4000;break; //AINP = AIN0 and AINN = GND
case 5: Config_Value += 0x5000;break; //AINP = AIN1 and AINN = GND
case 6: Config_Value += 0x6000;break; //AINP = AIN2 and AINN = GND
case 7: Config_Value += 0x7000;break; //AINP = AIN3 and AINN = GND
default : break;
}
switch(Ref)
{
case 0: Config_Value += 0x0000;break; //000 : FS = ±6.144V(1)
case 1: Config_Value += 0x0200;break; //001 : FS = ±4.096V(1)
case 2: Config_Value += 0x0400;break; //002 : FS = ±2.048V(1)
case 3: Config_Value += 0x0600;break; //003 : FS = ±1.024V(1)
case 4: Config_Value += 0x0800;break; //004 : FS = ±0.512V(1)
case 5: case 6: case 7: Config_Value += 0x0a00;break; //005 : FS = ±0.256V(1)
default : break;
}
CS_L;
dat = Write_ADS1118(Config_Value,1);
_nop_();
CS_H;
return dat;
}
头文件:
ADS1118.H
#ifndef __ADS1118_H__
#define __ADS1118_H__
#include <reg52.h>
#include <stdio.h>
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
#define CONFIG_VALUE 0X038B //AIN0-AIN1 4.096 128sps pull on DOUT
sbit SCLK = P2^0;
sbit MOSI = P2^2;
sbit MISO = P2^3;
sbit CS = P2^1;
#define SCLK_H SCLK = 1
#define SCLK_L SCLK = 0
#define MOSI_H MOSI = 1
#define MOSI_L MOSI = 0
#define MISO_H MISO = 1
#define MISO_L MISO = 0
#define CS_H CS = 1
#define CS_L CS = 0
#define READ_MISO P2 & 0X8
void ADS1118_Init(void);
uint Write_ADS1118(uint dat,uchar mode);
void delay_ms(uint z);
int ADS_SEL_Read(uchar road,uchar Ref);
#endif
当我把MISO 和 CS 个引脚更换一下时,就没输出了。 这是为什么?
不错,过来看看,学习学习。
难道程序卡死了?
只是 引脚换了一下 不可能把
看看先