Shop OBEX P1 Docs P2 Docs Learn Events
A little education needed — Parallax Forums

A little education needed

Dave-WDave-W Posts: 94
edited 2009-05-28 03:01 in BASIC Stamp
Hello All,
I do need some help understanding some variables and how they work and are parsed out. This program using the DS1307 works and for the life of me I cannot figure out exactly why it works the way it does. The main reason I am asking for help is that I cannot figure how to get the month and year variables out of the program. I attached the program ( excluding the lines that are not relevant ).
·
This is what I know so far..confused.gif
Line 70 defines the variable "secs" as the DS1307 time registers.
The program starts at line 101 where it "Gosubs to Get_Clock
Line 146 starts Subroutine Get_Clock ·where the registers are read into the variable secs that is 8 bytes long containing all the RTC data.
·
This is what I do not know....
The program returns to line 103 where somehow, through magic, the variable secs is now turned into the variable hrs? Likewise the variable mins contains the minutes data, again by magic... To make it worse somehow rawTime contains some magical value.
·
Also, why is hrs anded with 3F? Doesn't this strip off the two highest bits?
Help..


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dave W.

Comments

  • vaclav_salvaclav_sal Posts: 451
    edited 2009-05-21 02:16
    No magic – just poorly written documentation assuming few things.
    ·
    Read I2CIN command description of InputData formatting (STR Array \ Array length)
    The command reads data into an array and this array is constructed by defining the variables (secs,….) in a sequence. The sequence is the key. ·Keep in mind that each variable is actually a pointer to RAM ·hence the sequence of declarations is important.
    ·
    ·
    The description is little vague on the array length - it calls it "bytes".


    PS. Yes, the & $3F does mask the upper two bits. The commonly used term is "masking" unwanted data.
    After all,· the computer really does work only on ones and zeroes!


    Happy coding
    Vaclav

    ·
  • Dave-WDave-W Posts: 94
    edited 2009-05-21 02:41
    AOK Vaclav,
    That is some help, but where is the pointer to the RAM location ??
    Any array needs a pointer...

    Don't forget my other questions that need to be explained.
    So far it's clear as mud..

    Come on Mister Green, put your thoughts into this swamp and either drain it or clear it up..

    Thanks to all.

    Vaclav, so far this is not happy coding for me unless I can understand it.
    Please be my help.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Dave W.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-05-21 03:22
    The I2CIN instruction internally keeps track of the array pointer
    I2CIN SDA, DS1307, 0, [noparse][[/noparse]STR secs\8]
    The key is the modifier "STR secs\8" which tells the I2C to collect 8 successive bytes and store them in 8 successive memory locations in the Stamp RAM, starting at address STR. As Vaclav said, those variables are the bytes that have been defined as secs, mins, hrs and so on up to year and lastly control. So when the subroutine returns to line 103 those values have been populated.

    The value rawTime does not appear by magic, it is computed in the statement on line 106,
    rawTime = hrs * 60 + mins
    That is a number that tells the sequential minute of the day, from 0 at 00:00 up to 1449 at 23:59. This program uses that information to decide (in a very very roundabout way, I might add), whether it is AM or PM.

    The hrs returned from the clock can be either in 12 hour or 24 hour format. The program could set that format by writing a byte to the clock hours register with a 1 in bit 6. Then, when reading back the hours, the program would have to do hrs & $3f to get rid of that, and also in 12 hour mode program could read bit 5 to determine if it is AM or PM. However this program at another point assumes that the clock is in 24 hour mode, in which case it would not have to bother with the & $3f, because bit 6 would always be zero anyway. That is just one of the less than optimal things about this particular PBASIC code, which does not explicity set the clock to one mode or the other.

    This code also assumes that the clock is running. Bit 7 of the seconds has to be set to zero for the clock oscillator to start. That might not happen by accident, and this program does not do anything to write a zero to that bit.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 5/21/2009 3:29:53 AM GMT
  • Dave-WDave-W Posts: 94
    edited 2009-05-28 03:01
    Tracy,
    Words cannot say how grateful I am for your reply! Thanks a million. Boy, are your explanations great and did clear up a big stumbling block for me. I still cannot quite understand how the variables [noparse][[/noparse] seconds, minutes, hours, days, ect are automatically assigned but more work will be required ]
    If I assign [noparse][[/noparse]sec, min, hrs, dys, yrs.. ] in my program as the variables, will it still work ? or do I have to use the previous assignments ?
    Since you pointed out the 12 / 24 bit, it has me doing some research. I do see what you are talking about and your idea is much easier than the sample code I was using. If you could, would you help me isolate the fifth and sixth bit to set the 12/ 24 settings? I do have a·problem stripping the sixth and fifth bits to use to determine the 12/24 and the AM/PM identifier. If I 'and' it with· %110000 don't I change the lower four bits?
    Excuse me for omitting the full code in my previous post. I did not think you would catch the missing code. Sorry.
    Here is the not so final code I am using as test.
    Oh yes, the delay in getting back to you is not so simple. I was up until 3 am woking on your ideas for a change. I got it working as I wanted but then the next day I forced my wife to show her how well it worked. But it didn't ! It took me three additional hours to find a cold solder connection on the data line between the·series resistor and the solder pad going to the·Stamp. It looked good, but the ring around the pad could only be·seen with my eye loop. Bummer...

    If you could help·with the 12/24 bits 5 and 6·it·would be great.

    Thank you again·Tracy Allen,

    Dave



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Dave W.
Sign In or Register to comment.