ATY_LIB V2_102_230218
ATY_LIB for general devices or ALGO
 
Loading...
Searching...
No Matches
at24c08_nonlib.c
Go to the documentation of this file.
1// Copyright 2021 IOsetting <iosetting(at)outlook.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/***
16 * Demo: AT24C08 write and read with raw registers
17 * Board: STC8H3K32
18 *
19 * P32 -> SCL
20 * P33 -> SDA
21 * GND -> GND, A2
22 * 3.3V -> VCC
23 */
24
25#include "fw_hal.h"
26
27#define AT24C_ADDR 0xA0
28
29
30void Wait()
31{
32 while (!(I2CMSST & 0x40));
33 I2CMSST &= ~0x40;
34}
35
36void Start()
37{
38 I2CMSCR = 0x01;
39 Wait();
40}
41
42void SendData(char dat)
43{
44 I2CTXD = dat;
45 I2CMSCR = 0x02;
46 Wait();
47}
48
49void RecvACK()
50{
51 I2CMSCR = 0x03;
52 Wait();
53}
54
56{
57 I2CMSCR = 0x04;
58 Wait();
59 return I2CRXD;
60}
61
62void SendACK()
63{
64 I2CMSST = 0x00;
65 I2CMSCR = 0x05;
66 Wait();
67}
68
69void SendNAK()
70{
71 I2CMSST = 0x01;
72 I2CMSCR = 0x05;
73 Wait();
74}
75
76void Stop()
77{
78 I2CMSCR = 0x06;
79 Wait();
80}
81
82void I2C_Init(void)
83{
84 // Master mode
90 // Switch alternative port
92 // Start I2C
94}
95
96void GPIO_Init(void)
97{
98 // SDA
100 // SCL
102}
103
104int main(void)
105{
106 uint8_t i, buf[20];
107
108 SYS_SetClock();
109 // UART1 configuration: baud 115200 with Timer2, 1T mode, no interrupt
111
112 GPIO_Init();
113 I2C_Init();
114
115 P_SW2 |= 0x80;
116
117 Start();
119 RecvACK();
120 SendData(0x00);
121 RecvACK();
122 SendData(0xAF);
123 RecvACK();
124 SendData(0xBF);
125 RecvACK();
126 SendData(0xCF);
127 RecvACK();
128 SendData(0xDF);
129 RecvACK();
130 SendData(0xEF);
131 RecvACK();
132 SendData(0xFF);
133 RecvACK();
134 Stop();
135 SYS_Delay(50);
136
137 while (1)
138 {
139 Start();
141 RecvACK();
142 SendData(0x00);
143 RecvACK();
144 Start();
145 SendData(AT24C_ADDR|0x01);
146 RecvACK();
147 buf[0] = RecvData();
148 SendACK();
149 buf[1] = RecvData();
150 SendNAK();
151 Stop();
152 UART1_TxHex(buf[0]);
153 UART1_TxChar(':');
154 UART1_TxHex(buf[1]);
155 UART1_TxChar(' ');
156
157 Start();
159 RecvACK();
160 SendData(0x01);
161 RecvACK();
162 Start();
163 SendData(AT24C_ADDR|0x01);
164 RecvACK();
165 buf[0] = RecvData();
166 SendACK();
167 buf[1] = RecvData();
168 SendNAK();
169 Stop();
170 UART1_TxHex(buf[0]);
171 UART1_TxChar(':');
172 UART1_TxHex(buf[1]);
173 UART1_TxChar(' ');
174
175 Start();
177 RecvACK();
178 SendData(0x02);
179 RecvACK();
180 Start();
181 SendData(AT24C_ADDR|0x01);
182 RecvACK();
183 buf[0] = RecvData();
184 SendACK();
185 buf[1] = RecvData();
186 SendNAK();
187 Stop();
188 UART1_TxHex(buf[0]);
189 UART1_TxChar(':');
190 UART1_TxHex(buf[1]);
191 UART1_TxChar(' ');
192
193 SYS_Delay(200);
194 }
195}
void Start()
void Stop()
void SendNAK()
void RecvACK()
int main(void)
void I2C_Init(void)
void GPIO_Init(void)
char RecvData()
void Wait()
void SendData(char dat)
#define AT24C_ADDR
void SendACK()
__CODE int8_t dat[20]
#define GPIO_P3_SetMode(__PINS__, __MODE__)
Definition: fw_gpio.h:89
@ GPIO_Pin_2
Definition: fw_gpio.h:47
@ GPIO_Pin_3
Definition: fw_gpio.h:48
@ GPIO_Mode_Output_PP
Definition: fw_gpio.h:24
@ GPIO_Mode_InOut_QBD
Definition: fw_gpio.h:23
#define I2C_SetClockPrescaler(__DIV__)
Definition: fw_i2c.h:71
@ I2C_WorkMode_Master
Definition: fw_i2c.h:24
#define I2C_SetWorkMode(__MODE__)
Definition: fw_i2c.h:65
#define I2C_SetEnabled(__STATE__)
Definition: fw_i2c.h:64
@ I2C_AlterPort_P32_P33
Definition: fw_i2c.h:51
#define I2C_SetPort(__ALTER_PORT__)
Definition: fw_i2c.h:121
#define I2CMSST
#define I2CMSCR
#define I2CRXD
#define I2CTXD
void SYS_SetClock(void)
Definition: fw_sys.c:40
void SYS_Delay(uint16_t t)
Definition: fw_sys.c:65
@ HAL_State_ON
Definition: fw_types.h:71
unsigned char uint8_t
Definition: fw_types.h:18
void UART1_TxChar(char dat)
Definition: fw_uart.c:72
@ UART1_BaudSource_Timer2
Definition: fw_uart.h:34
void UART1_TxHex(uint8_t hex)
Definition: fw_uart.c:79
void UART1_Config8bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, uint32_t baudrate)
Definition: fw_uart.c:56
uint8_t __XDATA i