/**************************(^-^)START(^-^)**************************/ #include <mega16.h>
#include <delay.h> // Khai bao PORT I2C
Bắt Đầu
Giải mã Led 7 thanh
Tạo thời gian trễ& quét hiển thị
Khởi tạo I2C&DS1307
Xuất mã hiển thị cho LED & thanh
Thời gian trễ 2ms Quét qua từng LED Tăng giây Giây >59 Phút>59 Giờ >23 Xóa giây Tăng phút Xóa phút Tăng giờ Xóa giờ
#asm
.equ __i2c_port=0x18 ; // Cho phep PORTB doc du lieu tu DS1307 .equ __sda_bit=1 ; // Bit PORTB.1 la dau vao chan SDA cua DS1307 .equ __scl_bit=0 ; // Bit PORTB.1 la dau vao chan SCL cua DS1307 #endasm
#include <i2c.h> //Khai bao ham I2C trong thu vien Codevision
#include <ds1307.h> //Khai bao thu vien DS1307 trong thu vien Codevision //Dinh nghia cac chan dieu khien LED
#define led1 PORTC.0 #define led2 PORTC.1 #define led3 PORTC.2 #define led4 PORTC.3 #define led5 PORTC.4 #define led6 PORTC.5
//Dinh nghia cac chan vao cua phim nhan #define set PIND.0
#define up PIND.1 #define down PIND.2
//Cac bit de quan ly trang thai cac phim
bit set0=1,set1=1,up0=1,up1=1,down0=1,down1=1; //Ma Hex led 7 thanh
unsigned char code[]={0x40,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; unsigned char h=15,m=45,s=55;//Bien gio phut giay
unsigned char kt=0,a=0,x=0;
/*Bien kt de kiem tra xem dang o che do nao - kt=0 :Che do chay binh thuong
- kt=1 :Che do cai dat gio - kt=2 :Che do cai dat phut - kt=3 :Che do cai dat giay
-Bien a de kiem tra xem truoc do o che do cai dat nao khong - a=0 :Truoc do khong cai dat gi
- a=1 :Truoc do la o che do cai dat -->Dat lai du lieu cho DS1307*/ //Ham con void Kt_phim(); void Read_DS1307(); void Hien_thi(); void main(void) {
//Cho phep PORTA xuat du lieu PORTA=0xFF;
DDRA=0xFF;
//Cho phep PORTB nhan tin hieu vao PORTB=0x00;
DDRB=0x00;
//Cho phep PORTC xuat du lieu PORTC=0x00;
DDRC=0xFF;
//Cho phep PORTD nhan tin hieu vao PORTD=0xFF;
DDRD=0x00; // Khoi tao I2C i2c_init();
//Khoi tao DS1307 rtc_init(0,0,0); while (1) {
x++;//Bien chay quan ly nhap nhay cac so tuong ung voi che do cai dat Kt_phim(); Read_DS1307(); Hien_thi(); if(x==100){x=0;} }; }
//Ham kiem tra cai dat va doc du lieu tu DS1307 void Read_DS1307()
{
if(kt==0) {
if(a==1) //Kiem tra truoc do co cai dat lai gio,phut,giay khong { //Neu co thi cai dat lai du lieu dau vao cho DS1307 rtc_set_time(h,m,s);a=0;
delay_us(300); }
rtc_get_time(&h,&m,&s); //Doc du lieu tu DS1307 }
}
//Ham quet LED void Hien_thi() {
/*Kiem tra xem dang o che do nao de hien thi
- Neu kt=0:Che do chay binh thuong-->hien thi gio binh thuong - Neu kt khac 1 dang o che do cai dat khac-->hien thi gio binh thuong
- Neu kt=1 dang o che do cai dat gio-->Hien thi gio nhap nhay */ if((kt==0)||(kt!=1)||((kt==1)&&(x<50))) { PORTA=code[h/10];led1=1;delay_us(2000);led1=0; PORTA=code[h%10];led2=1;delay_us(2000);led2=0; }
if((kt==0)||(kt!=2)||((kt==2)&&(x<50))) //Tuong tu nhu KT gio {
PORTA=code[m/10];led3=1;delay_us(2000);led3=0; PORTA=code[m%10];led4=1;delay_us(2000);led4=0; }
if((kt==0)||(kt!=3)||((kt==3)&&(x<50)))//Tuong tu nhu KT gio {
PORTA=code[s/10];led5=1;delay_us(2000);led5=0; PORTA=code[s%10];led6=1;delay_us(2000);led6=0; }
}
//Ham kiem tra phim void Kt_phim() {
set0=set1;set1=set; //Kiem tra trang thai ban dau cua phim
if((set0==1)&&(set1==0)) //Kiem tra xem phim co dc nhan nha hay khong { //Neu dc nhan nha thi tang kt++,neu khong thi kt++;if(kt>3)kt=0; //doi toi khi nao phim dc nha moi tang bien kt }
switch(kt) //Lua chon che do ung voi gia tri cua bien kt {
case 0:break; //Tro lai che do chay binh thuong case 1: //Che do cai dat gio
{ up0=up1;up1=up;a=1; if((up0==1)&&(up1==0)) { h++;if(h>23)h=0; } down0=down1;down1=down;// if((down0==1)&&(down1==0)) { h--;if(h>23)h=23;
} break; }
case 2: //Che do cai dat phut { up0=up1;up1=up;a=1; if((up0==1)&&(up1==0)) { m++;if(m>59)m=0; } down0=down1;down1=down; if((down0==1)&&(down1==0)) { m--;if(m>59)m=59; } break; }
case 3: //Che do cai dat giay { up0=up1;up1=up;a=1; if((up0==1)&&(up1==0)) { s++;if(s>59)s=0; } down0=down1;down1=down; if((down0==1)&&(down1==0)) { s--;if(s>59)s=59; } break; } } } /******************** (^-^) THE END (^-^)************************/