PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Các ngôn ngữ lập trình khác (CCS C, HT PIC,...)

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 16-08-2012, 01:20 AM   #1
loveboom3012
Đệ tử 1 túi
 
Tham gia ngày: Jun 2011
Bài gửi: 18
:
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

thay đổi nội dung bởi: loveboom3012, 16-08-2012 lúc 02:18 AM.
loveboom3012 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


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


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam