Hi,
Following on from a suggestion on the main PBP forum, it is hoped to get people to post up code snippets for anything RC model related. I'll start the ball rolling by posting code that makes an LED pulse like a beacon and is controllable from a transmitter. - This was a result of my own bit of code and suggestions from guys on this forum on how to use pulsein command to detect the signal from the receiver.
Code:'**************************************************************** '* Name : RC Input - 12F675 * '* Author : * '* Notice : Copyright (c) 2009 * '* : All Rights Reserved * '* Date : 27/10/2009 * '* Version : 1.0 * '* Notes : * '* : * '**************************************************************** ' set up PIC config @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON __CONFIG _INTRC_OSC_NOCLKOUT _WDT_ON _PWRTE_ON _MCLRE_OFF _BODEN_ON OPTION_REG = %10000000 ' Pull-ups = off, GPIO.2 = I/O, prescaler to Timer1 GPIO = %00000000 ' All outputs = 0 on boot TRISIO = %00001000 ' GPIO.3 input, GPIO.0,2,3,4,5 output ANSEL = 0 ' Set all digital WPU = 0 ' Internal pull-ups = off ADCON0 = 0 ' A/D off ;set varibles signal VAR GPIO.3 'GPIO.3 is used to the receiver pulse VAR BYTE 'pulse is used to store the result led var GPIO.0 'GPIO.0 is the output we want to flash i var byte 'i is used for the for next loop to generate width of pulses getout var bit 'used to check switch routines '------------------------------------------------------------------------------- main: PulsIn signal, 1, pulse ' reads signal from receiver IF (pulse >= 100) AND (pulse <= 160) Then ' threshold for activation Low GPIO.0 ' turns LED off Else goto flash 'if pulse is ouside range goto flash EndIF GoTo main ' do it all again flash: for i = 1 to 254 ' for next loop Pwm GPIO.0,i,1 ' send pulse to GPIO.0 for lenght i increasing brightness pause 1 if i=253 then ' test to see value of i i=1 ' if test is true then set 1 to 1 and getout to 1 GetOut=1 if Getout=1 then goto down ' if getout is q then jump to down routine endif next i down: ' same as flash but decreases brightness for i = 254 to 1 step -1 Pwm GPIO.0,i,1 pause 1 if i=2 then ' test of value i, if i =1 then i=1 ' set i to 1 and getout to 0 GetOut=0 if Getout=0 then goto main ' test value of getout - if 0 go to start of program endif next i




Bookmarks