Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] [C] problem with MAX187 & ATmega16

Status
Not open for further replies.

jirkaj4

Newbie level 4
Newbie level 4
Joined
Jul 1, 2011
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,316
Hello.
I'm trying to communicate with the AD converter Max187 and ATmega16. But not me.

The display is no number, no nothing. So I think that with the AD converter establish a connection.

Do you know where the problem might be?
It is about connections: ATmega16 - SPI - Max187.
16MHz crystal. Codes attach here:

Code:
#include <avr/io.h>  
#include <stdio.h>
#include <util/delay.h>  
#include <avr/interrupt.h>  

#include "lcd.h"	//Communication with LCD2004
#include "spi.h"	//Communication on SPI bus
//set CPOL = 0 and CPHA = 0 from datasheet

unsigned char data;		//value of nuber from MAX187
char buffer[50];

void inicializace(){
        InitSPI();  //INIT SPI 
	lcd_init(LCD_DISP_ON);	//INITLCD
}

int main(){
	inicializace();

while(1){
	
	//read data from MAX187
       SELECT();       // Select SS slave         
       data = ReadByteSPI();   //read data from SPI  
       DESELECT();     // Stop SS Slave  

	_delay_ms(10);	//small delay

        //show value on LCD
	lcd_gotoxy(0,1);
	printf(buffer,"%d",data);
	lcd_puts(buffer);
      
}return(0);
}

And the whole project can be downloaded here [AVR Studio 4]
View attachment program.rar.

thank you
 

After a few hours of work, I solved the problem. Library "spi.h" I deleted it and replaced it with the following:
Code:
#include <avr/io.h>  
#include <stdio.h>
#include <util/delay.h>  
#include <avr/interrupt.h>  

#include "lcd.h"	//knihovna pro komunikaci s LCD 2004


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

char buffer[50];

unsigned int get_adc(unsigned char adata)
{
	static unsigned int temp=0;
	
	PORTB &= ~(1<<SS);					// SET CS do 0 (aktivovani ADC) 
	SPDR = adata;						// vložit data do odesílací registru
	while(!(SPSR & (1<<SPIF)));			        // Poslat 1 byte přes HW SPI a čeka na tx
	temp = SPDR;						// přijatá data nahraje do proměnné TEMP
	temp = temp << 8;					// posune proměnnou TEMP o 8 vlevo
	SPDR = adata;						// vloží data do odesílacího registru
	while(!(SPSR & (1<<SPIF)));			        // Poslat 1 byte přes HW SPI a čeka na tx	
	temp |= SPDR;						// přijatá data nahraje do proměnné TEMP
	PORTB |= (1<<SS);					// nastavi CS do 1 a tim zakaze ADC 
	return temp;						// navratí hodnotu ADcka
}



void spim_init(void)
{
	// SET PORT B jako výstup 
	DDRB |= (1<<MOSI) | (1<<SCLK) | (1<<SS);
	// SET MISO jako vstup
	DDRB &= ~(1<<MISO); 
	// Povoleni SPI, SPI Master, SPI CLK XTAL/16, LSB First
	SPCR |= (1<<SPE)|(1<<MSTR)|(1<<SPR0);		
	// nastaveni funkcniho registra SPI (SPCR), CPHA a CPOL v log 0
	SPCR &= ~ (1<<CPOL); SPCR &= ~ (1<<CPHA);	
	// CS HIGH protože SPI ADS je aktivní v 0
	PORTB |= (1<<SS);		 
}

int main (void)
{
	static unsigned int adc_value=0;	// value AD prevodniku
	
	lcd_init(LCD_DISP_ON);	//inicializace LCD
	spim_init();			// Inicializace SPI jako MASTER

	for(;;){
	adc_value = get_adc(0xFF);				// získání hodnoty z AD prevodniku

	lcd_gotoxy(0,1);
	sprintf(buffer,"%d",adc_value);
	lcd_puts(buffer);	

	}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top