Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 17th May 2008, 08:30 PM   (permalink)
Default PIC + I2C + MicroC

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.
Hesam Kamalan is offline  
Reply With Quote
Old 17th May 2008, 08:34 PM   (permalink)
Default

Quote:
Originally Posted by Hesam Kamalan View Post
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.
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
arhi is online now  
Reply With Quote
Old 17th May 2008, 09:14 PM   (permalink)
Default

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.
Hesam Kamalan is offline  
Reply With Quote
Old 17th May 2008, 10:25 PM   (permalink)
Default

Quote:
Originally Posted by Hesam Kamalan View Post
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.
I cannot make it simpler then this:
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);
  }
}
this is using HW i2c library ... tested (in ISIS) and working as expected ...
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
arhi is online now  
Reply With Quote
Old 18th May 2008, 06:48 AM   (permalink)
Default

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
Hesam Kamalan is offline  
Reply With Quote
Old 18th May 2008, 05:12 PM   (permalink)
Default

here you go, mikroC project and ISIS design included
Attached Files
File Type: zip yyy.zip (58.0 KB, 15 views)
__________________
http://www.it4um.com
arhi is online now  
Reply With Quote
Old 18th May 2008, 07:28 PM   (permalink)
Default

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.
Hesam Kamalan is offline  
Reply With Quote
Old 19th May 2008, 08:00 AM   (permalink)
Default

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?
Hesam Kamalan is offline  
Reply With Quote
Old 19th May 2008, 08:29 AM   (permalink)
Default

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.
Pommie is offline  
Reply With Quote
Old 19th May 2008, 10:20 AM   (permalink)
Default

@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
set's the device to 24h mode. (first read the content of address 0x02 and then return it with 6th bit cleared). The other way you could do this is to read the bit, and then read the data accordingly (you interpret data from address 0x02 differently for 24h and 12h 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);
note also that you need to turn the internal oscillator ON (CH, 7th bit of address 0x00) .. the example code does it:
Code:
  ss=read_ds1307(0);
  write_ds1307(0, ss & 0x7F); // enable oscillator(bit 7 =0)
What you can try to do now is to upgrade this app to display also the DATE, and additionally, you can upgrade it to "SET NEW DATE" ... you have all the software components you need, you only need to add some button switches to ISIS model so you can test user input to be able to set new time/date.

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
all clear now? if not, first read the datasheet and if still not clear post a question
__________________
http://www.it4um.com
arhi is online now  
Reply With Quote
Old 19th May 2008, 08:56 PM   (permalink)
Default

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...
Hesam Kamalan is offline  
Reply With Quote
Old 19th May 2008, 11:31 PM   (permalink)
Default

Quote:
Originally Posted by Hesam Kamalan View Post
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...
Going to be a pretty big watch.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is offline  
Reply With Quote
Old 23rd May 2008, 03:30 PM   (permalink)
Default

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);
}
}
Hesam Kamalan is offline  
Reply With Quote
Old 23rd May 2008, 03:38 PM   (permalink)
Default

Quote:
Originally Posted by Hesam Kamalan View Post
i wrote this code
Here's a nice tip. When you post code here, be sure to put it inside Code tags. Easiest way is to click the # in the menu just before pasting your code. Alternately, you can type in [code] before your code and [/code] after your code. This forces the web-site to keep your formatting so people can actually read your code. When you don't do this, it loses all indentation and turns into the unreadable lump in your original post.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================
futz is offline  
Reply With Quote
Old 23rd May 2008, 08:47 PM   (permalink)
Default

ok, i made it true and i have not any problem with that.
Hesam Kamalan is offline  
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
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



All times are GMT. The time now is 11:32 PM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.