#include"stm32f10x_lib.h"
#include<stdio.h>
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void I2C_Configuration(void);
u32 wait_event(u32 event);
void send_start(void);
void send_data(u8 data);
void send_stop(void);
u8 read_data(u8 add);
void write_data(u8 add,u8 data);
void write_page(u8 add,u8*data);
void read_page(u8 add,u8*data);
u8 data1;
u8 tab1[8];
u8 tab[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x07};
void delay(void);
FlagStatus Usart_Send_Suceess;
void Usart_Configuration(void);
int main()
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
Usart_Configuration();
I2C_Configuration();
GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_RESET);
while(1)
{ //write_data(0x08,0x00);
write_page(0x10,tab);
delay();
read_page(0x10,tab1);
printf("%d",tab1[4]);
}
}
void RCC_Configuration()
{
ErrorStatus HSEStartUpStatus ;
RCC_DeInit(); //将外设RCC寄存器重设为缺省值
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON); //开启外部高速晶振(HSE)
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待HSE起振
if(HSEStartUpStatus == SUCCESS) //若成功起振,(下面为系统总线时钟设置)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能FLASH预取指缓存
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_1); //设置FLASH存储器延时时钟周期数(根据不同的系统时钟选取不同的值)
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1); //设置AHB时钟=72 MHz
/* PCLK2 = HCLK/2 */
RCC_PCLK2Config(RCC_HCLK_Div2); //设置APB1时钟=36 MHz(APB1时钟最大值)
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div1); //设置APB2时钟=72 MHz
/* Configure ADCCLK such as ADCCLK = PCLK2/2 */
RCC_ADCCLKConfig(RCC_PCLK2_Div2); //RCC_PCLK2_Div2,4,6,8
/* PLLCLK = 4MHz * 9 = 36 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9); //PLL必须在其激活前完成配置(设置PLL时钟源及倍频系数)
/* 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)
{
}
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|
RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* GPIOB Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* I2C1 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
}
}
void GPIO_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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);
}
void NVIC_Configuration()
{
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
}
void Usart_Configuration()
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode= USART_Mode_Tx;
USART_InitStructure.USART_Clock=USART_Clock_Disable;
USART_InitStructure.USART_CPOL=USART_CPOL_Low;
USART_InitStructure.USART_CPHA=USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit=USART_LastBit_Disable;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET)
{
}
return ch;
}
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 = 0xff;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed =50000;
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1, &I2C_InitStructure);
}
u32 wait_event(u32 event)
{ u32 i,j;
for(i=100000;i>0;i--)
{
j=I2C1->SR1;
if(j&event)
{
return 1;
}
}
return 0;
}
void send_start()
{
I2C_GenerateSTART(I2C1, ENABLE);
wait_event(I2C_FLAG_SB);
I2C1->DR=0xa0;
wait_event(I2C_FLAG_ADDR);
I2C1->SR2;
}
void send_data(u8 data)
{
wait_event(I2C_FLAG_BTF);
I2C1->DR=data;
}
void send_stop()
{
wait_event(I2C_FLAG_TXE);
I2C_GenerateSTOP(I2C1, ENABLE);
}
u8 read_data(u8 add)
{
u8 data;
I2C_AcknowledgeConfig(I2C1,DISABLE);
send_start();
send_data(add);
I2C_GenerateSTART(I2C1, ENABLE);
wait_event(I2C_FLAG_SB);
I2C1->DR=0xa1;
wait_event(I2C_FLAG_ADDR);
I2C1->SR2;
wait_event(I2C_FLAG_RXNE);
data=I2C1->DR;
send_stop();
return data;
}
void write_data(u8 add,u8 data)
{
send_start();
send_data(add);
send_data(data);
send_stop();
}
void delay()
{
u32 i;
for(i=100000;i>0;i--);
}
void write_page(u8 add,u8*data)
{ u8 i;
send_start();
send_data(add);
for(i=0;i<8;i++)
{
send_data(*data);
data++;
}
send_stop();
}
void read_page(u8 add ,u8*data)
{ u8 i;
I2C_AcknowledgeConfig(I2C1,ENABLE);
send_start();
send_data(add);
I2C_GenerateSTART(I2C1, ENABLE);
wait_event(I2C_FLAG_SB);
I2C1->DR=0xa1;
wait_event(I2C_FLAG_ADDR);
I2C1->SR2;
for(i=0;i<7;i++)
{
wait_event(I2C_FLAG_BTF);
*data=I2C1->DR;
data++;
}
I2C_AcknowledgeConfig(I2C1,DISABLE);
wait_event(I2C_FLAG_RXNE);
*data=I2C1->DR;
send_stop();
}
没有合适的资源?快使用搜索试试~ 我知道了~
STM32-i2c.109.rar_STM32 I2C_STM32 I2C_STM32的I2C_stm32 I2c应用_stm
共86个文件
o:24个
crf:22个
d:22个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 191 浏览量
2022-09-24
16:11:29
上传
评论
收藏 748KB RAR 举报
温馨提示
stm32单片机的I2C的应用实例,供参考
资源推荐
资源详情
资源评论
收起资源包目录
STM32-i2c.109.rar (86个子文件)
i2c
24C02RW_Opt.Bak 4KB
main.c 7KB
24C02RW.Opt 3KB
obj
stm32f10x_dma.d 491B
stm32f10x_rtc.crf 22KB
stm32f10x_can.d 491B
stm32f10x_exti.crf 22KB
stm32f10x_pwr.crf 29KB
24C02RW.sct 479B
stm32f10x_usart.o 45KB
stm32f10x_wwdg.d 500B
stm32f10x_tim1.o 79KB
stm32f10x_lib.d 2KB
stm32f10x_can.crf 41KB
stm32f10x_gpio.d 500B
stm32f10x_adc.crf 42KB
stm32f10x_bkp.crf 29KB
stm32f10x_spi.d 491B
stm32f10x_i2c.d 491B
stm32f10x_wwdg.crf 28KB
cortexm3_macro.o 2KB
stm32f10x_it.crf 124KB
stm32f10x_gpio.crf 34KB
stm32f10x_systick.o 26KB
stm32f10x_flash.d 438B
main.crf 129KB
stm32f10x_exti.o 27KB
stm32f10x_dma.o 40KB
24C02RW.axf 129KB
24C02RW.plg 144B
stm32f10x_rtc.o 28KB
stm32f10x_tim1.crf 68KB
stm32f10x_systick.crf 21KB
24C02RW.tra 3KB
24C02RW.hex 25KB
stm32f10x_flash.crf 29KB
stm32f10x_dma.crf 33KB
stm32f10x_usart.crf 37KB
stm32f10x_pwr.d 491B
stm32f10x_adc.o 52KB
stm32f10x_i2c.o 50KB
stm32f10x_flash.o 37KB
stm32f10x_systick.d 454B
stm32f10x_tim.o 72KB
main.d 1KB
stm32f10x_wwdg.o 33KB
stm32f10x_bkp.o 35KB
stm32f10x_usart.d 509B
stm32f10x_nvic.crf 29KB
stm32f10x_rcc.d 422B
stm32f10x_nvic.d 430B
stm32f10x_tim1.d 500B
24C02RW.lnp 826B
stm32f10x_tim.crf 59KB
stm32f10x_nvic.o 40KB
stm32f10x_rtc.d 422B
stm32f10x_tim.d 491B
stm32f10x_adc.d 491B
24C02RW.htm 79KB
stm32f10x_bkp.d 491B
STM32F10x.o 5KB
stm32f10x_exti.d 430B
stm32f10x_it.d 2KB
stm32f10x_can.o 45KB
stm32f10x_spi.o 41KB
stm32f10x_rcc.o 41KB
main.o 134KB
stm32f10x_gpio.o 42KB
stm32f10x_rcc.crf 34KB
stm32f10x_lib.o 124KB
stm32f10x_iwdg.crf 20KB
stm32f10x_spi.crf 34KB
stm32f10x_i2c.crf 40KB
stm32f10x_lib.crf 122KB
stm32f10x_pwr.o 35KB
stm32f10x_it.o 136KB
stm32f10x_iwdg.d 430B
stm32f10x_iwdg.o 25KB
24C02RW.plg 143B
STM32F10x.s 11KB
24C02RW_Uv2.Bak 0B
lik
STM32F10x.lst 36KB
24C02RW.map 79KB
cortexm3_macro.lst 25KB
24C02RW_Target 1.dep 7KB
24C02RW.Uv2 4KB
共 86 条
- 1
资源评论
林当时
- 粉丝: 100
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功