PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > PIC - Thiết kế và Ứng dụng

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

PIC - Thiết kế và Ứng dụng Ý tưởng cho các sản phẩm sử dụng PIC/dsPIC và các sản phẩm của Microchip

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 06-08-2012, 01:44 AM   #1
huunguyen1990
Đệ tử 1 túi
 
Tham gia ngày: Oct 2010
Bài gửi: 13
:
Unhappy Giúp sửa lỗi RTC bằng Pic16f877a+DS1307+hiển thị LCD

Code em mới viết xong mô phỏng bằng ISS khi nhấn nút vào menu cài đặt thời gian không dc, các anh chị giúp em với ạ !
code :
#include <16f877a.h> //KHAI BAO THU VIEN PIC16F877A
#device *=16 adc=10 //KHAI BAO CON TRO VA ADC
#FUSES NOWDT, HS, NOPUT, NOPROTECT //THIET LAP CAU HINH CHO PIC
#use delay(clock=20000000) //KHAI BAO TAN SO DAO DONG LA 20MHZ
#include <lcd_lib_4bit.c>
#use i2c(Master,Slow,sda=PIN_C4,scl=PIN_C3) //BAT CHE DO GIAO TIEP I2C
#define status Pin_B0
#define mode Pin_B1
#define increase Pin_B2
#define decrease Pin_B3


int8 sec,min,hour,dow,date,month,year;
int8 sec1,sec2,min1,min2,hour1,hour2,day,date1,date2,mo nth1,month2,year1,year2;
void set_time(void) ;
void set_min(void);
void set_hour(void);
void set_date(void);
void set_day(void);
void set_month(void);
void set_year(void);
void update_time();
void display();
///////////////////////////
void main ()
{
set_tris_b(0xff);
set_tris_d(0x00);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
Lcd_init();
while(true)
{
if (mode==0) { set_time();}
update_time();
display();

}
}
////////////////////////////////
void update_time()
{
i2c_start(); //phat bit start I2C
i2c_write(0xD0); //gui dia chi cua slave
i2c_write(0x00); //thiet lap lai con tro - set register pointer
i2c_stop(); //phat bit stop I2C
I2C_start(); //phat bit start I2C
I2C_write(0xD1); //gui lenh doc du lieu
sec = i2c_read(1); //doc du lieu tai dia chi 0x00
min = i2c_read(1); //doc du lieu tai dia chi 0x01
hour = i2c_read(1); //doc du lieu tai dia chi 0x02
dow = i2c_read(1);
date = i2c_read(1);
month = i2c_read(1);
year = i2c_read(0);
i2c_stop(); //phat bit stop I2C
delay_us(100);
sec1=sec & 0x0F;
sec2=(sec & 0x70)>>4; //convert to BCD SEC
min1=min & 0x0F;
min2=(min & 0x70)>>4; //convert to BCD MIN
hour1=hour & 0x0F;
hour2=(hour & 0x30)>>4; //convert to BCD HOUR
day=(dow & 0x07);
date1=date & 0x0F;
date2=(date & 0x30)>>4; //convert to BCD DATE
month1=month & 0x0F;
month2=(month & 0x10)>>4;//convert to BCD MONTH
year1=year & 0x0F;
year2=(year & 0xF0)>>4; //convert to BCD YEAR
delay_ms(1);
}
/////////////////////////////
void update_ds1307(void) {
int8 data=0; // luu tru du lieu tam thoi de gui vao ds1307
i2c_start();
i2c_write(0xd0);
i2c_write(0x00); // ghi du lieu bat dau tu vi tri 00
data=sec1+(sec2<<4);
data=data & 0b01111111;
i2c_write(data);
data=min1+(min2<<4);
i2c_write(data);
data=hour1+(hour2<<4);
i2c_write(data);
data=day;
i2c_write(data);
data=date1+(date2<<4);
i2c_write(data);
data=month1+(month2<<4);
i2c_write(data);
data=year1+(year2<<4);
i2c_write(data);
data=0x00;
i2c_write(data);
i2c_stop(); // ket thuc truyen du lieu
}
///////////////////////
void set_time(void) {
set_year();
set_month();
set_date();
set_day();
set_hour();
while (mode==0) {}
update_ds1307();
}
////////////////////////////
void set_year(void) { // Cai dat nam
LCD_init();
while (mode ==0) {} // cho phim nha
do {
if (increase ==0) { // neu phim 2 duoc nhan
while (increase ==0) {} // khong lam gi cho phim nha
year1++;
if ((year2==9)&(year1==10)) {year1=year2=0;}
if (year1==10) {year1=0;year2++;}
}
if (decrease==0) { // neu phim 3 duoc nhan
while (decrease ==0) {} // khong lam gi cho phim nha
year1--;
if ((year2==0)&(year1==255)) {year1=year2=9;}
if (year1==255) {year1=9;year2--;}
}
Lcd_Setposition(0x80);
printf(Lcd_putchar,"Year: %u%u",year2,year1);
}
while (mode ==1);
}
////////////////////////
void set_month(void) { // Cai dat thang
LCD_init();
while (Pin_B1==0) {} // cho phim nha
do {
if (increase==0) { // neu phim 2 duoc nhan
while (increase==0) {} // khong lam gi cho phim nha
month1++;
if ((month2==1) & (month1==3)) { month2=0;month1=1;}
if (month1==10) { month2++;month1=0;}
}
if (decrease==0) { // neu phim 3 duoc nhan
while (decrease==0) {} // cho phim nha
month1--;
if ((month2==0) & (month1==0)) {month2=1;month1=2; }
if (month1==0) {month2--;month1=9;}
}
Lcd_Setposition(0x80);
printf(Lcd_putchar,"Moth: %u%u",month2,month1);
}
while (mode==1);
}
///////////////////////////////////
void set_date(void) { // Cai dat thang
LCD_init();
while (Pin_B1==0) {} // cho phim nha
do {
if (increase==0) { // neu phim 2 duoc nhan
while (increase==0) {} // khong lam gi cho phim nha
date1++;
if ((date2==3) & (date1==2)) { date2=0;date1=1;}
if (date1==10) { date2++;date1=0;}
}
if (decrease==0) { // neu phim 3 duoc nhan
while (decrease==0) {} // cho phim nha
date1--;
if ((date2==0) & (date1==0)) {date2=3;date1=1; }
if (date1==255) {date2--;date1=9;}
}
Lcd_Setposition(0x80);
printf(Lcd_putchar,"Date: %u%u",date2,date1);
}
while (mode==1);
}
///////////////////////////////////
void set_day(void) {
LCD_init();
while (Pin_B1==0) {};
do {
if (increase==0) {
while (increase==0) {}
day++;
if (day==8) { day=1;}
}
if (decrease==0) {
while (decrease==0) {}
day--;
if (day==255) {day=7;}
}
Lcd_Setposition(0x80);
printf(Lcd_putchar,"Day: %u",day);
}
while (mode==1);
}

/////////////////////////////////////
void set_hour(void) { // Cai dat thang
LCD_init();
while (Pin_B1==0) {} // cho phim nha
do {
if (increase==0) { // neu phim 2 duoc nhan
while (increase==0) {} // khong lam gi cho phim nha
hour1++;
if ((hour2==2) & (hour1==4)) { hour2=0;hour1=0;}
if (hour1==10) { hour2++;hour1=0;}
}
if (decrease==0) { // neu phim 3 duoc nhan
while (decrease==0) {} // cho phim nha
hour1--;
if ((hour2==0) & (hour1==255)) {hour2=2;hour1=3; }
if (hour1==255) {hour2--;hour1=9;}
}
}
while (mode==1);
}
////////////////////////////////////////////
void set_min(void) { // Cai dat thang
LCD_init();
while (Pin_B1==0) {} // cho phim nha
do {
if (increase==0) { // neu phim 2 duoc nhan
while (increase==0) {} // khong lam gi cho phim nha
min1++;
if ((min2==5) & (min1==10)) { min2=0;min1=0;}
if (min1==10) { min2++;min1=0;}
}
if (decrease==0) { // neu phim 3 duoc nhan
while (decrease==0) {} // cho phim nha
min1--;
if ((min2==0) & (min1==255)) {min2=5;min1=9; }
if (min1==255) {min2--;min1=9;}
}
Lcd_Setposition(0x80);
printf(Lcd_putchar,"-%u%u",min2,min1);
}
while (mode==1);
}

///////////////////////////////////
void display()
{
Lcd_Setposition(0x80);
printf(Lcd_putchar,"Time: %u%u",hour2,hour1);
Lcd_Setposition(0x88);
printf(Lcd_putchar,"-%u%u",min2,min1);
Lcd_Setposition(0x8b);
printf(Lcd_putchar,"-%u%u",sec2,sec1);
Lcd_Setposition(0xc0);
printf(Lcd_putchar,"Date: %u%u",date2,date1);
Lcd_Setposition(0xc8);
printf(Lcd_putchar,"-%u%u",month2,month1);
Lcd_Setposition(0xcb);
printf(Lcd_putchar,"-%u%u",year2,year1);
}
/////////////////////////
khi compile thì nó báo là (chủ yếu báo ở các câu lệnh IF(...):
Line34(1,1) : Condition always FALSE
Line105(1,1) : Condition always FALSE
.....
Link ảnh mô phỏng ISS:
http://www.mediafire.com/view/?h7ih5ihgs5ulh9q

Em xin cảm ơn !!
huunguyen1990 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 

Ðiều Chỉnh
Xếp Bà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à 02:52 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