Picaxe Timing Accuracy

RNovember

Well-known member
#41
I have been designing my circuit, and part of my circuit looks like this.
Code:
,----------.Control line
|          |----------------------------------------------.
|    P     |               _______________________________|___________
|    I     |Output 1      |         ,------.             _|           |OutPut 1
|    C     |--------------:---,-----|-|><|-|------------|-------------|---------
|    A     |              |   |     `---|--'             `|           |
|          |              |   |         |------<|---------|           | To EEPROM
|    X     |Output 2      |  _|     ,---|--.             _|           |OutPut 2
|    E     |--------------:-|-----,-|-|><|-|------------|-------------|---------
`----------'              |  `|   | `------'             `|           |
                          |   |   | ,------.             _|           |OutPut 2
                          |   |   '-|-|><|-|------------|-------------|---------------.
                          |   |     `---|--'             `|           |               |
                          |   |         |-----------------'           | To RTC        |
                          |   |     ,---|--.                          |OutPut 1       |
                          |   `-----|-|><|-|--------------------------|--------.      |
                          |         `------'                          |         |     |
                          |___________________________________________|         |     |
,------.
|-|><|-| = 4066 bilateral switch
`------'

-<|- = inverter
I was wondering if there was a chip that did this, or if there was some simple way of doing this?
 

hippy

Technical Support
Staff member
#42
The easiest way is usually to go to a bigger PICAXE chip which has enough I/O without having to invent multiplexing schemes.

If you are simply passing PICAXE outputs on to other chips then you could probably achieve the same using 74HC or CMOS logic gates. One can sometimes achieve it using just appropriate resistors and diodes.

Noting that you have both EEPROM and RTC mentioned in your diagram you may be trying to achieve something more complicated.

It might be best to describe what it is you are hoping to achieve or are trying to do, what actual devices you are connecting to, before deciding on the best way to go about that.

Trying to share I2C and SPI with each other, or use I2C or SPI pins also for digital I/O can get complicated, and you may also need additional components to make that work.
 

RNovember

Well-known member
#43
This part of the project was to connect the picaxe to the RTC so it could communicate with I2C, and then to switch the same pins to the EEPROM to set the data that was read from the RTC (time, date, temperature).

My current schematic has the picaxe connected so that B0-B4 and B6 are connected directly to the data lines of the EEPROM, and since B5 and B7 are the I2C pins, they need to either connect to the EEPROM, or to the RTC.

Since the data will be stored sequentially, I have a counter that increments the address of the EEPROM. The control for my switch thing, the clock, reset, and last pin of the binary counter, and the square wave output of the RTC are connected to the port C pins.

(Have I mentioned that I am using a 20X2)
 
Last edited:

AllyCat

Senior Member
#44
Hi,

The (HC / HCT) 4053 is a "change-over" version (three to a package) of the 4016/66 "On/Off" analogue switches, but they're not as commonly available as they used to be. I've used many in the past and also the four-way (2 per package) version (4051 IIRC) but "analogue" components are rather old hat nowadays. ;)

Cheers, Alan.
 

hippy

Technical Support
Staff member
#45
This part of the project was to connect the picaxe to the RTC so it could communicate with I2C, and then to switch the same pins to the EEPROM to set the data that was read from the RTC (time, date, temperature).
If the RTC uses I2C and the EEPROM uses I2C you can just connect them in parallel to use the same SDA and SCL I2C bus pins.

The two devices can share the bus because you can specify which one you are accessing using the HI2CSETUP command.

The following shows repeatedly reading a DS1307 RTC ( Device ID $D0) seconds count and placing that at an incrementing address in EEPROM ( Device ID $A0 ), not tested -
Code:
Do
  ; == Read RTC seconds
    HI2cSetup I2CMASTER, $D0, I2CSLOW, I2CBYTE
    HI2cIn $00, ( b0 )
  ; == Write to EEPROM
    HI2cSetup I2CMASTER, $A0, I2CSLOW, I2CWORD
    HI2cOut w1, ( b0 )
    w1 = w1 + 1
  ; == Wait a bit
    Pause 1000
Loop
 

RNovember

Well-known member
#46
Okay, I have a schematic of what I am planning on doing at this stage, because I don't think that I have explained it well enough for anyone to
get a picture of what I have described.
Schematic.png
Just to make it clear, if a line crosses, it doesn't connect. A line has to stop at another line to join it.
My idea so far is that on power up, the picaxe takes the time from the RTC, and converts it to binary (see post #16), and store it in the eeprom.
Then the clock for the counter is pulsed, and the temperature is read from the RTC. It is converted to binary, and stored in the eeprom. Then the picaxe pulses the clock for the counter, sets the RTC alarm to six hours later, and waits.

After the timer is reached, this process is repeated again. This goes until all the addresses of the eeprom have been filled. Then q11, on the counter, is high (because it is the last pin of the 12 bit counter) and that signals the picaxe to stop. The picaxe then might switch of a main power relay, so no more electricity is used, and it is ready to be investigated.
 
Last edited:

hippy

Technical Support
Staff member
#49
What hippy is saying is: use just 4 extra components (other than the PICAXE with download circuit). i2c Clock chip, i2c EEPROM and two pullup resistors. The rest is just software.
Exactly that -
Code:
-.------------------.-.-------.---------------.-- +V
 |  .----.         .|.|.      |               |
 `--|    |--.      |_|_|      |               |
 .--|    |  |       | |       |               |
 |  |    |--|- SCL -|-^---.---|---------.     |
 |  |    |--|- SDA -^---.-|---|-------. |     |
 |  `----'  |           | |   |       | |     |
 |   08M2   |         .-^-^---^-.  .--^-^-----^-.
 |          |         | I2C RTC |  | I2C EEPROM |
 |          |         `-------.-'  `----------.-'
 |          |                 |               |
-^----------^-----------------^---------------^-- 0V
Edited: Corrected 08M2 power rails

And if you purchase a cheap RTC and EEPROM module with pull-ups fitted it's even easier -
Code:
-,-------------------------------------.--------- +V
 |  .----.                             |
 `--|    |--.                          |
 .--|    |  |                          |
 |  |    |--|--- SCL -----.            |
 |  |    |--|--- SDA ---. |            |
 |  `----'  |           | |            |
 |   08M2   |         .-^-^------------^-.
 |          |         | I2C RTC + EEPROM |
 |          |         `----------------.-'
 |          |                          |
-^----------^--------------------------^--------- 0V
Note that can use an 08M2 which has a spare I/O to which a DS18B20 could be connected. Many of the RTC plus EEPROM boards actually have a space for a DS18B20 to be fitted and only require it to be fitted and an extra wire taking from the module to PICAXE.

I can appreciate that you may have an old parallel EEPROM lying around unused, but using that doesn't make sense when it makes things so complex compared to the easy way of doing things.

Many of the RTC modules have 32KB EEPROM allowing far more samples than you will otherwise have. 32KB will hold 20 years of samples taken 4 times a day. You therefore aren't limited to 4 samples per day, can be more fine-grained if you want. Once an hour could be easily handled for three years.

I suspect part of what's driving the hardware approach is fearing that the software will be too complex and beyond your experience which puts it outside your comfort zone. It's not really that complex and members here will be more than willing to guide you through it, help you get to grips with it.

Perhaps the best approach is to buy a cheap RTC + EEPROM module, hook it up and see for yourself, before committing to using what you have, something more complicated
 
Last edited:

RNovember

Well-known member
#50
I suspect part of what's driving the hardware approach is fearing that the software will be too complex and beyond your experience which puts it outside your comfort zone. It's not really that complex and members here will be more than willing to guide you through it, help you get to grips with it.
I guess I like the hardware part, and it makes more sense to me at this stage. I still don't know picaxe basic that well.

I started this project just throwing a circuit together with parts I knew how to use, and then planned on simplifying it where I could. I didn't think it got that easy!

How does one tell the RTC that it is its time to listen to the i2c lines, an then tell it to ignore the part that the EEPROM is meant to hear?
 

Aries

New Member
#52
This is an example of using two I2C devices in one program. I2cClockSlave is a Picaxe 20X2, I2cMemorySlave is an EEPROM. Whenever you need to read a device, the I2C is reset (CloseI2C) and then the new one is opened.
Code:
ReadMasterDataPacketFlag:
    setfreq I2CSpeed
    gosub CloseI2C
    hi2csetup i2cmaster,I2cClockSlave,i2cslow,i2cbyte
    ptr = My_MasterDataPacketFlag
    hi2cin I2C_MasterDataPacketFlag,_
                (@ptr)
    pause 100
    setfreq ChipSpeed
    return

CommonReadRecord:
    bptr = LC256Record
    gosub CloseI2C
    hi2csetup i2cmaster,I2cMemorySlave,i2cslow,i2cword
    hi2cin w1,_
    (@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,_
     @bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,_
     @bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,_
     @bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc)
  return
CloseI2C:
    hi2csetup off
    input I2C_SDA
    if I2C_SDApin = 0 then                ' I2C stuck
        sertxd(13,10,"I2C stuck")
        output I2C_SCL
        for b0 = 0 to 15
            if I2C_SDApin = 0 then
                low I2C_SCL                            ' pull low
                pause 10
                input I2C_SCL                        ' allow to go high
            endif
        next b0
    endif
    input I2C_SCL
    return
 

hippy

Technical Support
Staff member
#53
How does one tell the RTC that it is its time to listen to the i2c lines, an then tell it to ignore the part that the EEPROM is meant to hear?
All I2C devices continually listen to the bus but ignore everything which hasn't been directed at them. You can think of it like running an office with "Roger" who is your RTC, "Ethel" who is your EEPROM, and "Ted" your temperature sensor. They all sit there twiddling their thumbs, conserving their energy, until someone tells them to do something.

You're the 'boss man' playing the role of the PICAXE. You sit in your office, mostly snoozing, whenever you wake up you call in Roger and ask him what time it is, he tells you and leaves. If it's time to take a reading, you call in "Ted", ask him what the temperature is, then call in Ethel and tell her to write the temperature in her notebook. Then you go back to snoozing. And on and on that goes.
Code:
Do
  Do
    Snooze
    Call in Roger the RTC
    Ask the time
  Loop Until it is time to take a reading
  Call in Ted the Temperature maestro
  Ask the temperature
  Call in Ethel the EEPROM expert
  Tell her to log the time and temperature
  Snooze again
Loop
Your main challenge is how to maximise your snoozing. You can snooze for short periods and go back to snoozing when it's not time for a temperature sample. You can extend those periods to be somewhat longer, or you can bring an alarm clock into the mix which will wake you up.

It's exactly the same for a PICAXE and I2C hardware doing the job, just that you have to use the right HI2CSETUP command to call in the various devices and you need to know what HI2C commands are required for get the information they provide, or the time and temperature logged. The temperature sensor is slightly different, not being I2C, but you can think of it in the same way.

And just as with the real you doing the job you can start simple, then optimise so you are getting the longest snooze times. You would probably chat with your mates to determine what is the best strategy, what the pro's and con's of each are, before settling on one to use, one you are most comfortable with. But the basic idea remains the same.

When you are done, you'll have a PICAXE program which isn't that much different to what is above. Written in a different language than English, but doing the same job, achieving the same thing.
 

AllyCat

Senior Member
#54
Hi,

Ah, beaten to the keyboard by hippy again. ;)

How does one tell the RTC that it is its time to listen to the i2c lines, an then tell it to ignore the part that the EEPROM is meant to hear?
At the "physical" level, it's done by the "address" of the Slave device*, sent in the first byte transmitted within each I2Cmessage. So there can be up to about 127 different slave addresses with the Slave only responding to messages contaning its own specific address. The LSb of the address (set automatically by the PICaxe operating sytem) is the Read/Write bit which tells the Slave whether to just "Listen" or also to "Reply" (with the requested data).

* i.e. I2cClockSlave and I2cMemorySlave (which need to be defined by Symbol commands) in the above example (#52). With X2 devices, I believe the alternative Slave address may be also redefined within the HI2COUT [newslave]...... command.

Cheers, Alan.
 

Aries

New Member
#55
* i.e. I2cClockSlave and I2cMemorySlave (which need to be defined by Symbol commands) in the above example (#52). With X2 devices, I believe the alternative Slave address may be also redefined within the HI2COUT [newslave]...... command.
I suspect that - as in my example above - you need to do a new hi2csetup if the addressing mode (byte or word) changes. [newslave] can't change the mode because there is no provision for describing it.
 

RNovember

Well-known member
#57
"Roger" who is your RTC, "Ethel" who is your EEPROM, and "Ted" your temperature sensor.
Clever! That helps explain it.

I will look into a i2c eeprom that will work for my project.

I think that I am going to use the Temperature sensor on the RTC to take the readings from, since it's easier to read than an analogue senser. (so Roger and Ted are the same person:))
 
Last edited:

lbenson

Senior Member
#58
so Roger and Ted are the same person
Not the same person. On the typical modules, they dwell in the same triplex (the PCB) along with Ethyl, but they have different addresses (and in fact, the DS18B20 is accessed on its own pin, separate from the i2c pins through which you address Ethyl and Roger).
 

RNovember

Well-known member
#59
I have been looking for an I2C EEPROM, and I wondered if the 24LC32A was a good choice.
It has the right supply voltage, temperature range, and has a 400 kHz clock (same as the RTC), and it has 32 kilobytes of memory.

There is a data sheet here.
 

hippy

Technical Support
Staff member
#60
Almost any I2C EEPROM will likely do. The 24LC32A is quite small, just 4K bytes. I would go for a 24LC256 (32K bytes) and have done with it.
 
Top