📄 i2c.lst
字号:
ARM COMPILER V0.07, i2c 26/11/04 11:06:09 PAGE 1
ARM COMPILER V0.07, COMPILATION OF MODULE i2c
OBJECT MODULE PLACED IN i2c.OBJ
COMPILER INVOKED BY: C:\KEIL\ARM\BIN\CA.EXE i2c.c TABS(4)
stmt level source
1 /**********************/
2 /*i2c.c */
3 /*Pierre Seguin */
4 /*zpierre007@yahoo.fr */
5 /**********************/
6
7
8 #include "LPC210x.h"
9 #include "i2c.h"
10
11
12 #define I2C_ERROR 1 // these are the values that are stored in i2c_status
13 #define I2C_OK 2 // after reading or writing a byte and during an I2C operat
-ion
14 #define I2C_BUSY 0
15 #define EEPROM_ADDR 0xA0
16
17 unsigned char mask[] = {0xA8, 0x1F, 0x7F, 0x7F, 0x3F, 0x3F, 0x07, 0x1F,
18 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
19 // mask values for registers
20
21
22 void InitI2C(void)
23 {
24 1
25 1 I2C_I2CONCLR = 0xFF;
26 1
27 1 // Set pinouts as scl and sda
28 1 PINSEL0 |= 0x50;
29 1 PINSEL0 &= (uint32_t)0xFFFFFF5F;
30 1
31 1 I2C_I2SCLL=60; //speed at 375Khz for a VPB Clock Divider = 1
32 1 I2C_I2SCLH=70;
33 1 I2C_I2CONSET = 0x40; //Active Master Mode on I2C bus
34 1 }
35
36
37 void SendSlaveAdress(uint8_t Addr_S)
38 {
39 1 if(!(Addr_S&0x01)) //test if it's a master adress
40 1 {
41 2 //while(I2C_I2STAT!=0xF8);
42 2 I2C_I2CONSET = STA;
43 2 while(I2C_I2STAT!=0x08); //Set and wait the start
44 2 I2C_I2DAT = Addr_S; // Charge slave Address
45 2 I2C_I2CONCLR = SIC|STAC; // Clear i2c interrupt bit to send the data
46 2 while(I2C_I2STAT!=0x18&&!(I2C_I2CONSET&SI)); //wait the ACK
47 2 }
48 1 else //it's a slave adress
49 1 {
50 2 I2C_I2CONSET = STA;
51 2 I2C_I2CONCLR = SIC;
52 2 while(I2C_I2STAT!=0x10&&!(I2C_I2CONSET&SI)); //Set and wait the start
53 2 I2C_I2DAT = Addr_S; // Charge slave Address
54 2 I2C_I2CONCLR = SIC|STAC; // Clear i2c interrupt bit to send the data
55 2 while(I2C_I2STAT!=0x40&&!(I2C_I2CONSET&SI)); //wait the ACK
56 2 }
57 1 }
58
ARM COMPILER V0.07, i2c 26/11/04 11:06:09 PAGE 2
59
60 uint8_t ReadOnI2C(void)
61 {
62 1 I2C_I2CONCLR = SIC; //ACK from the master
63 1 while(I2C_I2STAT!=0x50&&!(I2C_I2CONSET&SI)); //wait the data
64 1 // Read the data...
65 1 return I2C_I2DAT;
66 1 }
67
68
69
70 void WriteOnI2C(uint8_t Data)
71 {
72 1 I2C_I2DAT = Data; // Charge Data
73 1 I2C_I2CONCLR = SIC; // Clear i2c interrupt bit to send the data
74 1 while(I2C_I2STAT!=0x28&&!(I2C_I2CONSET&SI)); //wait the ACK
75 1 }
76
77 uint8_t WriteRTC(uint8_t Addr,uint8_t Data)
78 {
79 1 SendSlaveAdress(EEPROM_ADDR); //Send the Eeprom Adress
80 1 WriteOnI2C(Addr); //Send Addr
81 1 WriteOnI2C(chartobcd(Data)); //Send Data
82 1 STOPI2C
83 1 }
84
85
86 uint8_t ReadRTC(uint8_t Addr, uint8_t maskoff)
87 {
88 1 uint8_t tmp;
89 1
90 1 SendSlaveAdress(EEPROM_ADDR);//Send the Eeprom Adress
91 1 WriteOnI2C(Addr); //Send Addr
92 1 SendSlaveAdress(EEPROM_ADDR|1);
93 1 tmp=ReadOnI2C();
94 1 if (maskoff) tmp &= mask[Addr]; // mask result if required
95 1 STOPI2C
96 1 // return bcdtochar(tmp); // convert to decimal and return result
97 1 return tmp;
98 1 }
99
100
101
102
103
104
105 // function chartobcd
106 // converts a decimal value to the BCD equivalent
107 // decimal values must be stored in the Real Time Clock in BCD format
108 // passed is the decimal value to convert
109 // returned is the BCD equivalent
110 unsigned char chartobcd(unsigned char n)
111 {
112 1 return ((n / 10) << 4) | (n % 10);
113 1 }
114
115 // function bcdtochar
116 // converts a number in BCD format to the decimal equivalent
117 // decimal values must be stored in the Real Time Clock in BCD format
118 // passed is the BCD format value
119 // returned is the decimal equivalent
120 unsigned char bcdtochar(unsigned char bcdnum)
121 {
122 1 return (bcdnum & 0x0f) + (bcdnum >> 4) * 10;
123 1 }
124
ARM COMPILER V0.07, i2c 26/11/04 11:06:09 PAGE 3
ARM COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -