• 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.

Code for the Real Time Clock MCP79510 with SPI bus

Winch

New Member
Thread starter #1
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?
 

Winch

New Member
Thread starter #3
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:
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:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#4
Today I spent the entire day with the SPI bus.
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:
      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
 

Winch

New Member
Thread starter #5
Thanks Ian,

At the end of the day it was a very stupid mistake I made!
I made the mistake of connecting both the inputs and outputs of the spi bus together!
It was a hardware mistake. When adjusting the output to input and vice versa, the problem was solved.
Anyway thanks for helping.
 

Latest threads

EE World Online Articles

Loading

 
Top