STM32F0F1F4内部flash擦写时间和寿命
游名:STM32F030+FT1610FF1高精度超声波测距模块测距教程及参...
分享STM32F051中文参考手册(重制书签版)
STM32固件库分享,超全系列整理
三创电子(Tcreate)-STM32F030调试之I2C篇
基于STM32F0的FOC5.2库PMSM驱动:程序+原理图+BOM等全部开源分享
【MCU实战经验】+STM32F030的步进电机加减速
【管管推荐】STM32经验分享篇
三创电子(Tcreate)-STM32F030核心板代码
STM32F0 ADC(DMA中断)多通道,注释超详细
我在网上找到你们的代码。我复制了一下。有些问题想了解一下
void I2C_GPIO_Configuration(void)
{' b3 |( t1 \4 `/ K1 J8 \ u1 k# Y# Z
GPIO_InitTypeDef GPIO_InitStruct; * E& v- g7 _ L- k/ `: f; p
/* Enable GPIOA clock */. i' c7 U6 E( O. o5 C3 N, H: U
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); ; P9 C7 R1 F/ a8 L) ?8 O
/*!< sEE_I2C Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE);- c/ d3 b! [1 \, n1 D* x% j k
/*!< GPIO configuration */ / Y( n7 Y0 C/ i/ o4 A3 c4 b
/*!< Configure sEE_I2C pins: SCL */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//GPIO_Mode_IN
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;7 P. u. t3 R, O* C! Z
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;//open-drain9 V/ u$ C+ z! {. z. H* I' Y P
GPIO_Init(GPIOA , &GPIO_InitStruct);
; r& H7 J- a8 t g/ q
/*!< Configure sEE_I2C pins: SDA */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;# p/ p; U0 V6 Y* V! G
GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Connect PXx to I2C_SCL*/
GPIO_PinAFConfig( GPIOA , GPIO_PinSource9, GPIO_AF_4); ! Z8 T; f) \7 @
/* Connect PXx to I2C_SDA*/+ r% ~, B" S$ Z2 L; m! Z9 C) e* V
GPIO_PinAFConfig( GPIOA ,GPIO_PinSource10, GPIO_AF_4);
}
$ M( s6 b! ?& K( l* [
//IIC从模式配置,在配置时,需要设置地址,在这里设置为0XA0,而从设备的时钟属于被动模式,有IIC的主端确定。) P# h+ B: L7 o; G
void I2C_Configuration(void); d9 Z$ V9 _" {$ C* U
{5 ^ X: o; E% f$ }8 T1 T
I2C_InitTypeDef I2C_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;! B. z8 f. f0 t/ j* t
8 ? C0 d- c$ p$ u# G0 T
/* I2C configuration */" S6 a+ U, t# d. c
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;: a1 y6 K& w ^6 P1 G- A
I2C_InitStruct.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStruct.I2C_DigitalFilter = 0x00;
I2C_InitStruct.I2C_OwnAddress1 = ADDR;; b& f0 A& e, W( h' x. C5 a( E
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;* c6 r9 L0 I! ` M
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_Timing = 0xb0420f13; //100Kbits8 B5 z$ i: Y( [7 [5 |) C M
/* I2C Peripheral Enable */# k& z+ D: x2 n4 K" p
I2C_Cmd(I2C1, ENABLE);
/* Apply I2C configuration after enabling it */9 K8 B" g4 o$ c9 Z. m
I2C_Init(I2C1, &I2C_InitStruct);
I2C1->CR1 |= (I2C_CR1_ADDRIE | I2C_CR1_STOPIE | I2C_CR1_TXIE | I2C_CR1_RXIE);
//---------------------------- Peripheral and DMA interrupts Initialization ------------------* n. o% h7 a& V5 f- w
// Initialize I2C1 interrupts
/* Enable the IRQ channel */3 ?7 u- O# T/ C% g8 D% l; Y- X) N, d
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
! }' R3 s& e& {
/* Configure NVIC for I2C1 Interrupt */0 Y2 ?* x* X* @' F6 l, o+ Z
NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn;+ V) {5 k- ^; g6 Z& F
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;* c/ w" v6 T: C; F7 m7 ]/ G/ T, N1 A
NVIC_Init(&NVIC_InitStructure); , {6 R- i" w# c
. V9 I% C U6 C( p3 U
}9 e5 U8 [) g3 }: }1 _1 K* h; ~
+ N8 r: a% w# y
//下面是IIC中断函数的实现
& |/ Y% s( n6 ?- v6 p3 F% I. \+ A
void I2c_Init(void)
{
I2C_GPIO_Configuration();8 P& x; H# B! F B
I2C_Configuration();" C7 j0 Q3 c* i; e5 \# i9 l
}3 m$ X! F4 O8 q, X# g. f
( d9 n4 U5 M( R4 O
void I2C1_IRQHandler(void)0 a, g# Q3 U1 A5 `
{- G6 @6 Z4 b0 u( F
u32 AddCode;
// I2C_ClearITPendingBit(I2C1, I2C_ISR_ADDR | I2C_ISR_STOPF |I2C_ISR_ADDCODE );
__IO uint32_t I2CFlagStatus = 0x00000000;
I2CFlagStatus = (uint32_t)(I2C1->ISR & (uint16_t)0x0000100FE);/ W- p3 M8 r- O. w$ S! W+ c( N* _
' V5 ~4 j1 f) y! u; t
// printf("recv data\r\n");& Z2 i' r* p) d
if ((I2CFlagStatus & I2C_ISR_ADDR) != 0)
{
AddCode = (I2C1->ISR & 0xfe0000); //读取ADDCODE 32:17位, \+ A0 w7 h$ @0 Q- w! |+ o) _" \4 L z
printf("匹配地址2 0x%x: \r\n",AddCode >> 17 );
. u, L3 S! b1 z3 H5 |8 d3 U" y5 Z
if(I2C1->ISR&I2C_ISR_DIR) //tx mode7 Y. @# T, w. H3 b! z' u5 a
{- S7 X" ~3 Y& w$ i
printf("发送模式 \r\n");+ ?) _6 ?' |0 w1 J, C5 S) L
Tx_count = 0;* `- z; n5 \. I2 O
I2C1->ISR |= I2C_ISR_TXE;9 o7 Z% `0 h" H/ L
I2C1->ICR |= I2C_ICR_ADDRCF;
if(I2C_GetITStatus( I2C1, I2C_IT_RXNE) != 0)
{9 D, G! J" g ^# Y+ J) B' _
Rx_buffer[Rx_count++]=I2C_ReceiveData(I2C1);
if(Rx_count>=Rx_MAX)# e8 c5 Q- p! t3 A& M% t( R! G
{
Rx_count=0;3 l( s9 j: B# `: [% ]
//rx ok
}: }% ?( G9 E0 _5 P6 e; ?+ R
printf("Rx_buffer : %s\r\n",Rx_buffer);
}' v, b& r. h; H1 M% h4 g0 y
} $ ?( Q; `' h4 U2 O& X: W
if((I2C1->ISR&I2C_ISR_DIR)==0) //rx mode) ?6 J: y/ P, E- S2 ]
{6 ^. p# F" y8 e
printf("接收模式 \r\n");5 W3 B% O. I) m
Rx_count= 0;& g$ l5 p2 }0 i) s; d# z
I2C1->ICR |= I2C_ICR_ADDRCF; 8 u* a a" Y/ P1 a
}# }- w( ?4 y1 L0 L) l) P
}$ n# V" @9 B: J3 ?' x
else if(I2C_GetITStatus( I2C1, I2C_IT_RXNE) != 0)( ^$ u$ V' L5 d. z5 B
{- V6 L9 d) h) f* S5 b! J
Rx_buffer[Rx_count++]=I2C_ReceiveData(I2C1);: f1 b! Q% m0 r9 j
if(Rx_count>=Rx_MAX)) i. r0 |' G1 D8 ]
{% r; k: e4 s z- C, h! @2 ]" w
Rx_count=0;
//rx ok; q& P. C! G, \ p/ y" ~
}; n5 Q- w; Y) N
printf("Rx_buffer : %s\r\n",Rx_buffer);3 D9 ]# C( B2 l! v' k/ ]* U) V
}" \+ H5 ]7 ~: R/ j7 m# l" ^
else if ((I2CFlagStatus & I2C_ISR_TXIS) != 0)* ~9 Y1 K9 M) s- \
{" B) _7 M5 c% ^4 e3 T
I2C_SendData(I2C1,Tx_buffer[Tx_count++]);* f1 {/ L1 g5 o, W8 z9 s
if(Tx_count >= Tx_MAX)6 N U5 s( _; V. _
{2 q0 R* t/ n" X
Tx_count = 0;
//tx ok
}
}
else if ((I2CFlagStatus & I2C_ISR_STOPF) != 0)
{( A, o8 Z/ ^4 g! j+ p
printf("停止检查标志位 \r\n");
I2C1->ICR |= I2C_ICR_STOPCF;
Rx_count = 0;% P) \0 c; H5 R4 R
Tx_count = 0;
}4 F# M; Y$ L' ] H
}
2 ^, d, j5 h% N" p. l3 d
这个是可以匹配到ADDCODE ,但是没有接收到数据的,我没仔细看这个时序寄存器的配置。不知道是不是由于时序不匹配造成没有接收到数据,还是因为ack没有给出造成读不到数据的。! u' a0 s9 D( \! |0 Y
void I2C_GPIO_Configuration(void)
{( Z7 r) m* o& H5 W+ q" B+ y
GPIO_InitTypeDef GPIO_InitStruct; ( ~6 \2 L5 o4 ?9 R6 W8 q
% r2 y) r) V2 I {
/* Enable GPIOA clock *// R& M' g9 Q" l8 S& `3 c
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/*!< sEE_I2C Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE);3 |: ?5 X& V* p* h
/*!< GPIO configuration */
/*!< Configure sEE_I2C pins: SCL */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;3 z) K* O+ t0 C- `6 D
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//GPIO_Mode_IN
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;2 H/ z. x( n9 S; Y, {3 N! H
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;//open-drain9 u# h9 O# F0 m1 ^ U
GPIO_Init(GPIOA , &GPIO_InitStruct);
' ]3 `, }9 [5 i4 M0 I
/*!< Configure sEE_I2C pins: SDA */! r. G$ X* t( f# P' c2 u
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStruct);- ^+ ?1 V6 O' ^" r9 G( j) A
$ i* j+ ]9 l# O: W6 _' E9 {
/* Connect PXx to I2C_SCL*/9 Y: s7 t3 F4 `
GPIO_PinAFConfig( GPIOA , GPIO_PinSource9, GPIO_AF_4); / r4 |2 j2 \& e+ v) I$ v
/* Connect PXx to I2C_SDA*/
GPIO_PinAFConfig( GPIOA ,GPIO_PinSource10, GPIO_AF_4);* D2 U5 A( E: \6 I7 {3 k
}
1 Q& m( ~# _+ [2 C6 x! A1 X3 ?9 g/ w- x
//IIC从模式配置,在配置时,需要设置地址,在这里设置为0XA0,而从设备的时钟属于被动模式,有IIC的主端确定。2 b6 Z9 ]& B! o8 h1 M+ ~8 U6 s
void I2C_Configuration(void)' h) o+ x+ p3 G1 U8 N8 Z. r1 F
{
I2C_InitTypeDef I2C_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
7 o/ S# g, u9 |+ _' I
/* I2C configuration */
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;# i# i8 J0 | ~5 F
I2C_InitStruct.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStruct.I2C_DigitalFilter = 0x00;/ b6 H B+ m% J6 U+ f2 Q9 G u
I2C_InitStruct.I2C_OwnAddress1 = ADDR;5 u+ t. |6 q8 j- E# q) p& R$ X
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_Timing = 0xb0420f13; //100Kbits! g" B( I& N& V# [" S% ]; i1 K
/* I2C Peripheral Enable */
I2C_Cmd(I2C1, ENABLE);
/* Apply I2C configuration after enabling it */
I2C_Init(I2C1, &I2C_InitStruct);
5 y; Y3 C. i1 o$ K3 Y
I2C1->CR1 |= (I2C_CR1_ADDRIE | I2C_CR1_STOPIE | I2C_CR1_TXIE | I2C_CR1_RXIE);
//---------------------------- Peripheral and DMA interrupts Initialization ------------------3 f( `7 M) ?% C4 k* P$ [% B
// Initialize I2C1 interrupts
/* Enable the IRQ channel */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
8 `. D% ]9 k6 P8 h8 X) V3 U
/* Configure NVIC for I2C1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;3 H- t9 L2 B" S5 m7 b X3 ~
NVIC_Init(&NVIC_InitStructure);
& e+ C# {$ i+ f) v5 t
}" P- i0 V4 v' C1 ]. N" ?, {- u
//下面是IIC中断函数的实现* Q4 ?5 A4 u* D) `
void I2c_Init(void)2 M( H8 t' j6 I K* m
{8 F+ F6 P0 J" x+ Z9 [. a
I2C_GPIO_Configuration(); C! b( g* ?) g! {$ L5 v
I2C_Configuration();
}. ]) S, f" o9 N; h; ^! h3 w; E
void I2C1_IRQHandler(void) E a: t$ N) q
{
u32 AddCode;& \% @* I" J5 E( {: [2 U
// I2C_ClearITPendingBit(I2C1, I2C_ISR_ADDR | I2C_ISR_STOPF |I2C_ISR_ADDCODE );! Z1 i, @( C2 s- T2 u ?
__IO uint32_t I2CFlagStatus = 0x00000000;& Y7 h$ P' ^% f8 _' A2 j3 O7 A
I2CFlagStatus = (uint32_t)(I2C1->ISR & (uint16_t)0x0000100FE);# | S6 s5 V6 Z: Z& p
// printf("recv data\r\n");
if ((I2CFlagStatus & I2C_ISR_ADDR) != 0)
{
AddCode = (I2C1->ISR & 0xfe0000); //读取ADDCODE 32:17位8 y4 J0 i- g* h# f: B
printf("匹配地址2 0x%x: \r\n",AddCode >> 17 );
, i3 I7 l3 A/ S& d( |* O2 g
if(I2C1->ISR&I2C_ISR_DIR) //tx mode
{# e/ H: X7 L4 r2 m) B
printf("发送模式 \r\n");
Tx_count = 0;
I2C1->ISR |= I2C_ISR_TXE;
I2C1->ICR |= I2C_ICR_ADDRCF;
# R2 ?0 `2 J* g
if(I2C_GetITStatus( I2C1, I2C_IT_RXNE) != 0)
{
Rx_buffer[Rx_count++]=I2C_ReceiveData(I2C1);6 E* W" n3 m; i N/ T1 r0 L
if(Rx_count>=Rx_MAX)& V/ \7 m# r; q
{
Rx_count=0;! V0 F7 F' E5 A" ?$ I* j
//rx ok& Q9 S$ B. k. f0 z" h
}
printf("Rx_buffer : %s\r\n",Rx_buffer);
}% ^0 N3 t& m I3 z; Z4 x
}
if((I2C1->ISR&I2C_ISR_DIR)==0) //rx mode3 S0 m% z. B9 z+ z
{; f7 E5 h( c% Q" |- i
printf("接收模式 \r\n");; i, w( Z) N* j, Q' |8 I
Rx_count= 0;* `# u4 i% V7 ], i# B' `" ^8 n
I2C1->ICR |= I2C_ICR_ADDRCF; 7 X3 y2 {2 M) P; T' ^1 L1 h. z- i
}) M; H9 p4 N' i7 |* b, D0 ^/ k
}
else if(I2C_GetITStatus( I2C1, I2C_IT_RXNE) != 0)
{
Rx_buffer[Rx_count++]=I2C_ReceiveData(I2C1);. V& b; ~: B* S/ q" m ~5 i( h
if(Rx_count>=Rx_MAX)
{$ @, @5 ^& s" `! I Q8 G& o
Rx_count=0;0 \' ]6 v, G1 u
//rx ok
}' a6 h. U4 p8 D% O" r8 u2 j- _' k
printf("Rx_buffer : %s\r\n",Rx_buffer);
}
else if ((I2CFlagStatus & I2C_ISR_TXIS) != 0)
{* e; Q' J; [ q/ b) |
I2C_SendData(I2C1,Tx_buffer[Tx_count++]);
if(Tx_count >= Tx_MAX)
{
Tx_count = 0;
//tx ok
}
}
else if ((I2CFlagStatus & I2C_ISR_STOPF) != 0): m9 w. y; N7 `4 p! U+ u5 b& g
{2 ~' m8 J, S5 c% I, G1 c8 T& ^5 K
printf("停止检查标志位 \r\n");
I2C1->ICR |= I2C_ICR_STOPCF;
Rx_count = 0;. ^& F0 O; \/ |+ B. F L8 g; T! n: p
Tx_count = 0;
}$ V; L1 k0 l$ A7 ]; g
}