当前位置:文档之家› IIC

IIC

IIC
IIC

#include "stm32f10x_lib.h"

#include "I2C_EE.h"

/* Private typedef -----------------------------------------------------------*/

typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; //定义枚举变量TestStatus;

/* Private define ------------------------------------------------------------*/

#define countof(a) (sizeof(a) / sizeof(*(a)))

#define EEPROM_WriteAddress1 0x5

#define EEPROM_ReadAddress1 0x5

#define BufferSize1 (countof(Tx1_Buffer)-1)

#define BufferSize2 (countof(Tx2_Buffer)-1)

#define EEPROM_WriteAddress2 (EEPROM_WriteAddress1 + BufferSize1)

#define EEPROM_ReadAddress2 (EEPROM_ReadAddress1 + BufferSize1)

/* Private macro -------------------------------------------------------------*/

u8 Tx1_Buffer[] = "STM32F10x I2C Firmware ";

u8 Tx2_Buffer[] = "Library Example */";

u8 Rx_Buffer[BufferSize1 + BufferSize2], Rx1_Buffer[BufferSize1],

Rx2_Buffer[BufferSize2];

volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

void Delay_Ms(u16 time);

void RCC_Configuration(void);

void GPIO_Configuration(void);

void NVIC_Configuration(void);

TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength);

extern void lcd_init (void);

extern void lcd_clear (void);

extern void lcd_putchar (char c);

extern void set_cursor (int column, int line);

extern void lcd_print (char *string);

extern void lcd_bargraph (int pos_x, int pos_y, int value); // STM32 Initialization

/* Private functions ---------------------------------------------------------*/

/******************************************************************************

* Function Name : main

* Description : Main program.

* Input : None

* Output : None

* Return : None

******************************************************************************* /

int main(void)

{

#ifdef DEBUG

debug();

#endif

u8 key1,key2;

u8 buf,lcd[5];

/* System Clocks Configuration */

RCC_Configuration();

/* NVIC configuration */

NVIC_Configuration();

/* Configure the GPIOs */

GPIO_Configuration();

/* Initialize the I2C EEPROM driver ----------------------------------------*/

I2C_EE_Init();

I2C_EE_BufferRead(Rx1_Buffer, EEPROM_ReadAddress1, BufferSize1);

lcd_init();

lcd_clear();

lcd_print_S("STM32_I2C !");

Delay_Ms(10);

while (1)

{

key1 = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8);

if(key1==0)

{

GPIO_SetBits(GPIOB,GPIO_Pin_10);

//IIC1_SCL=1;

GPIO_SetBits(GPIOF,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);

// I2C_EE_BufferWrite(Tx1_Buffer, EEPROM_WriteAddress1, BufferSize1);

GPIO_SetBits(GPIOB,GPIO_Pin_11);

//IIC1_SDA=1;

while(key1==0)

// 按键释放;

{

key1 = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8);

}

}

key2 = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);

if(key2==0)

{

GPIO_ResetBits(GPIOB,GPIO_Pin_10);

//IIC2_SCL=1

//IIC2_SDA=1;

GPIO_ResetBits(GPIOB,GPIO_Pin_11);

while(key2==0)

// 按键释放;

{

key2 = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);

}

}

set_cursor(0, 1);

lcd_print_S(Rx1_Buffer);

Delay_Ms(50);

}

}

/****************************************************************************** *

* 函数名: Delay_Ms(u16 time

* 功能: 延时配置

* 输入: 无

* 输出: 无

* 返回: 无

*******************************************************************************

/

void Delay_Ms(u16 time)

{

u16 i,j;

for(i=0;i

for(j=0;j<10260;j++);

}

/****************************************************************************** *

* 函数名: RCC_Configuration

* 功能: 时钟配置

* 输入: 无

* 输出: 无

* 返回: 无

******************************************************************************* /

void RCC_Configuration(void)

{

/* RCC system reset(for debug purpose) */

RCC_DeInit();

/* Enable HSE */

RCC_HSEConfig(RCC_HSE_ON);

/* Wait till HSE is ready */

while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);

/* Enable Prefetch Buffer */

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* Flash 2 wait state */

FLASH_SetLatency(FLASH_Latency_2);

/* HCLK = SYSCLK */

RCC_HCLKConfig(RCC_SYSCLK_Div1);

/* PCLK2 = HCLK */

RCC_PCLK2Config(RCC_HCLK_Div1);

/* PCLK1 = HCLK/2 */

RCC_PCLK1Config(RCC_HCLK_Div2);

/* PLLCLK = 8MHz * 9 = 72 MHz */

RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

/* Enable PLL */

RCC_PLLCmd(ENABLE);

/* Wait till PLL is ready */

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

/* Select PLL as system clock source */

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/* Wait till PLL is used as system clock source */

while(RCC_GetSYSCLKSource() != 0x08);

/* Enable GPIOA, GPIOC, ADC1 and TIM1 clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOF , ENABLE);

/* I2C1 Periph clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

}

/****************************************************************************** *

* 函数名: NVIC_Configuration

* 功能: 中断配置

* 输入: 无

* 输出: 无

* 返回: 无

******************************************************************************* /

void NVIC_Configuration(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM

NVIC_SetV ectorTable(NVIC_V ectTab_RAM, 0x0);

#else

NVIC_SetV ectorTable(NVIC_V ectTab_FLASH, 0x0);

#endif

}

/****************************************************************************** *

* 函数名: GPIO_Configuration

* 功能: GPIO配置

* 输入: 无

* 输出: 无

* 返回: 无

******************************************************************************* /

void GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

/* Configure Pb.8-11 as output push-pull */

GPIO_InitStructure.GPIO_Pin =

GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;//选择PX.8-11

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//管脚频率为50MHZ

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //模式为推挽输出

GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOB寄存器

GPIO_InitStructure.GPIO_Pin =

GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_6|GPIO_Pin_7;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOF, &GPIO_InitStructure);

/* Configure I2C1 pins: SCL and 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);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8; //选择PX.0 8 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//管脚频率为50MHZ

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //设置为下拉输入

GPIO_Init(GPIOA , &GPIO_InitStructure); //初始化GPIOA

寄存器

}

/****************************************************************************** *

* Function Name : Buffercmp

* Description : Compares two buffers.

* Input : - pBuffer1, pBuffer2: buffers to be compared.

* : - BufferLength: buffer's length

* Output : None

* Return : PASSED: pBuffer1 identical to pBuffer2

* FAILED: pBuffer1 differs from pBuffer2

******************************************************************************* /

TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength)

{

while(BufferLength--)

{

if(*pBuffer1 != *pBuffer2)

{

return FAILED;

}

pBuffer1++;

pBuffer2++;

}

return PASSED;

}

#ifdef DEBUG

/****************************************************************************** *

* Function Name : assert_failed

* Description : Reports the name of the source file and the source line number

* where the assert_param error has occurred.

* Input : - file: pointer to the source file name

* - line: assert_param error line source number

* Output : None

* Return : None

******************************************************************************* /

void assert_failed(u8* file, u32 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\r\n", file, line) */

/* Infinite loop */

while (1)

{

}

}

#endif

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

相关主题
文本预览
相关文档 最新文档