TWI problem with XMEGA-A3BU Xplained and ATAVRSBIN1

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey guys,
I have recently bought a Xmega-a3bu xplained board with a TWI ATAVRSBIN1 module. After i had ran some test program from atmel i tried to make my own program to get data from the ATAVRSBIN1 with TWI. I have read the documents from both, but i can't get the that from the module on the display of my xplained board. Could someone help me a hand with it.
Thanks in advance.

#include 
#include 
//#define Baud_rate 100000
#define TWI_BAUD(F_SYS, F_TWI) ((F_SYS / (2 * F_TWI)) - 5)

long int Data;
long int *p = &Data;


ISR(TWIC_TWIM_vect){
	volatile static uint8_t i;
	if(i==1){
		*p = TWIC_MASTER_DATA;
		TWIC_MASTER_CTRLC = 0X03;
		i=0;}
	else if(i==0){
		*p = 10;
		TWIC_MASTER_CTRLC = 0X03;
		i=1;
	}
}

int main(void){
	
	sensor_platform_init();
	gfx_mono_init();
	gpio_set_pin_high(NHD_C12832A1Z_BACKLIGHT);
	st7565r_set_contrast(ST7565R_DISPLAY_CONTRAST_MIN);

	gfx_mono_draw_string("test program",0, 0, &sysfont);
	delay_ms(2000);
	
/*TWI init*/
	/*Set Baud rate op 100KHZ*/
//	TWI_BAUD(sysclk_get_cpu_hz(),Baud_rate);
	TWIC_MASTER_BAUD |= 0x90;
	/*Sets Enable TWI master mode, Read interrupts mode en Medium interrupt lvl*/
	TWIC_MASTER_CTRLA|= 0x1D;
	/*Dwingt Bus in idle mode */
	TWIC_MASTER_STATUS|=0x01;
	/*Verzend Data request naar adres 0C met Read bit, omdat master in read mode staat*/
	static char format_buffer[1][16];
	while(1){
	TWIC_MASTER_ADDR = 0x38;
		LED_Toggle(LED3);
	delay_ms(500);
		LED_Toggle(LED1);
	delay_ms(500);
		LED_Toggle(LED0);
	delay_ms(500);
	cli();
	gfx_mono_draw_string("test program    ",0, 0, &sysfont);
	sprintf(format_buffer[0], "% ld", *p);
	gfx_mono_draw_string(format_buffer[0], 0, 10, &sysfont);
	sei();
	delay_ms(500);
	
	TWIC_MASTER_ADDR = 0x68;
	delay_ms(500);
	cli();
	gfx_mono_draw_string("test program 2",0, 0, &sysfont);
	sprintf(format_buffer[0], "% ld", *p);
	gfx_mono_draw_string(format_buffer[0], 0, 10, &sysfont);
	sei();
	}
}

I have included the ASF.h file for use of the display on the xplained board.

Edit:
i have programmed the AVR mega series of atmel, but i wanted to try the xmega serie, because it has a lot more power then the mega serie.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have changed a lot of the code from th previous post.
And 1 major thing was that the adress of the module wasn't right. After some searching in the example code the there was also another adress and that seems to work. Now that i get data i encountered another problem.
I don't always get data from the module or wrong values. Somtimes i get a couple of data bytes and after that a while wrong data or no data at all.
I can't find the reason for this problem. It there anyone that can lookin to it??

#include 
#include 
#define WIF (TWIC_MASTER_STATUS & 0x40)
#define RIF (TWIC_MASTER_STATUS & 0x80) 
#define ARBLOST (TWIC_MASTER_STATUS & 0x08)

#define Baud_rate 100000
#define TWI_BAUD(F_SYS, F_TWI) ((F_SYS / (2 * F_TWI)) - 5)
long int Data[1];

ISR(TWIC_TWIM_vect){
	volatile static uint8_t i=0;
	if(ARBLOST){TWIC_MASTER_STATUS= TWI_MASTER_BUSSTATE_IDLE_gc; TWIC_MASTER_STATUS ^= TWI_MASTER_ARBLOST_bp; return;}
	if(WIF!=0){
		TWIC_MASTER_ADDR = 0x71;
	}
	else if(TWIC_MASTER_DATA != 0xFF && TWIC_MASTER_DATA != 0x00){
		switch(i){
		case 0:
		Data[0]=TWIC_MASTER_DATA;
		TWIC_MASTER_CTRLC |= 0x02<<0;
		i=1;
		gfx_mono_draw_string("data binnen",50, 10, &sysfont);
		break;
		case 1:
		Data[1]=TWIC_MASTER_DATA;
		TWIC_MASTER_CTRLC |= 0x04;
		TWIC_MASTER_CTRLC |= 0x03<<0;
		i=0;
		gfx_mono_draw_string("data binnen",50, 20, &sysfont);
		break;
		}
	}
	else{
	TWIC_MASTER_STATUS^=TWI_MASTER_RIF_bm;
		return;}
}

int main(void){
	
	sensor_platform_init();
	gfx_mono_init();
	gpio_set_pin_high(NHD_C12832A1Z_BACKLIGHT);
	st7565r_set_contrast(ST7565R_DISPLAY_CONTRAST_MIN);

	gfx_mono_draw_string("test program",0, 0, &sysfont);
	delay_ms(2000);
	LED_Off(LED2);
	LED_Off(LED1);
	LED_Off(LED0);
	
/*TWI init*/
/*Set Baud rate op 100KHZ*/	
	TWIC_MASTER_BAUD |=TWI_BAUD(sysclk_get_cpu_hz(), Baud_rate);
/*Sets Enable TWI master mode, Read interrupts mode en Medium interrupt lvl*/
	TWIC_MASTER_CTRLA |= (TWI_MASTER_ENABLE_bm | TWI_MASTER_RIEN_bm | TWI_MASTER_INTLVL_MED_gc | !(TWI_MASTER_WIF_bm));
/*Dwingt Bus in idle mode */
	TWIC_MASTER_STATUS |= TWI_MASTER_BUSSTATE_IDLE_gc;
	
/*Verzend Data request naar adres met Read bit, omdat master in read mode staat*/

	static char format_buffer[1][16];
	while(1){
		TWIC_MASTER_ADDR = 0x71;
		LED_Toggle(LED1);
		delay_ms(100);
		LED_Toggle(LED0);
		delay_ms(100);
		LED_Toggle(LED2);
	gfx_mono_draw_string("test program    ",0, 0, &sysfont);
	cli();
	sprintf(format_buffer[0], "% 4ld", Data[0]);
	sprintf(format_buffer[1], "% 4ld", Data[1]);
	gfx_mono_draw_string(format_buffer[0], 0, 10, &sysfont);
	gfx_mono_draw_string(format_buffer[1], 0, 20, &sysfont);
	delay_ms(200);
	sei();
	}
}