打印

关于STM32 I2C的问题(新手,麻烦大侠帮下忙)

[复制链接]
1857|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yangliu793|  楼主 | 2012-10-1 12:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
write24c16(0xaa,0x55);
程序在这里停住,以下是我的全部代码:

#include "stm32f10x.h"
#include <stdio.h>
#define I2C1_SLAVE_ADDRESS7 0x30
#define AT24C02_Addr 0xA0
ErrorStatus HSEStartUpStatus;
USART_InitTypeDef USART_InitStructure;
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void I2C_Configuration(void);
ErrorStatus I2C_24c16_Status(void);
void I2C_Standby_24C(void);
u8 temp3=0xaa;
u8 temp4;
void write24c16(u8 nddr,u8 byte);
u8 read24c16(u8 nddr);
void delay(u32 z);
#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
int main()
{
  RCC_Configuration();
  NVIC_Configuration();
  GPIO_Configuration();
  USART_Configuration();
I2C_Configuration();
  while (1)
  {
    GPIO_ResetBits(GPIOD, GPIO_Pin_2);
    delay(1000);
      write24c16(0xaa,0x55);
    delay(1000);
    GPIO_SetBits(GPIOD, GPIO_Pin_2);
   temp4=read24c16(0xaa);
    delay(1000);     
    printf("\n\r神舟III号 串口1测试程序%x\n",temp4);
    GPIO_ResetBits(GPIOA, GPIO_Pin_8);
    delay(1000);
    GPIO_SetBits(GPIOA, GPIO_Pin_8);
    delay(1000);
  }
}

void GPIO_Configuration(void)
{
   GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
   GPIO_Init(GPIOD, &GPIO_InitStructure);

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
   GPIO_Init(GPIOA, &GPIO_InitStructure);

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
   GPIO_Init(GPIOA, &GPIO_InitStructure);

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
   GPIO_Init(GPIOA, &GPIO_InitStructure);

   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11 | GPIO_Pin_12;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

}
void USART_Configuration(void)
{
USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;/*设置数据位为8*/
  USART_InitStructure.USART_StopBits = USART_StopBits_1;     /*设置停止位为1位*/
  USART_InitStructure.USART_Parity = USART_Parity_No;        /*无奇偶校验*/
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;/*无硬件流控*/
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  /*发送和接收*/
  USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void RCC_Configuration(void)
{
    RCC_DeInit();    // RCC system reset(for debug purpose)
    RCC_HSEConfig(RCC_HSE_ON); // Enable HSE
    HSEStartUpStatus = RCC_WaitForHSEStartUp(); // Wait till HSE is ready
    if (HSEStartUpStatus == SUCCESS)               
    {
     RCC_HCLKConfig(RCC_SYSCLK_Div1);                 
   
     RCC_PCLK2Config(RCC_HCLK_Div1);                  
     RCC_PCLK1Config(RCC_HCLK_Div2);               
   
     FLASH_SetLatency(FLASH_Latency_2);                 // Flash 2 wait state
     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);  // Enable Prefetch Buffer
   
     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);   // PLLCLK = 8MHz * 9 = 72 MHz
     RCC_PLLCmd(ENABLE);            // Enable PLL
     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
     {
     ;               // Wait till PLL is ready
     }
     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);      // Select PLL as system clock source
     while (RCC_GetSYSCLKSource()!= 0x08)       // Wait till PLL is used as system clock source
     {
     ;
     }
   
   }
  
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);

}
void I2C_Configuration(void)
{
  I2C_InitTypeDef I2C_InitStructure;
   I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
   I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
   I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;
   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
   I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
   I2C_InitStructure.I2C_ClockSpeed = 100000;  
  I2C_Cmd(I2C1, ENABLE);   
  I2C_Init(I2C1, &I2C_InitStructure);
  I2C_AcknowledgeConfig(I2C1, ENABLE);
}
void NVIC_Configuration(void)
{
  #ifdef VECT_TAB_RAM
   NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
   #else
   NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
   #endif
}
void delay(u32 z)
{
u32 x,y;
for(x=z;x>0;x--)
    for(y=1000;y>0;y--);

}
void write24c16(u8 nddr,u8 byte)
{   
     while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
         I2C_GenerateSTART(I2C1, ENABLE);
         while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
         I2C_Send7bitAddress(I2C1, AT24C02_Addr, I2C_Direction_Transmitter);
     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
          I2C_SendData(I2C1, nddr);
     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))/* EV8 */
     {
     }   
     I2C_SendData(I2C1, byte);      
      I2C_GenerateSTOP(I2C1, ENABLE);
    I2C_Standby_24C();
         delay(100);
}




u8 read24c16(u8 nddr)
{
    u8 temp;
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
         I2C_GenerateSTART(I2C1, ENABLE);
         while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
         I2C_Send7bitAddress(I2C1, AT24C02_Addr, I2C_Direction_Transmitter);
         while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
     I2C_Cmd(I2C1, ENABLE);
     I2C_SendData(I2C1, nddr);
     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
         I2C_GenerateSTART(I2C1, ENABLE);
     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
         I2C_Send7bitAddress(I2C1, AT24C02_Addr, I2C_Direction_Receiver);
     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)){;}   
     temp=I2C_ReceiveData(I2C1);        
     I2C_AcknowledgeConfig(I2C1, ENABLE);
     return temp;
}


PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch); /*发送一个字符函数*/
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)/*等待发送完成*/
  {
    ;
  }
  return ch;
}
void I2C_Standby_24C(void) //判忙     
{
  vu16 SR1_Tmp;
  do
  {
    /*起始位*/
    I2C_GenerateSTART(I2C1, ENABLE);
    /*读SR1*/
    SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1);
    /*器件地址(写)*/
    #ifdef AT24C01A
I2C_Send7bitAddress(I2C1, EEPROM_ADDR, I2C_Direction_Transmitter);
#else
I2C_Send7bitAddress(I2C1, 0, I2C_Direction_Transmitter);
#endif
  }while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002));
  
  /**/
  I2C_ClearFlag(I2C1, I2C_FLAG_AF);
  /*停止位*/   
  I2C_GenerateSTOP(I2C1, ENABLE);
}
沙发
sunnyhey| | 2013-2-4 16:22 | 只看该作者
写完地址,没收到应答信号

使用特权

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

本版积分规则

1

主题

2

帖子

0

粉丝