Quantcast
Channel: MCS Electronics Forum
Viewing all articles
Browse latest Browse all 20577

BASCOM-AVR : Writing code to run multiple tasks at same time : REPLY

$
0
0
Although you mention multi-tasking, the examples given can still be executed sequentially. Set up your serial port (USART) to use interrupt driven, circular buffered data input. There are examples of this in the Bascom samples folder. Write your Android application to monitor the icons you have, and the slider setting, and to send a new Data Packet whenever comething changes, (LED State, Motor speed, etc). On the M168 do something along these lines: [code:1:60c49a5bee] Main: Do: Read in the USART data and get a full data packet If you have a full packet then PARSE the packet to determine what to do next. Loop Packetparser: 'Read in the packet, identify the command, (L1, LO, M5, etc) M5 = Motor at 5%, the value can range from 0-100% If command is valid then... If command = L1 then process LED on elseif command = Lo then process LED off elseif command = Mx then process set speed command end if return LedOn: Set POrtX.Y return etc. [/code:1:60c49a5bee] The data packet sent from the Android would typically start with a Start -of-Packet character, e.g. "<", and then be followed by several bytes of data, and then a CheckSum byte at the end of the packet. The packets might look like: <L1x <L0x <M100x <M0x etc. The parser looks at the command byte following the "<" and processes it. The CheckSum byte, (x above), is typically the sum of the preceding bytes, ignoring any carry. Add up the bytes and send that as a CheckSum. If the M168 adds up the bytes and calculates the same CheckSum value the you know the packet is valid. If not, then the packet should be discarded. Some protocols will automatically send a valid data packet received acknowledgement back to the phone. If one isn't received in a timely manner then the phone resends the last packet. There are many ways to set up the communications protocol, and they range from the simple to the very complex, (Auto retry, error correcting, etc.). Note that the M168 is running very fast. The slowest part of this system is the User pressing several buttons, and then the BT RF link. JC

Viewing all articles
Browse latest Browse all 20577

Trending Articles