Với phương châm vừa học hỏi vừa chia sẽ, tham gia dd đã lâu nhưng mình vẫn chưa đóng góp gì.
Nay mình vừa có chúc thời gian rảnh với chúc kiến thức tích lũy từ trước đến giờ(từ khi bắt đầu
đam mê điện tử nhất là VDK). Nay mình xin giới thiệu cái project nhỏ về led của mình cho mọi người
nếu ai có thích có thể lấy về làm theo nhé.
Thứ I project của mình có sự tích lũy từ anh chung 1608, thanks anh nhìu.
Thứ II là cách cấp nguồn led có làm theo cách cấp nguồn của hanhcd, thanks nhìu
Sau đây là code của mình, thật sự cái này cũng như là lập trình cho Matrix thôi nhưng $ pay thì lại vô
cùng nhỏ so với làm một cái Matrix cùng cở nhưng bị cái là sẽ có tiếng ồn tí và hơi bị giật nhưng có thể chấp
nhận được.
Đầu tiên là file main.c Code này đọc coi chừng bị tẩu hảo.
Những chổ màu đỏ là có liên quan đến hiệu ứng trôi số đó nhé.
Tiếp theo là file Font.H
File ds1307.H
Và cuối cùng là file hổ trợ giao tiếp chuẩn I2C DS1307.C
Và đây là sản phẩm mới hoàn thành đây.
File mạch in đây.
hardware vs 00.PDF
Nay mình vừa có chúc thời gian rảnh với chúc kiến thức tích lũy từ trước đến giờ(từ khi bắt đầu
đam mê điện tử nhất là VDK). Nay mình xin giới thiệu cái project nhỏ về led của mình cho mọi người
nếu ai có thích có thể lấy về làm theo nhé.
Thứ I project của mình có sự tích lũy từ anh chung 1608, thanks anh nhìu.

Thứ II là cách cấp nguồn led có làm theo cách cấp nguồn của hanhcd, thanks nhìu

Sau đây là code của mình, thật sự cái này cũng như là lập trình cho Matrix thôi nhưng $ pay thì lại vô
cùng nhỏ so với làm một cái Matrix cùng cở nhưng bị cái là sẽ có tiếng ồn tí và hơi bị giật nhưng có thể chấp
nhận được.
Đầu tiên là file main.c Code này đọc coi chừng bị tẩu hảo.

Những chổ màu đỏ là có liên quan đến hiệu ứng trôi số đó nhé.
Code:
#include "AT89x52.H"
#include "DS1307.H"
#include "FONT.H"
#define Num_ASII 32
#define COLUM 120
#define SPEED 1
#define shiftleftbyte(a,b,c) {a<<=1;if(b&c)a|=1;}
unsigned int Time,Time_on,static_i_1,i_mid;
extern unsigned char hour,min,sec,date,day,month,year;
unsigned char Colum,num_clock,l_clock;
unsigned char Menu,Static_Menu;
char num_i_clock=-1;
unsigned char Count,Speed,static_i_0,static_i_2;
bit Clock_tick_bit,Start_tt,Start,Run_Stand,ok_next=1, bit_save,Next,bit_do,bit_display,bit_big;
bit bit_left,bit_menu;
unsigned char n_left,byte_and;
idata struct MATRIX{
unsigned char hour_ten[lengh_font];
unsigned char hour_isl[lengh_font];
unsigned char min_ten[lengh_font];
unsigned char min_isl[lengh_font];
unsigned char sec_ten[lengh_font];
unsigned char sec_isl[lengh_font];
unsigned char Time_Save[8];
unsigned char Date_Save[15];
};
idata struct MATRIX Matrix;
// byte for display
unsigned char port_0,port_1;
code unsigned char code_date[7][3]={
"SUN",
"MON",
"TUE",
"WED",
"THU",
"FRI",
"SAT"
};
code unsigned char code_month[12][3]={
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC"
};
code unsigned char code_rr[]={
0xF0,0x1F,0xF8,0x3F,0xFC,0x7F,0xFE,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,// step 1
0xFF,0xFF,0xF0,0x1F,0xF8,0x3F,0xFC,0x7F,0xFE,0xFF, 0xFF,0xFF,0xFF,0xFF,// step 2
0xFF,0xFF,0xFF,0xFF,0xF0,0x1F,0xF8,0x3F,0xFC,0x7F, 0xFE,0xFF,0xFF,0xFF // step 3
};
code unsigned char code_ll[]={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0xFC,0x7F, 0xF8,0x3F,0xF0,0x1F,// step 1
0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0xFC,0x7F,0xF8,0x3F, 0xF0,0x1F,0xFF,0xFF,// step 2
0xFF,0xFF,0xFE,0xFF,0xFC,0x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,0x3F,0xFF,0xFF,// O
0xF0,0x1F,0xF0,0x1F,0xF9,0xFF,0xFC,0xFF,0xF0,0x1F, 0xF0,0x1F,0xFF,0xFF,// N
0xF3,0xFF,0xF7,0xDF,0xF0,0x1F,0xF0,0x1F,0xF7,0xDF, 0xF3,0xFF,0xFF,0xFF,// T
0xF0,0x1F,0xF0,0x1F,0xFE,0xFF,0xFE,0xFF,0xF0,0x1F, 0xF0,0x1F,0xFF,0xFF,// H
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,// space
0xF3,0xFF,0xF9,0x9F,0xFC,0x1F,0xFC,0x3F,0xF9,0xFF, 0xF3,0xFF,0xFF,0xFF,// Y
0xF0,0x1F,0xF0,0x1F,0xF2,0x9F,0xF2,0x9F,0xF3,0x9F, 0xF3,0x9F,0xFF,0xFF,// E
0xFC,0x1F,0xF8,0x1F,0xF6,0xFF,0xF6,0xFF,0xF8,0x1F, 0xFC,0x1F,0xFF,0xFF,// A
0xF0,0x1F,0xF0,0x1F,0xF6,0x7F,0xF6,0x3F,0xF0,0x9F, 0xF9,0xDF,0xFF,0xFF,// R
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF // space
};
code Tick_min[]={
0xFE,0x7F,0xFC,0xFE,0xF8,0x00,0xFC,0xFE,0xFE,0x7F
};
code Tick_hour[]={
0xFF,0x9F,0xFF,0x3E,0xFE,0x00,0xFF,0x3E,0xFF,0x9F
};
/*
code unsigned char code_JVC_RM-C1171_remoteTV_not[]={
// ADD : Command
0x81 0x8B // button Power
0x81 0xDA // button Sound
0x81 0xC9 // button Colour
0x81 0xBD // button Menu
0x81 0xBE // button Up
0x81 0x89 // button TV/VIDEO
0x81 0xAD // button left
0x81 0x85 // button Ok
0x81 0xAD // button right
0x81 0x82 // button Display
0x81 0x8E // button Muting =>not = 0111 1110 : 0111 0001 = DEC 239
0x81 0xBD // button down
0x81 0xBC // button DVD menu =>not = 0111 1110 : 0100 0001= DEC 191
0x81 0x8C // button Chanel -
0x81 0x8C // button Chanel +
0x81 0x8F // button Volume -
0x81 0x8F // button Volume +
.....
};
*/
void Delay_560us();
void init();
void IR_active_0();
void IR_active_1();
void Timer_init();
void Timer1();
void Clock();
void Save_Time();
void Save_date();
void Setting();
void Clock_tick();
void main(){
init();Timer_init();
DS_Init(); // init ds1307
Update_time(); // update hour-min-sec
Update_date(); // update date-day-month-year
Save_Time();Save_date();
while(1){
if(!Start_tt){while(!Start_tt){if(Start_tt){bit_me nu=1;Update_date();Save_date();}}}
if(Start&&Start_tt&&ok_next){
if(bit_save){bit_save=0;bit_menu=1;Save_Newtime(); }
if(!Menu){if(Clock_tick_bit)Clock_tick(); else Clock();}
else Setting();
Time=0; // clear time
if(ok_next){
Start=0;// clear bit
loop:
if(!Start){Time++;if(Time==0xFFFF){// reset
Time=0;Start_tt=0;init();
num_clock=0;l_clock=COLUM/2;num_i_clock=-1;
EX1=0; // disabbe interrupt 1
Next=1;
TR1=0;
P1=P0=0xFF;
}
else goto loop;} // count time&&stop program
}
}
}
}
void delay_560us(){
TMOD = 0x11; //timer1 & timer0 in mode1 16 bit
//560us 24Mhz
TL0 = 0xA0; //Lower Byte
TH0 = 0xFB; //Upper Byte
// 560us 27MHz
//TL0=0x14;
//TH0=0xFB;
TR0 = 1; // start timer 0
while(!TF0); // wail for timer full
TR0 = 0; // stop timer 0
TF0 = 0; // clear flag timer 0
}
bit while_P3_3_in_ir(){
unsigned char n=0;
bit true=0;
step_1: n++;delay_560us();
if(n>10){true = 0;goto step_2;}
if(P3_3) goto step_1;
if(n<10)true = 1;
step_2:return(true);
}
void IR_active_1() interrupt IE1_VECTOR{
unsigned int byte;
unsigned char num,n,Add,Command;
Next=0; // off led in cycle
P0=P1=0xFF; // led off
// in bit START
while(!P3_3);// while 0 in 16T ~ 9ms , T =560us,16T = 8960us
// in bit SYNC
// while 1 in 7,5T ~ 4,3ms ,7,5*T = 4200us//
while(P3_3){if(!while_P3_3_in_ir())goto Exit;}// while 1
// in ADDRESS BIT
Add=0;Command=0;
for(n=0;n<16;n++){
while(!P3_3); // while 0
// wail in time = (2/3)*2T = 747us; ~ 800us - 510us<time<1100us
delay_560us();
// bit 0
if(!P3_3){
if(n<8){Add|=0x80;Add>>=1;}
else {Command|=0x80;Command>>=1;}
}
// bit 1
else {
if(n<8){Add|=0x00;Add>>=1;}
else {Command|=0x00;Command>>=1;}
while(P3_3){if(!while_P3_3_in_ir())goto Exit;}// while 1
}
}
byte = Add+Command;
switch (byte){
// 0x81 0xDA // button Sound
case 163 : bit_big=!bit_big;
break;
// 0x81 0x8C // button Chanel -
case 241 : Time--;// up delay
break;
// 0x81 0x8F // button Volume +
case 238 : Time++;// up delay
break;
// button Muting for stand or run
case 239 : if(!Menu)Run_Stand =!Run_Stand;
break;
// button DVD menu for menu setting
case 193 : if(Menu!=0){Menu=0;bit_menu=1;}
else {Menu=1;Static_Menu=1;static_i_1=0;
}
break;
// button >> for change menu setting
case 208 : if(Menu>0)Menu++;
//Save_in_buffer_setting();
break;
// button 0k for save new setting
case 248 : if(Menu!=0){bit_save=1;Menu=0;}
break;
// button up for ++ Time
case 191 : if(Menu==1){num= ((hour>>4)*10)+(hour&0x0F);
if(num<23)num++;else num=0;
hour=num/10;hour<<=4;
hour+=num%10;
}
else if(Menu==2){num= ((min>>4)*10)+(min&0x0F);
if(num<59)num++;else num=0;
min=num/10;min<<=4;
min+=num%10;
}
else if(Menu==3){num= ((sec>>4)*10)+(sec&0x0F);
if(num<59)num++;else num=0;
sec=num/10;sec<<=4;
sec+=num%10;
}
else if(Menu==4){if(++date>7)date=1;Save_date();}
else if(Menu==5){num= ((day>>4)*10)+(day&0x0F);
if(num<31)num++;else num=0;
day=num/10;day<<=4;
day+=num%10;
}
else if(Menu==6){num= ((month>>4)*10)+(month&0x0F);
if(num<13)num++;else num=0;
month=num/10;month<<=4;
month+=num%10;Save_date();
}
else if(Menu==7){num= ((year>>4)*10)+(year&0x0F);
if(num<99)num++;else num=0;
year=num/10;year<<=4;
year+=num%10;
}
//Save_in_buffer_setting();
//bit_left_setting=1;
break;
// button down for ++ Time
case 192 : if(Menu==1){num= ((hour>>4)*10)+(hour&0x0F);
if(num>0)num--;else num=23;
hour=num/10;hour<<=4;
hour+=num%10;
}
else if(Menu==2){num= ((min>>4)*10)+(min&0x0F);
if(num>0)num--;else num=59;
min=num/10;min<<=4;
min+=num%10;
}
else if(Menu==3){num= ((sec>>4)*10)+(sec&0x0F);
if(num>0)num--;else num=59;
sec=num/10;sec<<=4;
sec+=num%10;
}
else if(Menu==4){if(--date<1)date=7;Save_date();}
else if(Menu==5){num= ((day>>4)*10)+(day&0x0F);
if(num>1)num--;else num=31;
day=num/10;day<<=4;
day+=num%10;
}
else if(Menu==6){num= ((month>>4)*10)+(month&0x0F);
if(num>1)num--;else num=12;
month=num/10;month<<=4;
month+=num%10;Save_date();
}
else if(Menu==7){num= ((year>>4)*10)+(year&0x0F);
if(num>1)num--;else num=99;
year=num/10;year<<=4;
year+=num%10;
}
//Save_in_buffer_setting();
//bit_left_setting=1;
break;
case 251: Clock_tick_bit=!Clock_tick_bit;break;
}
Exit: EX1=0;
}
void init(){
Colum=0;Time_on=0x0000;Run_Stand=1;Next=1;bit_disp lay=1;
n_left=0;byte_and=0x80;
EA=1; // for use interrupt
IT0=1; // External interrupt 0 edge sesitive
PX0=1; // External interrupt 0 high priority
EX0=1; // External interrupt 0 enable
EX1=0; // External interrupt 1 disable
}
void Timer_init(){
TMOD = 0x11; //timer1 & timer0 in mode1 16 bit
ET1=1;
TL1 = 0x00; //Lower Byte
TH1 = 0x00; //Upper Byte
TR1=0; //stop timer1
}
void Timer1() interrupt TF1_VECTOR{
TR1=0;// stop timer1
if(bit_display){
if(bit_do){bit_do=0;
P1=port_1;P0=port_0;}
}//display on
else if(bit_big){P0=0x7F;P1=0xFF;}// display off
bit_display=!bit_display;
TF1=0;// clear flag
TH1=(unsigned char)(Time_on>>8);TL1=(unsigned char)(Time_on&0x00FF);
if(Colum<COLUM)TR1=1; else {P1=0xFF;P0=0x7F;TR1=0;}
}
void IR_active_0() interrupt IE0_VECTOR{
if(!Start_tt){
if(++Count>29){Start_tt=1;Count=0;
EX1=1;//enable interrupt 1
l_clock=COLUM/2;
} // start program
if(Count==28)TR1=1;// count time a cycle
if(Count==29){TR1=0;// get timer1
Time_on=TH1;Time_on<<=8;Time_on|=TL1;
Time_on=Time_on/COLUM;Time_on=65535-((Time_on*2)/3);
}
}
else {
if(!Start){
Start=1;
if(Time>55)Time_on--;
if(Run_Stand)Speed++;
if(!Run_Stand&&Menu!=0)Run_Stand=1;
if(!EX1&&(Static_Menu==Menu||Menu==1||!Menu))EX1=1; // enable interrupt 1 affter resive device ok
if(!Next)Next=1; // on for led on affter IR resive done and next a new cycle
ok_next=1; // realdy for new cycle
TR1=0;// stop timer1
TF1=0;// clear flag of timer1
TH1=(unsigned char)(Time_on>>8);TL1=(unsigned char)(Time_on&0x00FF);
bit_display=1;
}
else if(ok_next){
Time_on++;// up time on display
ok_next=0;// not realdy for cycle new
}
else if(!ok_next&&Start)ok_next=1;
Time=0; // clear time
Colum=0;
}
}
void Save_Time(){
unsigned char n;
// save hour
Matrix.Time_Save[0] = (hour>>4) -14 + Num_ASII/2;
for(n=0;n<lengh_font;n++)Matrix.hour_ten[n]=code_tab[Matrix.Time_Save[0]][n];
Matrix.Time_Save[1] = (hour&0x0F) -14 + Num_ASII/2;
for(n=0;n<lengh_font;n++)Matrix.hour_isl[n]=code_tab[Matrix.Time_Save[1]][n];
// space
//Matrix.Time_Save[2] = ':'; Matrix.Time_Save[2]-=(Num_ASII+14);
Matrix.Time_Save[2] = 12;
// save min
Matrix.Time_Save[3] = (min>>4) -14 + Num_ASII/2;
for(n=0;n<lengh_font;n++)Matrix.min_ten[n]=code_tab[Matrix.Time_Save[3]][n];
Matrix.Time_Save[4] = (min&0x0F) -14 + Num_ASII/2;
for(n=0;n<lengh_font;n++)Matrix.min_isl[n]=code_tab[Matrix.Time_Save[4]][n];
// space
//Matrix.Time_Save[5] = ':'; Matrix.Time_Save[5]-=(Num_ASII+14);
Matrix.Time_Save[5] = 12;
// save sec
Matrix.Time_Save[6] = (sec>>4) -14 + Num_ASII/2;
for(n=0;n<lengh_font;n++)Matrix.sec_ten[n]=code_tab[Matrix.Time_Save[6]][n];
Matrix.Time_Save[7] = (sec&0x0F) -14 + Num_ASII/2;
for(n=0;n<lengh_font;n++)Matrix.sec_isl[n]=code_tab[Matrix.Time_Save[7]][n];
}
void Save_date(){
// save new date
Matrix.Date_Save[0]=code_date[date-1][0];Matrix.Date_Save[0]-=(Num_ASII+20);
Matrix.Date_Save[1]=code_date[date-1][1];Matrix.Date_Save[1]-=(Num_ASII+20);
Matrix.Date_Save[2]=code_date[date-1][2];Matrix.Date_Save[2]-=(Num_ASII+20);
// space
//Matrix.Date_Save[3] = '-'; Matrix.Date_Save[3]-=Num_ASII;
Matrix.Date_Save[3] = 1;
// save new day
Matrix.Date_Save[4] = (day>>4) -14+ Num_ASII/2;
Matrix.Date_Save[5] = (day&0x0F) -14+ Num_ASII/2;
// space
//Matrix.Date_Save[6] = '-'; Matrix.Date_Save[6]-=Num_ASII;
Matrix.Date_Save[6]=1;
// save new month
Matrix.Date_Save[7]=code_month[month-1][0];Matrix.Date_Save[7]-=(Num_ASII+20);
Matrix.Date_Save[8]=code_month[month-1][1];Matrix.Date_Save[8]-=(Num_ASII+20);
Matrix.Date_Save[9]=code_month[month-1][2];Matrix.Date_Save[9]-=(Num_ASII+20);
// space
//Matrix.Date_Save[10] = '-'; Matrix.Date_Save[10]-=Num_ASII;
Matrix.Date_Save[10] = 1;
// save new year
Matrix.Date_Save[11] = 2 -14+ Num_ASII/2;
Matrix.Date_Save[12] = 0 -14+ Num_ASII/2;
Matrix.Date_Save[13] = (year>>4) -14+ Num_ASII/2;
Matrix.Date_Save[14] = (year&0x0F) -14+ Num_ASII/2;
}
void Clock(){
unsigned int n;
unsigned char COLUM_on,byte,byte_0,num=0,i=0;
char _num_,_i_=0;
COLUM_on = (COLUM/2)- ((lengh_font/2)*8);
COLUM_on/=2;
if(l_clock>(COLUM/2)+1)_i_ = num_i_clock;
_num_ = num_clock;
if(bit_menu){ bit_menu=0;Save_Time();
n_left=0;byte_and=0x80;bit_left=0;// reset value
}
Update_time();// update hour-min-sec form ds1307
TR1=1;
for(n=0;n<COLUM;n++){
////////
if(n>=COLUM_on&&num<8){
if((num==7&&(((sec&0x0F)-14+Num_ASII/2)!=Matrix.Time_Save[num]))||
(num==6&&(((sec>>4)-14+Num_ASII/2)!=Matrix.Time_Save[num]))||
(num==4&&(((min&0x0F)-14+Num_ASII/2)!=Matrix.Time_Save[num]))||
(num==3&&(((min>>4)-14+Num_ASII/2)!=Matrix.Time_Save[num]))||
(num==1&&(((hour&0x0F)-14+Num_ASII/2)!=Matrix.Time_Save[num]))||
(num==0&&(((hour>>4)-14+Num_ASII/2)!=Matrix.Time_Save[num]))){
bit_left=1;
if(num==7){
if(n_left<8){
shiftleftbyte(Matrix.sec_isl[i],Matrix.sec_isl[i+1],0x80);
shiftleftbyte(Matrix.sec_isl[i+1],(code_tab[(sec&0x0F)-14+Num_ASII/2][i]),byte_and);
}
else {
shiftleftbyte(Matrix.sec_isl[i],Matrix.sec_isl[i+1],0x80);
shiftleftbyte(Matrix.sec_isl[i+1],(code_tab[(sec&0x0F)-14+Num_ASII/2][i+1]),byte_and);
}
//byte_0=Matrix.sec_isl[i];byte=Matrix.sec_isl[i+1];
}
else if(num==6){
if(n_left<8){
shiftleftbyte(Matrix.sec_ten[i],Matrix.sec_ten[i+1],0x80);
shiftleftbyte(Matrix.sec_ten[i+1],(code_tab[(sec>>4)-14+Num_ASII/2][i]),byte_and);
}
else {
shiftleftbyte(Matrix.sec_ten[i],Matrix.sec_ten[i+1],0x80);
shiftleftbyte(Matrix.sec_ten[i+1],(code_tab[(sec>>4)-14+Num_ASII/2][i+1]),byte_and);
}
//byte_0=Matrix.sec_ten[i];byte=Matrix.sec_ten[i+1];
}
else if(num==4){
if(n_left<8){
shiftleftbyte(Matrix.min_isl[i],Matrix.min_isl[i+1],0x80);
shiftleftbyte(Matrix.min_isl[i+1],(code_tab[(min&0x0F)-14+Num_ASII/2][i]),byte_and);
}
else {
shiftleftbyte(Matrix.min_isl[i],Matrix.min_isl[i+1],0x80);
shiftleftbyte(Matrix.min_isl[i+1],(code_tab[(min&0x0F)-14+Num_ASII/2][i+1]),byte_and);
}
//byte_0=Matrix.min_isl[i];byte=Matrix.min_isl[i+1];
}
else if(num==3){
if(n_left<8){
shiftleftbyte(Matrix.min_ten[i],Matrix.min_ten[i+1],0x80);
shiftleftbyte(Matrix.min_ten[i+1],(code_tab[(min>>4)-14+Num_ASII/2][i]),byte_and);
}
else {
shiftleftbyte(Matrix.min_ten[i],Matrix.min_ten[i+1],0x80);
shiftleftbyte(Matrix.min_ten[i+1],(code_tab[(min>>4)-14+Num_ASII/2][i+1]),byte_and);
}
//byte_0=Matrix.min_ten[i];byte=Matrix.min_ten[i+1];
}
else if(num==1){
if(n_left<8){
shiftleftbyte(Matrix.hour_isl[i],Matrix.hour_isl[i+1],0x80);
shiftleftbyte(Matrix.hour_isl[i+1],(code_tab[(hour&0x0F)-14+Num_ASII/2][i]),byte_and);
}
else {
shiftleftbyte(Matrix.hour_isl[i],Matrix.hour_isl[i+1],0x80);
shiftleftbyte(Matrix.hour_isl[i+1],(code_tab[(hour&0x0F)-14+Num_ASII/2][i+1]),byte_and);
}
//byte_0=Matrix.hour_isl[i];byte=Matrix.hour_isl[i+1];
}
else if(num==0){
if(n_left<8){
shiftleftbyte(Matrix.hour_ten[i],Matrix.hour_ten[i+1],0x80);
shiftleftbyte(Matrix.hour_ten[i+1],(code_tab[(hour>>4)-14+Num_ASII/2][i]),byte_and);
}
else {
shiftleftbyte(Matrix.hour_ten[i],Matrix.hour_ten[i+1],0x80);
shiftleftbyte(Matrix.hour_ten[i+1],(code_tab[(hour>>4)-14+Num_ASII/2][i+1]),byte_and);
}
}
if(n_left==15&&(i+2>=lengh_font)){
if(num==7)Matrix.Time_Save[num] = (sec&0x0F)-14 + Num_ASII/2;
else if(num==6)Matrix.Time_Save[num] = (sec>>4) -14+ Num_ASII/2;
else if(num==4)Matrix.Time_Save[num] = (min&0x0F)-14 + Num_ASII/2;
else if(num==3)Matrix.Time_Save[num] = (min>>4) -14+ Num_ASII/2;
else if(num==1)Matrix.Time_Save[num] = (hour&0x0F)-14 + Num_ASII/2;
else if(num==0)Matrix.Time_Save[num] = (hour>>4)-14 + Num_ASII/2;
}
}
if(num==0){byte= Matrix.hour_ten[i+1];byte_0=Matrix.hour_ten[i];}
else if(num==1){byte= Matrix.hour_isl[i+1];byte_0=Matrix.hour_isl[i];}
else if(num==2||num==5){byte=code_tab[Matrix.Time_Save[num]][i+1];byte_0=code_tab[Matrix.Time_Save[num]][i];}
else if(num==3){byte= Matrix.min_ten[i+1];byte_0=Matrix.min_ten[i];}
else if(num==4){byte= Matrix.min_isl[i+1];byte_0=Matrix.min_isl[i];}
else if(num==6){byte= Matrix.sec_ten[i+1];byte_0=Matrix.sec_ten[i];}
else if(num==7){byte= Matrix.sec_isl[i+1];byte_0=Matrix.sec_isl[i];}
///////////////////////////////////////// on half up//////////////////
//P1_0=0;
//P1_1=0;
if(!Next)byte=byte_0=0xFF;
port_0=0x00;port_1=0x00;//clear data
port_1 = 0x03;// off two led in side
if(byte&0x01)port_1|=0x04;
if(byte&0x02)port_1|=0x08;
if(byte&0x04)port_1|=0x10;
if(byte&0x08)port_1|=0x20;
if(byte&0x10)port_1|=0x40;
if(byte&0x20)port_1|=0x80;
if(byte&0x40)port_0|=0x01;
if(byte&0x80)port_0|=0x02;
if(byte_0&0x01)port_0|=0x04;
if(byte_0&0x02)port_0|=0x08;
if(byte_0&0x04)port_0|=0x10;
if(byte_0&0x08)port_0|=0x20;
if(byte_0&0x10)port_0|=0x40;
port_0&=0x7F;
///////////////////////////////////////////////////////////////////
i+=2;if(i>=lengh_font){i=0;num++;}
}
else if((n>=COLUM/2)&&(n<l_clock)){
if(_num_>=0&&_num_<15){
byte= code_tab[Matrix.Date_Save[_num_]][_i_-1];
if(!Next)byte=0xFF;
port_0=0x00;port_1=0x00;//clear data
port_1 = 0x07; //off three in side
if(byte&0x80)port_1|=0x08;
if(byte&0x40)port_1|=0x10;
if(byte&0x20)port_1|=0x20;
if(byte&0x10)port_1|=0x40;
if(byte&0x08)port_1|=0x80;
if(byte&0x04)port_0|=0x01;
if(byte&0x02)port_0|=0x02;
if(byte&0x01)port_0|=0x04;
byte= code_tab[Matrix.Date_Save[_num_]][_i_];
if(!Next)byte=0xFF;
if(byte&0x80)port_0|=0x08;
if(byte&0x40)port_0|=0x10;
if(byte&0x20)port_0|=0x20;
if(byte&0x10)port_0|=0x40;
port_0&=0x7F;
}
else {port_0=0x7F;port_1=0xFF;}
_i_-=2;if(_i_<0){_i_=lengh_font-1;_num_--;}
}
else {port_0=0x7F;port_1=0xFF;}
bit_do=1;
while(bit_do);// wait for timer2 display
Colum++;
}
Colum=COLUM;// for off display timer1
if(bit_left){
bit_left=0;byte_and>>=1;if(byte_and==0)byte_and=0x80;
if(++n_left>=16){n_left=0;byte_and=0x80;}
}
// SPEDD
if(Speed>=SPEED){Speed=0;
if(l_clock<COLUM)l_clock++;
if(l_clock>(COLUM/2)+1)num_i_clock+=2;
if(num_i_clock>=lengh_font){num_i_clock=1;num_clock++;}
if(num_clock>13+lengh_font){num_clock=0;l_clock=COLUM/2;num_i_clock=-1;}
}
}
void Setting(){
unsigned char byte,byte_0,COLUM_on,COLUM_off;
unsigned char COLUM_on_down,COLUM_off_down;
unsigned char i,i_0,i_2,i_1_1=0;
unsigned int n,i_1;
char i_3=lengh_font-1;
i_0=static_i_0;
i_2=static_i_2;
i_1=static_i_1;
if(Menu==4)i=2;
else if(Menu==6)i=9;
COLUM_on = (COLUM/2)- ((lengh_font/2)*7);
COLUM_on/=2;
COLUM_off=COLUM_on+((lengh_font/2)*7);
COLUM_on_down = (COLUM/2)-((lengh_font/2)*3);
COLUM_on_down/=2;
COLUM_off_down= COLUM_on_down+((lengh_font/2)*3);
TR1=1;
for(n=0;n<COLUM;n++){
if(n>=COLUM_on&&n<COLUM_off){
if(n<COLUM_on+(lengh_font/2)){
byte = code_rr[i_0+1];byte_0= code_rr[i_0];
i_0+=2;
}
else if(n>=COLUM_on+(lengh_font/2)&&n<COLUM_on+((lengh_font/2)*6)){
if(i_1<490){byte= code_setting[i_1+1];byte_0= code_setting[i_1];
i_1+=2;
}
else {byte= code_setting[i_1_1+1];byte_0= code_setting[i_1_1];
i_1_1+=2;
}
}
else {
byte = code_ll[i_2+1];byte_0= code_ll[i_2];
i_2+=2;
}
if(!Next)byte=byte_0=0xFF;
port_0=0x00;port_1=0x00;//clear data
port_1 = 0x03;// off two led in side
if(byte&0x01)port_1|=0x04;
if(byte&0x02)port_1|=0x08;
if(byte&0x04)port_1|=0x10;
if(byte&0x08)port_1|=0x20;
if(byte&0x10)port_1|=0x40;
if(byte&0x20)port_1|=0x80;
if(byte&0x40)port_0|=0x01;
if(byte&0x80)port_0|=0x02;
if(byte_0&0x01)port_0|=0x04;
if(byte_0&0x02)port_0|=0x08;
if(byte_0&0x04)port_0|=0x10;
if(byte_0&0x08)port_0|=0x20;
if(byte_0&0x10)port_0|=0x40;
}
else if(n>=COLUM/2){
if(n>=(COLUM/2)+COLUM_on_down&&n<(COLUM/2)+COLUM_off_down){
if(Menu<4||Menu==5||Menu==7||Menu==8){
if(n<(COLUM/2)+COLUM_on_down+3){byte=0xFF;byte_0=0xFF;} //off
else if(n<(COLUM/2)+COLUM_on_down+3+(lengh_font/2)){
if(Menu==1||Menu==8){byte = code_tab[(hour&0x0F)-14+Num_ASII/2][i_3-1];
byte_0= code_tab[(hour&0x0F)-14+Num_ASII/2][i_3];
}
else if(Menu==2){byte = code_tab[(min&0x0F)-14+Num_ASII/2][i_3-1];
byte_0= code_tab[(min&0x0F)-14+Num_ASII/2][i_3];
}
else if(Menu==3){byte = code_tab[(sec&0x0F)-14+Num_ASII/2][i_3-1];
byte_0= code_tab[(sec&0x0F)-14+Num_ASII/2][i_3];
}
else if(Menu==5){byte = code_tab[(day&0x0F)-14+Num_ASII/2][i_3-1];
byte_0= code_tab[(day&0x0F)-14+Num_ASII/2][i_3];
}
else {byte = code_tab[(year&0x0F)-14+Num_ASII/2][i_3-1];
byte_0= code_tab[(year&0x0F)-14+Num_ASII/2][i_3];
}
i_3-=2;if(i_3<0)i_3=lengh_font-1;
}
else if(n<(COLUM/2)+COLUM_on_down+3+((lengh_font/2)*2)){
if(Menu==1||Menu==8){byte = code_tab[(hour>>4)-14+Num_ASII/2][i_3-1];
byte_0= code_tab[(hour>>4)-14+Num_ASII/2][i_3];
}
else if(Menu==2){byte = code_tab[(min>>4)-14+Num_ASII/2][i_3-1];
byte_0= code_tab[(min>>4)-14+Num_ASII/2][i_3];
}
else if(Menu==3){byte = code_tab[(sec>>4)-14+Num_ASII/2][i_3-1];
byte_0= code_tab[(sec>>4)-14+Num_ASII/2][i_3];
}
else if(Menu==5){byte = code_tab[(day>>4)-14+Num_ASII/2][i_3-1];
byte_0= code_tab[(day>>4)-14+Num_ASII/2][i_3];
}
else {byte = code_tab[(year>>4)-14+Num_ASII/2][i_3-1];
byte_0= code_tab[(year>>4)-14+Num_ASII/2][i_3];
}
i_3-=2;if(i_3<0)i_3=lengh_font-1;
}
else {byte=0xFF;byte_0=0xFF;} //off
}
else {
if(Menu==4||Menu==6){
byte = code_tab[Matrix.Date_Save[i]][i_3-1];
byte_0= code_tab[Matrix.Date_Save[i]][i_3];
i_3-=2;if(i_3<0){i_3=lengh_font-1;i--;}
}
else {byte=0xFF;byte_0=0xFF;} //off
}
if(!Next)byte=byte_0=0xFF;
port_0=0x00;port_1=0x00;//clear data
port_1 = 0x07; //off three in side
if(byte&0x80)port_1|=0x08;
if(byte&0x40)port_1|=0x10;
if(byte&0x20)port_1|=0x20;
if(byte&0x10)port_1|=0x40;
if(byte&0x08)port_1|=0x80;
if(byte&0x04)port_0|=0x01;
if(byte&0x02)port_0|=0x02;
if(byte&0x01)port_0|=0x04;
if(byte_0&0x80)port_0|=0x08;
if(byte_0&0x40)port_0|=0x10;
if(byte_0&0x20)port_0|=0x20;
if(byte_0&0x10)port_0|=0x40;
port_0&=0x7F;
}
else {port_0=0x7F;port_1=0xFF;} //off
}
else {port_0=0x7F;port_1=0xFF;} //off
bit_do=1;
while(bit_do);// wait for timer2 display
Colum++;
} Colum=COLUM;// for off display timer1
// SPEDD
if(Speed>=SPEED&&Next){
if(Static_Menu!=Menu){
static_i_1+=2;
if(static_i_1==70||
static_i_1==140||
static_i_1==210||
static_i_1==280||
static_i_1==350||
static_i_1==420||
static_i_1==490){Static_Menu=Menu;if(Static_Menu>=8){Static_Menu=1;Menu=1;static_i_1=0;}
EX1=1; //enable interrupt 1
}
}
}
// SPEDD +1
if(Speed>=SPEED+1){Speed=0;
static_i_0+=lengh_font;if(static_i_0>=sizeof(code_rr))static_i_0=0;
static_i_2+=lengh_font;if(static_i_2>=sizeof(code_ll))static_i_2=0;
}
}
void Clock_tick(){
unsigned char COLUM_tick,COLUM_tick_0=0,COLUM_tick_1=0;
unsigned char num,byte,byte_0,tick=45,c;
unsigned int n;
unsigned char num_more=0;
Update_time();// update hour-min-sec form ds1307
COLUM_tick = COLUM/60;
TR1=1;
for(n=0;n<COLUM;n++){
if(COLUM_tick_0==0){
if(COLUM_tick_1==0){byte=0x1F;byte_0=0xFF;
if(tick==0){byte=0x0F;byte_0=0xFF;}
}
else {byte=0x3F;byte_0=0xFF;
if(tick==59||tick==1){byte=0x1F;byte_0=0xFF;}
}
if(++COLUM_tick_1>4)COLUM_tick_1=0;
}
else {byte=0x7F;byte_0=0xFF;}
// move tick
if(COLUM_tick_0==0){
///move hour tick /////////////////
num = ((hour>>4)*10)+(hour&0x0F);
if(num==0)num=12;
if(num>12) num-=12;
num*=5;if(num==60)num=0;
num_more=(((((min>>4)*10)+(min&0x0F))*10)/12);
if(num_more<=10&&num_more>0)num+=1;
else if(num_more<=20&&num_more>10)num+=2;
else if(num_more<=30&&num_more>20)num+=3;
else if(num_more<=40&&num_more>30)num+=4;
if((tick+2)>=60)c=(tick+2)-60;else c=tick+2;
if(c==num){byte&=Tick_hour[0];byte_0&=Tick_hour[1];}
if((tick+1)>=60)c=(tick+1)-60;else c=tick+1;
if(c==num){byte&=Tick_hour[2];byte_0&=Tick_hour[3];}
if(tick==num){byte&=Tick_hour[4];byte_0&=Tick_hour[5];}
if((num+1)>=60)c=(num+1)-60;else c=num+1;
if(tick==c){byte&=Tick_hour[6];byte_0&=Tick_hour[7];}
if((num+2)>=60)c=(num+2)-60;else c=num+2;
if(tick==c){byte&=Tick_hour[8];byte_0&=Tick_hour[9];}
//////////////////////////////////
///move min tick /////////////////
num = ((min>>4)*10)+(min&0x0F);
if((tick+2)>=60)c=(tick+2)-60;else c=tick+2;
if(c==num){byte&=Tick_min[0];byte_0&=Tick_min[1];}
if((tick+1)>=60)c=(tick+1)-60;else c=tick+1;
if(c==num){byte&=Tick_min[2];byte_0&=Tick_min[3];}
if(tick==num){byte&=Tick_min[4];byte_0&=Tick_min[5];}
if((num+1)>=60)c=(num+1)-60;else c=num+1;
if(tick==c){byte&=Tick_min[6];byte_0&=Tick_min[7];}
if((num+2)>=60)c=(num+2)-60;else c=num+2;
if(tick==c){byte&=Tick_min[8];byte_0&=Tick_min[9];}
//////////////////////////////////
///move sec tick /////////////////
num = ((sec>>4)*10)+(sec&0x0F);
if(tick==num){byte&=0xF0;byte_0&=0x00;}
//////////////////////////////////
tick++;if(tick>59)tick=0;
}
if(++COLUM_tick_0>=COLUM_tick)COLUM_tick_0=0;
port_0=byte;port_1=byte_0;
bit_do=1;
while(bit_do);// wait for timer2 display
Colum++;
}
}
Code:
#ifndef __FONT_H__
#define __FONT_H__
#define lengh_font 14
#define num_font 39 //95
code unsigned char code_tab[num_font][lengh_font]={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,// space 0
//0xFF,0xFF,0xF7,0x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if
Code:
#ifndef __DS1307_H__ #define __DS1307_H__ extern void I2c_start(void); extern void I2c_stop(void); extern unsigned char I2c_read(bit ack); extern bit I2c_write(unsigned char value); extern bit Ext_I2c_ready() ; extern void DS_Init(); extern void Update_time(); extern void Update_date(); extern void Save_Newtime(); #endif
Code:
#include "AT89x52.H"
unsigned char hour,min,sec,date,day,month,year;
//**************************************
// i2c programer
// Date : 11/03/2013 LY DUY TIN
//**************************************
///////////////// this program will for ds1307 run in mode 24h per day.
//define address of ds
#define SCK P2_2
#define SDA P2_1
#define ADD_Ds1307 0xD0
#define Add_sec 0x00
#define Add_min 0x01
#define Add_hour 0x02
#define Add_date 0x03
#define Add_day 0x04
#define Add_month 0x05
#define Add_year 0x06
//define address of Ram if use this
#define Add_timer_1 0x08
#define Add_timer_2 0x0C
#define Add_timer_3 0x10
#define Add_timer_4 0x14
#define Add_timer_5 0x18
#define Add_timer_6 0x1C
#define Add_timer_7 0x20
#define Add_timer_8 0x24
#define Add_timer_9 0x28
#define Add_timer_10 0x2C
#define Add_End_Of_Ram 0x3F
void I2c_start(void){
SDA = 1;
SCK = 1;
SDA = 0;
SCK = 0;
}
void I2c_stop(void){
SCK = 0;
SDA = 0;
SCK = 1;
SDA = 1;
}
unsigned char I2c_read(bit ack){
unsigned char i, result;
result = 0;
SDA = 1; //select input direction
SCK = 0;
for(i = 0; i < 8; i++)
{
SCK = 1;
result <<= 1;
if(SDA == 1) result++;
SCK = 0;
}
if(ack)SDA=0;else SDA=1;
SCK = 1;
SCK = 0;
return result;
}
bit I2c_write(unsigned char value){
unsigned char i, temp;
bit ack;
temp = value;
SCK = 0;
SCK = 0;
for(i = 0; i < 8; i++)
{
temp <<= 1;
SDA = CY;
SCK = 1;
SCK = 0;
}
SDA = 1; // read ACK
SCK = 1;
ack = SDA;
SCK = 0;
return ack;
}
bit Ext_I2c_ready() {
bit ack;
I2c_start(); // If the write command is acknowledged,
ack = I2c_write(ADD_Ds1307); // then the device is ready.
I2c_stop();
return !ack;
}
void DS_Init(){
unsigned char Test=0;
while(Test!=0x88){
while(!Ext_I2c_ready());
I2c_start();
I2c_write(ADD_Ds1307);
I2c_write(Add_End_Of_Ram);
I2c_stop();
I2c_start();
I2c_write(ADD_Ds1307|1);
Test=I2c_read(0);
I2c_stop();
if(Test!=0x88){
//set byte test
I2c_start();
I2c_write(ADD_Ds1307);
I2c_write(Add_End_Of_Ram);
I2c_write(0x88);
I2c_stop();
//set time first
I2c_start();
I2c_write(ADD_Ds1307);
I2c_write(Add_sec);
I2c_write(0x00);
I2c_write(0x00);
I2c_write(0x00);
I2c_write(0x03);
I2c_write(0x03);
I2c_write(0x03);
I2c_write(0x13);
I2c_stop();
//set time fo
}
// freguency 1 hz
I2c_start();
I2c_write(ADD_Ds1307);
I2c_write(0x07);
I2c_write(0x10);
I2c_stop();
}
}
void Update_time(){
while(!Ext_I2c_ready());
I2c_start();
I2c_write(ADD_Ds1307);
I2c_write(Add_sec);
I2c_stop();
I2c_start();
I2c_write(ADD_Ds1307|1);
sec=I2c_read(1);
min=I2c_read(1);
hour=I2c_read(0);
I2c_stop();
}
void Update_date(){
while(!Ext_I2c_ready());
I2c_start();
I2c_write(ADD_Ds1307);
I2c_write(Add_date);
I2c_stop();
I2c_start();
I2c_write(ADD_Ds1307|1);
date=I2c_read(1);
day=I2c_read(1);
month=I2c_read(1);
year=I2c_read(0);
I2c_stop();
}
void Save_Newtime(){
//set new time
I2c_start();
I2c_write(ADD_Ds1307);
I2c_write(Add_sec);
I2c_write(sec);
I2c_write(min);
I2c_write(hour);
I2c_write(date);
I2c_write(day);
I2c_write(month);
I2c_write(year);
I2c_stop();
}
File mạch in đây.

hardware vs 00.PDF



Comment