/******************ADS1118读写*********************/
/****************qq:774145445**********************/
/****************快手:共同学习STM8*****************/
/*****************2021020603版*********************/
// STM8S207R8T6 ADS1118
// PD6 63|DOUT----------DIN 10|DIN
// PD4 61|DIN-----------OUT 9|DOUT
// PD2 59|CS-------------CS 2|CS
// PD0 57|SCLK----------SCK 1|SCLK
// 3.3V|--------------VIN 8|3.3V
// GND|-----------------GND|GND
/**************************************************/
#include "STM8S207R.h"
#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
#define BIT5 0x20
#define BIT6 0x40
#define BIT7 0x80
#define BIT8 0x0100
#define BIT9 0x0200
#define BIT10 0x0400
#define BIT11 0x0800
#define BIT12 0x1000
#define BIT13 0x2000
#define BIT14 0x4000
#define BIT15 0x8000
#define ADS1118_DOUT_L PD_ODR&=~BIT6
#define ADS1118_DOUT_H PD_ODR|=BIT6
#define ADS1118_DIN PD_IDR&BIT4 //输入
#define ADS1118_CS_L PD_ODR&=~BIT2
#define ADS1118_CS_H PD_ODR|=BIT2
#define ADS1118_SCLK_L PD_ODR&=~BIT0
#define ADS1118_SCLK_H PD_ODR|=BIT0
void yanshi100ns(unsigned char x); //延时
unsigned int spi(unsigned int spi_a1); //SPI
/**************************************************/
unsigned int temp_a1;
unsigned int temp;
main()
{
/**************************************************/ //时钟切换
CLK_CKDIVR &=~(BIT3|BIT4); //HSI不分频16M
/**************************************************/ //IO配置
//ODR //输出数据寄存器
//IDR //输入寄存器
//DDR //数据方向寄存器
//CR1 //上拉电阻
//CR2 //输入中断,输出速度
PD_DDR |=BIT6; //ADS1118_DOUT //输出推挽
PD_CR1 |=BIT6;
PD_DDR &=~BIT4; //ADS1118_DIN//输入
PD_DDR |=BIT2; //ADS1118_CS
PD_CR1 |=BIT2;
PD_DDR |=BIT0; //ADS1118_SCLK
PD_CR1 |=BIT0;
/**************************************************/ //IO初始化
ADS1118_CS_H;
ADS1118_SCLK_L;
ADS1118_DOUT_L;
/************************************************** //AD1118寄存器配置(默认值即可)
//15 /启动单次转换;
//14-12 /AIN P 为 AIN0 且 AIN N 为 AIN1;
//11-9 /FSR 为 ±2.048V;
//8 /断电并采用单次转换模式;
//7-5 /128SPS;
//4 /1 = 温度传感器模式
//3 /使能 DOUT/DRDY 引脚的上拉电阻;
//2-1 /更新配置寄存器;
//0 /始终写入 1h;
//第一路单次转换
//spi_a1 =0B1000_0101_1001_1011;
//spi_a1 =0x859b;
//第二路单次转换
//spi_a1 =0B1011_0101_1001_1011;
//spi_a1 =0xb59b;
/**************************************************/
while(1)
{
yanshi100ns(1); //每次读都是上一次的值
temp_a1 =spi(0x859b);
//temp_a1 =spi(0x8f9b);
temp_a1 >>=2;
temp =temp_a1*0.03125;
yanshi100ns(1);
temp_a1 =spi(0xb59b);
//temp_a1 =spi(0x8f9b);
temp_a1 >>=2;
temp =temp_a1*0.03125;
}
}
/**************************************************/
void yanshi100ns(unsigned char x) //延时125ns
{
_asm("nop");
_asm("nop");
_asm("nop");
_asm("nop");
}
/**************************************************/
unsigned int spi(unsigned int spi_a1) //SPI
{
unsigned int spi_b1,spi_b2;
unsigned char spi_c1;
spi_b1 =spi_a1;
ADS1118_CS_L;
yanshi100ns(1);
for(spi_c1=0;spi_c1<16;spi_c1++)
{
if(spi_b1&0x8000)
{
ADS1118_DOUT_H;
}
else
{
ADS1118_DOUT_L;
}
spi_b1 <<=1;
ADS1118_SCLK_H;
yanshi100ns(1);
spi_b2 <<=1;
if(ADS1118_DIN) //输入
{
spi_b2 |=0x0001;
}
ADS1118_SCLK_L;
yanshi100ns(1);
}
ADS1118_SCLK_L;
yanshi100ns(1);
ADS1118_DOUT_L;
ADS1118_CS_L;
yanshi100ns(1);
return spi_b2;
}
/**************************************************/
/**************************************************/
|