我用的PIC的单片机写MAX11206的程序,转换横梁式称重电桥传感器的输出,总是得不到结果。时序是对的,读写寄存器都问题,但是状态寄存器的UR=1(64倍增益。表示超出最小转换值)。把增益设为1,输入接为参考Vref结果为0xAB1C1;不是0xFFFFF。请高手指点用法!我的程序:
#include <htc.h>
#define SCLK RD0
#define DOUT RD1
#define DIN RD2
#define CS RD3
#define SCLK_TRIS TRISD0
#define DOUT_TRIS TRISD1
#define DI_TRIS TRISD2
#define CS_TRIS TRISD3
#define OUTPUT 0b0
#define INPUT 0b1
void delay(unsigned int ms)
{
unsigned int j;
while((ms--) > 0){
j=100;
while((j--) > 0){;}
}
}
void write_max11200_byte(unsigned char dat )
{
char i;
unsigned char temp;
temp = dat;
SCLK = 0;
delay(10);
for(i = 0; i < 8; i++)
{
SCLK = 0;
delay(1);
if((temp & 0x80) == 0)
{
DIN = 0;
}
else
{
DIN = 1;
}
delay(1);
SCLK = 1;
delay(1);
temp = (temp << 1);
}
SCLK = 0;
}
void write_max11200_cmd(unsigned char dat )
{
CS = 1;
delay(5);
SCLK = 0;
delay(5);
SCLK = 1;
delay(5);
SCLK = 0;
delay(5);
CS = 0;
write_max11200_byte(dat);
}
void write_max11200_reg(unsigned char cmd, unsigned char dat )
{
CS = 1;
delay(5);
SCLK = 0;
delay(5);
SCLK = 1;
delay(5);
SCLK = 0;
delay(5);
CS = 0;
write_max11200_byte(cmd);
write_max11200_byte(dat);
}
unsigned char read_max11200_byte(void)
{
char i;
unsigned char temp;
temp = 0;
SCLK = 0;
asm("nop");asm("nop");
for(i = 0; i < 8; i++)
{
temp <<= 1;
SCLK = 0;
asm("nop"); asm("nop");
asm("nop"); asm("nop");
SCLK = 1;
asm("nop");asm("nop");
asm("nop");asm("nop");
if(DOUT)
temp += 1;
SCLK = 0;
}
return temp;
}
void init_max11200(void)
{
ANSELD=0X00; // RD口为数字I/O
SCLK_TRIS = OUTPUT;
DOUT_TRIS = INPUT;
DI_TRIS = OUTPUT;
CS_TRIS = OUTPUT;
CS = 0;
asm("nop");asm("nop");
asm("nop");asm("nop");
write_max11200_reg(0xC2,0xC0);// Write CTRL1 LINEF = 1 | unipolar | Continuous Conversion
delay(10);
write_max11200_reg(0xC6,0xDE);// Write CTRL3 Gain = 64 ,NOSCG=0, NOSCO=0
delay(10);
write_max11200_reg(0xC4,0xfa);// Write CTRL2 GPIOs are Output, 1010
delay(10);
write_max11200_cmd(0x85); //100SPS
}
unsigned long read_max11200(void)
{
unsigned long dat;
CS = 0;
write_max11200_byte(0XC9);
while(DOUT);
dat = read_max11200_byte();
dat <<= 8;
dat |= read_max11200_byte();
dat <<= 8;
dat |= read_max11200_byte();
dat >>= 4;
return dat;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>