Atmega8 MCU,QMC5883 Magnetometer,AT24C512 eeprom,ADM3485 Rs485 hangs

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

Hey all,

I have a Board such as an Atmega8 MCU,QMC5883 Magnetometer,AT24C512 eeprom,ADM3485 Rs485 IC.

reading from QMC is doing every 10ms by Interrupt pin.

When I send a data to board by RS485 and want to write it on eeprom ,MCU will be hanged and repeat START command of the I2C with QMC5883.

after it MCU jump to start of the main function.

Can e.b. help me to solve this function (jumping to start of the main and repeat START command)

tnx.

 

* Thread title changed to help everyone. Moderator. *

Last Edited: Sat. Nov 21, 2020 - 08:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm sure people will be happy to help once you provide more details. Post the schematics, photos of the PCB, details of your external connections, and your source code. Plus details of the tests that you have done.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

Show some code.    Get ONE device working at a time---don't try to solve all problems at the same time.  PLan ahead.

 

Writing to EEPROM is very slow...if you are about to write to EEPROM, stop other activities first (including interrupts), you will save a big headache.

 

When I send a data to board by RS485 and want to write it on eeprom --what kind of data?  The EEPROM is meant for only occasional writes, not a steady stream

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

thank you from your attention.

My code is following:

when I change uint8_t tx_array_global[24],uint8_t eeprom_buffer[24];   in 632 and 633th line to uint8_t tx_array_global1[24],uint8_t eeprom_buffer1[24]; the problem will be clear.

Why?!!!

Is it important to change name of the variables?

tnx.

//
//
//-----------------------------#Defines-----------------------------------------
/** \def F_CPU
    \brief CPU frequency in Hz

    The macro F_CPU specifies the CPU frequency to be considered by
    the delay macros.  
 */
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#define  EEPROM_TWI_BUS_ADDRESS 0b10100100
/** example sbi(PORTB, PB1); */
#define sbi(x, y) x |= _BV(y)                  // set bit
#define cbi(x, y) x &= ~(_BV(y))               // clear bit
#define tbi(x, y) x ^= _BV(y)                  // toggle bit
#define is_high(x, y) ((x & _BV(y)) == _BV(y)) //(for input) checks if the input is high (Logic 1)
/** defines the data direction (reading from I2C device) in i2c_start(),i2c_rep_start() */
#define I2C_READ    1
/** defines the data direction (writing to I2C device) in i2c_start(),i2c_rep_start() */
#define I2C_WRITE   0
/** I2C clock in Hz */
#define SCL_CLOCK  400000L
#define Header 0xA5
#define Footer 0x5A
#define MAGNETOMETER_TWI_BUS_ADDRESS 0x1A
#define TX_Mode	 sbi(PORTC,PC0)     //Tx_Enable
#define RX_Mode  cbi(PORTC,PC0)	   //Rx_Mode
#define global_address 0

//------------------------------------------------------------------------------------------
//
//--------------------------------------#include_Libraries----------------------------------
#include <avr/eeprom.h>
#include <avr/wdt.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <util/twi.h>
#include <util/crc16.h>
//----------------------------------------------------------------------------------------------------------
//
//-----------------------------#Commands-----------------------------------------
/** \def Commands
    \brief Commands
	
    Commands for Received serial data  
 */
#define Address_assigment_command				     0xAA
#define hex_file_is_sending_command					 0x01
#define hex_file_is_reading_command					 0x02
#define Start_BootLoader_Programming				 0x03
#define read_programed_log							 0x04
#define check_sum_status_request_command			 0x05
#define check_sum_status_response_command			 0x06
#define clock_calibration_value_read				 0x07
#define clock_calibration_value_write				 0x08
//-------------------------------------------------------------------------------
//
//-----------------------------#Types-----------------------------------------
/** \def types
    \brief types
	
    types for send serial data  
 */
#define hex_file_is_reading_type			0xD1
#define programmed_log_is_reading_type		0xD2
#define check_sum_status_response_type		0xD3
#define clock_calibration_value_read_type   0xD4
//
//
//
//
//
//--------------------------------------#Global_Variable----------------------------------
uint8_t i=0,serial_number[8],data_buffer_a[31],data_buffer_b[31],data_buffer_c[31];    //buffer_length + 1 byte empty indicator
uint16_t second_time=0,day_time=0;
volatile uint8_t data_buffer_rx[30],flag_data_received=0,command;
//----------------------------------------------------------------------------------
//
//
//
//-------------------------------------#Functions_Prototype---------------------------------------------------------------------
void hex_memory_calculation(uint8_t *input_buffer);
void eeprom_write_verify_function(uint16_t address,uint8_t *buffer,uint8_t leng);
//----------------------------------------------------------------------------------------------------------
//
//
//
//---------------------------------------#i2c_init-------------------------------------------
/**
 * \brief Initialization of the I2C bus interface. Need to be called only once
 * 
 * \param void
 * 
 * \return void
 */
void i2c_init(void){
	
  /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */

  TWSR = 0;                         /* prescaler=1 */
  TWBR = ((F_CPU/SCL_CLOCK)-16)/2;  /* must be > 10 for stable operation */

}
//----------------------------------------------------------------------------------
//
//
//
//--------------------------------------#i2c_stop-------------------------------------
/**
 * \brief Terminates the data transfer and releases the I2C bus
 * 
 * \param void
 * 
 * \return void
 */
void i2c_stop(void){
	
    /* send stop condition */
	TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
	
	// wait until stop condition is executed and bus released
	while(TWCR & (1<<TWSTO));

}
//-----------------------------------------------------------------------------------
//
//
//
//--------------------------------------#i2c_start-------------------------------------
/**
 * \brief Issues a start condition and sends address and transfer direction.
 * 
 * \param uint8_t address
 * 
 * \return uint8_t  return 0 = device accessible, 1= failed to access device
 */
uint8_t i2c_start(uint8_t address){
uint8_t   twst;

	// send START condition
	TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
	
	// wait until transmission completed
	while(!(TWCR & (1<<TWINT)));

	// check value of TWI Status Register. Mask prescaler bits.
	twst = TW_STATUS & 0xF8;
	if ( (twst != TW_START) && (twst != TW_REP_START)) {i2c_stop();return 1;}   //agar yek shavad yani start NOK shode!
	

	// send device address
	TWDR = address;
	TWCR = (1<<TWINT) | (1<<TWEN);

	// wail until transmission completed and ACK/NACK has been received
	while(!(TWCR & (1<<TWINT)));

	// check value of TWI Status Register. Mask prescaler bits.
	twst = TW_STATUS & 0xF8;
	
	if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) {i2c_stop();return 1;}   //yani ok nabud! 

	return 0;

}
//-----------------------------------------------------------------------
// 
//
//
//----------------------------------------#i2c_write-------------------------------------
/**
 * \brief Send one byte to I2C device
 * 
 * \param  uint8_t data  byte to be transfered
 * 
 * \return uint8_t  0 write successful,1 write failed         
 */
uint8_t i2c_write( uint8_t data ){	
	
    uint8_t   twst;
    
	// send data to the previously addressed device
	TWDR = data;
	TWCR = (1<<TWINT) | (1<<TWEN);

	// wait until transmission completed
	while(!(TWCR & (1<<TWINT)));

	// check value of TWI Status Register. Mask prescaler bits
	twst = TW_STATUS & 0xF8;
	if( twst != TW_MT_DATA_ACK) {i2c_stop();return 1;}   //ok_nabuuud
	return 0;

}
//------------------------------------------------------------------
//
//
//
//----------------------------------------#i2c_readAck-------------------------------------
/**
 * \brief Read one byte from the I2C device, request more data from device
 * 
 * \param  void
 * 
 * \return uint8_t  byte read from I2C device 
 */
uint8_t i2c_readAck(void){
	
	TWCR = ((1<<TWINT) | (1<<TWEN) | (1<<TWEA));

	while(!(TWCR & (1<<TWINT)));    

	return TWDR;

}
//----------------------------------------------------------------------------
//
//
//
//----------------------------------------#i2c_readNAck-------------------------------------
/**
 * \brief Read one byte from the I2C device, read is followed by a stop condition 
 * 
 * \param  void
 * 
 * \return uint8_t  byte read from I2C device 
 */
uint8_t i2c_readNack(void){
	
	TWCR = (1<<TWINT) | (1<<TWEN);
	while(!(TWCR & (1<<TWINT)));
	
	return TWDR;

}
//-----------------------------------------------------------------------
//
//
//---------------------------------------#javati_i2c_read--------------------------------------------------// ---- Read bytessss----
/**
 * \brief Read bytes and save them to data array 
 * 
 * \param  uint8_t twi_bus_address,uint8_t first_word_address,uint8_t second_word_address,uint8_t *data,uint8_t byte_count
 * 
 * \return void
 */
uint8_t javati_i2c_read(uint8_t twi_bus_address,uint8_t first_word_address,uint8_t second_word_address,uint8_t *data,uint8_t byte_count,uint8_t word_address){
	
	if(i2c_start(twi_bus_address+I2C_WRITE)) return 1;   //return 1 means error found!
	if(i2c_write(first_word_address)) return 1;
	if(word_address=='w'){if(i2c_write(second_word_address)) return 1;}    //'w' means word address
	if(i2c_start(twi_bus_address+I2C_READ)) return 1;
	
	for(i=0;i<byte_count-1;i++){                                     //read byte_count byte
		data[i]=i2c_readAck();
	}
	
	data[byte_count-1]=i2c_readNack();											//read last byte
	
	i2c_stop();
	return 0;
}
//------------------------------------------------------------------------------------------------------------------
//
//
//
//
//
//
//---------------------------------------#javati_i2c_write------------------------------------------------------------------------
/**
 * \brief write bytes to specificate array
 * 
 * \param  (uint8_t twi_bus_address,uint8_t first_word_address,uint8_t second_word_address,uint8_t *data,uint8_t byte_count
 * 
 * \return void
 */
uint8_t javati_i2c_write(uint8_t twi_bus_address,uint8_t first_word_address,uint8_t second_word_address,uint8_t *data ,uint8_t byte_count,uint8_t word_address){

	
	if(i2c_start(twi_bus_address+I2C_WRITE)) return 1;   //return 1 means error found!
	if(i2c_write(first_word_address)) return 1;
	if(word_address=='w'){if(i2c_write(second_word_address)) return 1;}
		
	for(i=0;i<byte_count-1;i++){                                        //write byte_count byte
                                    
		if(i2c_write(data[i])) return 1;
	}
	
	if(i2c_write(data[byte_count-1])) return 1;							//write last byte
	
	i2c_stop();
	return 0;
	
}
//---------------------------------------------------------------------------------------------------------------------------
//
//
//
//------------------------------------- #Timer1_interrupt-------------------------------------------
volatile uint8_t task_flag[10];
//uint16_t cnt_busy_line,random_cnt;
/**
 * \brief Timer1 overflow interrupt service routine
 * 
 */
ISR(TIMER1_OVF_vect){
static uint8_t cnt_task=0;

	TCNT1=-1000;
		
	
	switch (++cnt_task){
		
		case  0:task_flag[0]=1;       break;    //HMC_task
		case 10:task_flag[0]=1;       break;	//HMC_task
		case 20:task_flag[0]=1;       break;	//HMC_task
		case 30:task_flag[0]=1;       break;	//HMC_task
		case 40:task_flag[0]=1;       break;	//HMC_task
		case 50:task_flag[0]=1;       break;	//HMC_task
		case  4:task_flag[1]=1;       break;	//Intelligence_algorithm_task
		case 24:task_flag[2]=1;       break;	//eeprom_management_task
		case 14:task_flag[3]=1;       break;	//receive_frame_usart_task
		case 34:task_flag[3]=1;       break;	//receive_frame_usart_task
		case 54:task_flag[3]=1;       break;	//receive_frame_usart_task
		case 17:task_flag[4]=1;       break;	//transmit_frame_usart_task
		case 37:task_flag[4]=1;       break;	//transmit_frame_usart_task
		case 57:task_flag[4]=1;  cnt_task=-1;     break;	//transmit_frame_usart_task
		case 44:task_flag[5]=1;       break;	//r_and_m_task
		case 46:task_flag[6]=1;       break;	//timing_and_scheduling_task
		case 47:task_flag[7]=1;       break;	//program_task
		case 48:task_flag[9]=1;       break;//auxiliary_data_task
		case 49:task_flag[8]=1;       break;	//auxiliary_processing_task
		default:break;
	}
	
//	if(++cnt_busy_line<5000) {rx_line_free=0;} else {rx_line_free=1;}
//	++random_cnt;       //for build random number (offer=use crc value of ds2401 and this number to build random numbers)
}
//-------------------------------------------------------------------------------------------------------------
//
//
//
//------------------------------------- #HMC5883L_interrupt-------------------------------------------
/**
 * \brief External Interrupt=HMC5883L Interrupt
 * 
 */
volatile uint8_t flag_magnetometer_interrupt=0;
ISR(INT1_vect){

	flag_magnetometer_interrupt=1;	

}
//----------------------------------------------------------------------------------------------------------
//
//
//
//
//-----------------------------------------------#array_copy_verify-------------------------------------------------------
/**
 * \brief one buffer to another copy or verify or write 0xff to array
 * 
 * \param destinate array,source array,length,copy or verify command
 * 
 * \return void
 */
uint8_t array_copy_verify (uint8_t *destinate_array,volatile uint8_t *source_array, uint8_t array_length,uint8_t copy_or_free_or_verify){
	
	
	
	if(copy_or_free_or_verify=='c'){
		for(i=0;i<array_length;i++){
			destinate_array[i]=source_array[i];
			
		}
	}else if (copy_or_free_or_verify=='f') {
		
		for(i=0;i<array_length;i++){
			source_array[i]=0xff;
		}
				
	}else if (copy_or_free_or_verify=='v') {
		
		for(i=0;i<array_length;i++){
			if(destinate_array[i]!=source_array[i]) return 1;
		}
	}
	
	return 0;
}
//----------------------------------------------------------------------------------------------------------------
//
//
//
//
//-----------------------------------------------#crc_check-------------------------------------------------------
/**
 * \brief Check CRC of the a array
 * 
 * \param array
 * 
 * \return crc vlaue
	It is important that CRC is Check sume here.
		
 */
uint8_t crc_check(uint8_t *input_array,uint8_t input_array_length){
uint8_t crc_check_value=0;
	for(i=0;i<input_array_length;i++){
		crc_check_value+=input_array[i];
	//	udr(input_array[i]);
	}
	//  udr(crc_check_value);

	return crc_check_value;
}
//----------------------------------------------------------------------------------------------------------
//
//
//---------------------------------------------- #RX_interrupt-----------------------------------------------
/**
 * \brief RX Interrupt
 * 
 */
ISR( USART_RXC_vect ){
uint8_t udr_content;
static uint8_t k1,k2,flag_header_address=0,step=0,crc=0,rx_data_len=0;
	udr_content=UDR;	
	k1=k2;k2=udr_content;

	if(k1==Header && (k2==serial_number[0] | k2==global_address) && !flag_header_address){flag_header_address=1;}
	
	switch (step){
		
		case 1:  crc=udr_content;break;
				
		case 2:  rx_data_len=udr_content;break;
		
		case 3:  command=udr_content;break;
		
	}
			
	if( 3<step && step<rx_data_len+4) {data_buffer_rx[step-4]=udr_content;}
	if(step>=rx_data_len+4) {if(udr_content==Footer && crc==crc_check(data_buffer_rx,rx_data_len)) {flag_data_received=1;crc=0;} flag_header_address=0;step=0;}
	if(flag_header_address) step++;
	
}
//------------------------------------------------------------------------------------------------------------
//
//
//
//
//------------------------------------------------- #TX_interrupt-----------------------------------------------------
/**
 * \brief TX Interrupt
 * 
 */
uint8_t tx_pipe_0_index=0,tx_pipe_1_index=0,tx_pipe_2_index=0,tx_frame[37][3],tx_line_free=1,tx_pipe_0_andex,tx_pipe_1_andex,tx_pipe_2_andex;
ISR (USART_TXC_vect){
	
	//udr('q');for(i=0;i<30;i++) udr(tx_frame[i][pipe]);_delay_ms(10);
	
	sbi(PORTC,PC0);                 //TX Enable
	tx_line_free=0;
			 if(tx_pipe_0_index>0) {UDR=tx_frame[tx_pipe_0_index--][0];if(tx_pipe_0_index==0)tx_frame[0][0]=0;}
		else if(tx_pipe_1_index>0) {UDR=tx_frame[tx_pipe_1_index--][1];if(tx_pipe_1_index==0)tx_frame[0][1]=0;}
		else if(tx_pipe_2_index>0) {UDR=tx_frame[tx_pipe_2_index--][2];if(tx_pipe_1_index==0)tx_frame[0][2]=0;}
		else {tx_line_free=1;}
	cbi(PORTC,PC0);                 //TX disable
	
}
//---------------------------------------------------------------------------------------------------------------------
//
///
//
//-----------------------------------------------#HMC_task-------------------------------------------------------
/**
 * \brief Magnetometer task
 * 
 * \param void
 * 
 * \return void
	Duty:1-> Read and convert and save Magnetometer data
		 2-> Analysis input data include temperature (averages)
 */
uint16_t magnetometer_data_x_y_z;
#define n_tond 5
#define n_kond 100
void task0_HMC(void){
uint8_t magnetometer_data[9];
uint16_t x,y,z,temperature;
static uint8_t tond_data_index,kond_data_index;
static uint16_t x_array_kond[n_kond],x_array_tond[n_tond],y_array_kond[n_kond],y_array_tond[n_tond],z_array_kond[n_kond],z_array_tond[n_tond],
avg_x_tond,avg_y_tond,avg_z_tond,avg_x_kond,avg_y_kond,avg_z_kond;

	if(flag_magnetometer_interrupt){
		
		flag_magnetometer_interrupt=0;
	
		javati_i2c_read(MAGNETOMETER_TWI_BUS_ADDRESS,0,0,magnetometer_data,9,'b');
		
		x=magnetometer_data[1];
		y=magnetometer_data[3];
		z=magnetometer_data[5];
		temperature=magnetometer_data[8];
		
		
		    
		x=((x<<8)+magnetometer_data[0])+32768;
		y=((y<<8)+magnetometer_data[2])+32768;
		z=((z<<8)+magnetometer_data[4])+32768;
		temperature=(temperature<<8)+magnetometer_data[7]+32768;
		
		
		if(++tond_data_index>n_tond) tond_data_index=0;
		avg_x_tond=avg_builder(n_tond,x_array_tond,tond_data_index,x);
		avg_y_tond=avg_builder(n_tond,y_array_tond,tond_data_index,y);
		avg_z_tond=avg_builder(n_tond,z_array_tond,tond_data_index,z);
		
		if(++kond_data_index>n_tond) kond_data_index=0;
		avg_x_kond=avg_builder(n_kond,x_array_kond,kond_data_index,x);
		avg_y_kond=avg_builder(n_kond,y_array_kond,kond_data_index,y);
		avg_z_kond=avg_builder(n_kond,z_array_kond,kond_data_index,z);
		
		magnetometer_data_x_y_z=abs(avg_x_kond-avg_x_tond)+abs(avg_y_kond-avg_y_tond)+abs(avg_z_kond-avg_z_tond);
	
	

		
		    	
	}

}
//----------------------------------------------------------------------------------------------------------

//
//-----------------------------------------------#eeprom_read_function-------------------------------------------------------
/**
 * \brief eeprom_read_function
 * 
 * \param void
 * 
 * \return void	
 */
uint8_t eeprom_verify_error=0;
void eeprom_read_function(uint16_t address,uint8_t *buffer,uint8_t leng){
	
	javati_i2c_read(EEPROM_TWI_BUS_ADDRESS,address>>8,address,buffer,leng,'w');
	buffer[0] |=0b01000000;       

}
//----------------------------------------------------------------------------------------------------------
//
//
//-----------------------------------------------#eeprom_write_function-------------------------------------------------------
/**
 * \brief eeprom_write_function
 * 
 * \param void
 * 
 * \return void	
 */
void eeprom_write_function(uint16_t address,uint8_t *buffer,uint8_t leng){
	
	javati_i2c_write(EEPROM_TWI_BUS_ADDRESS,address>>8,address,buffer+1,leng,'w');
	
	buffer[0] |=0b01000000;       

}
//----------------------------------------------------------------------------------------------------------
//
//
#define HMC_start_address 0
#define Log_start_address 44800
#define hex_start_address 52480
#define auxiliary_data_start_address 64000
//
//
//----------------------------------#check_sum----------------------------------------
/**
 * \brief a function to check check_sum of tha data
 * 
 * \param  void
 * 
 * \return void
 */
uint8_t pack_number(uint16_t pack_num){
	uint8_t sum=0,eeprom_data[21],msb_pack_addr,lsb_pack_addr;

	msb_pack_addr=(hex_start_address+(pack_num*21+(pack_num/6)*2))>>8;
	lsb_pack_addr=hex_start_address+(pack_num*21+(pack_num/6)*2)  &  0x00FF;
	javati_i2c_read(EEPROM_TWI_BUS_ADDRESS,msb_pack_addr,lsb_pack_addr,eeprom_data,21,'w');           //read a pack from eeprom
	
	for(i=0;i<eeprom_data[0]+5;i++){
		
		sum+=eeprom_data[i];
	}
	
	if(sum!=0) return 1;
	else if(eeprom_data[3]==1) return 2;   //1=Fail , 2=finish and Pass , 3=continue
	return 3;
}
//---------------------------------------------------------------------------------
//
//	
//
//
//-----------------------------------------------#eeprom_management_task-------------------------------------------------------
/**
 * \brief eeprom_management_task
 * 
 * \param void
 * 
 * \return void
 
	Duty:1-> Supervision Of request Flags
		 2-> Erase Data
		 3-> Read Data
		 4-> Write Data
	one call will execute other call will be verify....
		
 */
#define HMC_eeprom_data_length 21
#define Log_eeprom_data_length 21
#define hex_eeprom_data_length 21
#define a_data_eeprom_data_length 21

uint16_t eeprom_address_curser [4]={HMC_start_address,Log_start_address,hex_start_address,auxiliary_data_start_address};
uint8_t tx_array_global[24];
uint8_t eeprom_buffer[24];
//-------------------------------------------#initial_setting--------------------------------------------------
/**
 * \brief initial_setting and clock calibration
 * 
 * \param  void
 * 
 * \return void
	such as Port directions and other settings
	clock calibration,...
	after settings the Global Interrupts will be enable in this function 
 */
uint8_t initial_setting(void){
uint8_t temp_array[8];

	i2c_init();

	//Ports setting
	DDRC |=(1<<DDRC0) |(1<< DDRC1);
	DDRD |=(1 << DDRD4) ;

	PORTD|=(1<<PORTD3);
	PORTD|=(1<<PORTD4);

	// Timer/Counter 1 initialization
	// Clock source: System Clock
	// Clock value: 1000/000 kHz
	TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (1<<CS11) | (0<<CS10);
	TCNT1H=0x00;
	TCNT1L=0x10;
	
	// Timer(s)/Counter(s) Interrupt(s) initialization
	TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (1<<TOIE1) | (0<<TOIE0);

	// External Interrupt(s) initialization
	// INT0: Off
	// INT1: On
	// INT1 Mode: Rising Edge
	GICR|=(1<<INT1) | (0<<INT0);
	MCUCR=(1<<ISC11) | (1<<ISC10) | (0<<ISC01) | (0<<ISC00);
	GIFR=(1<<INTF1) | (0<<INTF0);
		
	// USART initialization
	// Communication Parameters: 8 Data, 1 Stop, No Parity
	// USART Receiver: On
	// USART Transmitter: On
	// USART Mode: Asynchronous
	// USART Baud Rate: 9600

	UCSRB=(1<<RXCIE) | (1<<TXCIE) | (0<<UDRIE) | (1<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
	UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
	UBRRH=0x00;
	UBRRL=0x33;
	
	// Analog Comparator initialization
	// Analog Comparator: Off
	// The Analog Comparator's positive input is
	// connected to the AIN0 pin
	// The Analog Comparator's negative input is
	// connected to the AIN1 pin
	ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0);
	
	
	sbi(PORTD,PD4);                 //Vcc to hmc5883 and SCL and SDA pull-up lines
	sbi(PORTC,PC1);                 //Vcc to eeprom
	clock_calibration(0xBF);
	_delay_ms(1000);
	
	sei();   //Enable Global Interrupt
	
	data_buffer_a[0]=1;        //set receive buffer free
	data_buffer_b[0]=1;
	data_buffer_c[0]=1;
	tx_frame[0][0]=0;
	tx_frame[0][1]=0;
	tx_frame[0][2]=0;
	tx_array_global1[0]=0;

	

		
	javati_i2c_read(EEPROM_TWI_BUS_ADDRESS,0,0,serial_number,8,'w');

	temp_array[0]=0xCD;														//HMC Initial Setting...
	temp_array[1]=0x00;														//HMC Initial Setting...
	temp_array[2]=0x01;														//HMC Initial Setting...
			
	javati_i2c_write(MAGNETOMETER_TWI_BUS_ADDRESS,9,0,temp_array,3,'b');    //HMC Initial Setting...
		
	return 1;

}
//-----------------------------------------------------------------------------------------------------------
//
//
//
//
//--------------------------------------------------------#main-----------------------------------------------
/**
 * \brief main function
 * 
 * \param  void
 * 
 * \return int
 */
uint8_t i;
int main(void){
	
	while(initial_setting());

	while (1){
		
		if(task_flag[0]){task_flag[0]=0;task0_HMC();}	//HMC_task 		if(task_flag[2]){task_flag[2]=0;task2_eeprom_management();}      			//eeprom_management_task
 		if(task_flag[2]){task_flag[2]=0;task2_eeprom_management();}					//eeprom_management_task
		if(task_flag[3]){task_flag[3]=0;task3_receive_frame_usart_task();}			//receive_frame_usart_task
		
	}



 

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

Have you written a simple program that ONLY writes/reads to/from the EEPROM?  Get that working first before adding 100 different complications.   

 

This test program should be no more than maybe 50 lines of text.  You might even find you wiring is not correct.

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

hasanzadeh99 wrote:

when I change uint8_t tx_array_global[24],uint8_t eeprom_buffer[24];   in 632 and 633th line to uint8_t tx_array_global1[24],uint8_t eeprom_buffer1[24]; the problem will be clear.

 

Can we just be clear...

 

1) You define the arrays as "uint8_t tx_array_global[24]" and "uint8_t eeprom_buffer[24]"

2) You compile the code with no errors and no warnings.

3) You upload the code to the AVR

4) You test the code

5) It does not work

6) You change the definitions of the arrays as "uint8_t tx_array_global![24]" and "uint8_t eeprom_buffer![24]"

7) You make no others changes AT ALL

8) You compile the code with no errors and no warnings.

9) You upload the code to the AVR

10) You test the code

11) It works

 

Please confirm that is EXACTLY what happens

 

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

The code shown in #4 defines:

uint16_t eeprom_address_curser [4]={HMC_start_address,Log_start_address,hex_start_address,auxiliary_data_start_address};
uint8_t tx_array_global[24];
uint8_t eeprom_buffer[24];
//-------------------------------------------#initial_setting--------------------------------------------------

neither of those is referenced anywhere in that code.

 

But there is one (and only one) reference to tx_array_global1:

	sei();   //Enable Global Interrupt
	
	data_buffer_a[0]=1;        //set receive buffer free
	data_buffer_b[0]=1;
	data_buffer_c[0]=1;
	tx_frame[0][0]=0;
	tx_frame[0][1]=0;
	tx_frame[0][2]=0;
	tx_array_global1[0]=0;

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes I confirm EXACTLY

by use  "uint8_t tx_array_global[24]" and "uint8_t eeprom_buffer[24]"  the micro after receive some byte from usart will be restarted.

but by changing the variables it will be solved.

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

Yes,I delete some other function

but by this code micro will be hanged too!.