PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Các ngôn ngữ lập trình khác (CCS C, HT PIC,...) (http://www.picvietnam.com/forum/forumdisplay.php?f=12)
-   -   giúp em về đồng hồ thời gian thực ds1307 (http://www.picvietnam.com/forum/showthread.php?t=30203)

loveboom3012 16-08-2012 01:20 AM

giúp em về đồng hồ thời gian thực ds1307
 
Code:

#include <16f877a.h>
#include <def_877a.h>
#include <ds1307.c>
#fuses nowdt,noprotect,nolvp,xt,hs
#use delay(clock=4000000)
#use fast_io(b)
#use fast_io(d)
byte sec,min,hour;
byte const maled[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 };
byte gic,gid,pc,pd,gc,gd;
byte chinh_giay;
int i;
int16 j;
#bit hourc =0x08.5
#bit hourd =0x08.4
#bit minc =0x08.3
#bit mind =0x08.2
#bit secc =0x08.1
#bit secd =0x08.0
#bit sw1=0x07.0
#bit sw2=0x07.1
#bit sw3=0x07.5
#bit sw4=0x07.6
void hienthi();
void read_time();
void update_time();
void update_ds1307();
void set_sec();
void set_min();
void set_hour();
void main()
{
       
        set_tris_d(0x00);
        set_tris_b(0x00);
        init_ds1307();
        chinh_giay=0;
        while(1)
        {
                j++;
                read_time();
                hienthi();       
        if(sw1==0)
        {
            if(chinh_giay==0&&sw4==1)
            {
                while(sw1==0) {}
                set_sec(); chinh_giay=1;
            }
        if(sw4==0)
        {
            chinh_giay=0;
        }
        }
}
}
void read_time()
{
        sec=read_ds1307(0);
        min=read_ds1307(1);
        hour=read_ds1307(2);
        update_time();
}
void update_ds1307()
{
        write_ds1307(0,sec);
        write_ds1307(1,min);
        write_ds1307(2,hour);
}
void update_time()
{

        gd=sec&0x0f;
        gc=(sec&0xf0)>>4;
        pd=min&0x0f;
        pc=(min&0xf0)>>4;
        gid=hour&0x0f;
        gic=(hour&0x30)>>4;

}

void set_sec()
{
        gd=1;gc=1;pd=1;pc=1;gid=1;gic=1;
        sec=read_ds1307(0);
        gd=sec&0x0f;
        gc=(sec&0xf0)>>4;
        while(sw1==1 && sw4==1)
        {
        portb=maled[gd];gd=0;delay_us(100);gd=1;
        portb=maled[gc];gc=0;delay_us(100);gc=1;
                if(sw2==0)
                        {
                                while(sw2==0){}
                                if(gc==5 && gd==10)
                                        {gc=0;gd=0;}
                                else if(gd==10)
                                        {gc++;gd=0;}
                                else
                                        gd++;
                        }
                if(sw3==0)
                        {
                                while(sw3==0){}
                                if(gc==0&&gd==0)
                                {gc=5;gd=9;}
                                else if(gd==0)
                                {gc--;gd=9;}
                                else
                                gd--;
                        }
                sec=(gc<<4)+gd;
                write_ds1307(0,sec);
                delay_us(10);       
}
}


void hienthi()
{
        for(i=1;i<30;i++)
        {
                portb=maled[gd];secd=0;delay_us(100);secd=1;
                portb=maled[gc];secc=0;delay_us(100);secc=1;
                portb=maled[pd];mind=0;delay_us(100);mind=1;
                portb=maled[pc];minc=0;delay_us(100);minc=1;
                portb=maled[gid];hourd=0;delay_us(100);hourd=1;
                portb=maled[gic];hourc=0;delay_us(100);hourc=1;
        }
}

mấy anh xem em viết cái hàm chỉnh giây như vậy có sai ở đâu không ? mô phỏng với protues không chạy đuợc phần chỉnh giây

qloi 30-08-2012 10:11 PM

không thiết thực bạn ơi,bjo có mấy cái đồng hồ chỉnh giây đâu.chỉnh phút thui .bạn mún làm đồng hồ bằng DS1307 thì liên hệ leo007_e89 t chỉ cho làm.


Múi giờ GMT. Hiện tại là 04:02 PM.

Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam