Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

Project Request

Return
marek stručovský
mlike

posted on 2012/11/02 04:47:00 PM CET

Timers (Real time clock)

Help with 16F877A and DS1302

Hello.
Could someone help me with DS1302 and 16F877A? I programm in microbasic pro for pic ver. 5.4. My programm has two parts. First part is for 18b20 and it works fine, second part is for DS1302 but it part doesnt work. Could someone send me some advice?

program topenipokus
'
' rclk = ra1 DS1302
' io = ra2
' rst = RA3

dim LCD_RS as sbit at Re0_bit
LCD_EN as sbit at Re2_bit
LCD_D4 as sbit at Rd4_bit
LCD_D5 as sbit at Rd5_bit
LCD_D6 as sbit at Rd6_bit
LCD_D7 as sbit at Rd7_bit
LCD_RS_Direction as sbit at TRISe0_bit
LCD_EN_Direction as sbit at TRISe2_bit
LCD_D4_Direction as sbit at TRISd4_bit
LCD_D5_Direction as sbit at TRISd5_bit
LCD_D6_Direction as sbit at TRISd6_bit
LCD_D7_Direction as sbit at TRISd7_bit

' End Lcd module connections

const TEMP_RESOLUTION as byte = 12

dim text as char[5]
text1 as char[5]
temp, pomocny_cas as word
i,j,k, dummy1, tempa, pokus,mesice,
vteriny, minuty, hodiny,rok, dny as byte
cas as byte

sub procedure Display_Temperature( dim temp2write as word )
const RES_SHIFT = TEMP_RESOLUTION - 8

dim temp_whole as byte
temp_fraction as byte
text = "000.0"
' Check if temperature is negative
if (temp2write and 0x8000) then
text[0] = "-"
temp2write = not temp2write + 1
end if

' Extract temp_whole
temp_whole = word(temp2write >> RES_SHIFT)
' Convert temp_whole to characters
if ( temp_whole div 100 ) then
text[0] = temp_whole div 100 + 48
else
text[0] = " "
end if

text[1] = (temp_whole div 10)mod 10 + 48 ' Extract tens digit
text[2] = temp_whole mod 10 + 48 ' Extract ones digit

temp_fraction = word(temp2write) and 0x0f ' extract 0,5 stupne
if (temp_fraction >= 8) then
text[4] = 53
else text[4] = 48
end if
' Print temperature on Lcd
Lcd_Out(1, 6, text)
end sub


sub procedure cteni_casu
porta = 0
trisa = 0
clearbit(porta,1)
clearbit(porta,2)
setbit(porta,3)

for i = 0 to 7 'send command byte, k is variable of register which I want read
clearbit(porta,1) ' clk of DS1302
if testbit(k,0) = 1 then
setbit(porta,2)
else clearbit(porta,2)
end if
k = k >>1
delay_us(10)
setbit(porta,1)
clearbit(porta,2) ' end of clock
delay_us(10)
next i

clearbit(porta,2)
setbit(trisa,2) ' switch (porta,2) from output to input (is it right?)

for i = 0 to 7
if testbit(porta,2) = 1 then ' store value from (porta,2) into register cas
setbit(cas,7)
else clearbit(cas,7)
end if
setbit(porta,1)
delay_us(10)
clearbit(porta,1)
delay_us(10)
cas =cas >>1
next i
trisa = 0
porta = 0 'end of transfer
end sub

sub procedure nastaveni_casu 'set register
trisa = 0
clearbit(porta,1)
clearbit(porta,2)
setbit(porta,3)
for i = 0 to 15
if testbit(pomocny_cas,0) = 1 then
setbit(porta,2)
else clearbit(porta,2)
end if
delay_us(1)
setbit(porta,1)
delay_us(1)
clearbit(porta,1)
pomocny_cas = pomocny_cas >>1
next i
porta = 0
end sub

sub procedure transform_cas
vteriny = ((vteriny and 0xF0) >>4)*10 + (vteriny and 0xF0)
minuty = ((minuty and 0xF0) >>4)*10 + (minuty and 0xF0)
hodiny = ((hodiny and 0xF0) >>4)*10 + (hodiny and 0xF0)
rok = (dny and 0xC0) >>6
dny = ((dny and 0x30) >>4)*10 + (dny and 0x0F)
mesice = ((mesice and 0x10) >>4)*10 + (mesice and 0x0F)
end sub

sub procedure zobraz_cas
lcd_chr(2,16,(vteriny mod 10) +48)
lcd_chr(2,15,(vteriny / 10) + 48)
lcd_chr(2,14,".")
lcd_chr(2,13,(minuty mod 10) +48)
lcd_chr(2,12,(minuty / 10) + 48)
lcd_chr(2,11,":")
lcd_chr(2,10,(hodiny mod 10) +48)
lcd_chr(2,9,(hodiny /10) +48)
end sub


main:
porta = 0
adcon1 =6
cmcon =7
trisa = 0

text = "000.0"
i = 0
j = 0
k = 0

Lcd_Init() ' Initialize Lcd
Lcd_Cmd(_LCD_CLEAR) ' Clear Lcd
Lcd_Cmd(_LCD_CURSOR_OFF) ' Turn cursor off
Lcd_Out(1, 1, "Temp:")
Lcd_chr(1,11,223) ' Print degree character, "C" for Centigrades
Lcd_Chr(1,12,"C") ' If you see greek alpha letter try typing 178 instead of 223



bytetostr(dummy1,text1)
lcd_out(2,1,text1)

pomocny_cas = 0x008E 'set control register- write is on
nastaveni_casu()
delay_ms(1)
pomocny_cas = 0x0082 'set second register- 7th bit is 0 - clock is enable
nastaveni_casu()
delay_ms(1)
pomocny_cas = 0x808E 'set control register - read is enable
nastaveni_casu()


while (TRUE)
'--- Perform temperature reading
Ow_Reset(PORTb, 5) ' Onewire reset signal
Ow_Write(PORTb, 5, 0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTb, 5, 0x44) ' Issue command CONVERT_T
Delay_us(120)

Ow_Reset(PORTb, 5)
Ow_Write(PORTb, 5, 0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTb, 5, 0xBE) ' Issue command READ_SCRATCHPAD

temp = Ow_Read(PORTb, 5)
temp = (Ow_Read(PORTb, 5) << 8) + temp

'--- Format and display result on Lcd

Display_Temperature(temp)
Delay_ms(520)

k= 129 ' read second
cteni_casu()
vteriny = cas
delay_ms(1)

k = 131 'read minute
cteni_casu()
minuty = cas
delay_ms(1)

k = 133 'read hours
cteni_casu()
hodiny = cas
delay_ms(1)

k = 143 'read control register
cteni_casu()
dummy1 = cas

transform_cas() 'transform and
zobraz_cas() 'show time on display
wend

end.