Shop OBEX P1 Docs P2 Docs Learn Events
SX/B Word variable — Parallax Forums

SX/B Word variable

RsadeikaRsadeika Posts: 3,837
edited 2005-11-25 00:13 in General Discussion
I think I have been down this road before. I am trying to do a pbasic translation of an IR remote control/IR detector system. I have an IR detector on rc.0, which·is setup to scan for remote codes. The first hurdle, rctime is requesting a byte value, meaning I guess a value of 255 or less, I need to test for >1000. I am having trouble conceptializing the loop that would be necssary to take a value of >1000 and test for it in one cycle I guess. Basically it is supposed to test for >1000 first to get out of the first loop, then goto the next loop and test for 500, 300, 300, 300, 300, 300, 300. If I can get a grasp on that first do ... loop, then I could probably handle the rest.

Thanks for any help.


*** code
'test1.sxb
device sx28, oschs3, turbo, stackx, optionx
irc_cal irc_slow
freq 4_000_000
program start_point

WORD con 2
irPulse var byte (WORD)
remoteCode var byte

start_point:
tris_b=0

do
remoteCode = 0

·do
·rctime rc.0, 1, irPulse, 2 'error: byte parameter expected
·loop until irPulse > 1000 'error: byte parameter expected

loop

'led_on:
'rb=0
'return
'led_off:
'rb=1
'return
sleep

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-22 23:44
    Are you sure you don't want to use PULSIN?··If I remember correctly, a Sony IRCS start bit is 2.4 milliseconds wide.· You could wait for the start bit like this:

    Get_IR_Byte:
    · DO
    ··· PULSIN IrPin, 0, bitWidth
    · LOOP·WHILE bitWidth < 220

    This works because the default resolution of 10 uS means a 2.4 millisecon pulse from the IR receiver would put a value of 240 in the variable called bitWidth.· You could receive the next eight bits like this:

    Get_Bits:
    · temp1 = 0
    · FOR idx = 1 TO 8
    ··· temp1 = temp1 >> 1
    ··· PULSIN IrPin, 0, bitWidth
    ··· IF bitWidth > 100 THEN
    ····· temp1.7 =·1
    ··· ENDIF
    · NEXT
    · RETURN temp1

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • BeanBean Posts: 8,129
    edited 2005-11-22 23:44
    It would help if you could post the pbasic program your trying to convert.

    You need to use the resolution parameter of RCTIME.
    I see you are using "2" now, just increase it to "8" and the value returned will be 1/4. So if using "2" would have returned 1000, then using "8" would return 250 and you can just test if irPulse > 250

    irPulse VAR BYTE

    DO
    · rctime rc.0, 1, irPulse, 8
    LOOP UNTIL irPulse > 250

    rctime rc.0, 1, irPulse, 4
    IF irPulse > 150 THEN
    · ' whatever
    ENDIF
    IF irPulse > 250 THEN
    · ' whatever
    ENDIF

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    Those that would give up freedom for security will have neither.
    ·
  • RsadeikaRsadeika Posts: 3,837
    edited 2005-11-23 00:21
    The following program works, I use this with my BS2IC when I get disallusioned with the SX. This is pretty straight foreward with pbasic.



    ****code

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}


    ·IrDet PIN 2· 'Activity board



    ·ThresholdStart CON 1000
    ·ThresholdPulse CON 500
    ·ThresholdEdge CON 300

    ·One CON 1
    ·ChUp CON 16
    ·ChDn CON 17
    ·VolUp CON 18
    ·VolDn CON 19
    ·Power CON 21
    ·Mute CON 20
    ·Last CON 59

    ·IrPulse VAR Word
    ·remoteCode VAR Byte

    ·DO
    · GOSUB GetCode
    ·IF (remoteCode > 0) THEN GOSUB processCode

    ·LOOP



    END
    processCode:
    ' DEBUG CLS, "Press Button ",CR
    ' IF (remoteCode = ChUp) THEN DEBUG "CH+ BUTTON"
    ·IF (remoteCode = ChUp) THEN GOSUB GoFore
    ' IF (remoteCode = ChDn) THEN DEBUG "CH- BUTTON"
    ·IF (remoteCode = ChDn) THEN GOSUB GoBack
    ' IF (remoteCode = VolDn) THEN DEBUG "VOL- BUTTON"
    ·IF (remoteCode = VolDn) THEN GOSUB GoForeLeft
    ' IF (remoteCode = VolUp) THEN DEBUG "VOL+ BUTTON"
    ·IF (remoteCode = VolUp) THEN GOSUB GoForeRight
    ' IF (remoteCode = Power) THEN DEBUG "Power BUTTON"
    ·IF (remoteCode = Power) THEN GOSUB RobotStop
    ·IF (remoteCode = Last) THEN GOSUB DriftRight
    ·IF (remoteCode = Mute) THEN GOSUB DriftLeft
    RETURN

    GetCode:
    ·remoteCode = 0
    ·DO
    ·RCTIME IrDet, 1, IrPulse
    ·LOOP UNTIL irPulse > ThresholdStart

    · PULSIN IrDet, 0, irPulse
    · IF irPulse > ThresholdPulse THEN remoteCode.BIT0 = 1
    · RCTIME IrDet, 0, irPulse
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT1 = 1
    · RCTIME IrDet, 0, irPulse
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT2 = 1
    · RCTIME IrDet, 0, irPulse
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT3 = 1
    · RCTIME IrDet, 0, irPulse
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT4 = 1
    · RCTIME IrDet, 0, irPulse
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT5 = 1
    · RCTIME IrDet, 0, irPulse
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT6 = 1

    · IF (remoteCode < 10) THEN remoteCode = remoteCode + 1
    · IF (remoteCode = 10) THEN remoteCode = 0

    RETURN

    · GoFore:
    ·· HIGH 4
    ·· LOW 3
    ·· HIGH 5
    ·· LOW 6
    · RETURN

    · GoBack:
    ·· HIGH 3
    ·· LOW 4
    ·· HIGH 6
    ·· LOW 5
    · RETURN

    · GoForeRight:
    ·· HIGH 5
    ·· HIGH 3
    ·· LOW 6
    ··· PAUSE 1375·· '1275
    ·· LOW 5
    ·· LOW 3
    · RETURN

    · GoForeLeft:
    ·· HIGH 4
    ·· HIGH 6
    ·· LOW 3
    ·· LOW 5
    ··· PAUSE 1575
    ·· LOW 4
    ·· LOW 6
    · RETURN

    · RobotStop:
    ·· LOW 3
    ·· LOW 4
    ·· LOW 5
    ·· LOW 6
    · RETURN

    · DriftRight:
    ·· LOW 4
    ·· PAUSE 300
    · GOSUB GoFore
    · RETURN

    · DriftLeft:
    ·· LOW 5
    ·· PAUSE 300
    · GOSUB GoFore
    · RETURN
  • BeanBean Posts: 8,129
    edited 2005-11-23 00:55
    ·ThresholdStart CON 250 ' 1000 (will use resolution setting of 4 in SX/B)
    ·ThresholdPulse CON 100' 500 (BS2 pulsin is·in 2uSec, SX/B is in 10uSec)
    ·ThresholdEdge CON 150' 300 (will use resolution setting of 2 in SX/B)

    GetCode:
    ·remoteCode = 0
    ·DO
    · ·RCTIME IrDet, 1, IrPulse, 4
    ·LOOP UNTIL irPulse > ThresholdStart

    · PULSIN IrDet, 0, irPulse
    · IF irPulse > ThresholdPulse THEN remoteCode.BIT0 = 1
    · RCTIME IrDet, 0, irPulse, 2
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT1 = 1
    · RCTIME IrDet, 0, irPulse, 2
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT2 = 1
    · RCTIME IrDet, 0, irPulse, 2
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT3 = 1
    · RCTIME IrDet, 0, irPulse, 2
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT4 = 1
    · RCTIME IrDet, 0, irPulse, 2
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT5 = 1
    · RCTIME IrDet, 0, irPulse, 2
    · IF irPulse > ThresholdEdge THEN remoteCode.BIT6 = 1

    Try this...

    I've only posted the relavent code.

    Bean.




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    Those that would give up freedom for security will have neither.
    ·
  • RsadeikaRsadeika Posts: 3,837
    edited 2005-11-23 13:50
    Thanks Jon and Bean,

    What more could I ask for, two different ways to approach the problem. Now I have something to·work with.
  • RsadeikaRsadeika Posts: 3,837
    edited 2005-11-25 00:13
    Here is my latest code for the remote control program. I am having problems getting the get bits part to work. For some reason it's not getting the corect bits, or no bits at all. I guess it nust be something simple that I am not seeing. Any corrections would be appreciated.



    *****code

    ' =========================================================================
    '
    '·· File...... Test1.sxb
    '·· Purpose... SX/B Programming Template
    '·· Author....·
    '·· E-mail....·
    '·· Started...
    '·· Updated...
    '
    ' =========================================================================


    '
    ' Program Description
    '


    '
    ' Device Settings
    '

    DEVICE········· SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
    FREQ··········· 4_000_000


    '
    ' IO Pins
    '


    '
    ' Constants
    '


    '
    ' Variables
    '
    ·led1 var rb.0
    ·irpin var rc.0
    ·irpulse var byte

    ·temp1 var byte
    ·idx var byte

    '
    · INTERRUPT
    '

    ISR_Start:
    · ' ISR code here

    ISR_Exit:
    · RETURNINT ' {cycles}································


    ' =========================================================================
    · PROGRAM Start
    ' =========================================================================




    '
    ' Subroutine Declarations
    '
    ·ledon sub
    ·getcode sub
    ·processcode sub 1
    ·gofore sub

    '
    ' Program Code
    '

    Start:

    do
    ·ledon·· 'Turn on LED
    ·getcode
    ·processcode temp1
    ·'goto stop
    loop


    getcode:
    pause 1000· 'Turn on LED, to make sure I got here
    ·ledon

    do
    ·pulsin irpin, 0, irpulse
    loop while irpulse < 240 'Does not work with 220

    'Get bits
    ·temp1 = 0···· 'Temp1 is a byte, 8 bits long
    ·for idx = 1 to 8 'Step through eight times
    · temp1 = temp1 >> 1 'Right shift by one
    · pulsin irpin, 0, irpulse 'Get the pulse
    · if irpulse > 120 then· '1.2 ms = binary 1, 0.6 ms = binary 0
    ··· temp1 = 1·· 'Store value in bit space· ??
    · else
    ··· temp1 = 0··· 'Store value in bit space ??
    · endif
    ·next· 'Next bit

    'irpulse = temp1

    'if irpulse < 10 then
    'irpulse = irpulse + 1
    'endif

    'if irpulse = 10 then
    'irpulse = 0
    'endif

    return temp1

    processcode:
    irpulse = temp1
    if irpulse = 16 then· ' If ChUp button was pushed
    gofore· 'Turn on the LED
    endif
    return

    ledon:
    ·tris_b=0
    ·led1 = 0
    ·pause 1000
    ·led1 = 1
    return

    gofore:
    ·pause 1000· 'Turn on LED to make sure I got here
    ledon
    return


    stop:
    sleep
Sign In or Register to comment.