This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TLC4541 and Atmega16

Other Parts Discussed in Thread: TLC4541

hello.
I try to communicate with the TLC4541, but the problem is that the ADC value is false. And I can not detect the error.
The value does not change continuously but in steps (eg 2040> 4080> 8160> 16,320, .....). Tact MCU is 16MHz.

connection with the AVR.
AVR = TLC4541
CS = SS
SDO = MISO
SCK = SCK

FS pin is connected to +5 V

Program is HERE.

1616.TLC4541.c
/******************************************************************************
*             Program vy��t� hodnotu z 16- bit AD p�evodn�ku AD7688           *
*				a zobrazuje ji na LCD                         *
******************************************************************************/

#include <avr/io.h>  
#include <stdio.h>
#include <stdlib.h>  
#include <util/delay.h>  
#include <avr/interrupt.h> 
#include <math.h>

#include <inttypes.h>
#include <avr/pgmspace.h>

#include "lcd.h"  

// Mega16 Pinout
#define MOSI	5
#define MISO	6
#define SCLK	7
#define SS		4

char buffer[20];

//pr�m�rov�n�
uint32_t suma;
uint16_t adc_value; 

void TLC4541_init(void)
{
	PORTB &= ~(1<<SS);			//SET CS do 0 
		for (int x=0;x<5;x++)
		{
		PORTB |= (1<<SCLK);
		_delay_us(1);
		PORTB &= ~(1<<SCLK);
		_delay_us(1);
		}   
	PORTB |= (1<<SS);			//SET CS do 1
	_delay_ms(2);

		// ENable SPI, SPI Master, LSB First, SPI CLK XTAL/16)
	SPCR |= (1<<SPE)|(1<<MSTR)|(1<<SPR0);	
	// SET function register SPI (SPCR)
	SPCR &=  ~(1<<CPOL); 	//datasheet
	SPCR |=  (1<<CPHA);		//datasheet
	//nastaveni DORD (setup LSB and MSB)
	SPCR &= ~(1<<DORD);
	// CS HIGH (STOP AD)
	PORTB |= (1<<SS);
}

unsigned int get_adc(unsigned char adata)
{
   static unsigned int temp=0;

	_delay_ms(1);
	PORTB &= ~(1<<SS);			// SET CS to LOW (activate ADC)
	_delay_us(3);				// small dealy
	SPDR = adata;                 
	while(!(SPSR & (1<<SPIF)));	//MSB           
	temp = SPDR;                 
	temp <<=8;					//shift to upper byte                 
	SPDR = adata;                 
	while(!(SPSR & (1<<SPIF)));	//LSB
	temp += SPDR;				//add low byte
	PORTB |= (1<<SS);			//SET CS ti HIGH (stop 
	return temp;				//return 16bit value
}

void spim_init(void)
{
	// SET PORT B OUTput
	DDRB |= (1<<MOSI) | (1<<SCLK) | (1<<SS);
	// SET MISO INput
	DDRB &= ~(1<<MISO); 
		 
}
  
int main(){  
	unsigned int adc_value=0;	// ADC value


	//inicializace SPI
	spim_init();
	//inicializacia displeja 
    lcd_init(LCD_DISP_ON);  

		//inicializace ADC
	TLC4541_init();

for(;;){

	//adc_value = get_adc(0xFF);				//Get ADC value

	//lcd_gotoxy(0,1);
	//sprintf(buffer,"ADC: %05u",adc_value);		//zobrazi na LCD hodnotu adc_value na 4 m�sta (0000)
	//lcd_puts(buffer);

	suma = 0;
	for(uint8_t i=0; i<150; i++)	//zm��en� 100 vzork�
	suma += get_adc(0xFF);
	
	adc_value = suma / 150;		//aritmetick� pr�m�r ze 100 vzork�

	lcd_gotoxy(0,1);
	sprintf(buffer,"ADC: %05u",adc_value);		//zobrazi na LCD hodnotu adc_value na 4 m�sta (0000)
	lcd_puts(buffer);	

	}return 0;  
}

Perhaps someone can help. thank you