View Full Version : Discussion any free ccpm 120 servo control PIC sample code?
chinlin0924
Jul 04, 2006, 04:02 AM
hi all
I am newbie to PIC programming. I 've read some RC related source code.
but I am now looking for eCCPM 120 control sample code for PIC.
does anyone know where I could find a sample code?(16F84A would be great)
thank you
xorcise
Jul 04, 2006, 05:22 AM
I don't know what eCCPM 120 is exactly (I'm a newbie to many of these products) but I have written code that will drive from 1 to 8 50Hz servos on any pin of your selection on a single PIC port. It should work with any P16 PIC and it will compile with the free version of mikroBASIC.
mikroBASIC 8 Servo Driver (http://www.circuit-ed.com/forums/viewforum.php?f=8)
johnnywang
Jul 04, 2006, 10:02 AM
what is mikrobasic?
seems awlsome!
xorcise
Jul 04, 2006, 10:42 AM
mikroBASIC Here (http://www.mikroe.com/en/compilers/mikrobasic/pic/index.htm)
I've been using it for about a year. If you know Visual Basic then a move to mikroBASIC is not too difficult. Libraries and examples are all free. The non-registered version is good up to 2000 words of compiled assembly.
Malc C
Jul 04, 2006, 02:48 PM
I don't think that there are any routines that will do what you want, unless you can modify some existing code or write your own. How do you propose to control the output, three pots on three inputs or from a receiver and stock 4ch radio ?
If via POTs then the PIC could use A/D conversion and its value (0 - 254) used to drive the servo on the corresponding output via PPM. However you would then need to work out the mixing so that as say ch3 (pitch on a heli for example) controlls all three servos. It could be done... something like if ch3 = XXX then pin1 and pin2 and pin3 = YYY
I use PicBASIC pro and it has a POT command that works with PICs that don't have a dedicated A/D convertors. mikroBASIC probably has something similar, but you may find that the code wont fit in the limitations of the free version, maybe Xorcise could comment on this further
xorcise
Jul 04, 2006, 03:35 PM
With the mB servo driver and assuming a 4 MHz crystal (this is a resolution of 100 steps for full range in my code) you would add this code to "main:"
dim i as word ' i is a word variable declared earlier
i = ADC_READ(2) ' reads 10-bit ADC #2
i = i div 102 ' div rounds off to closest integer
servo0 = i ' three servos of eight run together
servo1 = i
servo5 = i
chinlin0924
Jul 05, 2006, 08:55 AM
thanks for the input.
it is so interesting.
but I wonder how good the generated assmbly code (or hex code) is optimized from the mikrobasic.
could you try some code for example to see how it is optimized by mikrobasic?
thank you
ex:
i=2
i=i+1
if i>10 then
end if
assembly code:
movlw 2
addlw 1
......
chinlin0924
Jul 05, 2006, 09:07 AM
btw, I am 10 experienced programmer, but I am newbie to the electronics.
what knowledges else should I get to become an PIC application developer along with simple circuit analysis?
I knows a little knowledge:
1. voltage
2. current
3. resistor
4. diode (p/n): just knows how it works
4. transistor (pnp npn) (I know what it is , but I can't accept the way text books explain, hard to convince myself and can't use it smartly)
5. TTL and CMOS IC (just begain to read about it)
6. capicitor (hard to understand, especially the calcuation)
7. RC circuit (make me crazy when analysing 2 RC circut for pulse generation)
8. read some datasheet about IC (just began to learn)
any easy learning curve or good way to learn?
thank you
xorcise
Jul 05, 2006, 03:05 PM
but I wonder how good the generated assmbly code (or hex code) is optimized from the mikrobasic.
could you try some code for example to see how it is optimized by mikrobasic?
thank you
ex:
i=2
i=i+1
if i>10 then
end if
assembly code:
movlw 2
addlw 1
......
program TEST_CODE
dim i as byte
main:
i=2
i=i+1
if i>10 then
end if
end.
; ASM code generated by mikroVirtualMachine for PIC - V. 5.0.0.2
; Date/Time: 7/5/2006 2:53:24 PM
; Info: http://www.mikroelektronika.co.yu
; ADDRESS OPCODE ASM
; ----------------------------------------------
$0000 $2804 GOTO TEST_CODE_main
$0004 $ TEST_CODE_main:
^TEST_CODE.pbas, 3 :: main:
$0004 $ TEST_CODE_main_main:
^TEST_CODE.pbas, 4 :: i=2
$0004 $3002 MOVLW 2
$0005 $1303 BCF STATUS, RP1
$0006 $1283 BCF STATUS, RP0
$0007 $00A0 MOVWF main_global_i
^TEST_CODE.pbas, 5 :: i=i+1
$0008 $3003 MOVLW 3
$0009 $00A0 MOVWF main_global_i
^TEST_CODE.pbas, 7 :: nop
$000A $ TEST_CODE_L_2:
^TEST_CODE.pbas, 8 :: end if
$000A $ TEST_CODE_L_3:
$000A $ TEST_CODE_L_0:
$000A $280A GOTO $
The problem with the above example is that mikroBASIC is smart enough to know that it only has to produce a result and thus doesn't go through any standard tests. Here's how it works in a loop:
program TEST_CODE
dim i as byte
main:
i=2
While true
i=i+1
if i>10 then
i = 2
end if
Wend
end.
; ASM code generated by mikroVirtualMachine for PIC - V. 5.0.0.2
; Date/Time: 7/5/2006 3:00:58 PM
; Info: http://www.mikroelektronika.co.yu
; ADDRESS OPCODE ASM
; ----------------------------------------------
$0000 $2804 GOTO TEST_CODE_main
$0004 $ TEST_CODE_main:
^TEST_CODE.pbas, 3 :: main:
$0004 $ TEST_CODE_main_main:
^TEST_CODE.pbas, 4 :: i=2
$0004 $3002 MOVLW 2
$0005 $1303 BCF STATUS, RP1
$0006 $1283 BCF STATUS, RP0
$0007 $00A0 MOVWF main_global_i
^TEST_CODE.pbas, 5 :: While true
$0008 $ TEST_CODE_L_2:
^TEST_CODE.pbas, 6 :: i=i+1
$0008 $0A20 INCF main_global_i, 0
$0009 $00F1 MOVWF STACK_1
$000A $0871 MOVF STACK_1, 0
$000B $00A0 MOVWF main_global_i
^TEST_CODE.pbas, 7 :: if i>10 then
$000C $0871 MOVF STACK_1, 0
$000D $3C0A SUBLW 10
$000E $1803 BTFSC STATUS, C
$000F $2812 GOTO TEST_CODE_L_7
$0010 $ TEST_CODE_L_6:
^TEST_CODE.pbas, 8 :: i = 2
$0010 $3002 MOVLW 2
$0011 $00A0 MOVWF main_global_i
$0012 $ TEST_CODE_L_7:
^TEST_CODE.pbas, 9 :: end if
$0012 $ TEST_CODE_L_8:
$0012 $2808 GOTO TEST_CODE_L_2
^TEST_CODE.pbas, 10 :: Wend
$0013 $2813 GOTO $
chinlin0924
Jul 05, 2006, 10:02 PM
xorcise ,
Thank you for your kindly help. That is so convinent with mikroBasic.
I might put it into consideration.
btw,I am downloading its demo verion. but I would like to know how you think about how good is its PWM linrary and float point library?
thank you
:)
xorcise
Jul 05, 2006, 10:22 PM
The PIC hardware PWM has some specific limitations...not all bad though. The mikroBASIC libraries are pretty solid and are getting more mature....and they're free. You can do plenty of work with the free version and the libraries are a big plus.
Floating point works fine. Personally, I avoid floating point like the plague when working with PIC's. I will attempt every way to do integer math and add decimals later. 8-bit floating point math is also a memory hog...in ROM and RAM.
chinlin0924
Jul 05, 2006, 10:38 PM
thanks! I 've tried the mikrobasic. it is easier than I thought!!
here is the pwm sample code with its library.
'
' This code demonstrates how to use PWM library functions and procedures.
' PWM module is set on RC2.
'************************************************* *****************************
program Pwm_lib
dim j as byte
main:
TRISB = 0 ' PORTB is output
PORTB = 0 ' Clear PORTB
j = 180 ' Initial j
TRISC = 0 ' PORTC is output
PORTC = $FF ' Set PORTC to $FF
Pwm_Init(5000) ' Initialize PWM module
Pwm_start ' Start PWM
while TRUE ' endless loop
Delay_ms(10) ' wait 10ms
Inc(j)
Pwm_Change_Duty(j) ' set new duty ratio
PORTB = CCPR1L ' send value of CCPR1L to PORTB
wend
end.
vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.