Shop OBEX P1 Docs P2 Docs Learn Events
TSL237 light to frequency detector — Parallax Forums

TSL237 light to frequency detector

ratronicratronic Posts: 1,451
edited 2013-08-16 17:06 in Accessories
I decided to check out Parallax's light sensor and wrote a simple program to display the output frequency to the serial terminal. Sitting indoors @ ~5:00 with the TV's on lights off I get around 3 - 4 kHz.

With the room light turned on I get ~ 53 khz. Since this is so sensitive I am averaging 512 samples to smooth it out. I seem to remember reading another thread were I think people were saying

that they usually don't reset the phsa in their program. I'm doing it here and it seems to work ok.
Con                            'capture frequency output from tsl237 to serial terminal (run tsl237 from 3.3v)
                               'assembly routine sets counter to capture clk cycles while APIN is low and return full clock freq averaged 512 times                           
  _CLKMODE = XTAL1 + PLL16X                              
  _XINFREQ = 5_000_000
  
  APIN = 2           'propeller port connected to tsl237 output pin
           
Var
  long freq
    
Obj
  pst : "parallax serial terminal"
  
                        
Pub Main
  
  pst.start(9600)
  
  cognew(@entry, @freq)
    
  repeat
  
    pst.char(1)
    pst.str(string("Frequency = "))
    pst.dec(clkfreq / freq)
    pst.chars(32, 10)
    
Dat
                                org 0
Entry
                                mov t1, cntr      'set t1 loop counter
                                movs ctra, #APIN  'set counter Apin
                                movi ctra, #101_000 'set counter to accumulate when counter Apin is low
                                waitpne pin, pin  'wait for counter Apin to be low
                                waitpeq pin, pin  'wait for counter Apin to be high
                                mov frqa, #1      'start accumulating in phsa when counter Apin/condition is met
:loop                          
                                waitpne pin, pin  'wait for counter Apin to go low
                                waitpeq pin, pin  'wait for counter Apin to go high
                                add t2, phsa      'add # clk cycles Apin was low in t2
                                mov phsa, #0      'reset accumulator to 0
                                djnz t1, #:loop   'repeat this loop t1(cntr) times
                                                                
                                sar t2, #9        'divide t2 by 512 to get average of A pin low lengths
                                shl t2, #1        'multiply t2 by 2 to represent a full clock length (high & low)  
                                wrlong t2, par    'write t2 to hub variable freq
                                mov t1, cntr      'set t1 loop counter
                                jmp #:loop        'start loop again
                                
 
pin     long  1 << APIN    'pinmask for counter Apin
cntr    long  512          '# of times to average output
t1      res   1            'temp var used for averaging loop counter
t2      res   1            'temp var used for processing and writing to hub clk length freq
Sign In or Register to comment.