'Buggy_cam 'receive ASCI characters from Sweex on RS232 on PIC 16F877A '20mhz clock 'send 5 300usec +5V pules 'the 4 0V gaps are varied 0.5 to 1.5 msec, one for each channel 'remaining long gap makes the period up to 20msec ' 9600 baud on hardware usart port TRISB = 0x00 'set all PORTB pins as outputs Dim char_in As Byte 'the command character received (value 0 to 255) ' the 1 to 2 millisec 0V control gaps between the 300 microsec 5V markers Dim gap_1 As Word 'the output pulse length 0 to 65,535 Dim gap_2 As Word Dim gap_3 As Word Dim gap_4 As Word Dim marker As Word ' these 300 nsec 5V pulses separate the signal gaps Dim baseline As Word ' use baseline length (20000 - gap_1 - gap_2 - gap_3 - gap_4) ' for 20 millisec period Hseropen 9600 'open hardware uart port for baud rate 9600 WaitMs 1000 'this delay should be used in a real device ' and removed in the simulator ' send a screen of information to the terminal console Hserout "Buggy control ready", CrLf, CrLf ' set the startup values gap_1 = 1220 'Channel 1 - rotate 2110 hard R, 450 hard L gap_2 = 1180 'Channel 2 - nod level 1440 UP,900 Down gap_3 = 1500 'Channel 3 - steering gap_4 = 1500 'Channel 4 - power marker = 300 'set the 5 marker pulses to 300 microsec baseline = 14000 ' period must be 20 millisec '-------------------------------------------------------- loop: PORTB = %11111111 WaitUs marker '300 microsec +5V marker pulse PORTB = %00000000 WaitUs gap_1 PORTB = %11111111 WaitUs marker '300 microsec +5V marker pulse PORTB = %00000000 WaitUs gap_2 PORTB = %11111111 WaitUs marker '300 microsec +5V marker pulse PORTB = %00000000 WaitUs gap_3 PORTB = %11111111 WaitUs marker '300 microsec +5V marker pulse PORTB = %00000000 WaitUs gap_4 PORTB = %11111111 WaitUs marker '300 microsec +5V marker pulse PORTB = %00000000 WaitUs baseline 'wait these microseconds for 20 millisec period Hserget char_in 'get one character from the usart input buffer If char_in = 0 Then Goto loop ' 0 means nothing received ' we only do the processing below when there is a received character '------------------------------------------------------------ ' re-center nod and rotate - send M (77) If char_in = 77 Then gap_1 = 1220 gap_2 = 1180 Endif '------------------------------------------------------------ 'Channel 1 - rotate If char_in = 76 Then ' (L) gap_1 = 450 Endif If char_in = 108 Then ' (l) gap_1 = 780 Endif If char_in = 114 Then ' (r) gap_1 = 1660 Endif If char_in = 82 Then ' (R) gap_1 = 2020 Endif '------------------------------------------------------------ 'Channel 2 - nod If char_in = 85 Then ' (U) gap_2 = 1440 Endif If char_in = 117 Then ' (u) gap_2 = 1360 Endif If char_in = 100 Then ' (d) gap_2 = 1140 Endif If char_in = 68 Then ' (D) gap_2 = 900 Endif '------------------------------------------------------------ 'Channel 3 - steering '------------------------------------------------------------ 'Channel 4 - motor control '------------------------------------------------------------ ' Increment rotate - < (60) left - > (62) right If char_in = 60 Then gap_1 = gap_1 - 40 Endif If char_in = 62 Then gap_1 = gap_1 + 40 Endif '------------------------------------------------------------ ' Increment nod - ^ (94) look up - V (86) look down If char_in = 94 Then gap_2 = gap_2 + 40 Endif If char_in = 86 Then gap_2 = gap_2 - 40 Endif '------------------------------------------------------------ ' diagonal increments ' nod up and rotate left ( (40) If char_in = 40 Then gap_2 = gap_2 + 40 gap_1 = gap_1 - 40 Endif ' nod up and rotate right ) (41) If char_in = 41 Then gap_2 = gap_2 + 40 gap_1 = gap_1 + 40 Endif ' nod down and rotate left [ (91) If char_in = 91 Then gap_2 = gap_2 - 40 gap_1 = gap_1 - 40 Endif ' nod down and rotate right ] (93) If char_in = 93 Then gap_2 = gap_2 - 40 gap_1 = gap_1 + 40 Endif '------------------------------------------------------------ ' keep values within bounds If gap_1 > 2040 Then gap_1 = 2040 Endif If gap_1 < 440 Then gap_1 = 440 Endif If gap_2 > 1440 Then gap_2 = 1440 Endif If gap_2 < 900 Then gap_2 = 900 '------------------------------------------------------------ Endif baseline = 18500 - gap_1 - gap_2 - gap_3 WaitMs 100 'this delay should be used in a real device Hserout "
Character Received
", char_in, CrLf 'char_in is a byte Hserout "
Rotate
", #gap_1, CrLf Hserout "
Nod
", #gap_2, CrLf Hserout "
Steering
", #gap_3, CrLf Hserout "
Power
", #gap_4, CrLf Hserout "
", CrLf, CrLf ' gap_1 is 2byte word, use # to print its value ' use nothing in front to see the 2 bytes as characters Goto loop 'loop forever