I'm trying to bit bang a 7953 16 channel, 12 bit converter. It appears to be talking but the data doesn't make sense.
Code...
#define ADS7953_CLK PIN_B1 //Clock
#define ADS7953_DOUT PIN_D4 //Data from Chip
#define ADS7953_CS PIN_D5 //Chip Select
#define ADS7953_DIN PIN_A2 //must change Data to Chip
//Base routines
void write_ADS7953(int16 data) //Sends 16 bits of data
{
int i;
output_low(ADS7953_CLK); //Initial State
output_low(ADS7953_CS);
delay_us(1);
output_high(ADS7953_CS); //CS High for two clock cycles
for(i=0;i<=2;i++) //Toggle Clk twice,lo to hi
{
output_high(ADS7953_CLK);
delay_us(1);
output_low(ADS7953_CLK);
delay_us(1);
}
output_low(ADS7953_CS); //clk low, now CS low
//start shifting data in
for(i=1;i<=16;++i)
{
output_high(ADS7953_CLK);
delay_us(1);
output_bit(ADS7953_DIN, shift_left(&data,2,0));
delay_us(1);
output_low(ADS7953_CLK);
}
output_high(ADS7953_CS); //End serial
}
//Base routines
long read_ADS7953() //Gets 16 bits of data
{
int i;
long data;
int8 chan;
data=0;
chan= 0;
output_low(ADS7953_DIN); //All Zeros - Doesn't matter in Auto Mode
output_low(ADS7953_CLK); //Initial States
output_low(ADS7953_CS);
delay_us(1);
output_high(ADS7953_CS); //CS high for 2 clk cycles
for(i=0;i<=2;i++) //toggle clk twice, lo to hi
{
output_high(ADS7953_CLK);
delay_us(1);
output_low(ADS7953_CLK);
delay_us(1);
}
output_low(ADS7953_CS); //make CS low
for(i=0;i<12;++i) { // shift 12 data bits from ADC
output_high(ADS7953_CLK);
delay_us(1);
shift_left(&data,2,input(ADS7953_DOUT));
output_low(ADS7953_CLK);
delay_us(1);
}
for(i=0;i<4;++i) { // shift 4 channel number bits from ADC
output_high(ADS7953_CLK);
delay_us(1);
shift_left(&chan,1,input(ADS7953_DOUT));
output_low(ADS7953_CLK);
delay_us(1);
}
output_high(ADS7953_CS);
printf("Channel: %d \r\n",Chan);
return(data);
}
//0 = Manual
//1 = Auto1
//2 = Auto2
void init_ADS7953(byte mode)
{ long ModeCfg, ProgCfg, ChanCfg;
//Only support Auto1 for Testing
/*
PGM1 Mode Control Register Layout (Frame 1)
Bit 0-3 GPIO Data for Outputs (Right)
Bit 4 0= Output 4 bit channel data with ADC Val,
1= Output 4 bit GPIO Values
Bit 5 0= Normal 1=Power Down after conversion
Bit 6 0= 2.5 volt Full Scale 1 = 5.0 volt full scale
Bit 7-9 Do not care
Bit 10 0 = Inc Channel 1= Start at lowest channel
Bit 11 0 = Retains Bits 0-10 every frame 1= Program 0-10
Bit 12-15 0010 = Auto Mode, 0001 = reset state
*/
ModeCfg = 0b001000000100000;
/*
PGM2 Program Register layout (Frame 2)
Bits 0-11 Don't Care
Bits 12-15 1000 - Enter Auto1 Mode
*/
ProgCfg = 0b100000000000000;
/*
PGM3 Channel Selection (Frame 3)
Bits 0-15 Which CHannels are on for us, 1111 1111 1111 1111
*/
ChanCfg = 0b1111111111111111;
//Write Mode Control Configuration Frame 1
Write_ADS7953(ModeCfg);
//Write Program Registers Frame 2
Write_ADS7953(ProgCfg);
//Write Channel Selections
Write_ADS7953(ChanCfg);
}