반응형

/* Atmega128 mtx128-s2보드와 ds3231 rtc모듈, char LCD wc0802C 제품으로 시계를 만들었다.

기능들을 함수로 빼놓았기 때문에 나중에 회로도와 핀맵만 맞는다면 사용할 수 있다.

회로도 그리기 너무 귀찮다아아

*/


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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * char_lcd.c
 *
 * Created: 2019-01-21 오후 4:25:21
 * Author : admin
 */
 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include <stdio.h>
 
#define PORT_DATA PORTD
#define PORT_CONTROL PORTC
#define DDR_DATA DDRD
#define DDR_CONTROL DDRC
 
#define RS_PIN 0
#define RW_PIN 1
#define E_PIN 2
 
#define COMMAND_CLEAR_DISPLAY 0x01
#define COMMAND_8_BIT_MODE 0x38
#define COMMAND_4_BIT_MODE 0x28
 
#define COMMAND_DISPLAY_ON_OFF_BIT 2
#define COMMAND_CURSOR_ON_OFF_BIT 1
#define COMMAND_BLINK_ON_OFF_BIT 0
 
extern uint8_t MODE;
 
void LCD_Pulse_enable();
void LCD_write_data(uint8_t data);
void LCD_write_command(uint8_t command);
void LCD_clear();
void LCD_init();
void LCD_write_string(char *string);
void LCD_goto_XY(uint8_t row, uint8_t col);
//////////////////////////////////////////////////////////
 
void LCD_Pulse_enable(){
    PORT_CONTROL |= (1<<E_PIN);
    _delay_us(1);
    PORT_CONTROL &= ~(1<<E_PIN);
    _delay_us(1);
}
void LCD_write_data(uint8_t data){
    PORT_CONTROL |= (1<<RS_PIN);
    if(MODE == 8){
        PORT_DATA = data;
        LCD_Pulse_enable();
    }
    else{
        PORT_DATA = data& 0xf0;
        LCD_Pulse_enable();
        PORT_DATA = (data<<4)&0xf0;
        LCD_Pulse_enable();
    }
    _delay_ms(2);
}
void LCD_write_command(uint8_t command){
    PORT_CONTROL&= ~(1<<RS_PIN);
    
    if(MODE ==8){
        PORT_DATA =command;
        LCD_Pulse_enable();
    }
    else {
        PORT_DATA = command & 0xf0;
        LCD_Pulse_enable();
        PORT_DATA = (command <<4& 0xf0;
        LCD_Pulse_enable();
    }
    _delay_ms(2);
}
void LCD_clear(){
    LCD_write_command(COMMAND_CLEAR_DISPLAY);
    _delay_ms(2);
}
void LCD_init(){
    _delay_ms(50);
    if(MODE == 8){
        DDR_DATA =0xff;
    }
    else DDR_DATA |= 0xf0;
    PORT_DATA = 0x00;
    DDR_CONTROL |= (1<<RS_PIN)|(1<<RW_PIN)|(1<<E_PIN);
    
    PORT_CONTROL &= ~(1<<RW_PIN);
    
    if(MODE == 8){
        LCD_write_command(COMMAND_8_BIT_MODE);
    }
    else{
        LCD_write_command(0x02);
        LCD_write_command(COMMAND_4_BIT_MODE);
    }
    
    uint8_t command = 0x08|(1<<COMMAND_DISPLAY_ON_OFF_BIT);
    LCD_write_command(command);
    
    LCD_clear();
    
    LCD_write_command(0x06);
}
void LCD_write_string(char *string){
    uint8_t i;
    for(i=0;string[i];i++){
        LCD_write_data(string[i]);
    }
}
void LCD_goto_XY(uint8_t row, uint8_t col){
    col %=16;
    row%=2;
    
    uint8_t address = (0x40*row)+col;
    uint8_t command =0x80+address;
    
    LCD_write_command(command);
}
 
uint8_t MODE = 4;
 
#define I2C_SCL PD0
#define I2C_SDA PD1
 
unsigned int sec, min, hour, day, month, year, week;
 
 
void uartInit(long buadrate)
{
    UCSR0A = 0x00// ready flag clear
    UCSR0B = 0x18// rx, tx enable
    UCSR0C = 0x06// tx data len : 8bit
    UBRR0H = 0;
    switch(buadrate){
        case 115200:
        UBRR0L = 8;
        break;
        
        case 57600:
        UBRR0L = 16;
        break;
        
        case 38400:
        UBRR0L = 25;
        break;
        
        case 19200:
        UBRR0L = 51;
        break;
        
        case 14400:
        UBRR0L = 68;
        break;
        
        case 9600:
        UBRR0L = 103;
        break;
        
        // Default 19200
        default:
        UBRR0L = 51;
        break;
    }
}
void send_data(unsigned char data)
{
    while(!(UCSR0A&0x20)); // 송신데이터를 받을 준비가 될때까지 대기
    UDR0 = data;
}
void SendLine(char *string)
{
    while(*string != '\0')
    {
        send_data(*string);
        string++;
    }
}
 
void init_i2c(){
    DDRD |= (1<<I2C_SCL);
    DDRD |= (1<<I2C_SDA);
    
    TWBR = 32// 200khz
}
void i2c_start(){
    TWCR = (1<< TWINT)|(1<<TWSTA)|(1<<TWEN);
    while(!(TWCR&(1<<TWINT))); // 시작완료 대기
}
void i2c_transmit(uint8_t data){
    TWDR = data;
    TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
    while(!(TWCR&(1<<TWINT))); // 전송완료 대기
}
uint8_t i2c_receive_ACK(){
    TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
    while(!(TWCR&(1<<TWINT)));
    return TWDR;
}
uint8_t i2c_receive_NACK(){
    TWCR = (1<<TWINT)|(1<<TWEN);
    while(!(TWCR&(1<<TWINT))); // 수신완료 대기
    return TWDR;
}
void i2c_stop(){
    TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
    _delay_us(100);
}
 
uint8_t bcd_to_decimal(uint8_t bcd){
    return (bcd>>4*10 +(bcd&0x0f);
}
uint8_t decimal_to_bcd(uint8_t decimal){
    return (((decimal/10)<<4)|(decimal%10));
}
 
int main(void)
{    
    LCD_init();
    char lcd_buff[100];
    init_i2c();
    
    sec = min = hour = day = month = year = week= 0;
    char str[1000];
    
    uint8_t address = 0x68;
    // 초 분 시 요일 일 월 연
    uint8_t date[] = {0,10,12,2,10,10,19};
    
    
    i2c_start();
    i2c_transmit(address <<1);
    i2c_transmit(0);
    for (int i =0; i<7;i++){
        i2c_transmit(decimal_to_bcd(date[i]));
    }
    i2c_stop();
    DDRA = 0xff;
    PORTA = 0x00;
    
    while (1
    {
        i2c_start();
        i2c_transmit(address <<1);
        i2c_transmit(0);
        i2c_stop();
        
        i2c_start();
        i2c_transmit((address<<1)+1);
        
        sec = bcd_to_decimal(i2c_receive_ACK());
        min = bcd_to_decimal(i2c_receive_ACK());
        hour = bcd_to_decimal(i2c_receive_ACK());
        week = bcd_to_decimal(i2c_receive_ACK());
        day = bcd_to_decimal(i2c_receive_ACK());
        month = bcd_to_decimal(i2c_receive_ACK());
        year = bcd_to_decimal(i2c_receive_NACK());
        
        i2c_stop();
        
        LCD_goto_XY(0,0);
        sprintf(lcd_buff,"%d/%d/%d",year,month,day);
        LCD_write_string(lcd_buff);
        LCD_goto_XY(1,0);
        sprintf(str,"%d:%d:%d",hour,min,sec);
        LCD_write_string(str);
        
        /* LCD example
        LCD_goto_XY(1,1);
        LCD_write_string("hi");
        */
        
        //sprintf(lcd_buff,"%d/%d/%d",year,month,day);
        //sprintf (str, "%d sec, %d min, %d hour, %d week, %d day, %d month, %d year\r\n",sec,min,hour,week,day,month,year);
        //sprintf(str,"%d sec\r\n",sec);
        
    }
    return 0;
}
cs


반응형

+ Recent posts