' ' LEGO Rotation Sensor test code ' ' Encoder on PA1 and switch/light sensor on PA2 and Switch on PA3 ' ' The lego rotation sensor is queried roughly 122hz ' ' NOTE WELL! You need to configure BASCOM for a larger hw stack than ' the default 32 bytes. Go to Options->Compiler->Chip and enter in at ' least 48 for the stack size. Real applications might need more. ' $regfile = "m16def.dat" $crystal = 8000000 $baud = 19200 $include "legolibdefine.bas" ' ' Configure AVR Timer for 122 hz interrupt rate: 8mhz / 256 / 256 ' The second 256 is due to the 8 bit counter simply rolling over. ' Config Timer0 = Timer , Prescale = 256 On Timer0 Timer0_isr ' Our timer interrupt hanlder Enable Timer0 Enable Interrupts ' ' Configure "Program" led on ARC board as an output ' Config Pinb.4 = Output Prog_led Alias Portb.4 ' ' Define ports which we will connect the sensors. ' Const Encoderport = 1 Const Lightport = 2 Const Switchport = 3 Call Initlego() ' ' My_encoder and Encoder state are needed to track an encoder. ' If a second encoder is being used, declare a second set of values. ' Dim My_encoder As Integer , My_encoderstate As Byte ' ' Do loop prints out only when Encoder changes value. Note, Encoder ' is only modified in the interrupt handler, below. Also note the ' enabling and disabling of interrupts to protect access to "my_encoder" ' Dim Temp As Integer , Sys_timer As Byte Print Print "LEGO Sensor test application" ' Uncomment "If" and "End if" if you don't want continuous output Do Disable Interrupts ' If My_encoder <> Temp Then Temp = My_encoder Enable Interrupts ' Enable interrupts before printing Print " Encoder=" ; Temp ; Print " Switch=" ; Switch(switchport) ; Print " Light=" ; Light(lightport) ; Print ' End If Enable Interrupts Idle ' Sleep until next interrupt. Loop End ' ' Interrupt handler for timer. This is called at roughly 122hz, based upon ' the timer0 setup. ' Timer0_isr: ' ' State machine to flash LED at 1 hz. ' Incr Sys_timer If Sys_timer = 25 Then Prog_led = 0 'enable low output (ON) Elseif Sys_timer = 122 Then Prog_led = 1 'disable output (OFF) Sys_timer = 0 'reset timer End If ' Call Dolegoencoder(encoderport , My_encoderstate , My_encoder) ' Return ' $include "legolib.bas" 'end program