picaxe exercise 3 - rtc, eeprom, temp logging

Upload: proepro

Post on 18-Oct-2015

161 views

Category:

Documents


1 download

DESCRIPTION

Written by John Schneeg, Adjunct Faculty at Glendale Community College, AZ for my ECE103 Intro to Engineering Problem Solving and Design course.We use PICAXE processors to introduce programming and embedded CPUs. This project connects the $5 Education Board to a small $5 module available on eBay that contains a Real Time Clock, EEPROM memory chip, digital one-wire temperature sensor and a rechargeable lithium ion coin cell.Illustrates programming and use of I2C and One-wire protocols with the PICAXE 08M2 chip.

TRANSCRIPT

  • ECE103AB Engineering Design Exercise 3: Data Logger using an I2C real-time clock and EEPROM with a One-Wire temperature sensor

    1. Wire up the circuit on a Solderless Breadboard

    You'll need the PICAXE School Experimenter board with a line of pins soldered along the one edge, to access connection points on the solderless breadboard. Additonally, you'll need the TinyRTC board containing the non-volatile memory, the RTC and thermal sensor chips.

    First plan the circuit on paper based on power, I2C and One-wire connections, then implement the circuit on the breadboard.

    2. Program the PICAXE 08M2 chip to set the clock, turn on the heartbeat LED, and loop temperature readings Copy the following test code, and edit it to set the correct time. Once running, the PICAXE should follow an endless loop that reads the

    time and temperature, sending the information as text down the serial communication line to the desktop computer. The debug statement will cause a window to pop-up in the PICAXE programming editor, displaying memory contents (showing the time and temperautre in C).

    ' program to set clock at 10:30:00am on April 23, 2012, then read the time ' set aside memory to hold values, set up the I2C, write data ' loop to read back the info, showing it on the desktop computer using debug symbol seconds = b0 symbol mins = b1 symbol hour = b2 symbol day = b3 symbol date = b4 symbol month = b5 symbol year = b6 symbol control = b7 symbol temp = b8 symbol tsensor = C.4 ' set DS1307 slave address, as described in the chip's technical documentation PDF ' note that binary values (digits 0 and 1 only) have percent sign prefixes in PICAXE Basic i2cslave %11010000, i2cslow, i2cbyte ' initialise the clock - "Mon 2012-04-23 10:30:00" all as BCD (binary coded decimal) ' note that hexidecimal values (base 16, digits 0-9 plus letters A-F) have dollar sign prefix day = $00 '$00 = Monday, $01 = Tuesday through $6 = Sunday year = $12 '$00 = 2000, $11 = 2011, etc. month = $04 '$01 = January, $02 = February through $12 = December date = $22 '$01 = first day of the month through $31 (as applicable per month) hour = $10 '$00 = midnight through $13 = 1 pm mins = $30 '$00 through $59 = minutes of the current hour seconds = $00 '$00 through $59 = seconds of the current minute control = %00010000 'the 1 enables a pulse output at 1Hz (heartbeat LED) - see PDF docs '

    writei2c 0,(seconds,mins,hour,day,date,month,year,control) '

    main: 'begin the test loop by reading the clock then the temp sensor readi2c 0,(seconds,mins,hour,day,date,month,year) readtemp tsensor, temp debug 'send info back to desktop using debug window pause 500 'wait a little before repeating the loop goto main 'for this test code, there is no end - loop till powered off

    Test if holding your fingers on the temperature sensor will make the reading increase. Edit the code to use the serout command to display a message on the desktop screen, instead of relying on the debug statement. See the

    example in the code for PICAXE Exercise 2, part 3. Review that handout to understand the meaning of the # sign and numbers 13 and 10. debug serout 0,n2400,("Time: ",#hour,":",#mins,":",#seconds," - temp C: ",#temp,13,10)

    3. Test code to see if you can store values in EEPROM memory on the TinyRTC board The small circuit board also contains a 24LC32A chip, an Electrically Erasable Programmable Read Only Memory (EEPROM), with 32Kbits

    of storage equivalent to 4,096 bytes of storage. It also uses the I2C protocol for communicating with a microprocessor. EEPROM is similar to FLASH in a USB thumb drive - it can be read and written to, but holds information when the power is removed (non-volatile memory, as opposed to RAM which is erased when the power is removed). EEPROM holds its values forever (at least until new values are stored).

    The PICAXE chip contains a small amount of EEPROM, but most of it is used to hold the BASIC program, and just 256 bytes are left over for data storage, accessible using the READ and WRITE commands. For making a "data logger" device to store sensor readings, you need much more storage, and having an externally connected chip is common. The next code sample can be used to make sure you can communicate with the chip, both writing and reading data. Once that is verified, you can put together these code snippets to make a complete data logger.

  • ' program to store the numbers 0 to 100 in EEPROM, then read back to pc screen using serout ' note that RAM memory in the PICAXE can be accessed as bytes (b0 through b27) or as words ' (where w0 = b0 as least significant byte, b1 as most significant byte; likewise ' w1=b2+256*b3, w2=b4+256*b5, etc. In other words, byte values overlap word values in RAM) symbol index = w0 symbol index.lsb = b0 symbol index.msb = b1 symbol value = b2 ' set 24LC32A slave address, as described in the chip's technical documentation PDF i2cslave %10100000, i2cslow, i2cword '

    main: ' first use a loop to count values, store each value within an EEPROM memory byte ' note I2C was set up to work in bytes, so we will just send the lower 8 bits of w0 (index) for index = 0 to 100 writei2c index,(index.lsb) pause 1000 serout 0,n2400,("Writing value: ",#index,13,10) next ' use a similar loop to read those values out, and print both index and memory content ' to show how EEPROM holds values forever, keep displaying memory all the way through the 4KB for index = 0 to 4095 readi2c index,(b2) serout 0,n2400,("Memory location: ",#index," holds :",#b2,13,10) next

    4. Combine code snippets to make a data logger. then test in a fridge for 5 to 10 minutes:

    This data logger software relies on the clock being preset using code discussed earlier. You can tell if the clock is running by verifying the "heartbeat" LED is blinking once per second. If it isn't blinking, first run the time set code with the variables set to the date and time the PICAXE is being programmed.

    1. When power is first applied, the logger should check if the pushbutton on the PICAXE board (input pin 3) is being held down. If not, the program skips to step #3 below.

    2. If the button is held down at startup, it indicates the user wants to dump the stored data (time and temperature in EEPROM) using the serial connection. Before powering up (i.e. connecting the 4.5VDC battery pack), the user should connect the PICAXE cable and launch the terminal window within the Program Editor software, by pressing function key F8. The data dump should start at the beginning of EEPROM memory (index=0, the first sample) and go only until the last point taken during the logging. This means the program must track the last data point index - we'll use the EEPROM internal to the PICAXE chip, using WRITE and READ to save and restore that value.

    3. Normal logging operation begins after a short delay to allow for placement of the data logger in its measuring environment. Any prior logging attempt that stored data should be appended to, by reading the last data index and using it as a starting point.

    4. Within a DO-WHILE loop, add 4 to the index (4 bytes per data record), then read time and temperature, storing the hour, minute, second and temperature into four consecutive bytes starting at this incremented index. Additionally, attempt to send the data to the computer screen - even if the serial cable is not connected to a computer, this will flash the red LED connected to port C.0, letting you know it is logging time and temp.

    5. Lastly in the DO-WHILE loop, compare the memory index to the chip capacity and stop if it is full. Storing the last sample's index in PICAXE internal EEPROM allows you to disconnect the battery to stop a run, then let the PICAXE pick back up at the next free slot, should the user want to add another data set to that. You can download the program to start completely over.

    F

    T

    F

    START

    read number samples in EEPROM into datapt

    Pushbutton at startup?

    serout to PC "Dumping # pts"

    All data printed?

    pause 1/2 second

    wait 30 seconds

    read number of last sample into index

    index = index+4

    Setup I2C for RTC readi2c hr, min, sec

    readtemp sensor

    Setup I2C for EEPROM writei2c time, temp

    STOP

    T

    EEPROM full?

    serout to PC time, temp

    write new index to PICAXE EEPROM

    10 sec pause between samples

    serout to PC (flash red LED)

    F

    T Pulling the battery during this part of the program is OK, since each loop records the last save index value in the internal PICAXE EEPROM.

  • EEPROM 0,(0,0) 'when first programmed, this zeros out the PICAXE chip internal EEPROM ' that will hold the number of the last temperature reading taken '@10 seconds apart, 4 bytes/sample, 4096 bytes EEPROM holds 170 minutes 'note you should not use the Program button of the PICAXE Programming ' Editor to restart the program - instead, disconnect then reconnect ' the AA battery pack to restart the logger, to append new temp readings symbol seconds = b0 symbol mins = b1 symbol hour = b2 symbol day = b3 symbol date = b4 symbol month = b5 symbol year = b6 symbol control = b7 symbol temp = b8 symbol pbtn = pin3 symbol tsensor = C.4 symbol datapt = w5 'equivalent to b10, b11 symbol index = w6 'equivalent to b12, b13 '

    if pbtn != 1 then initialize '

    dump: read 0,WORD datapt 'internal EEPROM memory 0, 1 hold number of last data point serout 0,n2400,("Dumping ",#datapt, " points of data",13,10) i2cslave %10100000, i2cslow, i2cword for index = 0 to datapt step 4 readi2c index,(hour,mins,seconds,temp) serout 0,n2400,(#index," ",#hour,":",#mins,":",#seconds," Temp C: ",#temp,13,10) pause 500 next goto dump 'repeat dumping data to the computer until powered down '

    initialize: wait 30 'wait is similar to pause, except number is in seconds read 0,WORD index 'internal EEPROM memory bytes 0 and 1 hold number of last data point '

    do index = index + 4 'set EEPROM storage offset to next empty memory i2cslave %11010000, i2cslow, i2cbyte 'set up to read from clock readi2c 0,(seconds,mins,hour) 'get time and temp readtemp tsensor, temp i2cslave %10100000, i2cslow, i2cword 'set up to write to memory writei2c index,(hour,mins,seconds,temp) 'store time and temp\ '

    serout 0,n2400,(#index," ",#hour,":",#mins,":",#seconds," Temp C: ",#temp,13,10) '

    write 0,WORD index 'update last data index to internal EEPROM memory pause 10000 'wait about 10 seconds till next data measurement '

    loop while index < 4096 'top of memory is 4096 bytes long end 'if not out of memory, repeat logging

    5. Fixing problems, getting credit for the exercise:

    One known problem - the one-wire temperature sensor is very easy to connect to a PICAXE, and the readtemp function in PICAXE Basic places the current temperature in Celcius into a byte size variable. However, a byte is a unit of memory used to store integers from zero to 255 - what happens when you try to store a negative number? The freezer will reach about 0F, which is around -18C. The code given will show very unusual values once temperatures dip negative - can you fix it? Tip - look at the PICAXE manual #2 (from the Help menu in the Programming Editor) under readtemp, then edit the code that sends the temperature down the serial cable to the desktop pc.

    To get full credit for the exercise, show the instructor that you can read back logged temperatures from EEPROM, and send your code to BlackBoard. If you are working in a team, remember to list everyone in your team during the submission so they get credit as well.

    You may also want to print your code, along with some representative data captured using Edit > Copy from the Serial Terminal window.