TLC2543 test. reading and display middle point of ADC.
Toolkit:STM Development System
Location:/bipom/devtools/STM32/examples/Wipom/tlc2543
int main(void) { delayMs(250); UINT cnt =0; UINT value; tprintf("\n\rWiPOM"); tprintf("\n\rTLC2543 Test"); TLC2543_Enable(); for(;;) { value = TLC2543_Read(0x0B); // mid point tprintf("\n\rcnt=%lu value=%lu", cnt++, value); delayMs(1000); } return 0; } //******************************************************************************** void TLC2543_SetBit(UINT bit) { GPIO_InitTypeDef GPIO_InitStructure; // GPIO_InitStructure.GPIO_Pin = (1UL<<bit); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOE, &GPIO_InitStructure); } //******************************************************************************** void TLC2543_ClrBit(UINT bit) { GPIO_InitTypeDef GPIO_InitStructure; // GPIO_InitStructure.GPIO_Pin = (1UL<<bit); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(GPIOE, &GPIO_InitStructure); GPIO_ResetBits(GPIOE,(1UL<<bit)); } //******************************************************************************** UINT TLC2543_GetBit(UINT bit) { return GPIO_ReadInputDataBit(GPIOE,(1UL<<bit)); } //******************************************************************************** void TLC2543_Enable(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); } //******************************************************************************** UINT TLC2543_Read( UBYTE channel ) { UINT value = 0; UBYTE i; channel <<= 4; channel &=0xF0; TLC2543_SetBit(TLC2543_ADCDOUT); // Configure input TLC2543_SetBit(TLC2543_ADCEOC); // Configure input TLC2543_ClrBit(TLC2543_ADCCLOCK); TLC2543_ClrBit(TLC2543_ADCCS); delayUs(TLC2543_DELAY_US); for( i=0; i<8; i++ ) { if( channel & 0x80 ) { TLC2543_SetBit(TLC2543_ADCDIN);} else { TLC2543_ClrBit(TLC2543_ADCDIN);} delayUs(TLC2543_DELAY_US); TLC2543_SetBit(TLC2543_ADCCLOCK); delayUs(TLC2543_DELAY_US); TLC2543_ClrBit(TLC2543_ADCCLOCK); delayUs(TLC2543_DELAY_US); channel = channel << 1; } TLC2543_ClrBit(TLC2543_ADCDIN); // Dummy 4 bits for( i=0; i<4; i++ ) { TLC2543_SetBit(TLC2543_ADCCLOCK); delayUs(TLC2543_DELAY_US); TLC2543_ClrBit(TLC2543_ADCCLOCK); delayUs(TLC2543_DELAY_US); } // Wait until end of conversion while(!TLC2543_GetBit(TLC2543_ADCEOC)); // Now read 12 bits of value from ADC value = 0; for( i=0; i<12; i++ ) { value <<= 1; value |= TLC2543_GetBit(TLC2543_ADCDOUT); TLC2543_SetBit(TLC2543_ADCCLOCK); delayUs(TLC2543_DELAY_US); TLC2543_ClrBit(TLC2543_ADCCLOCK); delayUs(TLC2543_DELAY_US); } TLC2543_SetBit(TLC2543_ADCCS); TLC2543_SetBit(TLC2543_ADCCLOCK); TLC2543_SetBit(TLC2543_ADCDIN); // return value; }