📄 ads7816.c
字号:
#include <REGX51.H>
#include <intrins.h>
#include <absacc.h>
#include <spi.h>
sbit DCLK=P1^1;
sbit Dout=P1^0;
sbit CE= P1^2;
sbit red=P2^7;
sbit green=P2^5;
void DelayMs(unsigned int ms)
{
unsigned int i=0;
unsigned int j=0;
for(i=0;i<ms;i++)
{
for(j=0;j<350;j++)
{
}
}
}
void init()
{
DCLK=1;
Dout=1;
CE=1;
}
unsigned int readADS()
{
unsigned int back_up=0;
unsigned char i=0;
bit data1=0;
Dout=1;
DCLK=0;
_nop_();
_nop_();
_nop_();
_nop_();
CE=1;
_nop_();
_nop_();
_nop_();
_nop_();
CE=0;
//sample
DCLK=1;
_nop_();
_nop_();
_nop_();
_nop_();
DCLK=0;
_nop_();
_nop_();
_nop_();
_nop_();
DCLK=1;
_nop_();
_nop_();
_nop_();
_nop_();
DCLK=0;
_nop_();
_nop_();
_nop_();
_nop_();
DCLK=1;
_nop_();
_nop_();
_nop_();
_nop_();
for(i=0;i<12;i++)
{
DCLK=0;
_nop_();
_nop_();
data1=Dout;
if(data1)
{
back_up|=(0x0800>>i);
}
DCLK=1;
_nop_();
_nop_();
}
CE=1;
return back_up;
}
void main()
{
unsigned int ad=0;
unsigned char i=0;
green=0;
red=1;
init();
init_spi();
for(i=0;i<8;i++)
{
combuf[i]=0xaa;
}
spi_send(combuf,8);
ad=readADS();
combuf[1]=(ad&0xff);
combuf[0]=(ad>>8)&0xff;
spi_send(combuf,2);
DelayMs(1000);
green=1;
red=0;
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -