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

Trả lời
 
Ðiều Chỉnh Xếp Bài
Old 30-11-2012, 03:09 PM   #1
tienrau2
Nhập môn đệ tử
 
Tham gia ngày: Nov 2012
Bài gửi: 3
:
Unhappy Đồng hồ số chạy quá lệch thời gian! Giúp mình với các bạn ơi.....

mình mới học VDK về Pic, làm đồ án mạch đồng hồ số dùng PIC 16F887A và DS1307. Viết được đoạn code nhận thời gian từ DS1307 và hiển thị lên Led, nhưng sao cái mô phỏng nó chạy chậm quá. Không đúng với đồng hồ mình so sánh ở máy tính. Mấy bạn xem giùm mình với nha. Có thể sữa ở chỗ nào?
Code:
#include <16F877A.h>
#FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG, NOBROWNOUT, NOLVP
#use delay(clock=20000000)
#use i2c(master,fast,sda=pin_d7,scl=pin_d6)

unsigned char sec,min,hour,day,month,year,date;
unsigned char a[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0};
unsigned char sec1,sec2,min1,min2,hour1,hour2,day1,day2,month1,m onth2,year1,year2;
///////////define//////
void update_time();
void display1();
void main()
{
while (1) {
update_time();
display1();
}
}
void update_time() {
i2c_start();
i2c_write(0xD0); //Gui dia chi cua slave
i2c_write(0x00); //thiet lap lai con tro - set register pointer
i2c_stop();
I2C_start();
I2C_write(0xD1); /* gui lenh doc du lieu */
sec = i2c_read(1);
min = i2c_read(1);
hour = i2c_read(1);
date =i2c_read(1);
day = i2c_read(1);
month = i2c_read(1);
year = i2c_read(0);

i2c_stop();
sec1=sec & 0x0F;
sec2=(sec & 0x70)>>4;
min1=min & 0x0F;
min2=(min & 0x70)>>4;
hour1=hour & 0x0F;
hour2=(hour & 0x30)>>4;
day1=day & 0x0F;
day2=(day & 0x30)>>4;
month1=month & 0x0F;
month2=(month & 0x10)>>4;
year1=year & 0x0F;
year2=(year & 0xF0)>>4;
}
void display1()
{
output_b(a[sec1]); output_high(pin_c0);//SEC1
delay_us(400); output_low(pin_c0);
output_b(a[sec2]); output_high(pin_c1);//SEC2
delay_us(400); output_low(pin_c1);
output_b(a[min1]); output_high(pin_c2);//SEC1
delay_us(400); output_low(pin_c2);
output_b(a[min2]); output_high(pin_c3);//SEC2
delay_us(400); output_low(pin_c3);
output_b(a[hour1]); output_high(pin_c4);//SEC1
delay_us(400); output_low(pin_c4);
output_b(a[hour2]); output_high(pin_c5);//SEC2
delay_us(400); output_low(pin_c5);
output_b(a[day1]); output_high(pin_c6);//SEC1
delay_us(400); output_low(pin_c6);
output_b(a[day2]); output_high(pin_c7);//SEC2
delay_us(400); output_low(pin_c7);
output_b(a[month1]); output_high(pin_d0);//SEC1
delay_us(400); output_low(pin_d0);
output_b(a[month2]); output_high(pin_d1);//SEC2
delay_us(400); output_low(pin_d1);
output_b(a[2]); output_high(pin_d2);//SEC1
delay_us(400); output_low(pin_d2);
output_b(a[0]); output_high(pin_d3);//SEC2
delay_us(400); output_low(pin_d3);
output_b(a[year1]); output_high(pin_d4);//SEC1
delay_us(400); output_low(pin_d4);
output_b(a[year2]); output_high(pin_d5);//SEC2
delay_us(400); output_low(pin_d5);
}
Còn đây là file mô phỏng và code:
File Kèm Theo
File Type: zip mo phong va code.zip (65.4 KB, 27 lần tải)
tienrau2 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 10-12-2012, 11:27 PM   #2
qloi
Đệ tử 3 túi
 
Tham gia ngày: Sep 2010
Bài gửi: 55
:
Trích:
Nguyên văn bởi tienrau2 View Post
mình mới học VDK về Pic, làm đồ án mạch đồng hồ số dùng PIC 16F887A và DS1307. Viết được đoạn code nhận thời gian từ DS1307 và hiển thị lên Led, nhưng sao cái mô phỏng nó chạy chậm quá. Không đúng với đồng hồ mình so sánh ở máy tính. Mấy bạn xem giùm mình với nha. Có thể sữa ở chỗ nào?
Code:
#include <16F877A.h>
#FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG, NOBROWNOUT, NOLVP
#use delay(clock=20000000)
#use i2c(master,fast,sda=pin_d7,scl=pin_d6)

unsigned char sec,min,hour,day,month,year,date;
unsigned char a[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0};
unsigned char sec1,sec2,min1,min2,hour1,hour2,day1,day2,month1,m onth2,year1,year2;
///////////define//////
void update_time();
void display1();
void main()
{
while (1) {
update_time();
display1();
}
}
void update_time() {
i2c_start();
i2c_write(0xD0); //Gui dia chi cua slave
i2c_write(0x00); //thiet lap lai con tro - set register pointer
i2c_stop();
I2C_start();
I2C_write(0xD1); /* gui lenh doc du lieu */
sec = i2c_read(1);
min = i2c_read(1);
hour = i2c_read(1);
date =i2c_read(1);
day = i2c_read(1);
month = i2c_read(1);
year = i2c_read(0);

i2c_stop();
sec1=sec & 0x0F;
sec2=(sec & 0x70)>>4;
min1=min & 0x0F;
min2=(min & 0x70)>>4;
hour1=hour & 0x0F;
hour2=(hour & 0x30)>>4;
day1=day & 0x0F;
day2=(day & 0x30)>>4;
month1=month & 0x0F;
month2=(month & 0x10)>>4;
year1=year & 0x0F;
year2=(year & 0xF0)>>4;
}
void display1()
{
output_b(a[sec1]); output_high(pin_c0);//SEC1
delay_us(400); output_low(pin_c0);
output_b(a[sec2]); output_high(pin_c1);//SEC2
delay_us(400); output_low(pin_c1);
output_b(a[min1]); output_high(pin_c2);//SEC1
delay_us(400); output_low(pin_c2);
output_b(a[min2]); output_high(pin_c3);//SEC2
delay_us(400); output_low(pin_c3);
output_b(a[hour1]); output_high(pin_c4);//SEC1
delay_us(400); output_low(pin_c4);
output_b(a[hour2]); output_high(pin_c5);//SEC2
delay_us(400); output_low(pin_c5);
output_b(a[day1]); output_high(pin_c6);//SEC1
delay_us(400); output_low(pin_c6);
output_b(a[day2]); output_high(pin_c7);//SEC2
delay_us(400); output_low(pin_c7);
output_b(a[month1]); output_high(pin_d0);//SEC1
delay_us(400); output_low(pin_d0);
output_b(a[month2]); output_high(pin_d1);//SEC2
delay_us(400); output_low(pin_d1);
output_b(a[2]); output_high(pin_d2);//SEC1
delay_us(400); output_low(pin_d2);
output_b(a[0]); output_high(pin_d3);//SEC2
delay_us(400); output_low(pin_d3);
output_b(a[year1]); output_high(pin_d4);//SEC1
delay_us(400); output_low(pin_d4);
output_b(a[year2]); output_high(pin_d5);//SEC2
delay_us(400); output_low(pin_d5);
}
Còn đây là file mô phỏng và code:
Mấy cái này không mô phỏng được đâu , test trực tiếp trên mạch thiệt là được thoy.
__________________
heart.touching.2010@gmail.com!
qloi vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 12-12-2012, 10:56 AM   #3
quanghuy_125
Đệ tử 2 túi
 
Tham gia ngày: Apr 2009
Bài gửi: 44
:
Ds1307

Trích:
Nguyên văn bởi tienrau2 View Post
mình mới học VDK về Pic, làm đồ án mạch đồng hồ số dùng PIC 16F887A và DS1307. Viết được đoạn code nhận thời gian từ DS1307 và hiển thị lên Led, nhưng sao cái mô phỏng nó chạy chậm quá. Không đúng với đồng hồ mình so sánh ở máy tính. Mấy bạn xem giùm mình với nha. Có thể sữa ở chỗ nào?
Code:
#include <16F877A.h>
#FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG, NOBROWNOUT, NOLVP
#use delay(clock=20000000)
#use i2c(master,fast,sda=pin_d7,scl=pin_d6)

unsigned char sec,min,hour,day,month,year,date;
unsigned char a[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0};
unsigned char sec1,sec2,min1,min2,hour1,hour2,day1,day2,month1,m onth2,year1,year2;
///////////define//////
void update_time();
void display1();
void main()
{
while (1) {
update_time();
display1();
}
}
void update_time() {
i2c_start();
i2c_write(0xD0); //Gui dia chi cua slave
i2c_write(0x00); //thiet lap lai con tro - set register pointer
i2c_stop();
I2C_start();
I2C_write(0xD1); /* gui lenh doc du lieu */
sec = i2c_read(1);
min = i2c_read(1);
hour = i2c_read(1);
date =i2c_read(1);
day = i2c_read(1);
month = i2c_read(1);
year = i2c_read(0);

i2c_stop();
sec1=sec & 0x0F;
sec2=(sec & 0x70)>>4;
min1=min & 0x0F;
min2=(min & 0x70)>>4;
hour1=hour & 0x0F;
hour2=(hour & 0x30)>>4;
day1=day & 0x0F;
day2=(day & 0x30)>>4;
month1=month & 0x0F;
month2=(month & 0x10)>>4;
year1=year & 0x0F;
year2=(year & 0xF0)>>4;
}
void display1()
{
output_b(a[sec1]); output_high(pin_c0);//SEC1
delay_us(400); output_low(pin_c0);
output_b(a[sec2]); output_high(pin_c1);//SEC2
delay_us(400); output_low(pin_c1);
output_b(a[min1]); output_high(pin_c2);//SEC1
delay_us(400); output_low(pin_c2);
output_b(a[min2]); output_high(pin_c3);//SEC2
delay_us(400); output_low(pin_c3);
output_b(a[hour1]); output_high(pin_c4);//SEC1
delay_us(400); output_low(pin_c4);
output_b(a[hour2]); output_high(pin_c5);//SEC2
delay_us(400); output_low(pin_c5);
output_b(a[day1]); output_high(pin_c6);//SEC1
delay_us(400); output_low(pin_c6);
output_b(a[day2]); output_high(pin_c7);//SEC2
delay_us(400); output_low(pin_c7);
output_b(a[month1]); output_high(pin_d0);//SEC1
delay_us(400); output_low(pin_d0);
output_b(a[month2]); output_high(pin_d1);//SEC2
delay_us(400); output_low(pin_d1);
output_b(a[2]); output_high(pin_d2);//SEC1
delay_us(400); output_low(pin_d2);
output_b(a[0]); output_high(pin_d3);//SEC2
delay_us(400); output_low(pin_d3);
output_b(a[year1]); output_high(pin_d4);//SEC1
delay_us(400); output_low(pin_d4);
output_b(a[year2]); output_high(pin_d5);//SEC2
delay_us(400); output_low(pin_d5);
}
Còn đây là file mô phỏng và code:
Mình thấy bài mo phỏng của bạn trên Proteus là đúng đấy. Proteus chạy mô phỏng sẽ chậm hơn đồng hồ thực ở máy tính, bạn phải nhìn và so sánh với thời gian ở góc trái ở dưới của time line Proteus ấy!
Chúc vui!
__________________
ĐT : 0167 6455 880 Mail: quanghuy1205@gmail.com
https://www.facebook.com/ThietKeDienTu
quanghuy_125 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 12-12-2012, 08:40 PM   #4
trasuasang
Nhập môn đệ tử
 
Tham gia ngày: Aug 2011
Bài gửi: 1
:
chào các bạn, các bạn cho mình hỏi với mạch đồng hồ thời gian như thế này,,lúc mình làm mạch thật thì mình phải thêm vào các điện trở vào trước các led 7 đoạn và các transistor trước các nút nguồn của các led 7s phai ko,,nhưng lúc mô phỏng thì nó lại chạy sai thời gian..
trasuasang vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Trả lời


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à 10:56 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