SHARE
TWEET

Untitled

a guest Jan 18th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void main(void)
  2. {
  3.     //...
  4.     for(int i = 0; i< 2; i++)
  5.     {
  6.         libmpu_init(&device, &device_configuration);
  7.     }
  8.     //...
  9. }
  10.  
  11. void libmpu_init(libmpu_t *p_dev, libmpu_cfg_t *p_dev_cfg)
  12. {
  13.     assert(p_dev != NULL);
  14.     assert(p_dev_cfg != NULL);
  15.  
  16.     unsigned char l_wake_up_device[2] = {LIBMPU_PWR_REG, LIBMPU_WAKEUP};
  17.  
  18.     p_dev->twi_cfg.clk_freq = TWI_CLK_FREQ_111KHz;
  19.     p_dev->twi_cfg.bus = TWI_BUS_0;
  20.  
  21.     /* Initialize twi. */
  22.     twi_init(&p_dev->twi, &p_dev->twi_cfg);
  23.  
  24.     /* Wake up device. */
  25.     twi_start_communication(&p_dev->twi);
  26.     twi_write(&p_dev->twi, l_wake_up_device, sizeof l_wake_up_device);
  27.     twi_stop_communication(&p_dev->twi);
  28.  
  29.     _delay_ms(3000); // 3 seconds delay!
  30. }
  31.  
  32. void twi_init(twi_t *p_this, twi_cfg_t *p_cfg)
  33. {
  34.     assert(p_this !=NULL);
  35.     assert(p_cfg != NULL);
  36.  
  37.    /* Set pin as output. */                                                                                            
  38.     DDRB = 0xFF;                                                                                                        
  39.  
  40.     /* Set pin in tristate. */                                                                                          
  41.     PORTB = 0x0;    
  42.  
  43.     /* Bitrate */
  44.     TWBR = 0x48;
  45.     TWSR = 0;
  46. }
  47.  
  48. void twi_start_communication(twi_t *p_this)
  49. {
  50.     assert(p_this != NULL);
  51.  
  52.     /* Generate start condition. */
  53.     TWCR = (1<<(TWINT))|(1<<TWSTA )|(1<<TWEN) | (0<<TWSTO);
  54.     while (!(TWCR &(1<<TWINT)));
  55.  
  56.     /* Initiate write.
  57.      * http://www.ti.com/lit/an/slva704/slva704.pdf
  58.      */
  59.     twi_send_slave_address(LIBTWI_WRITE_OPERATION);
  60. }
  61.  
  62. void twi_write(twi_t *p_this, char *p_buffer, int32_t p_buff_size)
  63. {
  64.     assert(p_this != NULL);
  65.  
  66.     for (int i = 0; i < p_buff_size; i++)
  67.     {
  68.         TWDR = p_buffer[i];
  69.         TWCR = (1<<TWINT) | (1<<TWEN);
  70.  
  71.         while (!(TWCR & (1<<TWINT)));
  72.     }
  73. }
  74.  
  75. void twi_stop_communication(twi_t *p_this)
  76. {
  77.     assert(p_this != NULL);
  78.  
  79.     /* Send stop condition. */
  80.     TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  81.     while (!(TWCR &(1<<TWINT)));
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top