1 | #include <avr/io.h>
|
2 |
|
3 | #define uint unsigned int
|
4 | #define uchar unsigned char
|
5 |
|
6 | void twi_init(void);
|
7 | void writebyte(uchar temp);
|
8 | uchar TWI_Write(uchar addr,uchar dat);
|
9 | uchar TWI_Read(uchar addr);
|
10 | uchar Decimal_to_BCD(uchar temp);
|
11 | uchar BCD_to_Decimal(uchar temp);
|
12 | void delay_2(uint z);
|
13 | void delay_1(void);
|
14 | void write_ds1307(void);
|
15 | void read_ds1307(void);
|
16 |
|
17 |
|
18 | //sda=PD1;//;TWI Data transmission bit
|
19 | //scl=PD0;//;TWI Clock control state flag
|
20 | //RS=PC0;//define the I / O hardware interface of LCD1602
|
21 | //RW=PC1;//define the I / O hardware interface 0f LCD1602
|
22 | //E=PC2; //define the I / O hardware interface 0f LCD1602
|
23 | uchar count;
|
24 | uchar Hours=16;//Definition of hours
|
25 | uchar Minutes=21;//Definition of minutes
|
26 | uchar Seconds=0;//Definition of seconds
|
27 |
|
28 | uint Year=12;//Definition of year
|
29 | uchar Month=7;//Definition of month
|
30 | uchar Date=27;//Definition of date
|
31 | uchar Weeks=5;//Definition of weeks
|
32 |
|
33 |
|
34 | #define START 0X08 //start() has been successfully sent
|
35 | #define RESTART 0X10 //The restart() has been successfully sent
|
36 | #define MT_SLA_ACK 0X18 //Load data has been sent ack()
|
37 | #define MT_SLA_NACK 0X20 //Load data has been sent nack()
|
38 | #define MT_DAT_ACK 0X28 //Data has been sent ack()
|
39 | #define MT_DAT_NACK 0X30 //Data has been sent nack()
|
40 | #define SL_SLA_ACK 0X40 //Data has been loaded from the machine to send ack()
|
41 | #define SL_SLA_NACK 0X48 //Data has been loaded from the machine to send nack()
|
42 | #define SL_DAT_ACK 0X50 //Slave data has been sent ack()
|
43 | #define SL_DAT_NACK 0X58 //Slave data has been sent nack()
|
44 |
|
45 | #define start() TWCR=((1<<TWINT)|(1<<TWSTA)|(1<<TWEN)) //Send start signal
|
46 | #define restart() TWCR=((1<<TWINT)|(1<<TWEN)|(1<<TWSTA)) //Sent to re-start signal
|
47 | #define wait() while(!(TWCR & (1<<TWINT))) //Waiting for the signal
|
48 | #define state() (TWSR&0XF8) //The register TWSR state extraction
|
49 | #define stop() TWCR=((1<<TWEN)|(1<<TWINT)|(1<<TWSTO)) //Stop signal
|
50 | #define twi() (TWCR=(1<<TWINT)|(1<<TWEN)) //Open the TWI function
|
51 | #define setnoAck() (TWCR&=~(1<<TWEA)) //Make Not Ack response
|
52 |
|
53 | #define r_direction 0xD1 //Read the device address
|
54 | #define w_direction 0xD0 //Write device address
|
55 |
|
56 | void twi_init(void) //TWI initialization
|
57 | {
|
58 | TWCR |=(1<<TWEN); //Start twi function
|
59 | TWBR=0X20; //Set the baud rate for the host mode
|
60 | TWSR=0; //Clear Status Register
|
61 | }
|
62 |
|
63 | void writebyte(uchar temp)//A byte data write to the TWI
|
64 | {
|
65 | TWDR=temp;
|
66 | TWCR=((1<<TWINT)|(1<<TWEN));
|
67 | }
|
68 | uchar TWI_Write(uchar addr,uchar dat) //Write data to TWI
|
69 | {
|
70 | start();//start signal
|
71 | wait();//Start TWI function
|
72 | if(state()!=START)//Wait for the nack/ack() signal
|
73 | return 1;
|
74 |
|
75 | writebyte(w_direction);//write Device address
|
76 | wait();
|
77 | if(state()!=MT_SLA_ACK)
|
78 | return 1;
|
79 |
|
80 | writebyte(addr);//write Byte address
|
81 | wait();
|
82 | if(state()!=MT_DAT_ACK)
|
83 | return 1;
|
84 |
|
85 | writebyte(dat);//write data
|
86 | wait();
|
87 | if(state()!=MT_DAT_ACK)
|
88 | return 1;
|
89 |
|
90 | stop();//stop signal
|
91 | return 0;
|
92 | }
|
93 |
|
94 | uchar TWI_Read(uchar addr) //Read data from TWI
|
95 | {
|
96 | uchar temp=0;
|
97 | start();//
|
98 | wait();
|
99 | if(state()!=START)
|
100 | return 1; //ACK*/
|
101 |
|
102 | writebyte(w_direction); //Write TWI slave address and write
|
103 | wait();
|
104 | if(state()!=MT_SLA_ACK)
|
105 | return 1; //ACK*/
|
106 |
|
107 | writebyte(addr); //Write the corresponding device register address
|
108 | wait();
|
109 | if(state()!=MT_DAT_ACK)
|
110 | return 1;//*/
|
111 |
|
112 | start(); //TWI re-start
|
113 | wait();
|
114 | if (state()!=RESTART)
|
115 | return 1;//*/
|
116 |
|
117 | writebyte(r_direction); //Write TWI slave device address and read
|
118 | wait();
|
119 | if(state()!=SL_SLA_ACK)
|
120 | return 1; //ACK*/
|
121 |
|
122 | twi(); //Start the TWI read
|
123 | wait();
|
124 | if(state()!=SL_DAT_NACK)
|
125 | return 1; //ACK*/
|
126 |
|
127 | temp=TWDR;//Read the TWI of the receive data
|
128 | stop();//
|
129 | return temp;
|
130 |
|
131 |
|
132 | }
|
133 | /////////////////////////////////////////
|
134 | //////////////////////////////////////////
|
135 | uchar Decimal_to_BCD(uchar temp)//Decimal converted to BCD code
|
136 | {
|
137 | uchar a,b,c;
|
138 | a=temp;
|
139 | b=0x00;
|
140 | if(a>=10)
|
141 | {
|
142 | while(a>=10)
|
143 | {
|
144 | a=a-10;
|
145 | b=b+16;
|
146 | c=a+b;
|
147 | temp=c;
|
148 | }
|
149 | }
|
150 | return temp;
|
151 | }
|
152 |
|
153 | uchar BCD_to_Decimal(uchar temp)//BCD to decimal
|
154 | {
|
155 | uchar a,b,c;
|
156 | a=temp;
|
157 | b=0x00;
|
158 | if(a>=16)
|
159 | {
|
160 | while(a>=16)
|
161 | {
|
162 | a=a-16;
|
163 | b=b+10;
|
164 | c=a+b;
|
165 | temp=c;
|
166 | }
|
167 | }
|
168 | return temp;
|
169 | }
|
170 | ////////////////////////////////////////
|
171 | void delay_2(uint z)//0.1msDelay
|
172 | {
|
173 | uchar x,x1;
|
174 | for(x1=0;x1<z;x1++)
|
175 | {
|
176 | for(x=0;x<114;x++);
|
177 | }
|
178 | }
|
179 |
|
180 | void delay_1(void)//0.1msDelay
|
181 | {
|
182 | uchar x,x1;
|
183 | for(x1=0;x1<2;x1++)
|
184 | {
|
185 | for(x=0;x<100;x++);
|
186 | }
|
187 | }
|
188 |
|
189 | void write_ds1307(void)//write ds1307
|
190 | {
|
191 | //Write a data to the device: (0xd0 is device write address; 0x01 ~ 06 are seconds to years register; Seconds ~ Year, data to be written)//
|
192 | Seconds=Decimal_to_BCD(Seconds);//Decimal converted to BCD code
|
193 | Minutes=Decimal_to_BCD(Minutes);//
|
194 | Hours=Decimal_to_BCD(Hours);//
|
195 |
|
196 | Date=Decimal_to_BCD(Date);//
|
197 | Year=Decimal_to_BCD(Year);//
|
198 | Month=Decimal_to_BCD(Month);//
|
199 |
|
200 | TWI_Write(0x00,Seconds);//write Seconds
|
201 | TWI_Write(0x01,Minutes);//write Minutes
|
202 | TWI_Write(0x02,Hours);//write Hours
|
203 |
|
204 | TWI_Write(0x03,Weeks);//write Weeks
|
205 |
|
206 | TWI_Write(0x04,Date);//write Data
|
207 | TWI_Write(0x05,Month);//write Month
|
208 | TWI_Write(0x06,Year);//write Year
|
209 | }
|
210 |
|
211 | void read_ds1307(void)//read ds1307
|
212 | {
|
213 | //Read a data to the device: (0xd0 is the DS1307 device write address; 0x01 ~ 06 are seconds to years register; 0xd1 is DS1307 device read address)
|
214 | Seconds=TWI_Read(0x00);
|
215 | Seconds=BCD_to_Decimal(Seconds);//BCD code converted to decimal
|
216 |
|
217 | Minutes=TWI_Read(0x01);
|
218 | Minutes=BCD_to_Decimal(Minutes);//
|
219 |
|
220 | Hours=TWI_Read(0x02);
|
221 | Hours=BCD_to_Decimal(Hours);//
|
222 |
|
223 | Weeks=TWI_Read(0x03);//
|
224 |
|
225 | Date=TWI_Read(0x04);
|
226 | Date=BCD_to_Decimal(Date);//
|
227 |
|
228 | Month=TWI_Read(0x05);
|
229 | Month=BCD_to_Decimal(Month);//
|
230 |
|
231 | Year=TWI_Read(0x06);
|
232 | Year=BCD_to_Decimal(Year);//
|
233 | }
|
234 |
|
235 | int main(void)
|
236 | {
|
237 | DDRA = 0x77;
|
238 | DDRB = 0x77;
|
239 |
|
240 | DDRC = 0x00;
|
241 | PORTC = 0x03;
|
242 |
|
243 | twi_init();//TWI Initialization
|
244 |
|
245 | //write_ds1307();//Write the initial value on ds1307//(Modification time initial value when used)
|
246 | while(1)
|
247 | {
|
248 | read_ds1307();
|
249 |
|
250 | if(Seconds & 1)
|
251 | {
|
252 | PORTA = 0x07;
|
253 | }
|
254 | else
|
255 | {
|
256 | PORTA = 0x00;
|
257 | }
|
258 |
|
259 | for(uint16_t i = 0x0FFF; i; i--);
|
260 | }
|
261 | }
|