只看到管脚配置,却找不到管脚定义(比如
- #define SCL_H GPIOB->BSRR = GPIO_Pin_6
)
程序如下:
24C02子程序
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/*******************************************************************************
* Function Name : I2C_Configuration
* Description : EEPROM1ü½ÅÅäÖÃ
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO , ENABLE);
/* Configure I2C1 pins: PB6->SCL and PB7->SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//ÕaàïÖ»óD¶Ë¿ú3õê¼»ˉÕò2»μ½¾ßìå
B6oíPB7òy½ÅμĶ¨òå
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x30;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000; /* 100KËù¶è */
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1, &I2C_InitStructure);
/*ÔêDí1×Ö½ú1ó|′eÄ£ê½*/
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
/*******************************************************************************
* Function Name : I2C_delay
* Description : Ñó3ùê±¼ä
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void I2C_delay(uint16_t cnt)
{
while(cnt--);
}
/*******************************************************************************
* Function Name : I2C_AcknowledgePolling
* Description : μè′y»ñè¡I2C×üÏß¿ØÖÆè¨ ÅD¶ÏÃ|×′ì¬
* Input : - I2Cx:I2C¼Ä′æÆ÷»ùÖ·
* - I2C_Addr:′óÆ÷¼tμØÖ·
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void I2C_AcknowledgePolling(I2C_TypeDef *I2Cx,uint8_t I2C_Addr)
{
vu16 SR1_Tmp;
do
{
I2C_GenerateSTART(I2Cx, ENABLE); /*Æeê¼Î»*/
/*¶áSR1*/
SR1_Tmp = I2C_ReadRegister(I2Cx, I2C_Register_SR1);
/*Æ÷¼tμØÖ·(D′)*/
#ifdef AT24C01A
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Transmitter);
#else
I2C_Send7bitAddress(I2Cx, 0, I2C_Direction_Transmitter);
#endif
}while(!(I2C_ReadRegister(I2Cx, I2C_Register_SR1) & 0x0002));
I2C_ClearFlag(I2Cx, I2C_FLAG_AF);
I2C_GenerateSTOP(I2Cx, ENABLE); /*í£Ö1λ*/
}
/*******************************************************************************
* Function Name : I2C_Read
* Description : í¨1yÖ¸¶¨I2C½ó¿ú¶á衶à¸ö×Ö½úêy¾Y
* Input : - I2Cx:I2C¼Ä′æÆ÷»ùÖ·
* - I2C_Addr:′óÆ÷¼tμØÖ·
* - addr:Ô¤¶áè¡×Ö½ú′æ′¢λÖÃ
* - *buf:¶áè¡êy¾YμÄ′æ′¢λÖÃ
* - num:¶áè¡×Ö½úêy
* Output : None
* Return : 3é1|·μ»Ø0
* Attention : None
*******************************************************************************/
uint8_t I2C_Read(I2C_TypeDef *I2Cx,uint8_t I2C_Addr,uint8_t addr,uint8_t *buf,uint16_t num)
{
if(num==0)
return 1;
while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
/*ÔêDí1×Ö½ú1ó|′eÄ£ê½*/
I2C_AcknowledgeConfig(I2Cx, ENABLE);
/* ·¢ËíÆeê¼Î» */
I2C_GenerateSTART(I2Cx, ENABLE);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));/*EV5,Ö÷Ä£ê½*/
#ifdef AT24C01A
/*·¢ËíÆ÷¼tμØÖ·(D′)*/
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/*·¢ËíμØÖ·*/
I2C_SendData(I2Cx, addr);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));/*êy¾YòÑ·¢Ëí*/
/*Æeê¼Î»*/
I2C_GenerateSTART(I2Cx, ENABLE);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
/*Æ÷¼t¶á*/
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
#else
/*·¢ËíÆ÷¼tμØÖ·(¶á)24C01*/
I2C_Send7bitAddress(I2Cx, addr<<1, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
#endif
while (num)
{
if(num==1)
{
I2C_AcknowledgeConfig(I2Cx, DISABLE); /* ×îoóò»Î»oóòa1رÕó|′eμÄ */
I2C_GenerateSTOP(I2Cx, ENABLE); /* ·¢Ëíí£Ö1λ */
}
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)); /* EV7 */
*buf = I2C_ReceiveData(I2Cx);
buf++;
/* Decrement the read bytes counter */
num--;
}
/* Ôù′ÎÔêDíó|′eÄ£ê½ */
I2C_AcknowledgeConfig(I2Cx, ENABLE);
return 0;
}
/*******************************************************************************
* Function Name : I2C_WriteOneByte
* Description : í¨1yÖ¸¶¨I2C½ó¿úD′èëò»¸ö×Ö½úêy¾Y
* Input : - I2Cx:I2C¼Ä′æÆ÷»ùÖ·
* - I2C_Addr:′óÆ÷¼tμØÖ·
* - addr:Ô¤D′èë×Ö½úμØÖ·
* - value
′èëêy¾Y
* Output : None
* Return : 3é1|·μ»Ø0
* Attention : None
*******************************************************************************/
uint8_t I2C_WriteOneByte(I2C_TypeDef *I2Cx,uint8_t I2C_Addr,uint8_t addr,uint8_t value)
{
/* Æeê¼Î» */
I2C_GenerateSTART(I2Cx, ENABLE);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
#ifdef AT24C01A
/* ·¢ËíÆ÷¼tμØÖ·(D′)*/
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/*·¢ËíμØÖ·*/
I2C_SendData(I2Cx, addr);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
#else
I2C_Send7bitAddress(I2Cx, addr<<1, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
#endif
/* D′ò»¸ö×Ö½ú*/
I2C_SendData(I2Cx, value);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* í£Ö1λ*/
I2C_GenerateSTOP(I2Cx, ENABLE);
I2C_AcknowledgePolling(I2Cx,I2C_Addr);
I2C_delay(1000);
return 0;
}
/*******************************************************************************
* Function Name : I2C_Write
* Description : í¨1yÖ¸¶¨I2C½ó¿úD′èë¶à¸ö×Ö½úêy¾Y
* Input : - I2Cx:I2C¼Ä′æÆ÷»ùÖ·
* - I2C_Addr:′óÆ÷¼tμØÖ·
* - addr:Ô¤D′èë×Ö½úμØÖ·
* - *buf:Ô¤D′èëêy¾Y′æ′¢λÖÃ
* - num
′èë×Ö½úêy
* Output : None
* Return : 3é1|·μ»Ø0
* Attention : None
*******************************************************************************/
uint8_t I2C_Write(I2C_TypeDef *I2Cx,uint8_t I2C_Addr,uint8_t addr,uint8_t *buf,uint16_t num)
{
uint8_t err=0;
while(num--)
{
if(I2C_WriteOneByte(I2Cx, I2C_Addr,addr++,*buf++))
{
err++;
}
}
if(err)
return 1;
else
return 0;
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
24c02.h文件如下:
/*
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
********************************************************************************************************/
#ifndef __24C02_H
#define __24C02_H
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
/* Private define ------------------------------------------------------------*/
#define AT24C01A /* 24C01A,I2Cê±DòoííùoóμÄ24C02ò»Ñù */
//#define AT24C01 /* 24C01,I2CμÄê±DòoíÆÕí¨μÄóDμã2»í¬ */
#define ADDR_24LC02 0xA0
#define I2C_PAGESIZE 4 /* 24C01/01Aò3»o3åêÇ4¸ö */
/* Private function prototypes -----------------------------------------------*/
void I2C_Configuration(void);
uint8_t I2C_Read(I2C_TypeDef *I2Cx,uint8_t I2C_Addr,uint8_t addr,uint8_t *buf,uint16_t num);
uint8_t I2C_Write(I2C_TypeDef *I2Cx,uint8_t I2C_Addr,uint8_t addr,uint8_t *buf,uint16_t num);
#endif
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
main.c程序如下:
/***********************************************************************
ÎļtÃû3Æ£omain.C
1| Äü£o
±àD′ê±¼ä£o
±à D′ èË£o
×¢ òa£o
***********************************************************************/
#include "main.h"
int main(void)
{
/* Setup STM32 system (clocks, GPIO, NVIC) */
System_Setup();
//NVIC_Configuration();
USART_Configuration();
I2C_Configuration();
I2C_Test(); //×¢òa¸Ã¶Î2aêÔ3ìDò2»òa·Åμ½Ñ-»·ÖD£¬EEPROM2áD′μÄ′ÎêyêÇóDÏTμÄ£¡£¡£¡
while (1)
{
;
}
}
/***********************************************************************
oˉêyÃû3Æ£oI2C_Test(void)
1| Äü£o2aêÔEEPROM
êäèë2Îêy£o
êä3ö2Îêy£o
±àD′ê±¼ä£o2012.11.22
±à D′ èË£o
×¢ òa£oÏèÏòEEPROMD′Ö¸¶¨êy¾Y£¬è»oóÔù¶á3öà′£¬×îoóí¨1yRS232·¢Ëíμ½μçÄÔéÏ£¬
òÔÑéÖ¤¶áD′êÇ·ñÕyè·
***********************************************************************/
void I2C_Test(void)
{
unsigned int i = 0;
unsigned char WriteBuffer[256];
unsigned char ReadBuffer[256];
for(i = 0;i < 256;i ++)//3õê¼»ˉ′yD′êy¾YÇø
{
WriteBuffer
= i;
}
I2C_Write(I2C1,ADDR_24LC02,0,WriteBuffer,sizeof(WriteBuffer)); //ÏòEEPROMD′êy¾Y
I2C_Read(I2C1,ADDR_24LC02,0,ReadBuffer,sizeof(WriteBuffer)); //ÏòEEPROM¶áêy¾Y
RS232_Send_Data(ReadBuffer,1); //í¨RS232½«¶áμ½μÄêy¾Y·¢3öè¥
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
/* Infinite loop */
while (1)
{}
}
#endif
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
/***********************************************************************
ÎļtÃû3Æ£mmain.h
1| Äü£o
±àD′ê±¼ä£o
±à D′ èË£o
×¢ òa£o
***********************************************************************/
#ifndef __MAIN_H
#define __MAIN_H
#include "stm32f10x.h"
#include "stm32f107.h"
#include "SCI.H"
#include "24C02.h"
void Time_Update(void);
void Delay(uint32_t nCount);
void I2C_Test(void);
#endif
一周热门 更多>