Moderadores: andre_luis, 51, guest2003, Renie
funcionando na "unha"
; ****************************************************
; * Rotina para escrita/leitura de hora/data no DS1307 *
; * essa rotina funciona em conjunto com TableBcd.Inc *
; * que é um conversor Decimal/Bcd/Decimal *
; * Status: APROVADO <> Xtal MCU = 4mhz *
; ****************************************************
;-----------
SendMinutoHora:
Bcf Intcon, Gie
Movf Seg, w
Call Conversor_Dec_Bcd
Movwf Secs
Movf Minuto, w
Call Conversor_Dec_Bcd
Movwf Mins
Movf Hora, w
Call Conversor_Dec_Bcd
Movwf Hrs
Call RTC_brst_wr
Bsf Intcon, Gie
Return
ReadMinutoHora:
Bcf Intcon, Gie
Call RTC_brst_rd
Movf Mins, w
Call Conversor_Bcd_Dec
Movwf Minuto
Movf Hrs, w
Call Conversor_Bcd_Dec
Movwf Hora
Bsf Intcon, Gie
Return
;-----------------------------------------
;-- RTC routines --
;-----------------------------------------
RTC_brst_rd:
DsI2C_START
movlw 0D0h ; slave address + write
call write_RTC
movlw .0 ; set word address to seconds register
call write_RTC
DsI2C_START
movlw 0D1h ; slave address + read
call write_RTC
call read_RTC ; read the seconds data
movwf SECS ; save it
call ack;
call read_RTC ; and so on
movwf MINS
call ack;
call read_RTC
movwf HRS
call ack;
call read_RTC
movwf DOW
call ack;
call read_RTC
movwf DAYS
call ack;
call read_RTC
movwf MON
call ack;
call read_RTC
movwf YRS
call nack;
DsI2C_STOP
return
;-----------
RTC_brst_wr:
DsI2C_START
movlw 0D0h ; slave address + write
call write_RTC
movlw .0 ; set word address to seconds register
call write_RTC
movf SECS, W
call write_RTC
movf MINS, W
call write_RTC
movf HRS, W
call write_RTC
movf DOW, W
call write_RTC
movf DAYS, W
call write_RTC
movf MON, W
call write_RTC
movf YRS, W
call write_RTC
DsI2C_STOP
return
;---- Read RTC into W ----
read_RTC:
BANK1
bsf TRISC,4 ; set SDA for input
BANK0
movlw 08h ; send 8 bits
movwf COUNT
bcf SCL ; clock data out
BANK1
bcf TRISC, 3 ; SCL low (output)
BANK0
clrf TMP ; clear var
rlf TMP, F ; rotate carry in
clrf TMP ; clear var again
I2C_read_loop:
rlf TMP, F
BANK1
bsf TRISC, 3 ; SCL high (input)
BANK0
btfsc SDA
bsf TMP, 0 ; if data out = 1, set bit
bcf SCL
BANK1
bcf TRISC, 3 ; SCL low (output)
BANK0
decfsz COUNT, F
goto I2C_read_loop
movf TMP, W
return
;---- ACK read (assumes SCL=0 on entry) ----
ack:
bcf SDA
BANK1
bcf TRISC, 4 ; set SDA for output
BANK0
BANK1
bsf TRISC, 3 ; SCL high (input)
BANK0
nop
bcf SCL
BANK1
bcf TRISC, 3 ; SCL low (output)
BANK0
return
;---- NACK read (assumes SCL = 0 on entry) ----
nack:
bsf SDA
BANK1
bcf TRISC, 4 ; set SDA for output
BANK0
BANK1
bsf TRISC, 3 ; SCL high (input)
BANK0
bcf SCL
BANK1
bcf TRISC, 3 ; SCL low (output)
BANK0
return
;--- Write the byte in W to RTC ---
;---- assumes CE is asserted ----
write_RTC:
movwf TMP ;Save the data
;
;--- Do a I2C bus write of byte in 'TMP' ---
;
I2C_write:
BANK1
bcf TRISC, 4 ; set SDA for output
BANK0
movlw 08h ; send 8 bits
movwf COUNT
bcf SCL
BANK1
bcf TRISC, 3 ; SCL low (output)
BANK0
I2C_w_loop:
bcf SDA ; assume data out is low
btfsc TMP, 7
bsf SDA ; if data out = 1, set bit
; nop
BANK1
bsf TRISC, 3 ; SCL high (input)
BANK0
rlf TMP, F
bcf SCL ; clock it in
BANK1
bcf TRISC, 3 ; SCL low (output)
BANK0
decfsz COUNT, F
goto I2C_w_loop
BANK1
bsf TRISC,4 ; set SDA for input
BANK0
bcf SCL
BANK1
bcf TRISC, 3 ; SCL low (output)
BANK0
; nop
BANK1
bsf TRISC, 3 ; SCL high (input)
BANK0
; if(sda) printf("Ack bit missing %02X\n",(unsigned int)d);
bcf SCL
BANK1
bcf TRISC, 3 ; SCL low (output)
BANK0
return
;********************************************************************
Conversor_Dec_Bcd:
Goto TableBCD
;-----------
Conversor_Bcd_Dec:
Movwf Conversor ; Carrega o valor BCD na variável "CONVERSOR"
Clrf ContadorBCD ; Reinicializa ponteiro do contador de ventos
LoopBCD:
Movf ContadorBCD, w ; Carrega o ponteiro da tabela
Call TableBCD ; Chama os dados contidos na tabela BCD
Subwf Conversor, w ; Verifica se "CONVERSOR" é igual ao dado da tabela BCD
Btfsc Status, z ; Faz a comparação!
Goto SaiLoopBcd ; Identificação do valor BCD
;
Incf ContadorBCD, f ; Contador de eventos (0 à 99 = 100 steps)"graus celcios"
Movf ContadorBCD, w ; Efetua a verificação de overflon
Sublw .102 ; Se for maior que 102 overflon
Btfsc Status, z ; Efetua comparação
Return ; Houve overflon, retornar sem nenhuma ação, mantendo o ultimo dado valido
Goto LoopBCD ; Não é igual continua verificando
SaiLoopBcd:
Movf ContadorBCD, w ; Fim da conversão o resultado esta no acumulador no formato Decimal
Return
;-----------
TableBCD:
Addwf Pcl, f
; DEZE UNID
; BCD 4bit ----|----
Retlw B'00000000' ; 00 BCD
Retlw B'00000001' ; 01 BCD
Retlw B'00000010' ; 02 BCD
Retlw B'00000011' ; 03 BCD
Retlw B'00000100' ; 04 BCD
Retlw B'00000101' ; 05 BCD
Retlw B'00000110' ; 06 BCD
Retlw B'00000111' ; 07 BCD
Retlw B'00001000' ; 08 BCD
Retlw B'00001001' ; 09 BCD
Retlw B'00010000' ; 10 BCD
Retlw B'00010001' ; 11 BCD
Retlw B'00010010' ; 12 BCD
Retlw B'00010011' ; 13 BCD
Retlw B'00010100' ; 14 BCD
Retlw B'00010101' ; 15 BCD
Retlw B'00010110' ; 16 BCD
Retlw B'00010111' ; 17 BCD
Retlw B'00011000' ; 18 BCD
Retlw B'00011001' ; 19 BCD
Retlw B'00100000' ; 20 BCD
Retlw B'00100001' ; 21 BCD
Retlw B'00100010' ; 22 BCD
Retlw B'00100011' ; 23 BCD
Retlw B'00100100' ; 24 BCD
Retlw B'00100101' ; 25 BCD
Retlw B'00100110' ; 26 BCD
Retlw B'00100111' ; 27 BCD
Retlw B'00101000' ; 28 BCD
Retlw B'00101001' ; 29 BCD
Retlw B'00110000' ; 30 BCD
Retlw B'00110001' ; 31 BCD
Retlw B'00110010' ; 32 BCD
Retlw B'00110011' ; 33 BCD
Retlw B'00110100' ; 34 BCD
Retlw B'00110101' ; 35 BCD
Retlw B'00110110' ; 36 BCD
Retlw B'00110111' ; 37 BCD
Retlw B'00111000' ; 38 BCD
Retlw B'00111001' ; 39 BCD
Retlw B'01000000' ; 40 BCD
Retlw B'01000001' ; 41 BCD
Retlw B'01000010' ; 42 BCD
Retlw B'01000011' ; 43 BCD
Retlw B'01000100' ; 44 BCD
Retlw B'01000101' ; 45 BCD
Retlw B'01000110' ; 46 BCD
Retlw B'01000111' ; 47 BCD
Retlw B'01001000' ; 48 BCD
Retlw B'01001001' ; 49 BCD
Retlw B'01010000' ; 50 BCD
Retlw B'01010001' ; 51 BCD
Retlw B'01010010' ; 52 BCD
Retlw B'01010011' ; 53 BCD
Retlw B'01010100' ; 54 BCD
Retlw B'01010101' ; 55 BCD
Retlw B'01010110' ; 56 BCD
Retlw B'01010111' ; 57 BCD
Retlw B'01011000' ; 58 BCD
Retlw B'01011001' ; 59 BCD
Retlw B'01100000' ; 60 BCD
Retlw B'01100001' ; 61 BCD
Retlw B'01100010' ; 62 BCD
Retlw B'01100011' ; 63 BCD
Retlw B'01100100' ; 64 BCD
Retlw B'01100101' ; 65 BCD
Retlw B'01100110' ; 66 BCD
Retlw B'01100111' ; 67 BCD
Retlw B'01101000' ; 68 BCD
Retlw B'01101001' ; 69 BCD
Retlw B'01110000' ; 70 BCD
Retlw B'01110001' ; 71 BCD
Retlw B'01110010' ; 72 BCD
Retlw B'01110011' ; 73 BCD
Retlw B'01110100' ; 74 BCD
Retlw B'01110101' ; 75 BCD
Retlw B'01110110' ; 76 BCD
Retlw B'01110111' ; 77 BCD
Retlw B'01111000' ; 78 BCD
Retlw B'01111001' ; 79 BCD
Retlw B'10000000' ; 80 BCD
Retlw B'10000001' ; 81 BCD
Retlw B'10000010' ; 82 BCD
Retlw B'10000011' ; 83 BCD
Retlw B'10000100' ; 84 BCD
Retlw B'10000101' ; 85 BCD
Retlw B'10000110' ; 86 BCD
Retlw B'10000111' ; 87 BCD
Retlw B'10001000' ; 88 BCD
Retlw B'10001001' ; 89 BCD
Retlw B'10010000' ; 90 BCD
Retlw B'10010001' ; 91 BCD
Retlw B'10010010' ; 92 BCD
Retlw B'10010011' ; 93 BCD
Retlw B'10010100' ; 94 BCD
Retlw B'10010101' ; 95 BCD
Retlw B'10010110' ; 96 BCD
Retlw B'10010111' ; 97 BCD
Retlw B'10011000' ; 98 BCD
Retlw B'10011001' ; 99 BCD
Retlw B'10100000' ; 100 BCD
Retlw B'10100001' ; 101 BCD
Retlw 0xFF ; OverFlon
Vonnilmam escreveu:Olá, mor_al
Mande pra mim, gostaria de apreciar o seu trabalho...
Mande nesse email: contato@grupoagi.com.br
Obrigado
Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante