Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC18f4550 with ds1307

Status
Not open for further replies.
You can interface it using I2C. You need a clock and data connection for this (SCL and SDA). On the PIC18F4550, Pin 21 is SDA and PIN 22 is SCL. Read chapter 19 of your PIC18F4550 datasheet for details on I2C.
 
ok, i tried to simulate this with proteus, but it doesn't work. what is the problem? it works with pic16f877, but when i try tu simulate pic18f4550, nothing is shown in lcd. what is the problem? (simulation with ds1307)

char seconds, minutes, hours, day, month, year; // Global date/time variables

// Software I2C connections
sbit Soft_I2C_Scl at RD1_bit;
sbit Soft_I2C_Sda at RD0_bit;
sbit Soft_I2C_Scl_Direction at TRISD1_bit;
sbit Soft_I2C_Sda_Direction at TRISD0_bit;
// End Software I2C connections

// LCD module connections
sbit LCD_RS at RD4_bit;
sbit LCD_RW at RD5_bit;
sbit LCD_EN at RD6_bit;
sbit LCD_D4 at RA0_bit;
sbit LCD_D5 at RA1_bit;
sbit LCD_D6 at RA2_bit;
sbit LCD_D7 at RA3_bit;

sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISD6_bit;
sbit LCD_RW_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISA0_bit;
sbit LCD_D5_Direction at TRISA1_bit;
sbit LCD_D6_Direction at TRISA2_bit;
sbit LCD_D7_Direction at TRISA3_bit;
// End LCD module connections

//--------------------- Reads time and date information from RTC (PCF8583)
/*void Set_Time() {

Soft_I2C_Start();
Soft_I2C_Write(0xd0);
Soft_I2C_Write(0x00);
Soft_I2C_Write(0x80);
Soft_I2C_Write(0x01);
Soft_I2C_Write(0x02);
Soft_I2C_Write(0x05);
Soft_I2C_Write(0x01);
Soft_I2C_Write(0x09);
Soft_I2C_Write(0x10);
Soft_I2C_Write(0x80);
Soft_I2C_Write(0x00);

}*/




void Read_Time() {

Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xD0); // Address PCF8583, see PCF8583 datasheet
Soft_I2C_Write(0x02); // Start from address 2
Soft_I2C_Start(); // Issue repeated start signal
Soft_I2C_Write(0xD1); // Address PCF8583 for reading R/W=1

seconds = Soft_I2C_Read(1); // Read seconds byte
minutes = Soft_I2C_Read(1); // Read minutes byte
hours = Soft_I2C_Read(1); // Read hours byte
day = Soft_I2C_Read(1); // Read year/day byte
month = Soft_I2C_Read(0); // Read weekday/month byte
Soft_I2C_Stop(); // Issue stop signal

}

//-------------------- Formats date and time
void Transform_Time() {
seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F); // Transform seconds
minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F); // Transform months
hours = ((hours & 0xF0) >> 4)*10 + (hours & 0x0F); // Transform hours
year = (day & 0xC0) >> 6; // Transform year
day = ((day & 0x30) >> 4)*10 + (day & 0x0F); // Transform day
month = ((month & 0x10) >> 4)*10 + (month & 0x0F); // Transform month
}

//-------------------- Output values to LCD
void Display_Time() {

Lcd_Chr(1, 6, (day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1, 7, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1, 9, (month / 10) + 48);
Lcd_Chr(1,10, (month % 10) + 48);
Lcd_Chr(1,15, year + 56); // Print year vaiable + 8 (start from year 2008)

Lcd_Chr(2, 6, (hours / 10) + 48);
Lcd_Chr(2, 7, (hours % 10) + 48);
Lcd_Chr(2, 9, (minutes / 10) + 48);
Lcd_Chr(2,10, (minutes % 10) + 48);
Lcd_Chr(2,12, (seconds / 10) + 48);
Lcd_Chr(2,13, (seconds % 10) + 48);
}


//------------------ Performs project-wide init
void Init_Main() {

TRISB = 0;
PORTB = 0xFF;
TRISB = 0xff;


ADCON1 = 7;

//ANSEL = 0; // Configure AN pins as digital I/O
//ANSELH = 0x00;
//C1ON_bit = 0; // Disable comparators
//C2ON_bit = 0;

Soft_I2C_Init(); // Initialize Soft I2C communication

LCD_RW = 0;
LCD_RW_Direction = 0;

Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off

Lcd_Out(1,1,"Date:"); // Prepare and output static text on LCD
Lcd_Chr(1,8,':');
Lcd_Chr(1,11,':');
Lcd_Out(2,1,"Time:");
Lcd_Chr(2,8,':');
Lcd_Chr(2,11,':');
Lcd_Out(1,12,"200");
}

//----------------- Main procedure
void main() {

//Soft_I2C_Init();


//Soft_I2C_Config(&PORTD, 4, 3); // Initialize full master mode

/*Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xd0); // Address PCF8583
Soft_I2C_Write(128); // Start from word at address 0 (configuration word)
Soft_I2C_Write(0x80); // Write 0x80 to config. (pause counter...)
Soft_I2C_Write(0); // Write 0 to cents word
Soft_I2C_Write(0); // Write 0 to seconds word
Soft_I2C_Write(0x30); // Write 0x30 to minutes word
Soft_I2C_Write(0x11); // Write 0x11 to hours word
Soft_I2C_Write(0x30); // Write 0x24 to year/date word
Soft_I2C_Write(0x08); // Write 0x08 to weekday/month
Soft_I2C_Write(0);
Soft_I2C_Stop(); // Issue stop signal*/

Delay_ms(2000);

Init_Main(); // Perform initialization

while (1) { // Endless loop
//Set_Time();
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD

Delay_ms(1000); // Wait 1 second
}
}
 
Last edited:
i am realy new to C myself so wont be much help, but i think you have a couple of problems. what compiler are you using? i think ADCON1 = 7; in your code should be something like ADCON1 =0x07, also i dont see anywhere that you have set the clock up for eigther internal or external, i have had alot of trouble in the past by not doing this, i dont know about the 18f4550 so check with the datasheet but for turning off comparrators on the 18f4685 its CMCON=0x07 and for setting the internal clk on the 4685 its OSCCONbits.IRCF0=1; OSCCONbits.IRCF1=1; OSCCONbits.IRCF2=1; this sets it to 8mghz (i think) but again check the sheet for your pic.
hope this is of some small help

p.s would probaly realy help if you posted the full code you are using, then one the smart people on here is more likely to be able to give you some help


actualy having looked thru it again looks like your using MIKROC so you shopuld probaly ignore everything i have said!
 
Last edited:
You can interface it using I2C. You need a clock and data connection for this (SCL and SDA). On the PIC18F4550, Pin 21 is SDA and PIN 22 is SCL. Read chapter 19 of your PIC18F4550 datasheet for details on I2C.

Are you sure that 21 and 22 pin I need?
 
because when i try this code, time appears in lcd, but time is not going. But simulation with proteus work perfectly. maybe problem is with setting? registers?
 
Status
Not open for further replies.

Latest threads

Back
Top