Shop OBEX P1 Docs P2 Docs Learn Events
RCTIME function outputting wrong data — Parallax Forums

RCTIME function outputting wrong data

Neillson01Neillson01 Posts: 10
edited 2013-03-14 08:44 in General Discussion
I am currently undertaking a University project involving Temperature sensing using a Zener diode. The system uses a parallax BS2 microcontroller.
The system works by charging a capacitor through the leakage current of the diode for 4 seconds, then measures the capacitor discharge time using RCTIME function within PBasic and output the discharge time value on an debug screen. There is an additional clock feature that has been added to the program, the discharge time measurement has been timed into the clock.

While using the RCTIME function, the only output value that is read from the debug screen is a 1 or 0, I need help to find out why the correct RCTIME is not being outputted, at room temperature the discharge time is found to be roughly 190ms by measuring it manually. I am using a 220 ohm resistor connected to a 1nF capacitor and zener diode in parallel.

I have C+P the code below:

'Project: Temperature sensor using semi conductor diode
'Main project
'{$STAMP BS2}
'{$PBASIC 2.5}


mins VAR Byte
hrs VAR Byte
mincounter VAR Byte
diff VAR Byte
result VAR Byte


main:
mins = 0
hrs = 0


minute:
FOR mincounter = 1 TO 60
DEBUG CLS
GOSUB chdis
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, DEC result
SLEEP 56
mins = mins + 1 'add 1 to minute
IF mins = 60 THEN hours
NEXT


hours:
mins = 0
hrs = hrs + 1 'add 1 to hours vairable
IF hrs = 24 THEN hrsreset 'reset hours if hrs = 24
GOTO minute


hrsreset:
hrs = 0 'reset hour to 0
GOTO main




chdis:


HIGH 7 'sets pin 7 to start charging the capacitor
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp"
PAUSE 1000
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp."
PAUSE 1000
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp.."
PAUSE 1000
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp..."
PAUSE 1000


RCTIME 7, 1, result
diff = 1000 - result
PAUSE diff
RETURN

Comments

  • Martin_HMartin_H Posts: 4,051
    edited 2013-03-13 04:59
    Hi and welcome to the forum. Your code will be easier to read if you post it using the following tip:

    attachment.php?attachmentid=78421&d=1297987572
  • BeanBean Posts: 8,129
    edited 2013-03-13 05:00
    Did you try making "result" and "diff" a WORD var instead of a BYTE.

    If I remember correctly, the RCTIME result is not in milliseconds. I think it is in 10uSec increments.

    Bean
  • Neillson01Neillson01 Posts: 10
    edited 2013-03-13 05:05
    Hi there, I have tried this but still no success. The RCTIME result for BS2 is in 2usec increments.
  • Hal AlbachHal Albach Posts: 747
    edited 2013-03-13 07:38
    Would you show us the circuit ( as it is wired, not what you think it should be) and supplying the part number of the zener and values of all the resistors and capacitors? Possibly the charge is so small that the discharge time will be extremely short.

    Hal
  • Neillson01Neillson01 Posts: 10
    edited 2013-03-13 07:58
    I only have access to the values of the components I'm afraid but I have attached the circuit as it is wired on the breadboard, the top left wiring can be ignored as this is to be later wired to an LCD screen.
    1024 x 1816 - 139K
  • Mike GreenMike Green Posts: 23,101
    edited 2013-03-13 08:19
    If you read the RCTIME chapter in the BASIC Stamp Syntax and Reference Manual, you'll see that the maximum time for an RCTIME with a BS2 is about 130ms due to the 2us granularity. The behavior of the RCTIME is not defined for longer intervals. You'll need to use a smaller value capacitor to get the time under 130ms. Even then, you'll have to use a word variable with the RCTIME statement. With a byte variable, the way you have it, the maximum time is 510us (255 x 2us)
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-03-13 08:26
    As it is wired, the capacitor is not "charging a capacitor through the leakage current of the diode for 4 seconds". It is charging the capacitor through the 220Ω resistor and discharging (RCTIME) through the leakage current of the diode.

    Component values: The voltage rating of the zener will make
    quite a difference in how the discharge proceeds, particularly if it is around 5 volts or less. There might be a number printed on the diode, but perhaps not. You can probably read the value of the capacitor from lettering on one side, a number such as "104".

    I'm sure the time to charge up the capacitor through the 220Ω will be much less than 4 seconds, probably less than a millisecond, judging from the appearance of the capacitor.

    I suggest you simplify the code, to help resolve the problem. Leave out the clock stuff. Just have the RCTIME in a loop repeated at intervals of a second or two.

    When RCTIME returns 1, that means the capacitor discharged instantly, less than 2µs. When RCTIME returns 0, that means it took too long to discharge, greater than 1.3107 second. (2µs * 65535).
  • Hal AlbachHal Albach Posts: 747
    edited 2013-03-13 08:48
    Sorry, I meant to say that we need the values of the compnents. The resistor looks to be 220 Ohm, the capacitor will have a 3 digit number like 104 or 105, and the diode will probably have a 1Nxxxx number.

    Hal
  • Neillson01Neillson01 Posts: 10
    edited 2013-03-14 05:31
    Tracy Allen, I apologies, you are right, the capacitor discharges through the leakage current of the capacitor, but The clock is one of the main parts of the project so would like to keep that in the programme. I have found the values of the capacitor to be 1.5nF where the capacitor has 1nF written on it. The diode has 1N4148PH written on it.

    As far as the RCTIME is concerned for the time being I have programmed the RCTIME function manually by using the following code:

    chdis:
    [/B]
      HIGH 7
      DEBUG CLS
      DEBUG DEC hrs, ":", DEC mins, CR, "temp"
      PAUSE 250                                              
      DEBUG CLS
      DEBUG DEC hrs, ":", DEC mins, CR, "temp."
      PAUSE 250
      DEBUG CLS
      DEBUG DEC hrs, ":", DEC mins, CR, "temp.."
      PAUSE 250
      DEBUG CLS
      DEBUG DEC hrs, ":", DEC mins, CR, "temp..."
      PAUSE 250
    
    
      INPUT 7                                             'sets the output pin as an input to discharge the capacitor
    
    
      DO
      PAUSE 10
      timeCounter = timeCounter + 1                      
      LOOP UNTIL IN7 = 0                                  'loops until the value is equal to 0
      DEBUG CLS
      DEBUG DEC hrs, ":", DEC mins, CR, DEC timecounter
      diff = 1000 - timecounter
      PAUSE diff
      RETURN
    [B]
    

    In terms of the discharge time it is measuring it at room temperature with the above code with the value 185 - 195. The problem is as the programme runs repeatedly this value fluctuates to random values, when the "reset" button is pressed on the microcontroller interface the values are constant so are assumed to be correct, is there a way of resetting the microcontroller in the code without physically pressing it?

    Thanks

    Neill
  • Neillson01Neillson01 Posts: 10
    edited 2013-03-14 07:07
    Sorry, this was a careless mistake by me where the timecounter variable needed reset for each loop. Problem solved
  • Mike GreenMike Green Posts: 23,101
    edited 2013-03-14 07:20
    Where do you re-initialize timeCounter?

    Remember that, with the PAUSE 10, timeCounter is in units of approximately 10ms, not milliseconds. Easiest way to fix this is to increment by 10 instead of by 1. Also remember that the other statements in the loop will contribute to the execution time. See this webpage and click on the "app-notes" link for a discussion of Stamp execution times. Along those lines, you may have noticed that the hours and minutes numbers are way off. That's also because all the statements in the program that execute contribute to the execution time, not just things like PAUSE and SLEEP. The DEBUG statements, for example, take about 1ms per character transmitted. Most statements take on the order of hundreds of microseconds.
  • Neillson01Neillson01 Posts: 10
    edited 2013-03-14 08:11
    I have reset the timecounter variable just before the do...until loop in the program now. OK thank you, the times were just put as a basis for now until the rest of the program is completely finished then will apply the appropriate delays to the pause and sleep times

    Thanks

    Neill
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-03-14 08:44
    The 1N4148 is a generic switching diode, not a zener diode. In addition to temperature, the leakage current will depend (to some degree, you can assess!) on light striking the glass package.

    "The clock is one of the main parts of the project so would like to keep that in the programme." It is sooo easy to reprogram a Stamp. If one small section of a program is giving us trouble, it is standard practice to work on it in isolation.

    Is the standard RCTIME function returning good values now?
Sign In or Register to comment.