I'm using a DS1302 clock and since I did not find libraries I'm creating management software. I tried to use the BcdToDec_GCB function but did not work properly, I wanted to figure out if I was wrong or never tested. I have created my own functions that go right:
Function bcdtodec (var)
Bcdtodec = var & b'00001111 '
Bcdtodec = (var / 16) * 10 + bcdtodec
End function
Function dectobcd (var)
Dectobcd = var & b'00001111 '
Dectobcd = (var / 10) * 16 + dectobcd
End function
I also did simple PicBasicPro-style functions to create a SPI software (Shiftin-Shiftout), but it could be very useful for other projects if I can put it in this section.
Thanks to all the forums
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Advise you take the DS1307 library and then adapt to the DS1302. Many of the calls will be the same and your new library will support Software and Hardware I2C.
:-)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@ Anobium I use the DS1302 because I have it in the drawer and I do not want to throw anything, I've done the necessary routines and it works fine.
I do not allow myself to write complete libraries but the features that may be useful to many chips using the spy 2-wire, I hope the text is quite understandable as I use a translator (possibly cast criticism to google).
You have not responded to me how the BcdToDec_GCB function is not good !
Function BcdToDec (va) as byte
BcdToDec = (va / 16) * 10 + va%16
End Function
I wanted to understand the reason to learn new things, I do not understand well %16 What should you get to give me a breakdown?
I put two simple functions that are very useful to create a SPI software like PicBasic, try it with DS1302 and work.
They use two pins (SPI_ck, SPI_dq) of the pic, a third pin is usually required to enable SPI must be handled manually before starting the call.
INITIALIZATION:
SPI_ck, SPI_dq must be defined and put as output
The SPI_ck pin must be set high or low as required by IDLE state.
call:
Hl defines which bit to be sent or received before
Hl = 0 first bit LOW hl = 1 first bit HIGHT
; Define SPI SOFTWARE shiftIN(pin_ck,pin_data,1 or 0) -- HL=imposta lettura 1=prima bit_alto
Function ShiftIn(spi_ck,SPI_dq,hl) ' -- SPI software come PBP (pinCK,pinDATA,bitFIRST,adr)
dim temp as byte
dir spi_dq in ' INP data hl=0 first bit LOW hl=1 first bit HIGHT
for temp=0 to 6
if hl=1 then
ShiftIn.0 = spi_dq
Rotate ShiftIn left ' legge prima bit HI
Else
ShiftIn.7 = spi_dq
Rotate ShiftIn right ' bit LO
end if
spi_ck= ! SPI_ck 'CK=1
'wait 50 us
spi_ck= ! SPI_ck 'CK=0
next
End function
; Define SPI SOFTWARE shiftOUT(pin_ck,pin_data,1 or 0,Data)
Function ShiftOut(spi_ck,SPI_dq,hl,adr) ' hl=0 first bit LOW hl=1 first bit HIGHT
dim temp as byte
dir spi_dq out ' OUT indirizzo READ
for temp=0 to 7
if hl=1 then
spi_dq = adr.7
Rotate adr left '
else
spi_dq = adr.0
Rotate adr right '
end if
spi_ck= ! SPI_ck 'CK=1
'wait 50 us
spi_ck= ! SPI_ck ' CK=0
next
End function
Last edit: Gigi 2017-05-19
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Decimal to BCD Converter is used to convert decimal (Base-10) integer to BCD (Binary-coded decimal).
BCD is a digital encoding method for decimal numbers in which each digit is represented by its own binary sequence. BCD is different from converting a decimal number to binary. For example 45, when converted to binary, is 101101, when represented in BCD is 01000101 which is 0X45 (hex).
Using the DecToBcd method above for where va=45 will return 0X45.
Let's use Excel to proove. =(INT(B1/10)*16)+MOD(B1,10), where B1 equals 45. The result will be 69d which is 045h... it works! You have to use the integer of B1/10 to ensure you get integer maths.
I have also used the following code to proove this and the attachment shows the results in EEPROM.
Good spot. This is an issue. We were overwriting system variables.
I have revised DecToBcd_GCB and BcdToDec_GCB to prevent overwriting of system variables. Get the latest library from here. Please replace the same file in your installation - in the GCB@SYN\GreatCowBASIC\Include\Lowlevel folder.
Thanks for reporting.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm using a DS1302 clock and since I did not find libraries I'm creating management software. I tried to use the BcdToDec_GCB function but did not work properly, I wanted to figure out if I was wrong or never tested. I have created my own functions that go right:
Function bcdtodec (var)
Bcdtodec = var & b'00001111 '
Bcdtodec = (var / 16) * 10 + bcdtodec
End function
Function dectobcd (var)
Dectobcd = var & b'00001111 '
Dectobcd = (var / 10) * 16 + dectobcd
End function
I also did simple PicBasicPro-style functions to create a SPI software (Shiftin-Shiftout), but it could be very useful for other projects if I can put it in this section.
Thanks to all the forums
Wonderful to here.
Advise you take the DS1307 library and then adapt to the DS1302. Many of the calls will be the same and your new library will support Software and Hardware I2C.
:-)
Not that there is much point in suppoerting I2C for an SPI device :)
The DS1302 has an SPI interface and the DS1307 has an I2C interface.
Still use the library and adapt to SPI.
@Gigi. If you are up to it. Would you be able to add hardware and software SPI?
:-)
@ Anobium I use the DS1302 because I have it in the drawer and I do not want to throw anything, I've done the necessary routines and it works fine.
I do not allow myself to write complete libraries but the features that may be useful to many chips using the spy 2-wire, I hope the text is quite understandable as I use a translator (possibly cast criticism to google).
You have not responded to me how the BcdToDec_GCB function is not good !
Function BcdToDec (va) as byte
BcdToDec = (va / 16) * 10 + va%16
End Function
I wanted to understand the reason to learn new things, I do not understand well %16 What should you get to give me a breakdown?
I put two simple functions that are very useful to create a SPI software like PicBasic, try it with DS1302 and work.
They use two pins (SPI_ck, SPI_dq) of the pic, a third pin is usually required to enable SPI must be handled manually before starting the call.
INITIALIZATION:
SPI_ck, SPI_dq must be defined and put as output
The SPI_ck pin must be set high or low as required by IDLE state.
call:
Hl defines which bit to be sent or received before
Hl = 0 first bit LOW hl = 1 first bit HIGHT
; Define SPI SOFTWARE shiftIN(pin_ck,pin_data,1 or 0) -- HL=imposta lettura 1=prima bit_alto
Function ShiftIn(spi_ck,SPI_dq,hl) ' -- SPI software come PBP (pinCK,pinDATA,bitFIRST,adr)
dim temp as byte
dir spi_dq in ' INP data hl=0 first bit LOW hl=1 first bit HIGHT
for temp=0 to 6
if hl=1 then
ShiftIn.0 = spi_dq
Rotate ShiftIn left ' legge prima bit HI
Else
ShiftIn.7 = spi_dq
Rotate ShiftIn right ' bit LO
end if
spi_ck= ! SPI_ck 'CK=1
'wait 50 us
spi_ck= ! SPI_ck 'CK=0
next
End function
; Define SPI SOFTWARE shiftOUT(pin_ck,pin_data,1 or 0,Data)
Function ShiftOut(spi_ck,SPI_dq,hl,adr) ' hl=0 first bit LOW hl=1 first bit HIGHT
dim temp as byte
dir spi_dq out ' OUT indirizzo READ
for temp=0 to 7
if hl=1 then
spi_dq = adr.7
Rotate adr left '
else
spi_dq = adr.0
Rotate adr right '
end if
spi_ck= ! SPI_ck 'CK=1
'wait 50 us
spi_ck= ! SPI_ck ' CK=0
next
End function
Last edit: Gigi 2017-05-19
:-)
OK. Let us try to resolve one question at a time.
The standard functions are shown below.
Decimal to BCD Converter is used to convert decimal (Base-10) integer to BCD (Binary-coded decimal).
BCD is a digital encoding method for decimal numbers in which each digit is represented by its own binary sequence. BCD is different from converting a decimal number to binary. For example 45, when converted to binary, is 101101, when represented in BCD is 01000101 which is 0X45 (hex).
Using the DecToBcd method above for where va=45 will return 0X45.
Let's use Excel to proove. =(INT(B1/10)*16)+MOD(B1,10), where B1 equals 45. The result will be 69d which is 045h... it works! You have to use the integer of B1/10 to ensure you get integer maths.
I have also used the following code to proove this and the attachment shows the results in EEPROM.
This logic is the reverse for BcdToDec.
Simple functions for SPI for a write operation is show below. I am sure the port works - well done. Please change to SUB (not Function).
Our method would be as follows:
Thanks Anobium, your functions are fine but the internal ones of GCB are not as verified by the following code, how come? :
chip 16f877a
bcd=0x17 ' BCD number
EPWrite 0, BcdToDec(bcd) ' 17 decimal PERFECT
EPWrite 1, BcdToDec_gcb(bcd) ' 10 decimal ERROR !!!!!!
end
function DecToBcd(in va ) as byte
;Convert pure decimal number to binary coded decimal
DecToBcd=( va /10)*16+ va %10
end function
;-----
function BcdToDec(in va ) as byte
;Convert binary coded decimal to pure decimal
BcdToDec=( va /16)*10+ va %16
end function
Good spot. This is an issue. We were overwriting system variables.
I have revised DecToBcd_GCB and BcdToDec_GCB to prevent overwriting of system variables. Get the latest library from here. Please replace the same file in your installation - in the GCB@SYN\GreatCowBASIC\Include\Lowlevel folder.
Thanks for reporting.
Well, thank you very fast and always available, but never sleep !!!!!