#include <mega128.h>
#include <delay.h>
#define LCD_RS PORTA.0
#define LCD_RW PORTA.1
#define LCD_EN PORTA.2
#define LCD_Data PORTA
int count_2msec = 0;
unsigned char msec = 0;
unsigned char sec = 50;
unsigned char minute = 59;
unsigned char hour = 23;
void LCD_E(void)
{
LCD_EN = 1;
delay_us(250);
LCD_EN = 0;
}
void LCD_Command(unsigned char rs, unsigned char rw, unsigned char data)
{
LCD_RS = rs;
LCD_RW = rw;
LCD_Data = ((LCD_Data & 0x0f) | (data & 0xf0));
LCD_E();
LCD_Data = ((LCD_Data & 0x0f) | ((data<<4) & 0xf0));
LCD_E();
delay_ms(2);
}
void LCD_Init(void)
{
delay_ms(30);
LCD_Command(0,0,0x20);
delay_ms(10);
LCD_Command(0,0,0x20);
delay_us(200);
LCD_Command(0,0,0x20);
LCD_Command(0,0,0x20);
LCD_Command(0,0,0x28);
LCD_Command(0,0,0x0C);
LCD_Command(0,0,0x01);
LCD_Command(0,0,0x06);
}
void LCD_Position(unsigned char x,unsigned char y)
{
if(y==0) LCD_Command(0,0,0x80+x);
else LCD_Command(0,0,0xC0+x);
}
void LCD_string(char x, char y, char flash *str)
{
LCD_Position(x,y);
while(*str) LCD_Command(1,0,*str++);
}
void main(void)
{
DDRC = 0xff;
PORTC = 0xff;
DDRA = 0xff;
LCD_Init();
TIMSK = 0x05; // timer_counter_0, 1 mask
TCCR0 = 0x05; // 128 분주, 0.008msec
TCNT0 = 6; // 250 개
TCCR1B = 0x05; // 1024 분주
TCNT1H = 0xE1;
TCNT1L = 0x7C;
LCD_string(0,0," = CLOCK! = ");
LCD_string(0,1,"xx] xx:xx:xx:xx ");
#asm("sei")
while (1){
LCD_Position(0,1);
if( hour >= 12 )LCD_Command(1,0,'P');
else LCD_Command(1,0,'A');
LCD_Position(1,1);
LCD_Command(1,0,'M');
LCD_Position(4,1);
LCD_Command(1,0,(hour/10)+0x30);
LCD_Command(1,0,(hour%10)+0x30);
LCD_Position(7,1);
LCD_Command(1,0,(minute/10)+0x30);
LCD_Command(1,0,(minute%10)+0x30);
LCD_Position(10,1);
LCD_Command(1,0,(sec/10)+0x30);
LCD_Command(1,0,(sec%10)+0x30);
LCD_Position(13,1);
LCD_Command(1,0,(msec/10)+0x30);
LCD_Command(1,0,(msec%10)+0x30);
if( count_2msec < 250 ){
LCD_Position(6,1); LCD_Command(1,0,':');
LCD_Position(9,1); LCD_Command(1,0,':');
LCD_Position(12,1); LCD_Command(1,0,':');
}
else {
LCD_Position(6,1); LCD_Command(1,0,' ');
LCD_Position(9,1); LCD_Command(1,0,' ');
LCD_Position(12,1); LCD_Command(1,0,' ');
}
}
}
interrupt [TIM0_OVF] void timer_0 (void)
{
TCNT0 = 6;
count_2msec++;
msec=count_2msec*5;
if( count_2msec >= 500 ) {
count_2msec = 0;
sec++;
PORTC.0 = ~PORTC.0;
}
if( sec >= 60 ){
sec = 0;
minute++;
}
if( minute >= 60 ){
minute = 0;
hour++;
}
if( hour >= 24 ) hour = 0;
}
interrupt [TIM1_OVF] void timer_1 (void)
{
TCNT1H = 0xE1;
TCNT1L = 0x7C;
PORTC.7 = ~PORTC.7;
}
'IT > CodeVision atmega128' 카테고리의 다른 글
atmega128을 이용한 초음파센서 동작 (0) | 2022.07.17 |
---|---|
ATmega128을 이용한 문자 'C' 표시하기 (0) | 2022.07.14 |
댓글