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