element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Pic Microcontrollers Forum PIC18F452 with i2c problems in receiving data from DS 1307
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 0 replies
  • Subscribers 189 subscribers
  • Views 185 views
  • Users 0 members are here
  • i2c
Related

PIC18F452 with i2c problems in receiving data from DS 1307

kavikarthi
kavikarthi over 11 years ago

Dear friends

                  In this below program i try to receive data for DS1307. during data write code execute correctly. but i case of receiving the 0xff in sspbuf.. plaese help me... i'm a new leaner..

 

#include<htc.h>

//#include "lcd4.h"

#pragma config OSC=HS,OSCS=OFF

#pragma config BORV=45,PWRT=ON,BOR=ON

#pragma config WDT=OFF

#pragma config DEBUG=ON,LVP=OFF,STVR=OFF

#define _XTAL_FREQ 8000000

#define _LEGACY_HEADERS

//#define PORTBIT(addr,bit)((unsigned)(&addr)*8+(bit))

//static bit scl @ PORTBIT(PORTC,3);

//static bit sda @ PORTBIT(PORTC,3);

#define ldata PORTD

#define devadd 0x68

#define memadd 0xD0

#define data0  50

#define data1     10

unsigned char REC_DATA[20];

unsigned char value,i;

void i2c_init();

void i2c_Bwrite();  

void i2c_Bread();

void startcond();

void stopcond();

void restartcond();

void poll();

//void Tdispay();

void tx_byte(char);

void rx_byte();

void main()

{

while(1)

{

i2c_init();

i2c_Bwrite();

i2c_Bread();

//Tdisplay();

}

}

 

void i2c_init(){

TRISC=0Xff;

SSPSTAT=0;

SMP=1;    //slew rate control disable

SSPADD=0X18; // for 8mhz and 100Khz bit rate   

SSPCON1=0X28;

SSPCON2=0X00;

SSPIF=0;

BCLIF=0;

}

void i2c_Bwrite()

{

startcond();

//tx_byte(devadd);

//tx_byte(memadd);

tx_byte(0xd0);// device address

tx_byte(0x01);// sec

tx_byte(0x30);//min

tx_byte(0x09);//hours

tx_byte(0x02);date

tx_byte(0x07);

tx_byte(0x09);

tx_byte(0x09);

stopcond();

}

void i2c_Bread()

{

startcond();

tx_byte(0xD0);// device address

restartcond();//restart

tx_byte(0xD1);// address which location want to read

rx_byte();

//ACKDT=1;

tx_byte(0xD3);

rx_byte();

ACKDT=1;

for (i=0;i<4;i++)

{

rx_byte();

ACKDT=0;

}

stopcond();

}

void tx_byte(char tx_value)

{

SSPIF=0;

SSPBUF=tx_value;

while(SSPIF==0);

while(ACKSTAT==1);

return;

}

void rx_byte()

{

static unsigned char RXI;

SSPIF=0;

RCEN=1;

while(SSPIF==0);

value=SSPBUF;

REC_DATA[RXI++]=value;

SSPIF=0;

ACKEN=1;

return;

}

void startcond()

{

SSPIF=0;

SEN=1;

while(SSPIF==0);

return;

}

void restartcond()

{

SSPIF=0;

RSEN=1;

while(SSPIF==0);

return;

}

void stopcond()

{

SSPIF=0;

PEN=1;

while(SSPIF==0);

return;

}

Attachments:
image
  • Sign in to reply
  • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube