real time clock - PCF8583


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    lupuccio's Avatar
    lupuccio Guest


    Did you find this post helpful? Yes | No

    Default here's the code.

    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

  2. #2
    Join Date
    Feb 2006
    Posts
    4


    Did you find this post helpful? Yes | No

    Default Help from FDAI - Brazil

    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

Similar Threads

  1. real time clock
    By maria in forum Code Examples
    Replies: 44
    Last Post: - 1st March 2022, 12:13
  2. Real Time Clock
    By in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd June 2012, 04:52
  3. Real time clock ICs
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 66
    Last Post: - 20th October 2008, 16:05
  4. Real Time Clock
    By savnik in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th December 2006, 02:02
  5. Real time clock... what a headache!
    By Eng4444 in forum mel PIC BASIC
    Replies: 2
    Last Post: - 8th June 2006, 21:56

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts