Guest User

Untitled

a guest
Jun 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. void main()
  2. {
  3. Configure_GPIO_I2C2();
  4. Configure_I2C2_Master(0xA0,1);
  5.  
  6. I2C_WriteByte(5,'k');
  7. charr= I2C_ReadByte(5);//the program get stuck here because no byte is
  8. //received from eeprom
  9. UART_Send_String(1,&charr);
  10.  
  11. }
  12.  
  13. void I2C_WriteByte(uint8_t addr,uint8_t bytetowrite)
  14. {
  15. I2C2->ISR=0x01;
  16. I2C2_StartWrite(2);//start
  17.  
  18. I2C2->TXDR = addr;//write addr
  19. while(!(I2C2->ISR & I2C_ISR_TXE));
  20.  
  21. //I2C2_StartWrite(1);
  22.  
  23. I2C2->TXDR = bytetowrite;
  24. while(!(I2C2->ISR & I2C_ISR_TXE));
  25.  
  26. I2C2->CR2 |= I2C_CR2_STOP;//stop
  27. while(I2C2->CR2 & I2C_CR2_STOP);
  28.  
  29. }
  30.  
  31. uint8_t I2C_ReadByte(uint8_t byteToRead)
  32. {
  33. I2C2->ISR=0x01;
  34. I2C2_StartWrite(1);
  35.  
  36. I2C2->TXDR = byteToRead;
  37. while(!(I2C2->ISR & I2C_ISR_TXE));
  38.  
  39. I2C2_StartRead(1);
  40. while(!(I2C2->ISR & I2C_ISR_RXNE));
  41. UART_Send_String(1,"r strt");
  42. uint8_t recv_data=I2C2->RXDR;
  43.  
  44.  
  45. I2C2->CR2 |= I2C_CR2_STOP;
  46. while(I2C2->CR2 & I2C_CR2_STOP);
  47.  
  48. return recv_data;
  49.  
  50. }
  51.  
  52.  
  53. /////////////////////////////////////////////////////////
  54. /////////////////////////////////////////////////////////
  55. void Configure_GPIO_I2C2(void)
  56. {
  57.  
  58. RCC->AHBENR |= RCC_AHBENR_GPIOFEN;
  59. GPIOF->MODER |= (2<<12) | (2<<14);
  60. GPIOF->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7;
  61.  
  62. GPIOF->OSPEEDR &= ~(1<<12);
  63. GPIOF->OSPEEDR &= ~(1<<14);
  64.  
  65. GPIOF->PUPDR &= ~(1<<12);
  66. GPIOF->PUPDR &= ~(1<<12);
  67. }
  68.  
  69. void Configure_I2C2_Master(uint8_t slave_addr,uint8_t no_of_bytes)
  70. {
  71.  
  72. RCC->APB1ENR |= RCC_APB1ENR_I2C2EN;
  73.  
  74. /* (1) Timing register value is computed with the AN4235 xls file,
  75. fast Mode @400kHz with I2CCLK = 48MHz, rise time = 140ns, fall time = 40ns */
  76. I2C2->CR1 &= ~I2C_CR1_PE;
  77. I2C2->TIMINGR |= (uint32_t)0x00B01A4B; /* (1) */
  78. I2C2->CR2 |= (uint8_t)slave_addr;
  79. I2C2->CR2 |= no_of_bytes<<16;
  80. I2C2->CR1 |= I2C_CR1_PE;
  81. //NVIC_SetPriority(I2C2_IRQn, 0); /* (7) */
  82. //NVIC_EnableIRQ(I2C2_IRQn); /* (8) */
  83. }
Add Comment
Please, Sign In to add comment