Pacific-Design.com

    
Home Index

1. Embedded Systems

2. RC Circuit Decay

Embedded Systems / RC Circuit Decay /

decay.pas

''--------------------------------------------------------------------'
'' Embedded Systems 
'' Written by: Kevin Duraj
''
'' Emphasizes interactions between circuits and multiple cogs
'' Test RC circuit decay measurements controlled by potenciometer.
''--------------------------------------------------------------------'
CON
  _clkmode      = xtal1 + pll16x
  _xinfreq      = 5_000_000
  POTENCIOMETER = 25
  'scale        = 16_777_216   ' 2^32 / 256
  scale         = 42_949_672   ' 2^32 / 100
  LED1          = 8
  MAX_DECAY     = 6520
  MIN_DECAY     = 45
  MAXIMUM       = 100

VAR
  long duty
  long stack[300]
  long wave

OBJ
  pst : "Parallax Serial Terminal" ' used for DEBUG


PUB Main | time, cog1

  ' Initialize boud rate for terminal
  ' Terminal pst changed  to run on Cog 7
  'DEBUG pst.Start(115_200)

  coginit(1, DimLED, @stack[150])
  waitcnt(clkfreq/2 + cnt)

  ctra[30..26] := %01000
  ctra[5..0]   := POTENCIOMETER
  frqa         := 1

  repeat
    dira[POTENCIOMETER]~~
    outa[POTENCIOMETER]~~
    waitcnt(clkfreq/100_000 + cnt)    ' Wait for circuit to charge
    phsa~                             ' Clear the phsa register
    dira[POTENCIOMETER]~              ' Pin to input stops charging circuit

    repeat 22
      waitcnt(clkfreq/60 + cnt)

   time := (phsa - 624) #> 0
   duty := time
   waitcnt(clkfreq/2 + cnt)

PUB DimLED | level, decay, wait

  ctra[30..26] := %00110
  ctra[5..0]   := LED1
  frqa         := duty * scale

  dira[LED1]~~

  '------ Calculate the flash period --------'
  decay := (MAX_DECAY - MIN_DECAY) / MAXIMUM

  repeat

    if(duty > MIN_DECAY)
      level := duty/decay
    else
      level := 0

    if(level > MAXIMUM)
      level := MAXIMUM

    wave := level * scale
    frqa := wave
    wait := duty/decay

    '-------------------------------------------------'
    '             DEBUG SECTION
    '-------------------------------------------------'
    'DEBUG pst.Str(String(pst#NL, "Range = "))
    'DEBUG pst.Dec(level)

    'DEBUG pst.Str(String(pst#NL, "Wave = "))
    'DEBUG pst.Dec(wave)

    'DEBUG pst.Str(String(pst#NL, "Decay = "))
    'DEBUG pst.Dec(decay)

    'DEBUG pst.Str(String(pst#NL, "Duty = "))
    'DEBUG pst.Dec(duty)

    'DEBUG pst.Str(String(pst#NL, "Wait = "))
    'DEBUG pst.Dec(wait)
    'DEBUG pst.NewLine
    '-------------------------------------------------'

    if(wait < 1)
      wait := 1

    waitcnt(clkfreq/(wait) + cnt)    ' Frequency of dim





syntax highlighted by Code2HTML, v. 0.9.1