twi veri alma problemi

Başlatan berkay_91, 18 Eylül 2014, 11:31:19

berkay_91

mrb, aşağıdaki master ve slave için koyduğum programlar twi ile iki microişlemcinin birbirine veri göndermesi için ancak ilk veri, master transmitter olduğunda gönderildikten sonra birdaha slave tarafından master 'a veri gelmiyor her ikisinede LCD bağlayıp denedim olmadı yardımcı olur musunuz? 


MASTER
/*
 * twi_master.c
 *
 * Created: 16.09.2014 12:51:27
 *  Author: BERKAY
 */ 

#define F_CPU 1000000
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <inttypes.h>
#include "lcd.h"


void TWI_start(void);
void TWI_repeated_start(void);
void TWI_init_master(void);
void TWI_write_address(unsigned char);
void TWI_read_address(unsigned char);
void TWI_write_data(unsigned char);
void TWI_read_data(void);
void TWI_stop(void);

volatile unsigned char address=0x20, read=1, write=0;
volatile unsigned char write_data=0x03, recv_data;

FILE lcd_str = FDEV_SETUP_STREAM(lcd_putc, NULL, _FDEV_SETUP_WRITE);

int main(void){
	
	DDRD=0XFF;
	
	lcd_init(LCD_DISP_ON);

	stdout = stdin = &lcd_str;
	
	_delay_ms(10);
	
	TWI_init_master();  // Function to initialize TWI
	
	lcd_clrscr();
	lcd_gotoxy(2,0);
	lcd_puts("TWI MASTER");
	
	while(1)
	{
		
		TWI_start(); // Function to send start condition
		TWI_write_address(address+write); // Function to write address and data direction bit(write) on SDA
		TWI_write_data(write_data);      // Function to write data in slave
		TWI_stop(); // Function to send stop condition
		
		_delay_ms(10); // Delay of 10 mili second
		
		TWI_start();
		TWI_read_address(address+read); // Function to write address and data direction bit(read) on SDA
		TWI_read_data(); // Function to read data from slave
		TWI_stop();
		
		_delay_ms(10);
		
		write_data = write_data * 2;
	}
	
	
}

void TWI_init_master(void) // Function to initialize master
{
	TWBR=0x01; // Bit rate
	TWSR=(0<<TWPS1)|(0<<TWPS0); // Setting prescalar bits
	// SCL freq= F_CPU/(16+2(TWBR).4^TWPS)
}

void TWI_start(void)
{
	// Clear TWI interrupt flag, Put start condition on SDA, Enable TWI
	TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT))); // Wait till start condition is transmitted
	while((TWSR & 0xF8)!= 0x08); // Check for the acknowledgement
}

void TWI_repeated_start(void)
{
	// Clear TWI interrupt flag, Put start condition on SDA, Enable TWI
	TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT))); // wait till restart condition is transmitted
	while((TWSR & 0xF8)!= 0x10); // Check for the acknoledgement
}

void TWI_write_address(unsigned char data)
{
	TWDR=data; // Address and write instruction
	TWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWI
	while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmitted
	while((TWSR & 0xF8)!= 0x18);  // Check for the acknoledgement
}

void TWI_read_address(unsigned char data)
{
	TWDR=data; // Address and read instruction
	TWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWI
	while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte received
	while((TWSR & 0xF8)!= 0x40);  // Check for the acknoledgement
}

void TWI_write_data(unsigned char data)
{
	TWDR=data; // put data in TWDR
	TWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWI
	while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmitted
	while((TWSR & 0xF8) != 0x28); // Check for the acknoledgement
}

void TWI_read_data(void)
{
	TWCR=(1<<TWINT)|(1<<TWEN);    // Clear TWI interrupt flag,Enable TWI
	while (!(TWCR & (1<<TWINT))); // Wait till complete TWDR byte transmitted
	while((TWSR & 0xF8) != 0x58); // Check for the acknoledgement
	recv_data=TWDR;
	
	lcd_gotoxy(0,1);
	printf("%d",recv_data);
}

void TWI_stop(void)
{
	// Clear TWI interrupt flag, Put stop condition on SDA, Enable TWI
	TWCR= (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
	while(!(TWCR & (1<<TWSTO)));  // Wait till stop condition is transmitted
}


SLAVE

/*
 * twi_slave.c
 *
 * Created: 16.09.2014 13:06:27
 *  Author: BERKAY
 */ 

#define F_CPU 1000000
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd.h"

void TWI_init_slave(void);
void TWI_match_read_slave(void);
void TWI_read_slave(void);
void TWI_match_write_slave(void);
void TWI_write_slave(void);

volatile unsigned char write_data,recv_data;

FILE lcd_str = FDEV_SETUP_STREAM(lcd_putc, NULL, _FDEV_SETUP_WRITE);

int main(void){
	
	DDRD=0XFF;
	
	lcd_init(LCD_DISP_ON); 

	stdout = stdin = &lcd_str;

	TWI_init_slave(); // Function to initilaize slave
	
	lcd_clrscr();
	lcd_gotoxy(3,0);
	lcd_puts("TWI SLAVE");
	
	while(1)
	{
		TWI_match_read_slave(); //Function to match the slave address and slave direction bit(read)
		TWI_read_slave(); // Function to read data
		
		write_data=recv_data; // Toggle the receive data
		
		TWI_match_write_slave(); //Function to match the slave address and slave dirction bit(write)
		TWI_write_slave();       // Function to write data
	}
}

void TWI_init_slave(void) // Function to initilaize slave
{
	TWAR=0x20; // Fill slave address to TWAR
}

void TWI_write_slave(void) // Function to write data
{
	TWDR= write_data;           // Fill TWDR register whith the data to be sent
	TWCR= (1<<TWEN)|(1<<TWINT);   // Enable TWI, Clear TWI interrupt flag
	while((TWSR & 0xF8) != 0xC0); // Wait for the acknowledgement
}

void TWI_match_write_slave(void) //Function to match the slave address and slave dirction bit(write)
{
	while((TWSR & 0xF8)!= 0xA8) // Loop till correct acknoledgement have been received
	{
		// Get acknowlegement, Enable TWI, Clear TWI interrupt flag
		TWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT);
		while (!(TWCR & (1<<TWINT)));  // Wait for TWINT flag
	}
}

void TWI_read_slave(void)
{
	// Clear TWI interrupt flag,Get acknowlegement, Enable TWI
	TWCR= (1<<TWINT)|(1<<TWEA)|(1<<TWEN);
	while (!(TWCR & (1<<TWINT))); // Wait for TWINT flag
	while((TWSR & 0xF8)!=0x80); // Wait for acknowledgement
	recv_data=TWDR; // Get value from TWDR
	
	lcd_gotoxy(0,1);
	printf("%d",recv_data);
	 
}

void TWI_match_read_slave(void) //Function to match the slave address and slave dirction bit(read)
{
	while((TWSR & 0xF8)!= 0x60)  // Loop till correct acknoledgement have been received
	{
		// Get acknowlegement, Enable TWI, Clear TWI interrupt flag
		TWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT);
		while (!(TWCR & (1<<TWINT)));  // Wait for TWINT flag
	}
}