You are on page 1of 8

26/04/2012

Code mu PIC - Giao Tip I2C Vi IC Thi gian thc DS1307 | in T My Tnh | K Thut in T | Vi

Trang Ch

Kim Tin Trn Mng

Vic Lm Trn Mng

Em ail

Facebook

Tw itter

Subcribe via RSS

Trang Ch

VXL PIC

VXL ARM

IC S

in t c bn

Altium Designer

Photoshop CS5

Lp Trnh Web

Th Thut

search

Code mu PIC - Giao Tip I2C Vi IC Thi gian thc DS1307


08:27 Tjtanja

search...

. Tham kho thm: Hng dn - IC thi gian thc DS1307 Chng trnh thc hin giao tip I2C gia PIC 16F877A v IC DS1307 ci t thi gian, c thi gian t DS1307, hin th ln LCD, truyn qua RS232. Vi LCD, chng trnh s c d liu DS1307 v cp nht LCD lin tc, cn khi truyn ln my tnh s da vo ngt RB0 a vo t xung 1Hz ca DS1307, tc l mi 1s s truyn d liu 1 ln. Trong chng trnh c s dng cc th vin: lcd_lib_4bit.c, ds1307.c. S nguyn l m phng trn Proteus:

Cc ch code ASM mu 8086 code mu 8051 Code Mu cho ARM - LPC1343 code mu pic Cu hnh Joomla 2.5 Gii Thut hng dn Altium Designer Hng dn lm web IC s Khong Lng Tm Hn Lp Trnh C Module Sim548 Photoshop CS5 Phn Mm thit k robot t ng d ng Th Thut VHDL Vi X L 8051 Vi X L 8086 Vi X L ARM Vi X L PIC in t c bn Bi ng ph bin Code mu cho PIC - Kt hp: ADC o nhit , in p, hin th LCD v giao tip vi my tnh Chng trnh thc hin o nhit , in p, hin th kt qu ln mn hnh LCD 16x2, v truyn gi tr ln my tnh. M phng trn Proteus:... code mu cho 8051 - o nhit , hin th LCD, truyn d liu qua RS232 Chng trnh thc hin kt hp o nhit , hin th ln LCD, v truyn d liu ln my tnh qua giao tip RS232. M phng trn ISIS - Pro... Module GSM - SIM548C Gii thiu Module SIM548 1. Module SIM548 v cc thit b i km. y l module GSM/GPRS v GPS ca hng SIMCOM

Truyn ln hyper terminal:

www.dientumaytinh.com/2012/04/code-mau-pic-giao-tiep-i2c-voi-ic-thoi.html

1/8

26/04/2012

Code mu PIC - Giao Tip I2C Vi IC Thi gian thc DS1307 | in T My Tnh | K Thut in T | Vi
Module SIM548 M... Hng Dn CCS Cho PIC - Bi 4: Chuyn i ADC - Cc Hm Vo/Ra trong CCS Bi trc: + Bi 3: Cc Hm X L S, X L Bit, Delay trong CCS Bi sau: + Bi 5: Truyn Thng Ni Tip RS232 - X L Chui Trong CCS ... Code mu cho PIC - o nhit , Hin th ln LCD 16x2 B chuyn i t tng t sang s l mt khi mch in t quan trng, c mt trong rt nhiu thit k in t. Cc b ADC thc t c ng... Hng Dn CCS Cho PIC - Bi 7: Cc Ngt Trong PIC Bi trc: + Bi 6: Giao Tip SPI Bi 7: Cc Ngt Trong PIC I / C CH HOT NG CA NGT : 1 / Ngt 1 cp : + Trn PIC 14 , 12... Gii thiu c bn v LCD 16x2 * Gii thiu : Ngy nay, thit b hin th LCD (Liquid Crystal Display) c s dng trong rt nhiu cc ng dng ca VK. LCD ... Hng dn lp trnh cho PIC bng CCS ver3.242 Bi tip: + Bi 2: S dng Bin v Hm, Cu trc lnh, Ch th tin x l trong CCS Bi 1: Hng dn lp trnh cho PIC bng CCS ver3.24... Code mu PIC - Giao Tip I2C Vi IC Thi gian thc DS1307 . Tham kho thm: Hng dn IC thi gian thc DS1307 Chng trnh thc hin giao tip I2C gia PIC 16F877A v IC DS1307 ci t thi... Code mu cho PIC - Giao tip SPI song cng gia 2 VK PIC Giao tip SPI song cng gia 2 PIC: PIC Master trn truyn d liu t PortB (cng tc trn) qua PIC Slave di hin th ra PortD (LED...

M ngun chng trnh chnh:

#include "16f877a.h" #include "def_877a.h"

#device *=16 ADC=10 #use delay(clock=20000000) #FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT #use i2c(Master, sda = PIN_C4, scl=PIN_C3) #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#include "lcd_lib_4bit.c" #include "ds1307.c"

#define Slave_add 0x68 #define Read 1 #define Write 0

ng Nhp ( theo di bi vit) Tham gia trang web ny

void send(int8 a);

int8 sec,min,hrs,day,month,yr,dow;

vi Kt ni Bn b ca Google

//ngat o chan RB0: Truyen len cong RS232 #int_EXT void EXT_isr(void) //moi 1s truyen len may tinh 1 lan { ds1307_get_date(day,month,yr,dow); ds1307_get_time(hrs,min,sec); send(hrs); putc(45); send(min); putc(45);

Follow by Email Email address... Submit

www.dientumaytinh.com/2012/04/code-mau-pic-giao-tiep-i2c-voi-ic-thoi.html

2/8

26/04/2012
send(sec); putc(10); return;

Code mu PIC - Giao Tip I2C Vi IC Thi gian thc DS1307 | in T My Tnh | K Thut in T | Vi

} void main() { enable_interrupts(INT_EXT);//cho phep ngat RB0 ext_int_edge(0,H_TO_L);//dat suon ngat enable_interrupts(GLOBAL);//cho phep ngat toan cuc LCD_init(); //Khoi tao LCD. delay_ms(10); ds1307_init();// khoi tao DS1307, tao xung 1Hz o chan 7 DS1307. // Set date : 12-4-2012 // Set time : thu 5 - 12 gio, 59 pht 10 giy ds1307_set_date_time(12,4,12,5,12,59,10);

while(1) { ds1307_get_date(day,month,yr,dow); ds1307_get_time(hrs,min,sec); //Truyen len LCD //o day chi hien gio, phut, giay. cac thong tin khac thuc hien tuong tu. LCD_PutCmd(0x80); LCD_PutChar(hrs/10+48); LCD_PutChar(hrs%10+48); LCD_PutChar(45); LCD_PutChar(min/10+48); LCD_PutChar(min%10+48); LCD_PutChar(45); LCD_PutChar(sec/10+48); LCD_PutChar(sec%10+48); } } void send(int8 a) { if(a<10) { putc(a+48); } if(a>9&&a<100) { unsigned char c=a/10; unsigned char d=a%10; putc(c+48); putc(d+48); } if(a>99) { unsigned char t=a/100; unsigned char c=a/10-10*t; unsigned char d=a%10; putc(t+48); putc(c+48); putc(d+48); } }

Th vin lcd_lib_4bit.c:
#include <stddef.h>

#define LCD_RS //#define LCD_RW #define LCD_EN

PIN_D2 PIN_A1 PIN_D3

www.dientumaytinh.com/2012/04/code-mau-pic-giao-tiep-i2c-voi-ic-thoi.html

3/8

26/04/2012
#define LCD_D4 #define LCD_D5 #define LCD_D6 #define LCD_D7

Code mu PIC - Giao Tip I2C Vi IC Thi gian thc DS1307 | in T My Tnh | K Thut in T | Vi
PIN_D4 PIN_D5 PIN_D6 PIN_D7

// misc display defines#define Line_1 0x80 #define Line_2 0xC0 #define Clear_Scr 0x01

// prototype statements #separate void LCD_Init ( void );// ham khoi tao LCD #separate void LCD_SetPosition ( unsigned int cX );//Thiet lap vi tri con tro #separate void LCD_PutChar ( unsigned int cX );// Ham viet1kitu/1chuoi len LCD #separate void LCD_PutCmd ( unsigned int cX) ;// Ham gui lenh len LCD #separate void LCD_PulseEnable ( void );// Xung kich hoat #separate void LCD_SetData ( unsigned int cX );// Dat du lieu len chan Data // D/n Cong #use standard_io (C) #use standard_io (D)

//khoi tao LCD********************************************** #separate void LCD_Init ( void ) { LCD_SetData ( 0x00 ); delay_ms(200); /* wait enough time after Vdd rise >> 15ms */ output_low ( LCD_RS );// che do gui lenh LCD_SetData ( 0x03 ); /* init with specific nibbles to start 4-bit mode */ LCD_PulseEnable(); LCD_PulseEnable(); LCD_PulseEnable(); LCD_SetData ( 0x02 ); LCD_PulseEnable(); LCD_PutCmd ( 0x2C ); LCD_PutCmd ( 0x0C ); LCD_PutCmd ( 0x06 ); LCD_PutCmd ( 0x01 ); } /* set 4-bit interface */ /* send dual nibbles hereafter, MSN first */ /* function set (all lines, 5x7 characters) */ /* display ON, cursor off, no blink */ /* entry mode set, increment & scroll left */ /* clear display */

#separate void LCD_SetPosition ( unsigned int cX ) { /* this subroutine works specifically for 4-bit Port A */ LCD_SetData ( swap ( cX ) | 0x08 ); LCD_PulseEnable(); LCD_SetData ( swap ( cX ) ); LCD_PulseEnable(); }

#separate void LCD_PutChar ( unsigned int cX ) { /* this subroutine works specifically for 4-bit Port A */ output_high ( LCD_RS ); LCD_PutCmd( cX ); output_low ( LCD_RS ); }

#separate void LCD_PutCmd ( unsigned int cX ) { /* this subroutine works specifically for 4-bit Port A */ LCD_SetData ( swap ( cX ) ); /* send high nibble */ LCD_PulseEnable(); LCD_SetData ( swap ( cX ) ); /* send low nibble */ LCD_PulseEnable(); } #separate void LCD_PulseEnable ( void )

www.dientumaytinh.com/2012/04/code-mau-pic-giao-tiep-i2c-voi-ic-thoi.html

4/8

26/04/2012

Code mu PIC - Giao Tip I2C Vi IC Thi gian thc DS1307 | in T My Tnh | K Thut in T | Vi

{ output_high ( LCD_EN ); delay_us ( 3 ); // was 10 output_low ( LCD_EN ); delay_ms ( 3 ); } // was 5

#separate void LCD_SetData ( unsigned int cX ) { output_bit ( LCD_D4, cX & 0x01 ); output_bit ( LCD_D5, cX & 0x02 ); output_bit ( LCD_D6, cX & 0x04 ); output_bit ( LCD_D7, cX & 0x08 ); }

Th vin ds1307.c:
BYTE bin2bcd(BYTE binary_value); BYTE bcd2bin(BYTE bcd_value);

void ds1307_init(void) { BYTE initsec = 0; BYTE initmin=0; BYTE inithr=0; BYTE initdow=0; BYTE initday=0; BYTE initmth=0; BYTE inityear=0; i2c_start(); i2c_write(0xD0); // WR to RTC i2c_write(0x00); // REG 0 i2c_start(); i2c_write(0xD1); // RD from RTC initsec = bcd2bin(i2c_read() & 0x7f); initmin = bcd2bin(i2c_read() & 0x7f); inithr = bcd2bin(i2c_read() & 0x3f); initdow = bcd2bin(i2c_read() & 0x7f); // REG 3 initday = bcd2bin(i2c_read() & 0x3f); // REG 4 initmth = bcd2bin(i2c_read() & 0x1f); // REG 5 inityear = bcd2bin(i2c_read(0)); // REG 6 i2c_stop(); delay_us(3);

i2c_start(); i2c_write(0xD0); // WR to RTC i2c_write(0x00); // REG 0 i2c_write(bin2bcd(initsec)); // Start oscillator with current "seconds value i2c_write(bin2bcd(initmin)); // REG 1 i2c_write(bin2bcd(inithr)); // REG 2 i2c_write(bin2bcd(initdow)); // REG 3 i2c_write(bin2bcd(initday)); // REG 4 i2c_write(bin2bcd(initmth)); // REG 5 i2c_write(bin2bcd(inityear)); // REG 6 i2c_start(); i2c_write(0xD0); // WR to RTC i2c_write(0x07); // Control Register i2c_write(0x90); // squarewave output pin 1Hz i2c_stop();

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec) { sec &= 0x7F; hr &= 0x3F;

www.dientumaytinh.com/2012/04/code-mau-pic-giao-tiep-i2c-voi-ic-thoi.html

5/8

26/04/2012

Code mu PIC - Giao Tip I2C Vi IC Thi gian thc DS1307 | in T My Tnh | K Thut in T | Vi

i2c_start(); i2c_write(0xD0); i2c_write(0x00);

// I2C write address // Start at REG 0 - Seconds

i2c_write(bin2bcd(sec)); // REG 0 i2c_write(bin2bcd(min)); // REG 1 i2c_write(bin2bcd(hr)); // REG 2 i2c_write(bin2bcd(dow)); // REG 3 i2c_write(bin2bcd(day)); // REG 4 i2c_write(bin2bcd(mth)); // REG 5 i2c_write(bin2bcd(year)); // REG 6 i2c_write(0x90); // REG 7 - 1Hz squarewave output pin i2c_stop(); }

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow) { i2c_start(); i2c_write(0xD0); i2c_write(0x03); // Start at REG 3 - Day of week i2c_start(); i2c_write(0xD1); dow = bcd2bin(i2c_read() & 0x7f); // REG 3 day = bcd2bin(i2c_read() & 0x3f); // REG 4 mth = bcd2bin(i2c_read() & 0x1f); // REG 5 year = bcd2bin(i2c_read(0)); // REG 6 i2c_stop(); }

void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec) { i2c_start(); i2c_write(0xD0); i2c_write(0x00); // Start at REG 0 - Seconds i2c_start(); i2c_write(0xD1); sec = bcd2bin(i2c_read() & 0x7f); min = bcd2bin(i2c_read() & 0x7f); hr = bcd2bin(i2c_read(0) & 0x3f); i2c_stop();

BYTE bin2bcd(BYTE binary_value) { BYTE temp; BYTE retval;

temp = binary_value; retval = 0;

while(1) { // Get the tens digit by doing multiple subtraction // of 10 from the binary value. if(temp >= 10) { temp -= 10; retval += 0x10; } else // Get the ones digit by adding the remainder. { retval += temp; break; }

www.dientumaytinh.com/2012/04/code-mau-pic-giao-tiep-i2c-voi-ic-thoi.html

6/8

26/04/2012
}

Code mu PIC - Giao Tip I2C Vi IC Thi gian thc DS1307 | in T My Tnh | K Thut in T | Vi

return(retval); }

// Input range - 00 to 99. BYTE bcd2bin(BYTE bcd_value) { BYTE temp;

temp = bcd_value; // Shifting upper digit right by 1 is same as multiplying by 8. temp >>= 1; // Isolate the bits for the upper digit. temp &= 0x78;

// Now return: (Tens * 8) + (Tens * 2) + Ones

return(temp + (temp >> 2) + (bcd_value & 0x0f)); }

//tjtanja
Nhn: code mu pic, giao tip i2c pic 16f877a, giao tip i2c pic16f877a vi ic ds1307, hin th ln lcd 16x2, truyn thng ni tip RS232

0 nhn xt: ng mt Nhn xt

Cc lin kt vi bi ny
To mt Lin kt Bi ng Mi hn Trang ch Bi ng C hn

Lu tr Blog
thng t 2012 (10) thng ba 2012 (76) thng hai 2012 (40) thng mi 2011 (1)

Lin Kt
nh p Nht Hng Khuyn Mi Th Gii Kim Tin Vic Lm Trn Mng Kim Tin Trn Mng

Lin Kt 2
Tin Nng Trong Ngy c Truyn Tranh

Truy Cp

www.dientumaytinh.com/2012/04/code-mau-pic-giao-tiep-i2c-voi-ic-thoi.html

7/8

26/04/2012

Code mu PIC - Giao Tip I2C Vi IC Thi gian thc DS1307 | in T My Tnh | K Thut in T | Vi
Tin Hot

thng chn 2011 (5) thng tm 2011 (1) thng ba 2011 (1)

HOME ABOUT CONTACT US ADVERTISE


2010 My Blog - All Rights Reserved

Design by Premium Wordpress Themes. Web Design, Buy Plusones, Templates.

www.dientumaytinh.com/2012/04/code-mau-pic-giao-tiep-i2c-voi-ic-thoi.html

8/8

You might also like