打印

请教stm32 i2c dma接收

[复制链接]
3932|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
effx|  楼主 | 2010-12-14 22:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 effx 于 2010-12-21 10:23 编辑

大家好。我先想AT24C02里写8个字符,然后DMA读出来,再把读出来的数据发到超级终端看是否正确。我发的字符时‘1’,‘2’,......,'8',但超级终端显示的只有'1',其他都是空格。程序写得很烂,大家不要笑话。写AT24C02是没问题的,因为开始我没用DMA接收,结果是正确的。
void DMA_Config(void)
{   
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  DMA_DeInit(DMA1_Channel7);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)rx_data;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 8;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize =DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel7, &DMA_InitStructure);
}

省掉不相关的。。。。。。
{
  /*Master Transfer Start*/
  I2C1->CR1 |= 1<<8;
  /*Wait until SB be set*/
  while(((I2C1->SR1)&(1)) == 0);
  I2C1->DR = 0xA0;
  /*Wait until ADDR be set*/
  while(((I2C1->SR1)&(1<<1)) == 0);
  /*Read SR1*/
  temp_read = I2C1->SR1 ;
  /*Read SR2*/
  temp_read = I2C1->SR2;

  I2C1->DR = 0;
  /*Wait until BTF be set*/
  while(((I2C1->SR1)&(1<<2)) == 0);

  I2C1->DR = '1';
  /*Wait until BTF be set*/
  while(((I2C1->SR1)&(1<<2)) == 0);

  I2C1->DR = '2';
  /*Wait until BTF be set*/
  while(((I2C1->SR1)&(1<<2)) == 0);

  /*Wait until BTF be set*/
  I2C1->DR = '3';
  /*Wait until BTF be set*/
  while(((I2C1->SR1)&(1<<2)) == 0);

  I2C1->DR = '4';
  /*Wait until BTF be set*/
  while(((I2C1->SR1)&(1<<2)) == 0);

  I2C1->DR = '5';
  /*Wait until BTF be set*/
  while(((I2C1->SR1)&(1<<2)) == 0);

  I2C1->DR = '6';
  /*Wait until BTF be set*/
  while(((I2C1->SR1)&(1<<2)) == 0);

  I2C1->DR = '7';
  /*Wait BTF be set*/
  while(((I2C1->SR1)&(1<<2)) == 0);

  I2C1->DR = '8';
  /*Wait until BTF be set*/
  while(((I2C1->SR1)&(1<<2)) == 0);

  /*I2C Stop*/
  I2C1->CR1 |= 1<<9;
  /*Wait until Stop be cleard*/
  while(((I2C1->CR1)&(1<<9)) == 1);
  
  /*延时等待EEPROM的内部写操作完成*/
  Delay(0xfffff);
  Delay(0xfffff);
  Delay(0x5ffff);
  Delay(0xfffff);
  Delay(0xfffff);
  Delay(0x5ffff);

  /*24C02 Dummy Write*/
  /*I2C Start*/
  I2C1->CR1 |= 1<<8;
  /*Wait until SB be set*/
  while(((I2C1->SR1)&(1)) == 0);
  I2C1->DR = 0xA0;
  /*Wait until ADDR be set*/
  while(((I2C1->SR1)&(1<<1)) == 0);
  /*Read SR1*/
  temp_read = I2C1->SR1;
  /*Read SR2*/
  temp_read = I2C1->SR2;
  I2C1->DR = 0;
  /*Wait until BTF be set*/
  while(((I2C1->SR1)&(1<<2)) == 0);
  
  DMA_Config();
  /* Enable DMA1 Channel7 */
  DMA_Cmd(DMA1_Channel7, ENABLE);
  /* Set Last bit to have a NACK on the last received byte */
  I2C1->CR2 |= (1<<12);
  /* Enable I2C DMA requests */
  I2C1->CR2 |= (1<<11);  

  /*读24C02*/
  /*I2C Start*/
  I2C1->CR1 |= 1<<8;
  /*Wait until SB  be set*/
  while(((I2C1->SR1)&(1)) == 0 );
  I2C1->DR = 0xA1;
  /*Wait until ADDR be set*/
  while(((I2C1->SR1)&(1<<1)) == 0 );
  /*Read SR1*/
  temp_read = I2C1->SR1 ;
  /*Read SR2*/
  temp_read = I2C1->SR2;

  while(((DMA1->ISR)&(1<<25)) == 0);
  DMA1_Channel7->CCR &= ~(1);
  DMA1->IFCR |= 1<<25;
  /*Set STOP*/
  I2C1->CR1 |= (1<<9);
  while(((I2C1->CR1)&(1<<9)) == (1<<9));
  
  USART_TX_Byte(rx_data[0]);
  USART_TX_Byte(rx_data[1]);
  USART_TX_Byte(rx_data[2]);
  USART_TX_Byte(rx_data[3]);
  USART_TX_Byte(rx_data[4]);
  USART_TX_Byte(rx_data[5]);
  USART_TX_Byte(rx_data[6]);
  USART_TX_Byte(rx_data[7]);
}
沙发
effx|  楼主 | 2010-12-15 16:09 | 只看该作者
自己顶以下。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

166

帖子

2

粉丝