![]() |
![]() |
![]() |
|
|
|||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
|
|
Thread Tools | Display Modes |
|
|
(permalink) |
|
hi,
unfortunately i can not understand MicroC I2C sample. if it's possible, learn me how can i use defined function (Software I²C Library or I²C Library) for PIC16F877A and DS1307, please. i want one sample code for learning that functions. |
|
|
|
|
|
|
(permalink) |
|
I'm not sure I understand the question. Do you want "sample code" that uses MikroC libraries for hardware and software I2C to talk with DS1307 ? or you want us to "explain" sample code you have ?
__________________
http://www.it4um.com |
|
|
|
|
|
|
(permalink) |
|
Thank you arhi,
yes i want "sample code" that uses MikroC libraries for hardware and software I2C to talk with DS1307. i want that be simple to understand and learning. |
|
|
|
|
|
|
(permalink) | |
|
Quote:
Code:
unsigned short hh;
unsigned short mm;
unsigned short ss;
void write_DS1307(unsigned short address, unsigned short data)
{
unsigned short status;
I2C_Start();
I2C_Wr(0xd0);
I2C_Wr(address);
I2C_Wr(data);
I2C_Stop();
}
unsigned short read_DS1307(unsigned short address)
{
unsigned short data;
I2C_Start();
I2C_Wr(0xd0);
I2C_Wr(address);
I2C_Repeated_Start();
I2C_Wr(0xd1);
data=I2C_Rd(0);
while (!I2C_Is_Idle()) asm nop;
I2C_Stop();
return(data);
}
void main(){
PORTB = 0;
TRISB = 0;
PORTD = 0;
TRISD = 0;
hh = 0;
mm = 0;
ss = 0;
Lcd_Init(&PORTD);
Lcd_Out(1,1,"Init...");
I2C_Init(100000); //DS1307 operates at 100Khz only
ss=read_ds1307(0);
write_ds1307(0, ss & 0x7F); // enable oscillator(bit 7 =0)
ss=read_ds1307(2);
write_ds1307(2, ss & 0b10111111); // set 24H mode
while(1)
{
ss=read_ds1307(0); // read second
mm=read_ds1307(1); // read minute
hh=read_ds1307(2); // read hour
/*
day=read_ds1307(3); // read day
date=read_ds1307(4); // read date
month=read_ds1307(5); // read month
year=read_ds1307(6); // read year
*/
Lcd_Chr(1,1, 48+ ((hh & 0b00110000) >> 4));
Lcd_Chr(1,2, 48+ (hh & 0b00001111) );
Lcd_Chr(1,3, ':');
Lcd_Chr(1,4, 48+ ((mm & 0b01110000) >> 4));
Lcd_Chr(1,5, 48+ (mm & 0b00001111) );
Lcd_Chr(1,6, ':');
Lcd_Chr(1,7, 48+ ((ss & 0b01110000) >> 4));
Lcd_Chr(1,8, 48+ (ss & 0b00001111) );
delay_ms(1000);
}
}
using the software library is exactly the same, only you have to use Soft_I2C_Start(); Soft_I2C_Write(12); .. and similar functions from software library, just use them instead of these from the hw library and it will work (do not forget to first run Soft_I2C_Config() to configure software library for the pins you will use for SDA and SCL you have here 2 simple functions defined write_DS1307() read_DS1307() these two functions are basically "read/write single byte from I2C device" only the 0xD0 slave address of the DS1307 is hardcoded. if for example you use some other slave device, you can read write from any other I2C device (for e.g. 24cxxx i2c eeprom) if you have any questions .. feel free to ask. If you have ISIS I can upload the design so you can play with it
__________________
http://www.it4um.com |
||
|
|
|
|
|
(permalink) |
|
Dear arhi,
thank you very much for your assistance. it was very good to me. i have ISIS, please upload that files, if it's possible. have a nice day |
|
|
|
|
|
|
(permalink) |
|
here you go, mikroC project and ISIS design included
__________________
http://www.it4um.com |
|
|
|
|
|
|
(permalink) |
|
thank you dear arhi,
you solved my problem. my english is not well and i can not tell my intention to you. so i can say thank you, only. |
|
|
|
|
|
|
(permalink) |
|
Dear Arhi,
i understood your code but i have a problem with this part of code: Lcd_Chr(1,1, 48+ ((hh & 0b00110000) >> 4)); Lcd_Chr(1,2, 48+ (hh & 0b00001111) ); i can not understand this segment. please explain me why you "and" hh and 0b00110000? why you shift 4 times? and why add those with 48? |
|
|
|
|
|
|
(permalink) |
|
The first line is printing the 10s of hours. I.E 21:45
So, hh & 0b00110000 will give either 0x00, 0x10 or 0x02. When shifted it becomes 0,1 or 2. The add 48 then converts it to "0", "1" or "2". The second line prints the unit hours and so doesn't need the shift. I.E 21:45 This will only work if the DS1307 is in 24 hour mode. Mike. |
|
|
|
|
|
|
(permalink) |
|
@hesam,
You have to read the data sheet to be able to use any device .. same thing is with DS1307 ... If you look at the datasheet, on the address 0x02 bit 6 set's the clock in 12/24 hour mode. the line Code:
ss=read_ds1307(2); write_ds1307(2, ss & 0b10111111); // set 24H mode As Mike already explained, the anding and shifting is used to read data as per data sheet. If you look at the data sheet carefully, you will notice that for minutes and seconds the "first digit" or "tens of seconds/minutes" is written in bits 4,5,6, so I use and to mask those bits and then shift left 4 times to get the actual value. 48 is ASCII of '0' so adding digit value to 48 will result of ASCII value of that digit. the second digit (seconds/minutes) is just masked (no need to shift as it is already at right place) If you want "value" of the minutes for example and not "ASCII DIGITS" then you would do something like: Code:
minute =((mm & 0b01110000) >> 4)*10 + (mm & 0b00001111); Code:
ss=read_ds1307(0); write_ds1307(0, ss & 0x7F); // enable oscillator(bit 7 =0) note also that DS1307 have 56 bytes of RAM that you can use to store data from your program. That data will be present while batt is connected to the DS1307 so you can use it as a small i2c eeprom to store 56 bytes of data... example how to read/write data : Code:
//first parameter is address, you can use 0x08-0x3fH //second parametar is the 8bit value you want to save write_DS1307(0x08, 123); //first parameter is address, you can use 0x08-0x3fH val = read_DS1307(0x08); //val now have value of 123 as that is what we stored in prev statement
__________________
http://www.it4um.com |
|
|
|
|
|
|
(permalink) |
|
thank you very much dear arhi.
i am designing a watch with 12, 7-segments and some latchs and decoder and lm35, buzzer, led, . . . when schematic is done, for understanding of my designing errors, i will send it to you. thank you so much... |
|
|
|
|
|
|
(permalink) |
|
Going to be a pretty big watch.
|
|
|
|
|
|
|
(permalink) |
|
Hi,
i wrote this code with "Software I²C Library" functions. after rewriting Arhi code with "Software I²C Library" functions, i compiled it, but when i simulate it with ISIS, nothing appeared on LCD. i think that my program is not right. tell me where is my problem, please. //-------------------------------------------------------------------------------------- unsigned short hh; unsigned short mm; unsigned short ss; unsigned char x[17]; void write_DS1307(unsigned short address, unsigned short data) { unsigned short status; Soft_I2C_Start(); Soft_I2C_Write(0xd0); Soft_I2C_Write(address); Soft_I2C_Write(data); Soft_I2C_Stop(); } unsigned short read_DS1307(unsigned short address) { unsigned short data; Soft_I2C_Start(); Soft_I2C_Write(0xd0); Soft_I2C_Write(address); //Soft_I2C_Read(); Soft_I2C_Write(0xd1); data=Soft_I2C_Read(0); //while (!I2C_Is_Idle()) asm nop; Soft_I2C_Stop(); return(data); } void main(){ PORTD = 0; TRISD = 0; hh = 0; mm = 0; ss = 0; Lcd_Init(&PORTD); Lcd_Out(1,1,"Init..."); //I2C_Init(100000); //DS1307 operates at 100Khz only Soft_I2C_Config(&PORTC, 0, 1); ss=read_ds1307(0); write_ds1307(0, ss & 0x7F); // enable oscillator(bit 7 =0) ss=read_ds1307(2); write_ds1307(2, ss & 0b10111111); // set 24H mode while(1) { ss=read_ds1307(0); // read second mm=read_ds1307(1); // read minute hh=read_ds1307(2); // read hour /* day=read_ds1307(3); // read day date=read_ds1307(4); // read date month=read_ds1307(5); // read month year=read_ds1307(6); // read year */ Lcd_Chr(1,1, 48+ ((hh & 0b00110000) >> 4)); Lcd_Chr(1,2, 48+ (hh & 0b00001111) ); Lcd_Chr(1,3, ':'); Lcd_Chr(1,4, 48+ ((mm & 0b01110000) >> 4)); Lcd_Chr(1,5, 48+ (mm & 0b00001111) ); Lcd_Chr(1,6, ':'); Lcd_Chr(1,7, 48+ ((ss & 0b01110000) >> 4)); Lcd_Chr(1,8, 48+ (ss & 0b00001111) ); delay_ms(1000); } } |
|
|
|
|
|
|
(permalink) |
|
Here's a nice tip.
__________________
========================= Futz's Microcontrollers & Robotics ========================= |
|
|
|
|
|
|
(permalink) |
|
ok, i made it true and i have not any problem with that.
|
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Latest |
| Quik PIC Programming kit | Krumlink | General Electronics Chat | 5 | 27th January 2008 11:27 PM |
| Capturing and reproducing audio with a PIC | Fred.Amoson | Micro Controllers | 14 | 14th December 2007 08:21 PM |
| Controlling i2c data bus by PIC 16f84a | rukshankb | Micro Controllers | 3 | 30th October 2007 01:55 PM |
| High ADC sampling rate PIC, 18F needed? | bananasiong | Micro Controllers | 24 | 28th October 2007 12:13 PM |
| Need help! I2c data bus with pic 16f84a | rukshankb | Micro Controllers | 2 | 7th October 2007 11:57 AM |