OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 910|回复: 2

請問STM32F407的I2C的程式碼

[复制链接]

52

主题

75

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
249
金钱
249
注册时间
2018-10-17
在线时间
40 小时
发表于 2022-2-5 16:57:20 | 显示全部楼层 |阅读模式
1金钱
各位大大...........我有一個STM32F407ZGT6的開發版,但是我利用API函式不管怎麼弄,我的PB.6 PB.7
就是沒有任何I2C的信號出現,事實上是甚麼信號都沒有,雖然正點原子使用的是模擬I2C的方式是沒有
問題的.但是我好奇的是為什麼我用STM32的API就是噴不出任何信號出來??
請問各位是否有STM32F407可以讀寫EEPROM的程式碼可以給貼給我看,希望是使用原廠API的函式來做的

請問各位可以把STM32F407讀寫EEPROM  24C02的程式碼給我嗎???謝謝

/**
  ******************************************************************************
  * @file    CYE-STM32F4-C V1.0/main.c
  * @Author  CYESS Howard Lin <howard.lin@cyess.com>& Green Yeh
  * @version V1.0.0
  * @date    2015/01/31 main
  * @brief   Main program body       
  */
       
#include "main.h"
#define MESSAGE1   "CYESS UART Example"
#define MESSAGE2   " Testing on  "
#define MESSAGE3   "   UART1"
__IO uint8_t USARTFLAG=0;
__IO uint32_t         TimingDelay = 0;
USART_InitTypeDef USART_InitStructure;

uint8_t Tx1Buffer[256];
uint8_t Rx1Buffer[256];

typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength);
__IO TestStatus TransferStatus1 = FAILED;

/* Private function prototypes -----------------------------------------------*/
#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__ */
void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction);
void I2C_write(I2C_TypeDef* I2Cx, uint8_t data);
void I2C_stop(I2C_TypeDef* I2Cx);
void init_I2C1();
int main(void)
{
        uint16_t i;
  SystemCoreClockUpdate();
        if (SysTick_Config(SystemCoreClock /1000)){
    while (1);
  }               
        I2C_LowLevel_Init();       
   init_I2C1();
                while(1){       
                        
                          I2C_start(I2C1, 0xA0, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode
        I2C_write(I2C1, 0x00); // write one byte to the slave
        I2C_stop(I2C1);
                          Delay(10);
                          I2C_write(I2C1, 0x55); // write another byte to the slave
        I2C_stop(I2C1); // stop the transmission
                          Delay(10);
                       
                }
}

void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction){
    // wait until I2C1 is not busy anymore
    while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));

    // Send I2C1 START condition
    I2C_GenerateSTART(I2Cx, ENABLE);

    // wait for I2C1 EV5 --> Slave has acknowledged start condition
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));

    // Send slave Address for write
    I2C_Send7bitAddress(I2Cx, address, direction);

    /* wait for I2C1 EV6, check if
    * either Slave has acknowledged Master transmitter or
    * Master receiver mode, depending on the transmission
    * direction
    */
    if(direction == I2C_Direction_Transmitter){
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    } else if(direction == I2C_Direction_Receiver){
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    }
}


void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
{
    I2C_SendData(I2Cx, data);
    // wait for I2C1 EV8_2 --> byte has been transmitted
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}

void I2C_stop(I2C_TypeDef* I2Cx)
        {
    // Send I2C1 STOP Condition
    I2C_GenerateSTOP(I2Cx, ENABLE);
}


/*1ms delay for
Delay(1)*/
void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;
  while(TimingDelay != 0);
}

/*Handle the time decrecing*/
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00){
    TimingDelay--;
  }
}

TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength)
{
  while(BufferLength--)
  {
    if(*pBuffer1 != *pBuffer2)
    {
      return FAILED;
    }

    pBuffer1++;
    pBuffer2++;
  }

  return PASSED;  
}

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(EVAL_COM1, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  {}

  return ch;
}

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)
{
  while (1){}
}
#endif
void init_I2C1()
{
    GPIO_InitTypeDef GPIO_InitStruct;
    I2C_InitTypeDef I2C_InitStruct;
    // ?? I2C1 ? RCC ??
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
    // ?? GPIOB ??,????? PB6 ??? SCL ???PB7 ??? SDA ??
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // ?? PB6 ? PB7
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // ????? ?? ( AF - Alternate Function )
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // ?? GPIO ??? 50 MHz
    GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // set output to open drain --> the line has to be only pulled low, not driven high
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // ??????
    GPIO_Init(GPIOB, &GPIO_InitStruct); // ??? GPIOB
    // ?? I2C1 ? AF
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); // SCL
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); // SDA
    // ?? I2C1
    I2C_InitStruct.I2C_ClockSpeed = 100000; // ?? I2C ????? 100kHz
    I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C ??
    I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard
    I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode
    I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on)
    I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // ?? I2C ????? 7 bit
    I2C_Init(I2C1, &I2C_InitStruct); // ??? I2C1
    // ?? I2C1
    I2C_Cmd(I2C1, ENABLE);

}


正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

7

主题

70

帖子

0

精华

高级会员

Rank: 4

积分
727
金钱
727
注册时间
2020-6-24
在线时间
241 小时
发表于 2022-2-8 09:10:58 | 显示全部楼层
本帖最后由 qq1198228505 于 2022-2-8 09:13 编辑

湾湾的朋友你好F407硬件IIC
回复

使用道具 举报

4

主题

896

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
4305
金钱
4305
注册时间
2019-9-4
在线时间
896 小时
发表于 2022-2-8 11:12:09 | 显示全部楼层
你指的原厂API指的是硬件IIC吧?楼上发了一个硬件IIC的帖子,你参考看下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-26 03:39

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表