BoxtecSupportShopKontaktPlaygroundSitemap

Author Topic: Real time with 18F4550 and ds3234  (Read 1173 times)

niko

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
Real time with 18F4550 and ds3234
« on: December 06, 2016, 07:54:47 AM »
i am studying about pic18f4550, my teacher gave me code for real time using 18f4550 and ds1306, he asked me replace ds1306 by ds3234, i dont know what different between ds1306 and ds3234, someone help me?
Code: [Select]
#include <p18f4550.h>
#include <delays.h>
 
#pragma config PBADEN = OFF
 
#define Sec_Unit_Digit PORTAbits.RA0
#define Sec_Ten_Digit PORTAbits.RA1
#define Min_Unit_Digit PORTAbits.RA2
#define Min_Ten_Digit PORTAbits.RA3
#define Hour_Unit_Digit PORTAbits.RA4
#define Hour_Ten_Digit PORTAbits.RA5
#define Data_Display PORTD

#define SCL PORTBbits.RB1
#define SDI PORTBbits.RB0
#define SDO PORTCbits.RC7
#define CE PORTCbits.RC2
 
#define SCL_Direction TRISBbits.TRISB1
#define SDI_Direction TRISBbits.TRISB0
#define SDO_Direction TRISCbits.TRISC7
#define CE_Direction TRISCbits.TRISC2

void Setup_RTC(void);
unsigned char SPI(unsigned char);
unsigned char Display(unsigned char k);
 
unsigned char data[7];
void main()
{
   unsigned char i, j;
   unsigned char Hour_Ten, Hour_Unit;
   unsigned char Min_Ten, Min_Unit;
   unsigned char Sec_Ten, Sec_Unit;
 
   ADCON1 = 0x0F;
   
   TRISA = 0x00;
   TRISD = 0x00;
   SSPSTAT = 0;
   SSPCON1 = 0x22;
   CE_Direction = 0;
   SCL_Direction = 0;
   SDI_Direction = 1;
   SDO_Direction = 0;
   Sec_Unit_Digit = 0;
   Sec_Ten_Digit = 0;
   Min_Unit_Digit = 0;
   Min_Ten_Digit = 0;
   Hour_Unit_Digit = 0;
   Hour_Ten_Digit = 0;
   Setup_RTC();
 
   while(1)
      {
  CE = 0;
  Delay100TCYx(50);
  SPI(0x00);
  for(i = 0;i < 7; i++)
     {
        data[i] = SPI(0x00);
     }
  CE = 1;
 
     Hour_Ten = data[2] >> 4;
     Hour_Unit = data[2] & 0b00001111;
     Min_Ten = data[1] >> 4;
     Min_Unit = data[1] & 0b00001111;
     Sec_Ten = data[0] >> 4;
     Sec_Unit = data[0] & 0b00001111;
 
     for (j = 0; j < 42 ; j++)
        {
    Data_Display = Display(Hour_Ten);
    Hour_Ten_Digit = 1;
                  Delay10TCYx(50);
    Hour_Ten_Digit = 0;
    Data_Display = Display(Hour_Unit);
    Hour_Unit_Digit = 1;
    Delay10TCYx(50);
    Hour_Unit_Digit = 0;
 
    Data_Display = Display(Min_Ten);
    Min_Ten_Digit = 1;
                  Delay10TCYx(50);
    Min_Ten_Digit = 0;
    Data_Display = Display(Min_Unit);
    Min_Unit_Digit = 1;
    Delay10TCYx(50);
    Min_Unit_Digit = 0;
 
    Data_Display = Display(Sec_Ten);
    Sec_Ten_Digit = 1;
    Delay10TCYx(50);
    Sec_Ten_Digit = 0;
    Data_Display = Display(Sec_Unit);
    Sec_Unit_Digit = 1;
    Delay10TCYx(50);
    Sec_Unit_Digit = 0;
    }
 }
}

// Initialize DS1306
void Setup_RTC()
{
   CE = 0; // Enable the RTC.
   Delay100TCYx(50); .
   SPI(0x8E);
   SPI(0x0F);
   CE = 1;
   Delay100TCYx(50);
  CE = 0; .
   Delay100TCYx(50);
   SPI(0x8F);
   SPI(0x0F);
   CE = 1;
   Delay100TCYx(50);
   CE = 0;
   SPI(0x80);
   SPI(0x55);
   SPI(0x58);
   SPI(0x17);
   SPI(0x03);
   SPI(0x26);
   SPI(0x01);
   SPI(0x14);
   CE = 1;
   Delay100TCYx(50);
}

unsigned char SPI(unsigned char myByte)
{
   SSPBUF = myByte;
   while(!SSPSTATbits.BF);
      return SSPBUF;
}

unsigned char Display(unsigned char k)
{
   unsigned char Number;
   unsigned char Code_Tab[ ] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92,0x82,
      0xF8, 0x80, 0x90};
      Number = Code_Tab[k];
      return (Number);
}

pylon

  • freakyfriday
  • Full Member
  • *
  • Posts: 131
  • Karma: +15/-0
Re: Real time with 18F4550 and ds3234
« Reply #1 on: December 06, 2016, 04:46:15 PM »
The lower 14 registers of the DS3234 are identical to the DS1306, so for most applications the usage is more or less the same. From the hardware point of view the DS3234 has a temperature compensated oscillator, so it's much more precise in the long term.

 

anything