Shop OBEX P1 Docs P2 Docs Learn Events
New to Prop...looking for some help with my code — Parallax Forums

New to Prop...looking for some help with my code

gennarobasso81gennarobasso81 Posts: 84
edited 2013-06-27 20:49 in Propeller 1
hello every, Thank you for taking the time to read this post. I am looking to learn the prop language... I have read a lot of material on it already, now I am looking to learn by doing. what I have done is hooked up the 3 axis gyroscope (L3g4300D). That is working with this current code...I also have the quick start board light up P16...showing me it is working. What I would like to do now is light up pin 17 when x axis is above 0 or maybe another nominal number to show x movement...then the same with p18 to show Y axis movement. I have the current code attached
{LED WITH 3 AXIS L3G4200D
Test File for Learning multi-functions with a sensor}                      
VAR
    long stack[30]
    byte cog

OBJ
axis  : "3-axis_L3g4200D" 
PUB LaunchBlinkCogs 
    dira [16..18]~~
    cognew(stay(16), @stack[0])
    cognew((axis.go), @stack[8]) 
  
PUB Blink( pin, rate, reps)
    dira[pin]~~
    outa[pin]~
    
    repeat reps * 2
       waitcnt(rate/2 + cnt)
       !outa[pin]
pub stay (pin)
  dira[pin] :=1
  !outa[pin] 
  repeat
    repeat until cog := 0
      stop
   
      
  
PUB Start(Pin, DelayMS, Count): Success
 
    stop                                                            
    Success := (Cog := cognew(blink(Pin, DelayMS, Count), @Stack) + 1)
pub stop
    if Cog
      cogstop(Cog~ - 1)
And the Object code is
OBJ
  pst          : "FullDuplexSerial"
CON
  _clkmode      = xtal1 + pll16x
  _clkfreq      = 80_000_000
  SDApin         = 0            ' SDA of gyro connected to P0          
  SCLpin         = 1            ' SCL of gyro connected to P1 
  WRITE         = $D2           ' Request Write operation
  READ          = $D3           ' Request Read operation
  ' Control registers
  CTRL_REG1     = $20 
  CTRL_REG2     = $21
  CTRL_REG3     = $22
  CTRL_REG4     = $23
  STATUS_REG    = $27    
  OUT_X_INC     = $A8
  x_idx = 0
  y_idx = 1
  z_idx = 2
    
VAR
  long x
  long y
  long z
   
  long cx
  long cy
  long cz
   
  long ff_x
  long ff_y
  long ff_z
   
  long multiBYTE[3]
      
PUB Go | last_ticks
  pst.start(31, 30, 0, 4800)
  ' Set modes
  Wrt_1B(CTRL_REG3, $08)                    ' Fata ready signal
  Wrt_1B(CTRL_REG4, $80)                    ' Block data update 
  Wrt_1B(CTRL_REG1, $1F)                    ' Enable all axes
  last_ticks := cnt
  repeat
    pst.tx(1)                               ' Set Terminal data at top of screen
    WaitForDataReady 
    Read_MultiB(OUT_X_INC)                  ' Read XYZ bytes
    ' Divide by 114 to reduce noise
    x := (x - cx) / 114
    y := (y - cy) / 114
    z := (z - cz) / 114
    
    RawXYZ
    WaitCnt(ClkFreq / 4  + Cnt)             ' Delay before next loop
   
PUB RawXYZ
  'Display Raw X,Y,Z data
  pst.str(string("RAW X ",11))                                                 
  pst.dec(x)
       
  pst.str(string(13, "RAW Y ",11))
  pst.dec(y)
        
  pst.str(string(13, "RAW Z ",11))
  pst.dec(z)

'' Below here routines to support I2C interfacing
    
PUB WaitForDataReady | status
    repeat
      status := Read_1B(STATUS_REG)
      if (status & $08) == $08
        quit
                        
PUB Wrt_1B(SUB1, data)
  ''Write single byte to Gyroscope.
  
      start
      send(WRITE)
      send(SUB1)
      send(data)
      stop
 
PUB Read_1B(SUB3) | rxd
 ''Read single byte from Gyroscope
      start
      send(WRITE)
      send(SUB3)
      stop
      start
      send(READ)
      rxd := receive(false)
      stop
  
     result := rxd
PUB Read_MultiB(SUB3) 
 ''Read multiple bytes from Gyroscope
     start
      send(WRITE)
      send(SUB3)
      stop
      start
      send(READ)
      multiBYTE[x_idx] := (receive(true)) |  (receive(true)) << 8
      multiBYTE[y_idx] := (receive(true)) |  (receive(true)) << 8                                                                                 
      multiBYTE[z_idx] := (receive(true)) |  (receive(false)) << 8 
      stop
  
      x := ~~multiBYTE[x_idx]
      y := ~~multiBYTE[y_idx]
      z := ~~multiBYTE[z_idx]
PRI send(value)
  value := ((!value) >< 8)
  repeat 8
    dira[SDApin]       := value
    dira[SCLpin]       := false
    dira[SCLpin]       := true
    value >>= 1
  dira[SDApin]         := false
  dira[SCLpin]         := false
  result               := not(ina[SDApin])
  dira[SCLpin]         := true
  dira[SDApin]         := true
PRI receive(aknowledge)
  dira[SDApin]         := false
  repeat 8
    result <<= 1
    dira[SCLpin]       := false
    result             |= ina[SDApin]
    dira[SCLpin]       := true
  dira[SDApin]         := (aknowledge)
  dira[SCLpin]         := false
  dira[SCLpin]         := true
  dira[SDApin]         := true
PRI start
  outa[SDApin]         := false
  outa[SCLpin]         := false
  dira[SDApin]         := true
  dira[SCLpin]         := true
PRI stop
  dira[SCLpin]         := false
  dira[SDApin]         := false

Comments

  • gennarobasso81gennarobasso81 Posts: 84
    edited 2013-06-27 11:20
    just incase anyone knows...my enter key doesn't work in this forum. Anyone else have that problem?
  • TyroneRTyroneR Posts: 22
    edited 2013-06-27 19:01
    gennarobasso81

    Im new to parallax also. Im looking forward to someone helping you with this code.I think what your trying to do will be a good learning curve for both
    of us newby's. Here's a thought. What if the leds slowly brighten as you tilt the gyro. and slowly dim as you move to center , and off when centered.
    Food for thought.
  • gennarobasso81gennarobasso81 Posts: 84
    edited 2013-06-27 20:20
    that is a great idea....I actually love it. I don't know how to code it though. Do you have any ideas? we could work on it together. You know what I have so far. I'll look up the code for led brightening and post it...we can try to figure out how to combine the code to go with the pitch, roll, and yew count
  • Cluso99Cluso99 Posts: 18,069
    edited 2013-06-27 20:27
    just incase anyone knows...my enter key doesn't work in this forum. Anyone else have that problem?
    Are you using the latest IE10 ? You need to set compatibility mode in IE10. If you cannot find it, search the General forum for the details (about a month ago IIRC)
  • TyroneRTyroneR Posts: 22
    edited 2013-06-27 20:40
    http://www.youtube.com/watch?v=eANiAChPoAA&feature=player_embedded. Check this out. Jason Dorie has a code that does the same thing, but he is using servos instead. He plans on releasing the code when he has it together
  • TyroneRTyroneR Posts: 22
    edited 2013-06-27 20:49
    Im in the middle of learning how to build a talking security system. Im using the Emic2,sound impact sensor, and an IR senor. I better stay with one project at a time. Untill I become more experienced with the propeller.
    Thanks anyhow. But when you do finish your code. Please post it so I can learn what you did.
Sign In or Register to comment.