void ini
tialize(void);
void reset_converter(void);
void toggle_sdo(void);
char receive_byte(void);
void transfer_byte(char);
void write_to_register(char command,char low,char mid, char high);
void read_register(char command);
void acquire_conversion(char command);
/*** Byte Memory Map Equates ***/
sfr P1=0x90; /*Port One*/
sfr ACC=0xE0; /*Accumulator Register Equate*/
sbit CS=0x90; /* Chip Select, only used in four-wire mode*/
sbit SDI=0x91; /* Serial Data In*/
sbit SDO=0x92; /*Serial Data Out*/
sbit SCLK=0x93; /*Serial Clock*/
/*** Global Variable ***/
char command, /*Memory Storage Variable for Command Byte */
high_byte, /*Memory Storage Variable for Most Significant Byte*/
mid_byte, /* Memory Storage Variable for Most Significant Byte*/
low_byte, /* Memory Storage Variable for Most Significant Byte*/
temp, /*General Purpose Temporary Variable*/
mode; /*Variable Stores Mode of Operation 0 = three wire, 1 = 4 wire*/
main()
{
mode = 1; /*Make Communication be Four-Wire Mode*/
initialize(); /*Call Routine to Initialize 80C51 and CS5525/6/9*/
while(1){
command = 0x82; /*Prepare to Write to Gain Register*/
high_byte= 0x80; /*Make High_byte 80 (HEX)*/
mid_byte= 0x00; /*Make Mid_byte all Zero’s*/
low_byte= 0x00; /*Make low_byte all Zero’s*/
write_to_register(command,low_byte,mid_byte,high_byte);/*Write to gain Register*/
read_register(0x92); /*Read Contents of Gain Register*/
while(1)
{
acquire_conversion(0xC0); /*Acquire a Single Conversion*/
}/*End inner while loop*/
}/*End While Loop*/
}/*end main*/
void initialize()
/*** Local Variables ***/
data int counter;
/*** Body of Subroutine ***/
/*** Initialize 80C51’s Port 1 ***/
P1 = 0xF4; /*SCLK - Output */
for(counter=0;counter<2047;counter++){
SCLK = 0x01; /*Assert SCLK*/
SCLK = 0x00; /*Deassert*/
}
/*Reset Serial Port on CS5525/6/9*/
SDI = 0x01; /*Assert SDI*/
for(counter=0;counter<255;counter++) {
SCLK = 0x01; /*Assert SCLK*/
SCLK = 0x00; /*Deassert SCLK*/
}
SDI = 0x00; /*Deassert SDI PIN*/
SCLK = 0x01; /*Assert SCLK*/
SCLK = 0x00; /* Deassert SCLK*/
}
void calibrate()
{ write_to_register(0x84,0x01,0x00,0x00); /*Assert RS bit*/
/*Read Configuration Register Until DF Bit is Asserted*/
do {
read_register(0x94); /*Read Configuration Register*/
temp = low_byte&0x08; /*Mask DF bit to 1*/
} while (temp != 0x08);
read_register(0x92); /*Deasserts DF Bit*/
}/*End calibrate */
void write_to_register(char command,char low,char mid,char high){
if(mode == 1) P1 = 0xF4; /*Assert if necessary*/
CS
transfer_byte(command); /*Transfer Command Byte to CS5525/6/9*/
transfer_byte(high); /*Transfer High Byte to CS5525/6/9*/
transfer_byte(mid); /*Transfer Middle Byte to CS5525/6/9*/
transfer_byte(low); /*Transfer Low Byte to CS5525/6/9*/
if(mode == 1) P1 = 0xF5; /*Deassert if necessary*/
CS
}
void read_register(char command){
if(mode == 1) P1 = 0xF4; /*Assert if necessary */
CS
transfer_byte(command); /*Transfer Command Byte to CS5525/6/9*/
high_byte = receive_byte(); /*Receive Command Byte from CS5525/6/9*/
mid_byte = receive_byte(); /*Receive Command Byte from CS5525/6/9*/
low_byte = receive_byte(); /*Receive Command Byte from CS5525/6/9*/
if(mode == 1)P1 = 0xF5; /*Deassert if necessary */
CS
}
void acquire_conversion(char command){
/*** Read Configuration Register to Prevent Previously Set Bits from being Altered ***/
read_register(0x94); /*Read Configuration Register*/
low_byte = low_byte|0x20; /*Assert Port Flag Bit*/
write_to_register(0x84,low_byte, mid_byte, high_byte);/*Actually Send Commands*/
/*Acquire a Conversion*/
if(mode == 1)P1 = 0xF4;
transfer_byte(0xC0); /*Transfer Command to CS5525/6/9*/
toggle_sdo(); /*Clear SDO*/
high_byte = receive_byte(); /*Receive Command Byte from CS5525/6/9*/
mid_byte = receive_byte(); /*Receive Command Byte from CS5525/6/9*/
low_byte = receive_byte(); /*Receive Command Byte from CS5525/6/9*/
if(mode == 1) P1 = 0xF5; /*Deassert if necessary*/
}
一周热门 更多>