-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
53 lines (42 loc) · 1.35 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "stm32f10x.h"
#include "stm32f10x_i2c.h"
#include "stm32f10x_gpio.h"
#include "AT24C02.h"
#include "stm32f10x_rcc.h"
void main(){
// set clock 18 MHz for MCU
PLL_SRC_Sel(2);
PLL_MUL(2);
Src_Sys_Clk(3);
AHB_Div(4);
APB1_APB2_Div(1, 1);
// set Clocks Peripherals MCU
SetPeriphAPB1(APB1_I2C1);
SetPeriphAPB2(APB2_IOPB);
SetPeriphAPB2(APB2_AFIO);
// config pins for I2C
GPIO_InitTypeDef GPIOPB6_Init;
GPIOPB6_Init.GPIO_Mode = GPIO_Mode_AF_OD;
GPIOPB6_Init.GPIO_Pin = 6;
GPIOPB6_Init.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIOPB6_Init);// config SCL
GPIO_InitTypeDef GPIOPB7_Init;
GPIOPB7_Init.GPIO_Mode = GPIO_Mode_AF_OD;
GPIOPB7_Init.GPIO_Pin = 6;
GPIOPB7_Init.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIOPB7_Init);// config SDA
// initialize I2C1
I2C_Cmd(I2C1, ENABLE);
I2C1_Init.I2C_Ack = I2C_Ack_Enable;
I2C1_Init.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C1_Init.I2C_ClockSpeed = 40000;
I2C1_Init.I2C_DutyCycle = I2C_DutyCycle_16_9;
I2C1_Init.I2C_Mode = I2C_Mode_I2C;
I2C1_Init.I2C_OwnAddress1 = 0x00;
I2C_Init(I2C1, &I2C1_Init);
I2C_Cmd(I2C1, ENABLE);
AT24C02_Write(I2C1, 0xA0, 10, 123); //0xA0=> address device; 10=> address byte;123 => write value in address byte;
// wait >= 10ms
unsigned char value = AT24C02_Read(I2C1, 0xA0, 10); //0xA0=> address device; 10=> address byte;
while(1);
};