1. 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.
    Dismiss Notice

Code for the Real Time Clock MCP79510 with SPI bus

Discussion in 'Oshonsoft' started by Winch, Jan 13, 2018.

  1. Winch

    Winch New Member

    Joined:
    Sep 5, 2014
    Messages:
    26
    Likes:
    0
    Has anyone written a piece of software for the RTC MCP79510 before?
    I would like to start with this but no experience with the SPI BUS.
    Can someone help with a code?
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,393
    Likes:
    923
    Location:
    Rochdale UK
    ONLINE
  3. Winch

    Winch New Member

    Joined:
    Sep 5, 2014
    Messages:
    26
    Likes:
    0
    Thanks Ian

    Today I spent the entire day with the SPI bus.
    But do not continue with results?
    I have tried to translate it back to basic. But now I'm not sure whether it's the code or the hardware?

    With this code I hoped to make the seconds from the RTC visible on the display! But I only keep seeing the zero from the variable 'Data'
    I need some feedback to understand what is happening?

    Code (basic):

    DEVICE PIC16F887

    Define CONFIG1 = 0x23e4  '23e4
    Define CONFIG2 = 0x3eff

    OSCCON = %01110101  'Oscillator control (datasheet page 66)

    'Defines the parameters of the LCD interfacing
    Define LCD_BITS = 4
    Define LCD_DREG = PORTD
    Define LCD_DBIT = 4
    Define LCD_RSREG = PORTD
    Define LCD_RSBIT = 2
    Define LCD_EREG = PORTD
    Define LCD_EBIT = 3
    Define LCD_RWREG = PORTD
    Define LCD_RWBIT = 0

    'Defines the parameters of the PSI interfacing
    Define SPI_SCK_REG = PORTC
    Define SPI_SCK_BIT = 3
    Define SPI_SDI_REG = PORTC
    Define SPI_SDI_BIT = 4
    Define SPI_SDO_REG = PORTC
    Define SPI_SDO_BIT = 5
    Define SPI_CS_REG = PORTD
    Define SPI_CS_BIT = 0
    SPIPrepare

    AllDigital  'alle ports digital

    'declaring variable
    Dim ledgreen As Byte
    Dim data As Byte

    'A gate set to the desired value
    '76543210
    PORTA = %00000000  'make all ports "PORTA" low
    TRISA = %11101111  'RA7,RA6,RA5,RA3,RA2,RA1,RA0 are set for input. RA4, is set for output!
    ANSEL = %11000010  '1 = Analog input, RA1,RA6 and RA7 are set for analog input, 0 = digital input, RA0,RA2,RA3,RA4 and RA5 are set digital I/O input.

    'B gate set to the desired value
    '76543210
    PORTB = %00000000  'make all ports "PORTB" low
    TRISB = %11111111  'all ports are set for input
    WPUB = %11001010  'weak pull-up portB register. 1 = pull-up enabled 0 = pull-up disabled (datasheet page 51)

    'C gate set to the desired value
    '76543210
    PORTC = %00000000  'make all ports "PORTC" low
    TRISC = %10111001  'port RC1 & RC2 (CCP1 & CCP2) are set for output

    'D gate set to the desired value
    '76543210
    PORTD = %00000000  'make all ports "PORTD" low
    TRISD = %11111111  'are set for input

    Lcdinit 0  'no cursor

    ledgreen = 1  'turn on the green led!

            SPICSOn  'select the RTC bij pulling CS low
            SPISend 0x12  'send the write command
            SPISend 0x01  'send address for 'seconds'
            SPISend 0  'set seconds to zero
            SPICSOff  'deselect the RTC bij pulling CS high

    main:  'endless loop

            SPICSOn  'select the RTC bij pulling CS low
            SPISend 0x13  'send the read command
            SPISend 0x01  'send address for 'seconds'
            SPIReceive data  'recieve data and store it in 'data'
            SPICSOff  'deselect the RTC bij pulling CS high
            Lcdcmdout LcdClear
            Lcdout "Sec             "
            Lcdcmdout LcdLine1Pos(5)
            Lcdout #data
            WaitMs 100

    Goto main
    End  'end program
     
     
    Last edited by a moderator: Jan 14, 2018 at 12:44 PM
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,393
    Likes:
    923
    Location:
    Rochdale UK
    ONLINE

    Not the best way to spend a Sunday!!

    Most of the RTC have a stop condition... Namely the seconds register bit 7 "ST" This needs to be written to start the RTC...
    Code (basic):

          SPICSOn  'select the RTC bij pulling CS low
            SPISend 0x12  'send the write command
            SPISend 0x01  'send address for 'seconds'
            SPISend 0x80  'set seconds to zero
            SPICSOff  'deselect the RTC bij pulling CS high
     
     

Share This Page