WAT-910BD H/W MANUAL
//----------------------------------------------------------------------
// FUNCTION: SPI_WRITE_3W
//
// SPI WRITE SEND_BUFF[0...(W_Byte-1)]
// In case of Indirect access, it must W_Adr = 0x36 and W_Byte = 8.
// In case of 3Wire, it need change GPIO(MISO/MOSI) direction
// input/output.
//----------------------------------------------------------------------
unsigned char
SPI_WRITE_3W(unsigned char W_Adr, unsigned char W_Byte)
{
int i ;
int j ;
int WriteCMD ;
unsigned char
SendBit ;
// make SPI WRITE COMMAND
WriteCMD = (int)W_Adr << 1 ;
SEND_BUFF[0] = (unsigned char)WriteCMD ;
// lower 8bit
SEND_BUFF[1] = (unsigned char)(WriteCMD >> 8) ;
// higher 8bit
i = 0 ;
// Byte Counter
// initialize SPI bus
p3_5 = 1 ;
// SS = H
p3_4 = 1 ;
// SCLK = H
wait_nop() ;
// SPI WRITE COMMAND send start
pd3_3 = 1;
// set port direction MOSI(output)
p3_3 = 1 ;
// MOSI = H
p3_5 = 0 ;
// SS = L
while( i < W_Byte ){
// W_Byte must 8 in INDIRECT ACCESS
j = 1 ;
// Bit Mask
while(j < 0xFF){
// 8bit send
SendBit = SEND_BUFF[i] & j ? 1 : 0 ;
// bit test
j = j << 1 ;
// j *= 2 ;
p3_4 = 0 ;
// SCLK = L
p3_3 = SendBit ;
// output LSB first bit data on MOSI
wait_nop() ;
p3_4 = 1 ;
// SCLK = H(latch)
}
i ++ ;
// Next byte
wait_nop() ;
}
p3_5 = 1 ;
// SS = H(END)
pd3_3 = 0;
// set port direction MISO(input)
wait_ms(1) ;
// CTL-COMMAND execution is done ?
READ_BUFF[0] = 0xAA ;
// initialize
while( READ_BUFF[0] == 0xAA ){
// 0xAA means busy
- 55 -