int main(void)
{
char Received_From_device[500] = ""; // Maximum data from device of 500 bytes
//char character[10] = "";
char ETX_character;
int i,a,lazy_timeout_counter,lazy_timeout_max,tmp,adc_value;
char c;
RCC_Configuration();
GPIO_Configuration();
SPI2_Configuration(); //SPI configure
USART1_Configuration();
GPIO_SetBits(GPIOB, GPIO_Pin_12); //set Chip Select HIGH for SPI
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART1, 0x49); // Send 'I'
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 0x42); // Send 'B'
SPI_Loop:
Delay(1000); //1second delay
//goto Jump2Here;
//Select CH0 of LTC2492
GPIO_ResetBits(GPIOB, GPIO_Pin_12); // Assert -CS
Delay_us(10); // overkill
// You need semicolons after the whiles otherwise they repeat the next statement
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == 0); //wait for transmit buffer to be empty, should be
SPI_I2S_ReceiveData(SPI2); // Clear RXNE
SPI_I2S_SendData(SPI2, LTC_Internal_Temperature_select); //LTC_Internal_Temperature_select
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == 0); //wait for transmit buffer to not be empty (ie data loaded in)
SPI_I2S_SendData(SPI2, 0); // Fill for 32-bits, remaining 16-bits in holding buffer
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == 0); // wait for last bit to be sent
SPI_I2S_ReceiveData(SPI2); // Clear RXNE, first word
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == 0); // wait for last bit to be sent
SPI_I2S_ReceiveData(SPI2); // Clear RXNE, second word
Delay_us(10);
GPIO_SetBits(GPIOB, GPIO_Pin_12); // Release -CS
Delay(1000); //1second delay
//await result readable via SPI and read in result via SPI two 16bit reads over SPI
GPIO_ResetBits(GPIOB, GPIO_Pin_12); // Assert -CS
Delay_us(10); // overkill
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == 0); // should be empty
SPI_I2S_ReceiveData(SPI2); // clear RXNE
SPI_I2S_SendData(SPI2, LTC_Internal_Temperature_select); // send command word
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == 0); //wait for transmit buffer to not be empty (ie data loaded in)
SPI_I2S_SendData(SPI2, 0); // Fill for 32-bits, remaining 16-bits in holding buffer
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == 0);
tmp = SPI_I2S_ReceiveData(SPI2); // read word from receive buffer, high order 16-bits
adc_value = tmp * 65536;
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == 0); // wait until receive buffer is not empty
tmp = SPI_I2S_ReceiveData(SPI2); // read word from receive buffer, low order 16-bits
adc_value = adc_value + tmp;
Delay_us(10);
GPIO_SetBits(GPIOB, GPIO_Pin_12); // Release -CS
Jump2Here:
//send result via RS232
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 0x43); // Send 'C'
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 0x48); // Send 'H'
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 0x30); // Send '0'
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 0x3A); // Send ':'
// Sends as binary, not ascii
//adc_value = 1234567890;
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ((adc_value >> 24) & 0xFF));
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ((adc_value >> 16) & 0xFF)
);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ((adc_value >> 8) & 0xFF)
);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ((adc_value >> 0) & 0xFF)
);
//goto SPI_Loop;
while (1);
}