555 and picaxe

Munger

New Member
I may be stupid but I can't get my head around 555 chips and picaxe!!! Can someone explain it to me through PM or MSN (cheese_munger (at) hotmail (dot) com).

Thanks in advance
 
Last edited:

Rickharris

Senior Member
I will do it here in the hope it may be of benefit to others as well.

I guess you are new to electronics?

The 555 is a cheap and easy to use timer IC With few connections (get a data sheet and have a look at the suggested circuits) it can be made into a Mono stable (one shot) - that is a circuit that will change its output once for a set amount of time on receiving a trigger pulse. This is often used as a timer circuit.

Or an Astable (flip flop) where the output will change from high to low at a regular and continuous pace. This is often used to flash LEDs.


The Picaxe family however is a set of programmable IC's that can do pretty much whatever you program them to do. the best place to start is the PDF documents you can see at the top of this page. the picaxe could do exactly what the 555 does but the 555 will cost .15p and the picaxe £1.00 so you choose the best one for your application.

Sorry but it realy is best for you if you dig into the PDF manuals they explain what Microprocessors are as well as how they work in nice easy language. Then you cqn ask specific questions if you are not clear.
 

Munger

New Member
OK, a more specific question.
To get an accurate timer (a pulse a second) I use a 555 chip (attached to the capacitors and resistors necessary), I connect it to one of the input pins on the picaxe.. but is this what I need to do? Or is there a better chip for accurate second pulses?
 

Michael 2727

Senior Member
An NE555 or equiv is a dedicated TIMER Chip, and only that.
By changing R/C/Feedback values (resistor capacitor feedback)
on its I/O pins you can change the length, speed and duty of the
output. Usually that is all that can be done with it and nothing eles.

Though there have been some very clever and inovative ways the
chip has been used in the last 37 years, it is and will only ever be
a TIMER chip. (and not a particularly accurate one by todays standards)

A Picaxe on the other hand is a dedicated MICROCONTROLLER.
A microcontroller can process an instruction code and then make
its output/s do stuff accordingly. A micro usually has memory
inputs and outputs and will interact with the inputs the code
instructions to produce the desired output.
They have internal or external timer components which can be
controlled by the program code to produce various outputs also.

Because the 555 has been used in a million different designs
over the years the Picaxe has been refered to as the NEW
MILLENIUM 555, probably because of the 08/08M 8 pin design
and the versatility of the chip itself.
They don't compare in any other way.

I wish the Picaxe 08M had been invented back in 1970 :)
 

Michael 2727

Senior Member
How accurate does this timing need to be ?

I have made a 3 digit Stopwatch with an 08M, the
timing is only accurate to +/- 2 Sec/15 min though.

There is a Dallas RTC (real time clock) chip that can
be used with Picaxe chips also, they will be accurate.

Hippy has done some work using a 32KHz watch Crystal
and picaxe chips (this may or may not be of use to you)

There are hundreds of threads in the archives to do with
timing applications, I think there is even a mention of
using an NE555 as the timing pulse, I wouldn't use a 555.
You will have to do a search and see what you can find.

It all depends on the accuracy and you want and how
much you are prepared to spend or the application
needed - you never said what you want it to do ?
 

Munger

New Member
It does not need to be very accurate. It will be used in a count down timer (maximum of 9999 seconds (2.7 hours). So, it can lose maximum 1/2 sec per hour. How would you use the Dallas RTC ?
 

andrewpro

New Member
It does not need to be very accurate. It will be used in a count down timer (maximum of 9999 seconds (2.7 hours). So, it can lose maximum 1/2 sec per hour. How would you use the Dallas RTC ?

The Dallas RTC chip most commonly used around these here parts is the 1307. RTC stands for "real time clock". It's essentially a very complex counter. You give it a series of pulses (or a crystal so that it can make it's own pulses) at a rate of 32,768hz. Why 32768? It's easily divisible by binary counting down to 1... in this case giving you a pulse every 1 second.

The 1304 has a series pre-setable BCD decade counters inside that count these pulses (this means that is has counters that give binary numbers 0-9 and only count up to 10). If you set the time at 12:00:00, then the next pulse it receives will make the counters increment to 12:00:01, etc.


This also works with dates and years, etc, but the counters inside are a bit more complex. Still the same concept though. When the counters go from 11:59:59 to 00:00:00, it passes a pulse over to the day counter and makes it incremement 1.


To read out the time on one of these you need a picaxe capable of using I2C (actually I^2 C or IIC, Inter Integrated Circuit). the chip will provide you with a series of bytes, each containing two BCD encoded numbers (one for seconds, minutes, hours, etc). Once you have the time inside the picaxe, this is where you start processing it into the information you need.

In this case, you would have, say, a start time in the picaxe. You can either set the time inside the RTC, or read it out as the start time. Then you recover the ending time from the clock chip and do some maths to see how much time has elapsed.

Using a DS1307, a DS37KHZ, and some resistors and capacitors I've been able to get a stable clock accurate to about 2 minutes or less per year as long as you dont constantly freeze it then throw it in the oven over and over again. You're not going ot get much more stable than that without a serious outlay of cash, time, frustration, and effort.


I was also part of a project once that phase locked a GPS pulse per second output to a 3.2768 mhz oscillator to get into the parts per tens of billions accuracy range, but that's slightly overkill. Just to show you what's possible though :p

--Andy P
 

Munger

New Member
As i dont need the timer to be that accurate, i could just use the picaxe with no external timer..... but i have no idea how inaccurate the picaxe is..... do any of you know?
 

lbenson

Senior Member
I found this thread by Brian Lavery (after Hippy) to be very informative on the use of Timer1: http://www.picaxeforum.co.uk/showthread.php?t=5849. You could calibrate the actual time over two hours to adjust the counters to what you want, but note that the timing would fluctuate with temperature and between chips. Note also the fiction of 64 second "minutes"--you can change that.

I'll attach the code for one 08M program that uses this technique. The heart of the matter is in the "Tick_Dvr" subroutine. This particular program tested running the 08M at 32kHz, so you'll want to ignore that part.

Code:
 **************************************************************************
; *                                                                        *
; *     Test Timer1 timing for 08M running at 32kHz          TST32KZC.BAS  *
; *                                                                        *
; **************************************************************************

        SYMBOL VERSION_MAJOR = 1        ' 1.0
        SYMBOL VERSION_MINOR = 0

; .------------------------------------------------------------------------.
; |                                                                        |
; |     Version         1.0                                                |
; |     Last Update     2007-08-17                                         |
; |                                                                        |
; |     Target          PICAXE-08M                                         |
; |     Compiler Used   Revolution Programming Editor 5.1.5                |
; |     Memory Used     Approx ??? bytes of 256                            |
; |                                                                        |
; |     Author          Lance Benson                                       |
; |                                                                        |
; `------------------------------------------------------------------------'

; ---Timer Definitions per Brian Lavery after the Happy Hippy---------------
symbol PIC_TMR1H = 0x0F         ' (MSB) PIC Timer1 data (counter) register
symbol PIC_TMR1L = 0x0E         ' (LSB) PIC Timer1 data (counter) register
symbol PIC_T1CON = 0x10         ' PIC Timer1 Control Register
' GP Picaxe bit variables that must be PRESERVED at all times in your code:
symbol Tick_flag = bit0         ' set for 1 exec loop per tick (second)
symbol Tick_hi = bit1           ' hi or lo half second of each tick
symbol Mins_Flag = bit2   
symbol HalfMin_Flag = bit3       ' used for second temp sensor read--good for one main loop only      
' 1-byte peek/poke RAM variables:
' (choose suitable values in 0x50 - 0x7F area)
symbol Systime0 = 0x52          ' 6.5 bit tick counter in S S S S S S h 0         lsB
symbol Systime1 = 0x53          ' MINS counter (approx!.  1 "min" = 64 ticks!)
symbol Systime2 = 0x54          '                                                 msB
symbol TimerLast = 0x55
' 2-byte peek/poke RAM variables:
' (again choose suitable values in 0x50 - 0x7F area)
#rem
symbol LoopCounter = 0x56?
symbol LoopsPerTik = 0x58?
#endrem
; -------------------End of Timer Definitions-------------------------------

#picaxe 08M                  ' leg 8: 0V; leg 7: O0, serout, infraout; leg 6: I1, O1, ADC1; leg 5: I2, O2, ADC2, pwm2
                             ' leg 4: I3; leg 3: I4, O4; leg2: serial in; leg 1: 5V
                             ' connections: O0-transmitter; 
#define AXE08M

symbol MyId              = 1 ' picaxe ID--different for each sensor detector module

symbol SaveW6        = 0x56 ' 2-byte peek/poke Ram variable

start:
    let dirs = %00000       ' set pins 1,2,3,4 input from sensors--08M only
    disablebod              ' saves battery
  #ifndef SIMULATE
    poke PIC_T1CON, 0x31    ' Start PIC timer1 running at 524 mSec 16-bit rollover
  #else
    b1 = 5
    poke TimerLast, b1      ' For simulation. let it loop
  #endif

initiate:
  if pin3 = 1 then         ' beginning of pulse on 3
    poke PIC_TMR1H, 0      ' clear high and low bytes of Timer1
    poke PIC_TMR1L, 0
    poke PIC_T1CON, 0x31   ' Start PIC timer1 running at 524 mSec 16-bit rollover
    Tick_hi = 0            ' reset half-secont flag (524 ticks)
    Tick_flag = 0          ' reset second flag (524 ticks)
    Mins_Flag = 0          ' reset minute flag
    setint %00000000,%00001000 ' interrupt on pin3 low
    pin2 = 1               ' turn on led to mark detection of pulse
  else                     ' end of pulse on 3
    poke PIC_T1CON, 0      ' stop timer1
    gosub printTime
    setint %00001000,%00001000 ' interrupt on pin3 high
    pin2 = 0               ' turn off led
  endif

main:
  ModuleID = MyID  ' only CheckPackets will change this
  gosub Tick_Dvr
  #ifdef TEST1
    Mins_Flag = 1        ' test temperature
    MinuteCount = 59     ' test hourly time check
  #endif
  gosub readSensors
  gosub readTemps
  gosub checkTime
  if Tick_flat = 1 then
    gosub checkPackets    '  see if any packets are scheduled to be resent
  endif
  goto main

readSensors:
' #ifdef INCLUDECODE
  return

readTemps:                            ' uses b1, b2-b3 (as w1), b4-b5 (as w2), b11; saves & restores w6
  return

checkTime:               ' send checkin packet
  if Mins_flag = 1 then  ' we rolled over to a new minute
    inc MinuteCount
  endif
  if MinuteCount = 60 then  ' check in
    peek SysTime0, word w1
'    ModuleID = MyID
    MessageType = MsgTimeCk 
    PacketData1 = b2
    PacketData2 = b3
    MinuteCount = 0
    gosub sendPacket
  endif
  return

checkPackets:  ' see if there is packet in ring buffer to be resent--each is sent 4 times
  SentCount = ?
  
  return
    

    
Tick_Dvr:
    ' Note: comments below refer to 4mHz clock--this routine tests 31kHz
    '  rollover of PIC_TMR1H every 16 seconds approximately: Tick_flag=1 means 32 seconds have passed
    '
    ' 1 TICK defined as 1 second
    ' Maintain a 23 bit TICK counter at SYSTIME (in half-tix)
    ' TICK flag occurs once each tick.  All drivers may detect ONE tick-flag each second
    ' MINS flag occurs once each minute.  All drivers may detect ONE mins-flag each minute
    ' Any routine is entitled to peek the elapsed time from systime0, systime1, systime2
    ' MMMMMMMM mmmmmmmm SSSSSSh0
    ' Mm=Mins            S=Secs        h=half-tik (half sec)
    ' "Secs" is actually 524*2 = 1048 mSecs,   "Mins" is actually 64 "secs".
    ' This routine does not destroy any register values except for b0 
     '    (Tick_flag, Tick_hi, Mins_Flag, HalfMins_Flag bits (0,1,2,3)

    poke SaveW6, word w6            ' This routine uses w6,b12,b13, so save the entry value    
    Tick_flag = 0                   ' Flags last only one exec loop
    Mins_Flag = 0
    HalfMin_Flag = 0
    peek TimerLast, b13             ' What was the Timer1 value last time round the loop?
    #ifdef SIMULATE
      b12 = b13 - 1                 ' Simulate 1 tick per loop
    #else
      peek PIC_TMR1H, b12           ' Read msb of PIC's timer1
    #endif
    poke TimerLast, b12
    if b13 > b12 then               ' Test whether timer1 has started from zero again?
                                    ' Arrive here once per every 524 mSec
        tick_flag = Tick_hi         ' Flag a new tick (High only ONCE / 1048 mSec)
        Tick_hi = 1- Tick_hi        ' Toggle the Tick-hi flag. Half sec on, half sec off
        Peek systime0, b12          ' Fetch system SECS counter - format = SSSSSSh0
        b12 = b12  + 2 And 0xFE     ' Add 2 every half-tick
        Poke systime0, b12          ' Store SECS counter back again
        if b12 = 0 then             ' Byte overflow every 64 seconds
            peek systime1, word w6  ' Fetch 16-bit MINS counter 
            inc w6                  ' Rollover onto higher 16 bits (MINS counter - format = MMMMMMMM mmmmmmmm )
            poke systime1, word w6  ' Store Mins back again
            Mins_Flag = 1           ' Flag a new minute
        EndIf
        #ifdef Temp2 then           ' set a half-minute flag to trigger read of second sensor
          if b12 = $10000000 then
            HalfMin_Flag = 1        ' Flag new half-minute
          endif
        #endif
    EndIf
    peek SaveW6, word w6            ' Restore the value of w6 that we entered with

    return
    

; **************************************************************************
; *                                                                        *
; *     End of Sensor module shell for the PICAXE-08M                      *
; *                                                                        *
; **************************************************************************
 

Rickharris

Senior Member
As i dont need the timer to be that accurate, i could just use the picaxe with no external timer..... but i have no idea how inaccurate the picaxe is..... do any of you know?
That isn't a question that can be directly answered. The internal clock is controlled by capacitors and resistors, manufacturing tolerances will make almost every picaxe different if you examine it accurately enough.

Add on to that the frequency will drift according to temperature so simply warming up or cooling down over an extended period will affect accuracy. So it isn't enough to find out ho accurate your particular picaxe is and then set it as you need because it will change.

MUCH depends on the time scale and the absolute accuracy you need. 1/2 a second per hour is pretty accurate it's 0.014% more or less.

You might get a better accuracy by counting a relatively short time and then counting how many times you do that. E.G. count 1 minute and do this 162 times.

Pause 1000 effectively counts the clock pulses, it counts in mili seconds 1/1000 sec.

Wait 1 is a software implementation of pause 1000.

Don't forget that the commands take time as well and the more instructions you add the more your program counting will be in error.
 

Munger

New Member
ok im just going to use pause 1000 and see how inaccurate it is... if it is stupidly bad then i will think about using something else
 

BCJKiwi

Senior Member
The Dallas DS1307 RTC actually has a 1 second square wave output on pin 7 by default - so you could just use that but its an expensive chip to use for just getting a 1 second squarewave pulse.

If you use a PICAXE that takes an external resonator, you will get much greater accuracy as the time base will be more stable. You can tweak your program counter to adjust for any variation from the theoretical clock speed.
 
Last edited:

Mycroft2152

Senior Member
Another option for a stable timebase is to use the guts form a cheap ($1) digital clock to provide the 1 cesond pulse. This has been discuseed previously on the forum and a asearch should provide the links.
 
Top