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 09-05-2009, 11:51 AM   #5
tungtot_vl
Đệ tử 6 túi
 
tungtot_vl's Avatar
 
Tham gia ngày: Jul 2007
Bài gửi: 154
:
ds1037

cái này minh chưa làm mạch thật nhưng chạy mô phỏng tốt. bạn xem tạm nhé

#include<16F877a.h>
#fuses NOLVP,NOWDT,PUT,hs,NOPROTECT,NOBROWNOUT
#device 16F877a*=16 ADC=10
#include <math.h>
#use delay(clock=20000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#use fast_io(a)
#byte porta=0x05
#use fast_io(b)
#byte portb=0x06
#use fast_io(c)
#byte portc=0x07
#use fast_io(d)
#byte portd=0x08
#use fast_io(e)
#byte porte=0x09
#define RTC_SCL PIN_B0
#define RTC_SDA PIN_B1
#bit RS = portA.0
#bit RW = portA.1
#bit E = portA.2

//#define RTC_RST PIN_B2
#use i2c(master, fast, sda=RTC_SDA, scl=RTC_SCL)
//================================================

int sec,min,hour,date,month,year,a,b;
unsigned char so[] = "0123456789";
// unsigned int led[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80};

void write_RTC(int add, BYTE data)
{
short int status;
i2c_start(); // bao hieu bat dau giao tiep i2c
i2c_write(0xd0); // dia chi thiet bi nhan
i2c_write(add); // dia chi thanh ghi
i2c_write(data); //gui data vao dia chi thanh ghi cua thiet bi nhan
i2c_stop(); // bao hieu ngung ghi
i2c_start(); // bao hieu bat dau giao tiep
status=i2c_write(0xd0); // kiem tra trang thai cua thiet bi nhan
while(status==1) // lap lai cho den khi thiet bi da nhan xong (No Ack)
{
i2c_start();
status=i2c_write(0xd0);
}
delay_us(10);
}

BYTE read_RTC(int add)
{
BYTE data;
i2c_start();
i2c_write(0xd0);
i2c_write(add);
i2c_start();
i2c_write(0xd1);
data=i2c_read(0); // No Ack
i2c_stop();
delay_ms(10);
return(data);

}
void init_RTC() // ham khoi tao eeprom
{
i2c_start(); // bao hieu bat dau giao tiep i2c
i2c_write(0xd0); // dia chi thiet bi nhan
i2c_write(0x00); // enable RTC
i2c_write(0x01);// sec // dia chi thanh ghi
i2c_write(0x00);// min //gui data vao dia chi thanh ghi cua thiet bi nhan
i2c_write(0x08);// hur
i2c_write(0x04);//day
i2c_write(0x11);// date
i2c_write(0x02);// month
i2c_write(0x09);// year
i2c_stop(); // bao hieu ngung ghi
i2c_start(); // bao hieu bat dau giao tiep
}
int rm_bcd(BYTE data) // CHUYEN BCD SANG SO NGUYEN
{
int i;

i=data;
data=(i>>4)*10;
data=data+(i<<4>>4);

return data;
}
////////////////////////////////////////////////////////////
/*Ham yeu cau goi lenh dieu khien LCD*/
void command()
{
RS = 0;
RW = 0;
E = 1;
E = 0;
delay_ms(1);
}
/*Ham yeu cau goi du lieu hien thi len LCD*/
void display()
{
RS = 1;
RW = 0;
E = 1;
E = 0;
delay_ms(1);
}

void main()
{
set_tris_b(0);
set_tris_a(0);
set_tris_c(0);
set_tris_e(0);
set_tris_d(0);
init_RTC();
while(true)
{//write_RTC(addsec,0);
// sec = read_RTC(addsec); // doc thoi gian tu RTC
// min = read_RTC(addmin);
// hour = read_RTC(addhour);
portd = 0x38; // Hai hang, ma tran dot 5*7, 8 bit interface
command();
portd = 0x0C; // Bat hien thi, tat con tro
command();
//////////////////////////////////////////////////
hour = rm_bcd(read_RTC(0x02));
min = rm_bcd(read_RTC(0x01));
sec = rm_bcd(read_RTC(0x00));
date = rm_bcd(read_RTC(0x04));
month = rm_bcd(read_RTC(0x05));
year = rm_bcd(read_RTC(0x06));
//////////////////////////////////////////////////////////////////
a = sec/10; b = sec-a*10;
portd = 0x8b; command(); portd = so[a]; display();
portd = 0x8c; command(); portd = so[b]; display();
portd = 0x8a; command(); portd = ':'; display();
///// hien thi min len LCD ////////
a = min/10; b = min-a*10;
portd = 0x88; command(); portd = so[a]; display();
portd = 0x89; command(); portd = so[b]; display();
portd = 0x87; command(); portd = ':'; display();
///// hien thi sec len LCD ////////
a = hour/10; b = hour-a*10;
portd = 0x85; command(); portd = so[a]; display();
portd = 0x86; command(); portd = so[b]; display();

///////////////////////////////////
a = year/10; b = year-a*10;
portd = 0xcb; command(); portd = so[a]; display();
portd = 0xcc; command(); portd = so[b]; display();
portd = 0xca; command(); portd = '-'; display();
///// hien thi min len LCD ////////
a = month/10; b = month-a*10;
portd = 0xc8; command(); portd = so[a]; display();
portd = 0xc9; command(); portd = so[b]; display();
portd = 0xc7; command(); portd = '-'; display();
///// hien thi sec len LCD ////////
a = date/10; b = date-a*10;
portd = 0xc5; command(); portd = so[a]; display();
portd = 0xc6; command(); portd = so[b]; display();


}

}
File Kèm Theo
File Type: rar 1037.rar (49.6 KB, 505 lần tải)
tungtot_vl 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à 10:32 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