[ 本帖最后由 hello丶酷狗 于 2021-12-2 16:04 编辑 ]\n\nsszxxm 发表于 2021-11-29 18:42
你的硬件和相应驱动贴上来,帮你看下。
[code]/*
* ads1232.c
*
* Created on: 2020年12月28日
* Author: Administrator
*/
#include <app_driver/ads1232.h>
<div style="text-align: center;">unsigned char ads1232_buf_count = 0;</div><p style="line-height: 30px; text-indent: 2em;"></p><p style="line-height: 30px; text-indent: 2em;"></p><p style="line-height: 30px; text-indent: 2em;"></p><p style="line-height: 30px; text-indent: 2em;"></p><p style="line-height: 30px; text-indent: 2em;"></p>
unsigned char ads1232_rate_flag = RATE_80HZ;
unsigned char ads1232_ti_enable_flag = false;
unsigned long advalue_buf[ADVALUE_BUF_SIZE];
void ads1232_init(void)
{
/*GPIO时钟使能*/
CMU_ClockEnable(cmuClock_GPIO, true);
/*io方向输出*/
GPIO_PinModeSet(ADS1232_PWDN_PORT, ADS1232_PWDN_PIN,gpioModePushPull, 0);
/*io方向输入*/
GPIO_PinModeSet(ADS1232_DOUT_PORT, ADS1232_DOUT_PIN,gpioModeInputPull, 1);
/*io方向输出*/
GPIO_PinModeSet(ADS1232_SCK_PORT, ADS1232_SCK_PIN,gpioModePushPull, 1);
/*io方向输出gpioModePushPull*/
GPIO_PinModeSet(ADS1232_SPEED_PORT, ADS1232_SPEED_PIN,gpioModePushPull, 0);
/*GPIO NVIC初始化*/
GPIOINT_Init();
/*回调函数注册*/
GPIOINT_CallbackRegister(ADS1232_DOUT_PIN, gpioCallback);
/*初始化选择快速采样*/
set_ads1232_rate(RATE_80HZ);
/*先自动校准*/
ADS1232_PWDN();
delay_ms(2);
ADS1232_PWUP();
delay_ms(5);
ADS1232_Offset_Calibration();
ads1232_read_count();
TPS79901_POWER_OFF();
}
/*******************************************************************
* @函数名称:ADS1232_Offset_Calibration
* @功能说明:ADS1232校准
* @传入参数:
* @函数返回:
* @函数作者:
* @修改时间:
*******************************************************************/
void ADS1232_Offset_Calibration(void)
{
int i;
int checkcount;
unsigned long ad_value=0;
/*唤醒ADS1232*/
ads1232_sck_low();
/*等待DOUT=0*/
while(ads1232_dout_state)
{
if(checkcount++>100)
{
return ;
}
};
/*采集24位AD值*/
for(i=0;i<24;i++)
{
ad_value=ad_value<<1;
ads1232_sck_hig();
delay_us(1);
if(ads1232_dout_state==1)//DOUT=1
{
ad_value|=0x00000001;
}
ads1232_sck_low();
delay_us(1);
}
ads1232_sck_hig();
delay_us(1);
ads1232_sck_low();
delay_us(1);
ads1232_sck_hig();
delay_us(1);
ads1232_sck_low();
delay_us(1);
}
void ads1232_ti_enable(void)
{
/*使能中断*/
GPIO_IntConfig(ADS1232_DOUT_PORT, ADS1232_DOUT_PIN, false, true, true);
/*使能标志设置*/
ads1232_ti_enable_flag = true;
}
void ads1232_ti_disable(void)
{
/*关闭中断*/
GPIO_IntConfig(ADS1232_DOUT_PORT, ADS1232_DOUT_PIN, false , false, false);
GPIO_IntDisable(ADS1232_DOUT_PIN);
/*使能标志清除*/
ads1232_ti_enable_flag = false;
}
void set_ads1232_rate(unsigned char rate)
{
switch(rate)
{
case RATE_10HZ:
{
GPIO_PinOutClear(ADS1232_SPEED_PORT,ADS1232_SPEED_PIN);
ADS1232_PWUP();
memset(advalue_buf,0,40); //共用
ads1232_sck_low();
ads1232_ti_enable();
break;
}
case RATE_80HZ:
{
GPIO_PinOutSet(ADS1232_SPEED_PORT,ADS1232_SPEED_PIN);//快速采样
ads1232_sck_hig();
ads1232_ti_disable();
break;
}
default : break;
}
ads1232_rate_flag = rate;
}
unsigned long ads1232_read_count(void)
{
unsigned char i;
volatile unsigned long count = 0;
ads1232_sck_low(); // enable : SCK = 0
ads1232_ti_enable(); // 开启转换结束引脚中断
ENTER_EM2(); // enter low power mode
ads1232_ti_disable(); // 关闭引脚中断
for (i = 0; i < 24; i++)
{
ads1232_sck_hig(); // impulsing :SCK = 1
count = count << 1; // read bit
ads1232_sck_low(); // SCK = 0
if (ads1232_dout_state)
{
count++;
}
}
ads1232_sck_hig(); // 25 count CLK
delay_us(1);
ads1232_sck_low(); // select channel A gain 128, CLK = 0
delay_us(1);
#if defined WGT_ADCOUNT_19BIT
// count = count>>5;
// count = count^0x40000;
return (count >> 5);
return count;
#elif defined WGT_ADCOUNT_20BIT
return (count >> 4); // 取20位数据
#elif defined WGT_ADCOUNT_21BIT
return (count >> 3); // 取21位数据
#endif
}
void ads1232_read_isr(void)
{
unsigned char i;
volatile unsigned long count = 0;
ads1232_ti_disable(); // 关闭引脚中断
for (i = 0; i < 24; i++)
{
ads1232_sck_hig(); // impulsing :SCK = 1
count = count << 1; // read bit
ads1232_sck_low(); // SCK = 0
if (ads1232_dout_state)
{
count++;
}
}
ads1232_sck_hig(); // 25 count CLK
ads1232_sck_low(); // select channel A gain 128, CLK = 0
#if defined WGT_ADCOUNT_19BIT
advalue_buf[ads1232_buf_count] = (count >> 5);
#elif defined WGT_ADCOUNT_20BIT
advalue_buf[ads1232_buf_count] = (count >> 4); // 取20位数据
#elif defined WGT_ADCOUNT_21BIT
advalue_buf[ads1232_buf_count] = (count >> 3); // 取21位数据
#endif
if ((++ads1232_buf_count) >= ADVALUE_BUF_SIZE)
{
ads1232_buf_count = 0;
}
ads1232_ti_enable(); // 开启转换结束引脚中断
}
[/code] |