Bueno continuando con los ejemplos ahora tenemos la lectura de un sensor de temperatura ds1624 y continuando con el ejemplo anterior le agregamos el data loger con una memoria 24lc65 aclaro que esta muy lejos de estar terminado ase falta optimizar y mejorar el código pero como ejemplo vale.
Lectura del ds1624
DEVICE 16F876A
XTAL=4
'****** configuracion puerto serie fisico *****
HSERIAL_BAUD=2400
HSERIAL_RCSTA=%10010000
HSERIAL_TXSTA=%00100000
HSERIAL_CLEAR=ON
DIM i2c_in[3] AS BYTE 'data received over I2C bus
DIM temp AS BYTE
main:
GOSUB Config_Register 'Set Configuration
GOSUB Start_Convert 'Start continuous conversion
TOP:
i2c_in[0]=0
i2c_in[1]=0
i2c_in[2]=0
temp=0
DELAYMS 500
GOSUB Read_Temp 'Read the current temperature
i2c_in[2] = i2c_in[1] >> 3 'Shift 5 decimal bits to LS position
temp = (i2c_in[1]*1000) 'PIC Doesn’t like decimals, but there is a way to work around this
temp =(temp ** 2048)/100
HSEROUT [DEC i2c_in[0],".",DEC2 temp ,13,10]'Outputs temperature to terminal
GOTO TOP 'Loops forever
Config_Register: 'Set continuous conversion
HBSTART 'Start Condition
HBUSOUT %10010000 'Send Address, (device= %1001, A0= 0, A1= 0, A2= 0, R/W= 0)
HBUSOUT $AC 'Send “Access Configuration” command
HBUSOUT $00 'Send “Continuous Conversion” command
HBSTOP 'Stop Condition
RETURN
Start_Convert: 'Start Conversion
HBSTART
HBUSOUT %10010000 'Send Address, (device= %1001, A0= 0, A1= 0, A2= 0, R/W= 0)
HBUSOUT $EE 'Send “Start to Convert” command
DELAYMS 10
HBSTOP
RETURN
Read_Temp: 'Read temperature
HBSTART
HBUSOUT %10010000 'You must “write” the command to read the temperature before
HBUSOUT $AA 'Send “Read Temperature” command
HBRESTART '* Reissue Start Condition *
HBUSOUT %10010001 'Send Address, (device= %1001, A0= 0, A1= 0, A2= 0, R/W= *1*)
i2c_in[0] =HBUSIN
HBUSACK
i2c_in[1] =HBUSIN
HBUSACK
HBSTOP
RETURN
Data loger
DEVICE 16F876A
XTAL=20
DIM MINU AS BYTE
DIM PONE AS BYTE
DIM PTWO AS BYTE
'****** configuracion puerto serie fisico *****
HSERIAL_BAUD=9600
HSERIAL_RCSTA=%10010000
HSERIAL_TXSTA=%00100000
HSERIAL_CLEAR=ON
'****** configuracion puerto analogico/digital ***
DECLARE ADIN_RES 8
DECLARE ADIN_TAD frc
DECLARE ADIN_DELAY 50
TRISA=%00000111
ADCON1=%00000000
'****** CONFIGURACION RESTO DE PUERTOS **************
INPUT PORTB.2 : SYMBOL sw2=PORTB.2
INPUT PORTB.1 : SYMBOL sw1=PORTB.1
INPUT PORTB.0 : SYMBOL sw0=PORTB.0
OUTPUT PORTC.0 :SYMBOL ledr=PORTC.0
OUTPUT PORTC.1: SYMBOL ledv1=PORTC.1
OUTPUT PORTC.2: SYMBOL ledv2=PORTC.2
'*********** configuracion 24lc65
DIM addres AS WORD
addres=0
DIM data_out AS BYTE
data_out=0
DIM data_in AS BYTE
DIM reading AS BYTE
reading=1
SYMBOL control_write=%10100000
SYMBOL control_red=%10100001
'****** DS1620 control pins *********************
SYMBOL DQ= PORTB.5 ' DS1620 DQ pin #1
SYMBOL CLK0=PORTB.4 ' DS1620 CLK pin #2
SYMBOL RST=PORTB.3 ' DS1620 RST pin #3
DIM decimal AS BYTE
DIM Temp AS WORD
'======== variables rtc =======
DIM SEGUNDOS AS BYTE
DIM MINUTOS AS BYTE
DIM HORA AS BYTE
DIM dia AS BYTE
DIM fecha AS BYTE
DIM mes AS BYTE
DIM ano AS BYTE
Init:
HBUSOUT %11010000,7,[%00010000] 'int externa 1hz
HBUSOUT %11010000,0,[0] 'inicialisa ds1
DELAYMS 40
'******** transmitir la hora actual al ds1307 via pueto serial *********
nopres:
ledr=1
ledv1=0
ledv2=0
IF sw2=0 THEN
Loadtime:
DELAYMS 40
SEGUNDOS=HRSIN ' Read the Seconds serially
MINUTOS=HRSIN ' Read the Minutes serially
HORA=HRSIN ' Read the Hours serially
fecha=HRSIN ' Read the Date serially
mes=HRSIN ' Read the Month serially
ano=HRSIN ' Read the Year serially
SetTime:
HBUSOUT $D0,$00,[SEGUNDOS,MINUTOS,HORA,dia,fecha,mes,ano] ' write time
GOTO main
ENDIF
GOTO nopres
'**************************************************************************
'* rutina principal
'*
'***************************************************************************
main:
ledr=0
WHILE 1=1
GOSUB GetTime
data_out=fecha:GOSUB eprom_write
data_out=mes:GOSUB eprom_write
data_out=MINUTOS:GOSUB eprom_write
data_out=HORA:GOSUB eprom_write
GOSUB Read_1620
data_out=Temp:GOSUB eprom_write
data_out=decimal:GOSUB eprom_write
GOSUB piloto
HRSOUT "estoy grabando" ,10,13
WEND
'================== leer la hora actual =============
GetTime:
HBUSIN $D1,$00,[ SEGUNDOS,MINUTOS,HORA,dia,fecha,mes,ano]
RETURN
'***************** leer ds1620 ************
Read_1620:
RST = 1
SHOUT DQ,CLK0,LSBFIRST,[$0C,$02] ' Continuous convert, CPU mode
RST = 0
DELAYMS 10 ' Minimum wait time after write
HIGH RST ' Enable 1620
SHOUT DQ, CLK0, LSBFIRST, [$EE] ' Send start temp convert command
LOW RST ' Disable 1620
DELAYMS 1000 ' Wait for conversion to complete
HIGH RST ' Enable 1620
SHOUT DQ, CLK0, LSBFIRST, [$AA] ' Send read temp command
SHIN DQ, CLK0, LSBPRE, [Temp\9] ' Read 9 bit temperature
LOW RST
IF Temp.7=0 THEN
decimal=5
ELSE
decimal=0
ENDIF ' Disable 1620
Temp = Temp/2 ' Scale reading to whole degrees C.
RETURN
'****************** gravar en memoria extena ***************
eprom_write:
HBUSOUT control_write,addres,[data_out]
addres = addres + 1
IF addres >= 100 THEN
addres=0
ledr=0
GOTO descargar
ENDIF
RETURN
piloto:
ledr=1
DELAYMS 100
ledr=0
DELAYMS 100
RETURN
'***************** descargar memoria extena ****************
descargar:
IF sw1=0 THEN exit
GOSUB piloto2
GOTO descargar
exit:
ledv1=0
WHILE 1=1
GOSUB eprom_read
data_in=fecha
GOSUB eprom_read
data_in=mes
GOSUB eprom_read
data_in=MINUTOS
GOSUB eprom_read
data_in=HORA
GOSUB eprom_read
data_in=Temp
GOSUB eprom_read
data_out=decimal
HRSOUT "time"," ", HEX2 HORA,":",HEX2 MINUTOS," ","data"," ",HEX2, fecha ,"/",HEX2,mes,10,13
HRSOUT "******************************************",0,10,13
HRSOUT "temperatura_ds1620="," ",DEC Temp,".",DEC decimal,0,10,13
GOSUB piloto3
WEND
'*************** leer memoria externa **************************
eprom_read:
data_in = HBUSIN control_red,addres
addres = addres + 1
IF addres >= 100 THEN
addres=0
ledv2=0
GOTO main
ENDIF
RETURN
piloto2:
ledv1=1
DELAYMS 100
ledv1=0
DELAYMS 100
RETURN
piloto3:
ledv2=1
DELAYMS 100
ledv2=0
DELAYMS 100
RETURN