PDA

View Full Version : real time clock - PCF8583



maria
- 22nd May 2004, 12:37
Hi there,

Does anyone out there have PIC BASIC code to interface a PIC (I'm using the PIC16F877) to a PCF8583 real time clock IC?

Thanks!

Maria.

lupuccio
- 17th June 2005, 07:14
Anyone can help me ?
I'm looking for 8583 routines to read and write data in i2c.
How can i increase minutes and then rewrite to 8583 ?
Thanks to all.....

angelika
- 17th June 2005, 13:45
The pins: sda and scl as inputs or outputs to microcontoller?

If As outputs:

sda=1 'high the portx.y, x is the port of micr.,y is the bit of portx.
scl=1 'high the same as previous

DECLARE HBUS_BITRATE 100 ' and other values

TRISC=%00000000 'as outputs

after you try with:hbstart,hbusout,hbstop (in pic basic functions)

if you have other problem you ask me.

regards

mister_e
- 17th June 2005, 15:04
DECLARE HBUS_BITRATE 100 ' and other values

TRISC=%00000000 'as outputs

after you try with:hbstart,hbusout,hbstop (in pic basic functions)


this will never work with Melabs. In case it's working... you should have a look on that forum ;) (http://www.picbasic.org/forum/index.php?)

Emil G
- 17th June 2005, 19:25
Hi Maria, Lupuccio and PCF8583 users.

Code:
'************************************************* ***************
' Ime programa: RTC.BAS
' Opis: program ilustruje primenu sata realnog vremena PCF8583.
' Za povezivanje sa mikrokontrolerom se koristi I2CREAD
' naredba.
'************************************************* ***************

DEFINE LCD_DREG PORTD ' I/O port na kome se nalazi LCD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTD
DEFINE LCD_RSBIT 2 ' Register select pin
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 3 ' Enable pin
DEFINE LCD_BITS 4 ' 4-bitni data bus
DEFINE LCD_LINES 2 ' LCD ima dva reda karaktera

symbol SCL = PORTC.3 ' I2C data pin
symbol SDA = PORTC.5 ' I2C clock pin

Sec var bit
B0 var byte
S var byte ' Sekunde
M var byte ' Minuti
H var byte ' Sati
D var byte ' Dan
Mn var byte ' Mesec
Y var byte ' Godina

OPTION_REG = $7f ' Omoguci PORTB pullup otpornike
lcdout $fe,1 ' Obrisi ekran displeja
Main:

I2CREAD SDA,SCL,%10100001,2,[S]
I2CREAD SDA,SCL,%10100001,3,[M]
I2CREAD SDA,SCL,%10100001,4,[H]
I2CREAD SDA,SCL,%10100001,5,[D]
I2CREAD SDA,SCL,%10100001,6,[Mn]
I2CREAD SDA,SCL,%10100001,6,[Y]



lcdout $fe,1 ' Obrisi displej
lcdout $fe,2 ' Prvi red prvi karakter
lcdout "Time: ", hex(H),":", hex(M)," ", hex(S)
lcdout $fe,$c0 ' Drugi red, prvi karakter
lcdout "Date: ", hex(D),".", hex(Mn),".20",hex(Y)

Pause 500
goto Main ' Ponovi sve

End ' Kraj programa

lupuccio
- 24th June 2005, 15:59
Thanks to all for the answers.....
The only problem that i have... ( a big problem ) is set hour and minutes with buttons.
Unfortunely i haven't the code here.
Next week i will post my code.
and then i hope that someone can help me.
Thanks again to all.

mister_e
- 24th June 2005, 16:12
That's not a problem... learning opportunuities.

USE I2CWRITE with the same settings as I2CREAD should work

you just need to test your pushbbuton...


lcdout $fe,1 ' Obrisi displej
lcdout $fe,2 ' Prvi red prvi karakter
lcdout "Time: ", hex(H),":", hex(M)," ", hex(S)
lcdout $fe,$c0 ' Drugi red, prvi karakter
lcdout "Date: ", hex(D),".", hex(Mn),".20",hex(Y)
'<== Here
Pause 500
goto Main ' Ponovi sve


but certainely better if you use something like


lcdout $fe,1 ' Obrisi displej
lcdout $fe,2 ' Prvi red prvi karakter
lcdout "Time: ", hex(H),":", hex(M)," ", hex(S)
lcdout $fe,$c0 ' Drugi red, prvi karakter
lcdout "Date: ", hex(D),".", hex(Mn),".20",hex(Y)

While Delay<5000 ' Delay loop of 500 mSec
' Select case pushbutton and set/hour time here+I2CWRITE
' if there's no push button pressed Do a PAUSEUS 100 and increment
' Delay Variable.
'
'
Wend
goto Main ' Ponovi sve


yeah just an idea. not everything on a plate and you'll have to do some homework around the above.

NavMicroSystems
- 24th June 2005, 22:43
The PCF8583 is very similar to DS1307.

Melanie has written an excellent example for the DS1307.

click here (http://www.melabs.com/resources/samples/submitted/MN1307.txt) for MN1307.txt

It should work on the PCF8583 with very small modifications.

lupuccio
- 1st July 2005, 14:20
part is mine and part i' have found on the net.
with my circuit it works
Thanks to all.
'********************************
device = 16f876A
xtal = 4
CONFIG XT_OSC , WDT_OFF , PWRTE_ON , BODEN_OFF , LVP_OFF , CP_OFF , DEBUG_OFF
LCD_DTPIN portb.4 ' dati lcd
LCD_RSPIN portb.1 ' register select
LCD_ENPIN portb.3 ' enable lcd 1
LCD_INTERFACE 4 ' interfaccia lcd 4 bit
LCD_LINES 2 ' linee lcd
DECLARE SDA_PIN PORTC.4
DECLARE SCL_PIN PORTC.3
'*** variabili ************************************************** ***************
delayms 200 'stabilizza LCD
cls
dim bcd_to_bin 'variable used in BCD and Binary conversion routines
dim low_bits 'low byte for Binary to BCD routine
dim high_bits 'high byte for Binary to BCD routine
dim pcf8583_address 'pcf8583 internal registers address(2=seconds,3=minutes etc.)
Dim second as byte
Dim minute as byte
Dim hour as byte
dim setup as byte
symbol clkout=%10100000 'set the 8583 to receive data
symbol clkin=%10100001 'set the 8583 to transmit data
Symbol button1 = portc.0
Symbol button2 = portc.1
Symbol button3 = portc.2
pcf8583_address = 0
setup = 0
second = 0
minute = 0
hour = 0
cls
delayms 200
' qui inizia il programma che legge dal 8583
C_upd:
'*** Visualizzazione ora ************************************************** *****
busin %10100001,2,[second,minute,hour]
pcf8583_address = 2:gosub read_time 'read seconds data from 8583
second=bcd_to_bin 'convert to binary
pcf8583_address = 3:gosub read_time 'read minute data from 8583
minute=bcd_to_bin 'convert to binary
pcf8583_address = 4:gosub read_time 'read hour data from 8583
hour=bcd_to_bin 'convert to binary
print at 1,5, dec2 hour,":",dec2 minute,":",dec2 second
'*** menu start
if button1 = 0 then inc setup
if setup = 1 then goto set_min
if setup = 2 then goto set_hour
if setup > 2 then
print at 2,1," "
setup = 0
endif
goto C_upd

'************************************************* ******************************
set_min:
print at 2,5,"Set Min "
if button2 =0 then
busin clkin,3,[bcd_to_bin] 'leggi i minuti
gosub conv_to_bin ' li converte in binario
inc bcd_to_bin
if bcd_to_bin > 59 then bcd_to_bin = 0
delayms 100
gosub convert_to_bcd
busout clkout,3,[bcd_to_bin]
delayms 10
endif
goto c_upd
'************************************************* ******************************
set_hour:
print at 2,5,"Set Hour"
if button2 =0 then
busin clkin,4,[bcd_to_bin] 'leggi le ore
gosub conv_to_bin ' li converte in binario
inc bcd_to_bin
if bcd_to_bin > 23 then bcd_to_bin = 0
delayms 100
gosub convert_to_bcd
busout clkout,4,[bcd_to_bin]
delayms 10
endif
goto c_upd
'*** antirimbalzo ************************************************** ************
'Debounce: DELAYMS 200
' Update=1
' goto c_upd
'************************************************* ******************************
' legge il dato dall'indirizzo puntato da "pcf8583_address"
' il dato viene memorizzato nella variabile "bcd_to_bin"
read_time:
busin clkin,pcf8583_address,[bcd_to_bin]
gosub conv_to_bin
return
'************************************************* ******************************
' Questa routine converte il byte da binario a decimale e lo carica nella
' variabile "bcd_to_bin" e lo ritorna nella stessa variabile "bcd_to_bin"
' Se il valore è "9" o meno, non fa nulla perche in BCD o BIN sono uguali.
conv_to_bin:
select bcd_to_bin
case 16 to 25
bcd_to_bin=bcd_to_bin-6
case 32 to 41
bcd_to_bin=bcd_to_bin-12
case 48 to 57
bcd_to_bin=bcd_to_bin-18
case 64 to 73
bcd_to_bin=bcd_to_bin-24
case 80 to 89
bcd_to_bin=bcd_to_bin-30
case 96 to 105
bcd_to_bin=bcd_to_bin-36
case 112 to 121
bcd_to_bin=bcd_to_bin-42
case 128 to 137
bcd_to_bin=bcd_to_bin-48
case 144 to 153
bcd_to_bin=bcd_to_bin-54
endselect
return
'************************************************* ******************************
' Conversione da binario a BCD
' Il byte convertito viene caricato nella variabile bcd_to_bin
' e viene restituito nella stessa variabile bcd_to_bin
convert_to_bcd:
low_bits=bcd_to_bin//10 'get lsb,same for Bin and BCD
high_bits=bcd_to_bin/10 'get msb
bcd_to_bin=high_bits*16 'convert msb to BCD
bcd_to_bin=bcd_to_bin+low_bits 'add BCD msb and lsb together
return

ffambrin
- 26th February 2006, 19:37
Dear friends, Maria and all:
My name's Francisco Fambrini and
I am a Brazilian Electronic Engineer and my website is www.fdai.net

I would like to help my friends that have problems with PCF8583 and Melabs PICBasicPRO
My code for this :
'************************************************* ***************
' PCF 8583 APREDENDO_1.0
' programa para testar o chip PCF8583
' na placa do Distribuidor de Energia
' 26/Fev/2006
'************************************************* ***************
define OSC 20
DEFINE ADC_BITS 10
define ADC_CLOCK 1
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_BITS 4
DEFINE LCD_LINES 2
symbol SCL = PORTC.3 ' I2C clock
symbol SDA = PORTC.5 ' I2C data
symbol UP = PORTA.4
SYMBOL DOWN = PORTA.3
SYMBOL ENTER = PORTA.2
SYMBOL SEL = PORTA.1
;*******************************************
; Configuração do PIC
;*******************************************
TRISA=%00111111
TRISB=%00000000
TRISC=%00100000
TRISD=%00000000
ADCON1 = $0E 'Configura somente RA0 como entrada de AD
'******************************************
PORTB=%00000000
PORTC=%00000000
PORTD=%00000000
'*****************************************
'------------------------
' VARIAVEIS DO PROGRAMA
HOUR VAR BYTE
MINUTE VAR BYTE
DAY VAR BYTE
MONTHY VAR BYTE
YEAR VAR BYTE
S VAR BYTE
M VAR BYTE
H VAR BYTE
D var byte
Mn var byte
Y var byte
TEMPO VAR WORD
'-----------------------
lcdout $fe,1 'limpa o display
Pause 500
lcdout "PCF8583 Aprender"
Lcdout $fe,$c0
LCDOUT "FDAI Eletronica"
Pause 1500
lcdout $fe,1 'limpa o display
Pause 100

Main:
I2CREAD SDA,SCL,%10100001,2,[S]
I2CREAD SDA,SCL,%10100001,3,[M]
I2CREAD SDA,SCL,%10100001,4,[H]
I2CREAD SDA,SCL,%10100001,5,[D]
I2CREAD SDA,SCL,%10100001,6,[Mn]
I2CREAD SDA,SCL,%10100001,7,[Y]

IF PORTA.1=0 THEN GOSUB ACERTAR

lcdout $fe,1 'Limpa o LCD
lcdout $fe,2 'cursor n primeira posição
lcdout "Hora: ", hex(H),":", hex2(M),":", hex2(S)
lcdout $fe,$c0 'segunda linha do display
lcdout "Data: ", hex2(D),".", hex2(Mn),".20",hex2(Y)

IF PORTA.1=0 THEN GOSUB ACERTAR

Pause 500
goto Main 'VOLTA PARA O LOOP PRINCIPAL

'************************************************* *****************
' Rotina de ajuste do relogio RTC
ACERTAR:
lcdout $fe,1
Lcdout "Acertar hora"
Pause 1000
TEMPO = 1000
'Verifica se algum botao de ajuste foi pressionado:
'------------------------
VER_BOTOES:

IF TEMPO =0 THEN
GOSUB GRAVAR_HORARIO
RETURN
ENDIF


tempo = tempo -1
' Checa se algum botao de ajuste de h/min foi pressionado
If PORTA.3 = 0 Then decmin
If PORTA.4 = 0 Then incmin ' Last 2 buttons set minute
If PORTA.1 = 0 Then dechr
If PORTA.2 = 0 Then inchr ' First 2 buttons set hour
pause 50
Goto VER_BOTOES

'*************************************************
' Increment minutes
incmin: minute = minute + 1
If minute >= 60 Then
minute = 0
Endif
Goto debounce

' Increment hours
inchr: hour = hour + 1
If hour >= 24 Then
hour = 0
Endif
Goto debounce

' Decrement minutes
decmin: minute = minute - 1
If minute >= 60 Then
minute = 59
Endif
Goto debounce

' Decrement hours
dechr: hour = hour - 1
If hour >= 24 Then
hour = 23
Endif
goto debounce
'************************************
'essa é uma parte da rotina de acerto do relogio
debounce: 'atualiza o relogio na tela na hora do acerto do relogio
pause 200
tempo= 90
lcdout $fe,1
Lcdout $fe,2
Lcdout "Acerte Horario:"
lcdout $fe,$c0
Lcdout dec2 hour, ":", dec2 minute, ":00"
goto VER_BOTOES
'************************************

'*************************************
GRAVAR_HORARIO:
'Converte para Hexadecimal
DAY = 26
MONTHY = 4
YEAR=06

M = MINUTE/10*16 + MINUTE//10
H= hour/10*16 + hour//10
D= DAY/10*16 + DAY//10

Mn= MONTHY/10*16 + MONTHY//10
Y = YEAR/10*16 + YEAR//10

I2cwrite SDA,SCL,%10100001,2,[0]
I2Cwrite SDA,SCL,%10100001,3,[M]
I2Cwrite SDA,SCL,%10100001,4,[H]
I2CWRITE SDA,SCL,%10100001,5,[D]
I2CWRITE SDA,SCL,%10100001,6,[Mn]
I2CWRITE SDA,SCL,%10100001,7,[Y]
RETURN
'************************************
End

EAST___
- 14th April 2010, 10:47
I'm trying to make a propeller clock with 22 LEDs and was wondering if I can help with handcuffs programming ...:confused:... in part to pass the information that the PCF8583 fornesi my PIC (16F877) and passes it to the LEDs ... was shackled to help me say something ... thanks;)

ps.: I'm working with miKroC

Acetronics2
- 14th April 2010, 10:58
Hi, East

You could find much better help on the ... MikroC Forum.

Here: http://www.mikroe.com/forum/viewforum.php?f=13

Don't you think ???

Alain

malc-c
- 14th April 2010, 18:04
ps.: I'm working with miKroC

So why post on a forum that deals with picBASIC

ffambrin
- 15th April 2010, 15:10
Dear EAST

a "propeller clock with 22 LEDs" is a diferent project.
It´s not similar than LCD clock, like my code was describe.

I am sorry, but it's very dferent.

EAST___
- 15th April 2010, 15:41
Thanks to all...;)