Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Mar 5th, 2012  |  syntax: None  |  size: 7.68 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. {{
  2.    ***************************************
  3.    * Polling_Remote                      *
  4.    ***************************************
  5.    *  See end of file for terms of use.  *              
  6.    ***************************************
  7.  
  8.     Accepts data from base unit:
  9.       L+value = sets PWM on LED
  10.       B+Value = sets frequency of buzzer
  11.       R =       Read and return Light value
  12.     Send ack back (1) for control items  
  13.  
  14.    *******************************************************
  15.    * Martin Hebel, Electronic Systems Technologies, SIUC *
  16.    * Version 1.0                                         *
  17.    *******************************************************
  18. }}
  19.  
  20. CON
  21.   _clkmode = xtal1 + pll16x
  22.   _xinfreq = 5_000_000
  23.  
  24.   ' Set pins and Baud rate for XBee comms  
  25.   XB_Rx     = 0    ' XBee DOUT
  26.   XB_Tx     = 1    ' XBee DIN
  27.   XB_Baud   = 9600 ' XBee Baud Rate
  28.  
  29.   LED       = 7
  30.   PhotoT    = 6
  31.   Buzzer    = 4
  32.  
  33.   MY_Addr   = 2    ' ***** Set address of node
  34.  
  35.   CR = 13          ' Carriage Return
  36.      
  37. OBJ
  38.    XB    : "XBee_Object"
  39.  
  40. VAR
  41.   Long LastFreq
  42.  
  43. Pub  Start | DataIn, Light
  44.   XB.start(XB_Rx, XB_Tx, 0, XB_Baud)       ' Initialize comms for XBee
  45.   XB.AT_Init                               ' Set up XBee for fast Command Mode
  46.   XB.AT_ConfigVal(string("ATMY "),MY_Addr) ' Configure node's address
  47.   XB.AT_Config(string("ATDL 0"))           ' Configure address of base
  48.    
  49.   XB.RxFlush                               ' Ensure XBee buffer empty
  50.   repeat
  51.      DataIn := XB.Rx                       ' Wait for byte
  52.      case DataIn
  53.        "L": DataIn := XB.RxDecTime(500)    ' If byte L, accept data from LED PWM
  54.             if DataIn <> -1                ' Ensure wasn't timeout
  55.               XB.Tx(1)                     ' Send ack byte of 1
  56.               PWM_Set(LED,DataIn)          ' Set PWM
  57.      
  58.    
  59.        "B": DataIn := XB.RxDecTime(500)    ' If byte B, accept data for buzzer frequency
  60.             if DataIn <> -1                ' Ensure wasn't timeout
  61.               XB.Tx(1)                     ' Send ack byte of 1
  62.               Freqout_Set(Buzzer,DataIn)   ' Set buzzer frequency
  63.              
  64.        "R": Light := RCTime(PhotoT)        ' If R, read RCTime of sensor
  65.             XB.DEC(Light)                  ' Send value
  66.             XB.CR
  67.            
  68.  
  69. Pub RCTime(pin) | count                    ' Perform RC Time
  70.   dira[pin]~~                              ' Set pin output
  71.   outa[pin]~~                              ' Set pin high
  72.   XB.Delay(10)                             ' Allow cap to charge
  73.   Count := cnt                             ' Get initial count
  74.   dira[pin]~                               ' Set pin as input
  75.   repeat                                   ' Wait for pin to go low
  76.   until ina[pin] == 0
  77.   Count := (cnt - Count)                   ' Get exit count
  78.   return Count                             ' Calculate difference
  79.  
  80.  
  81. PRI Freqout_Set(pin, freq) | temp, ch
  82. ' Configures cog counter to control frequency
  83. ' Adapted from Andy Lindsay's work
  84.  
  85. ch := 1                       ' Set Channel of counter
  86.    if Freq == LastFreq        ' If same Freq, do not adjust
  87.      return
  88.  
  89.    LastFreq := Freq
  90.    
  91.    if freq == 0                ' freq = 0 turns off square wave
  92.       waitpeq(0, |< pin, 0)    ' Wait for low signal
  93.       dira[pin]~
  94.       if ch==0
  95.         ctra := 0              ' Set CTRA/B to 0
  96.       else                    
  97.         ctrb := 0              ' Set CTRA/B to 0
  98.    
  99.        
  100.   temp := pin                  ' CTRA/B[8..0] := pin
  101.   temp += (%00100 << 26)       ' CTRA/B[30..26] := %00100
  102.   if ch==0
  103.     ctra := temp               ' Copy temp to CTRA/B
  104.     frqa := calcFrq(freq)      ' Set FRQA/B
  105.     phsa := 0                  ' Clear PHSA/B (start cycle low)
  106.   else
  107.     ctrb := temp               ' Copy temp to CTRA/B
  108.     frqb := calcFrq(freq)      ' Set FRQA/B
  109.     phsb := 0                  ' Clear PHSA/B (start cycle low)
  110.   dira[pin]~~                  ' Make pin output
  111.   result := cnt                ' Return the start time
  112.    
  113.  
  114. PRI CalcFrq(freq)
  115.  
  116. ' Solve FRQA/B = frequency * (2^32) / clkfreq with binary long division
  117. ' Adapted from Andy Lindsay's work  
  118.  
  119.   repeat 33                                    
  120.     result <<= 1
  121.     if freq => clkfreq
  122.       freq -= clkfreq
  123.       result++        
  124.     freq <<= 1
  125.    
  126. Pub PWM_Set(Pin, Duty) | Scale, Resolution
  127. ' Uses Cog Counter to produce PWM
  128. ' Adapted from Andy Linsday's work
  129.  
  130.   Resolution := 10
  131.   if duty == 0                    ' freq = 0 turns off square wave
  132.      ctra := 0                    ' Set CTRA/B to 0
  133.      dira[pin]~                   ' Make pin input
  134.   else
  135.      Scale := 2_147_483_647 / (1<< (Resolution-1))  ' Calculate scale
  136.       ctra[30..26] := %00110      ' Set ctra to DUTY mode
  137.       ctra[5..0] := pin           ' Set ctra's APIN
  138.       frqa := duty * scale        ' Set frqa register
  139.       dira[pin]~~                 ' set direction
  140. {{
  141. ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  142. │                                                   TERMS OF USE: MIT License                                                  │                                                            
  143. ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
  144. │Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation    │
  145. │files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,    │
  146. │modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
  147. │is furnished to do so, subject to the following conditions:                                                                   │
  148. │                                                                                                                              │
  149. │The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
  150. │                                                                                                                              │
  151. │THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE          │
  152. │WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR         │
  153. │COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,   │
  154. │ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                         │
  155. └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  156. }}