Exemple #1
0
/*!\brief Continuous read (multiple register reading).
 * \param device_add The device address
 * \param read_buffer the buffer that will contain the values read from the registers
 * \param reg The register start address
 * \return 1 to confirme the oparation and 0 for an error
 */
char readRegMulti(char device_add, unsigned char *read_buffer, char start_address, unsigned char numBytes) {

    char error = 0;
    unsigned int i = 0;
    while (!error) {
        error = 1;
        error &= e_i2c_start();
        error &= e_i2c_write(device_add); // Device address
        error &= e_i2c_write(start_address | 0x80); // address of first register to be read
        error &= e_i2c_restart();
        error &= e_i2c_write(device_add + 1); // To change data direction ([bit 0]=1)

        for (i = 0; i < numBytes; i++) {
            error &= e_i2c_read(&read_buffer[i]); // read the next byte
            if (i == (numBytes - 1)) { // the last byte to be read, must send nack
                error &= e_i2c_nack();
            } else {
                error &= e_i2c_ack(); // not the last byte, send ack
            }
        }
        e_i2c_stop(); // End read cycle
        if (error)
            break;
        e_i2c_reset();
    }
    return error;
}
char e_i2cp_read_string(char device_add, char *read_buffer, char start_address, char string_length)

{
	char error=0;
	error=1;
	error&=e_i2c_start();
	error&=e_i2c_write(device_add);    		// Device address
	error&=e_i2c_write(start_address);		// address of first register to be read
	error&=e_i2c_restart();
	error&=e_i2c_write(device_add+1);    	// To change data direction ([bit 0]=1)

	while (string_length) {
		error&=e_i2c_read(read_buffer);		// read a single byte
		read_buffer++;
		string_length--;

		if(string_length == 0) {             /* If last char, generate NACK sequence */
			error&=e_i2c_nack();			// the last byte to be read, must send nack
		} else {                       /* For other chars,generate ACK sequence */
			error&=e_i2c_ack();
		}
		while(I2C1CONbits.ACKEN == 1);    /* Wait till ACK/NACK sequence is over */
	}
	e_i2c_stop();             				// End read cycle
	return error;
}
// Write 1 byte to register reg of devantec sensor
void e_i2cd_write (char device_add, char reg, char value)
{
	e_i2c_start();
	e_i2c_write(device_add);    // Writing the device (slave) address
	e_i2c_write(reg);     		// Device register address
	e_i2c_write(value);       // Data to device
	e_i2c_stop();             // Ending the communication
	e_i2c_reset();
}
void write_ext_eeprom(unsigned short address, char data)
{
 	char error=0;								// Initialize to no error

	while(!error)								// While they are still some errors...
	{
		error=1;								// We initilize the error to 1
    	//while(!ext_eeprom_ready());			// Proposed in 2432.c from PICC, not very useful here
		error&=e_i2c_start();					// We intiate a start and...
		error&=e_i2c_write(0x10);				// ... write the hardware selectable address A0 = 0, A1 = 1, A2 = 0 and we write (last bit = 0)
		error&=e_i2c_write(0x00);	// ... write the most significant byte of the memory address
		error&=e_i2c_write(0x05);				// ... and finally the value that we want to store
		error&=e_i2c_stop();					// ... the tranfer is then terminated by a stop
	}
//	delay_ms(8);
}
//read 1 byte from the register reg of devantec sensor
char e_i2cd_readb(char device_add, char reg)
{
	char value;

	e_i2c_start();				// start
	e_i2c_write(device_add);   	// Device address
	e_i2c_write(reg);     		// Register address

	e_i2c_restart();			// 2nd start
	e_i2c_write(device_add+1);    // Device address
 	e_i2c_read(&value);    		// read single byte
	e_i2c_nack();				// only 1 byte is being read, so send nack
	e_i2c_stop();             	// end read cycle
	e_i2c_reset();

   	return value;
}
/*! \brief Write a specific register on a device
 * \param device_add The address of the device you want information
 * \param reg The register address you want read on the device
 * \param value The data you want to write
 * \return 1 to confirme the oparation and 0 for an error
 */
char e_i2cp_write (char device_add, char reg, char value)
{
	char error=0;

	while(!error)
	{
		error=1;
		error&=e_i2c_start();
		error&=e_i2c_write(device_add);		// Writing the device (slave) address
		error&=e_i2c_write(reg);			// Device register address
		error&=e_i2c_write(value);			// Data to device
		error&=e_i2c_stop();				// Ending the communication	
		if(error)
			break;
		e_i2c_reset();
	}
	return error;
}
char read_ext_eeprom(unsigned short address)
{
	char error=0;								// Initialize to no error
	char data=0x00;								// Initialize the data

	while(!error) { 							// While they are still some errors...
		error=1;								// We initilize the error to 1
		//while(!ext_eeprom_ready());			// Proposed in 2432.c from PICC, not very useful here
		error&=e_i2c_start();					// We intiate a start and...
		error&=e_i2c_write(0x10);				// ... write the hardware selectable address A0 = 0, A1 = 1, A2 = 0 and we write (last bit = 0)
		error&=e_i2c_write(0x01);	// ... then the least significant byte of the memory address 
		error&=e_i2c_restart();					// ... In order to indicate to the slave that he can send the data, we iniate a restart
		error&=e_i2c_write(0x11);				// ... write the hardware selectable address A0 = 0, A1 = 1, A2 = 0 and we read (last bit = 1)
		error&=e_i2c_read(&data);				// ... and read the data sent by the slave (here the memory)
		error&=e_i2c_nack();					// ... only 1 byte is being read, so send nack
		error&=e_i2c_stop();					// ... the tranfer is then terminated by a stop
	}
	return(data);
}
char e_i2cp_write (char device_add,char reg, char value)
{
	char error=0;								// Initialize to no error

	while(!error) {								// While they are still some errors...
		error=1;								// We initilize the error to 1
		error&=e_i2c_start();					// We intiate a start and...
		error&=e_i2c_write(device_add);    		// ... write the I2C MODULE ADDRESS we want to write
		error&=e_i2c_write(reg);     	  		// ... and the REGISTER ADDRESS we want to write
		error&=e_i2c_write(value);       		// ... and send the value we want to write
		error&=e_i2c_stop();             		// ... the tranfer is then terminated by a stop

		if(error) {
			break;
		}
		e_i2c_reset();
	}
	return error;
}
char e_i2cp_read(char device_add,char reg)
{
	char error=0;								// Initialize to no error
	char value;									// Value that will be returned

	while(!error) {							// While they are still some errors...
		error=1;								// We initilize the error to 1
		error&=e_i2c_start();					// We intiate a start and...
		error&=e_i2c_write(device_add);    		// ... write the I2C MODULE ADDRESS we want to read
		error&=e_i2c_write(reg);     			// ... and the REGISTER ADDRESS we want to read
		error&=e_i2c_restart();					// ... In order to indicate to the slave that he can send the data, we iniate a restart
		error&=e_i2c_write(device_add+1);   	// ... and rewrite the MODULE ADDRESS with change of data direction ([bit 0]=1)
		error&=e_i2c_read(&value);    			// ... and read the data sent by the slave (the I2C Module)
		e_i2c_nack();							// ... only 1 byte is being read, so send nack
		e_i2c_stop();             				// ... the tranfer is then terminated by a stop
		if(error) {
			break;
		}
		e_i2c_reset();
	}
   	return value;
}
/*! \brief Read a specific register on a device
 * \param device_add The address of the device you want information
 * \param reg The register address you want read on the device
 * \return The readed value
 */
char e_i2cp_read(char device_add, char reg)
{
	char error=0;
	char value;
	while(!error)
	{
		error=1;
		error&=e_i2c_start();
		error&=e_i2c_write(device_add);    	// Device address
		error&=e_i2c_write(reg);     		// Register address

		error&=e_i2c_restart();
		error&=e_i2c_write(device_add+1);   // To change data direction ([bit 0]=1)
 		error&=e_i2c_read(&value);    		// read single byte
		e_i2c_nack();						// only 1 byte is being read, so send nack
		e_i2c_stop();             			// end read cycle
		if(error)
			break;
		e_i2c_reset();
	}
	
   	return value;
}
Exemple #11
0
int main(void) {

	// Locals General. -----------------------------------------------------
	char imu_board_id[9] = {0,0,0,0, 0,0,0,0, 0};
	char message[1024];	// Any message to send by UART1
	InitOscillator();			// Initialize the PLL (also disables wdt)
	__delay32(_50MILLISEC);

	// Init mcu ports ------------------------------------------------------

	init_port();    	// Initialize ports
	LED_ORNG =0;
	LED_RED = 1;

	// Init UARTS. ---------------------------------------------------------

	init_UART1();		// Initialize the serial communication (TTL / RS-232)
	init_UART2();
    broadcast_message("RC Testing program\n");

	// Init Analog Channels. -----------------------------------------------

	analog_initAnalog();	// Init the ADC module

	// Init SPI. ---------------------------------------------------------

	init_SPI();

	// Init I2C. ---------------------------------------------------------

	e_i2cp_init();
	e_i2cp_enable();
	__delay32(_50MILLISEC);
	e_i2c_write(0x00);	// dummy byte to get the START bit on the bus (believe it or not!)


	// Init RC. ----------------------------------------------------------
	initControlVariables(NULL);

    broadcast_message("Initialising RC\n");
	LED_ORNG =1;
	LED_RED = 1;

	BuzzerBip(1,1);		// Make x bips of the Buzzer (blocking)
	RCInitReception();
    // RCSetType(RC_WK2401);
    RCSetType(RC_WK2402);
    
    broadcast_message("Initialising IMU\n");
	// Init BUZZER. ----------------------------------------------------------
	__delay32(_200MILLISEC);  // Wait for the IMU to boot

	while (INT_IMU==0)		// Wait for the IMU to boot
	{
		FlashORNG ();						// Flash the LED	
	}
	read_imu_version(imu_board_id); imu_board_id[8] = 0;
	
    broadcast_message("Entering main loop\n");
	// Init BUZZER. ----------------------------------------------------------
	BuzzerBip(3,1);		// Make x bips of the Buzzer, blocking
	InitLoopTimer(control.params.T_ctrl_ms);// Initialize & Enable control loop timer (20ms; 50Hz)
	LED_RED = 0;
    while (1) {
        __delay32(5*_10MILLISEC);
        sprintf(message, "RC %+.2f %+.2f %+.2f %.2f %.2f %+.2f %+.2f %+.2f %s CTRL %02X %s %s\n", 
                RC_THROTTLE, RC_YAW, RC_ROLL, RC_PITCH,
                RC_THROTTLE_TRIM, RC_YAW_TRIM, RC_ROLL_TRIM, RC_PITCH_TRIM,
                string_of_rc_state(RCSMGetState()),
                control.flags.CONTROL_MODE,
                string_of_control_mode(control.flags.CONTROL_MODE),
                string_of_control_type(control.flags.CONTROL_MODE));
        broadcast_message(message);
    }

    return 0;
	
}	// End of main