* THE FORUM FOR ALL THOSE INTERESTED IN BUILDING AND OPERATING MODEL SUBMARINES *

Who is online?

In total there are 3 users online :: 0 Registered, 0 Hidden and 3 Guests :: 1 Bot

None


Most users ever online was 37 on Sun Mar 13, 2011 11:27 am

Latest topics

» Magnetic couplings..
Yesterday at 6:43 pm by tattooed

» U boat Kit
Yesterday at 11:31 am by alanw

» Pinch valves
Sat Aug 30, 2014 5:58 pm by david f

» Welfreighter
Sat Aug 30, 2014 2:06 pm by John Wrennall

» SAS Sub-drivers
Fri Aug 29, 2014 1:45 pm by merriman

» Navigator RC system
Thu Aug 28, 2014 9:43 am by John Wrennall

» Interesting video clips from John W
Wed Aug 27, 2014 5:31 pm by John Wrennall

» Haydock Park 2014
Wed Aug 27, 2014 2:34 pm by david f

» September Sub Day in Birmingham.
Wed Aug 27, 2014 12:32 pm by david f

Statistics

Our users have posted a total of 8232 messages in 1297 subjects

We have 538 registered users

The newest registered user is Jim52

Similar topics

    PIC microprocessors

    Share

    david f
    First Officer
    First Officer

    Posts: 1213
    Join date: 2010-11-10
    Age: 64
    Location: Cumbria

    PIC microprocessors

    Post  david f on Tue Apr 05, 2011 1:29 pm

    I'm just starting this thread to see who may be interested. (Harry B for one.)
    PICs are very useful devices for R/C devices. They are small, cheap and can be easily programmed using an ordinary PC.

    If you have done any progamming in the past (e.g Basic on a BBC computer)you can start to progam them in Assembler or now C.

    If you look in the For Sale section you can buy some programmed PICs from the AMS on a "Have a go" basis for £5. Applications so far are level control, pump switching and piston tank control (using Hall effect sensors)and of course the Pinger.

    Where to start? I learned everything through reading "Everday Practical Electronics" which was (and is) big on PICs


    Last edited by david forrest on Fri Apr 08, 2011 11:45 am; edited 1 time in total

    david f
    First Officer
    First Officer

    Posts: 1213
    Join date: 2010-11-10
    Age: 64
    Location: Cumbria

    Re: PIC microprocessors

    Post  david f on Fri Apr 08, 2011 11:44 am

    Based on Harry's approval, I enclose a sample of PIC assembler code. It may seem a little off putting at first but assembler grows on you!

    This is the code which measures the transmitter input pulse (stick movement if you like) and it incorporates a fail safe routine.

    This routine is used in all my programs and is called up as a interrupt subroutine when the pulse first comes in.

    **************************************************************************=

    ; INTERRUPT

    ; Measure input pulse and then output it

    ;**************************************************************************=


    ;* Measure transmitter input pulse, value in ip_pulse =


    ;* 0 = 864uS and 255 = 2139uS measured to the nearest 5uS =

    ; (With lost signal routine, taken from switcher program)

    ; Based on work by Ken Hewitt

    ;**************************************************************************


    ;INPUTS PORTA,pulse_ip

    ;OUTPUTS ip_pulse


    meas_ip nop

    clrf ip_pulse ;clear the file reg


    ;Load up counters(temp and temp1)

    movlw D'216' ;was 216

    movwf temp ;Load up temp counters

    clrw


    meas_1 nop ;Lost signal routine. If no input, increment lost signal counters

    ; INCF lostsig,1 ;Add 1 to lostsig - this section commented out to reduce loop time

    ; BCF STATUS,2

    ; movf lostsig,0

    ; sublw D'255' ;Timer minus 255

    ; btfsc STATUS,2 ;skip if clear

    INCF lostsig1,1 ;clock countr div by 255

    BCF STATUS,2

    MOVF lostsig1,0 ;Copies lostsig1

    sublw D'255' ;Timer minus 255

    btfsc STATUS,2

    INCF lostsig2,1 ;clock countr div by 255

    BCF STATUS,2

    MOVF lostsig2,0 ;Copies lostsig1

    sublw D'255' ;Timer minus 255

    btfsc STATUS,2

    goto fail_safe



    btfss GPIO,pulse_ip ;wait for ip line to be low before doing anything, then continue

    goto meas_1 ;


    clrf lostsig ;If we've got to here, we have an input ,so clear lost sig counters

    clrf lostsig1

    clrf lostsig2

    clrf lostsig3

    clrf FLAGS



    ;Time incoming pulse. Wait 864 uS if pulse is <864 uS then error

    TimeA2 nop

    decfsz temp,1 ;loop 216*4uS = 864 uS

    goto TimeA2


    ;Next test to ensure ip is still hi


    btfss GPIO,pulse_ip

    goto short_pulse


    ;Loop to meas input (first test at 1000 uS) this loop is 5uS long

    ;250*5 us = 1250us


    Time_ip btfss GPIO,pulse_ip ;test the ip is still hi

    goto end_ip ;go and test again

    incfsz ip_pulse,1

    goto Time_ip

    goto max_pulse


    ;end of ip pulse

    end_ip nop

    ;NO return with measured value in reg

    goto out


    max_pulse

    movlw D'253' ;YES set reg to 253

    movwf ip_pulse

    goto out


    short_pulse

    movlw D'3' ;

    movwf ip_pulse ;

    goto out


    fail_safe NOP ;Once all the lostsigy cntrs full, come here

    MOVLW D'127' ;servo failsafe position, was 5

    MOVWF ip_pulse

    goto out



    out nop


    ;**************************************************************************=


    Harry.Brash

    Posts: 3
    Join date: 2011-04-12

    PIC Microcontrollers

    Post  Harry.Brash on Sat Apr 16, 2011 11:29 am

    I've used a lot of different PICs over many years, from 8 pin PIC12 up. I've used assembler a lot but over recent years most of my programming has been in Microchip C18 for PIC18 devices. The Microchip development system is free as is the version of C18 which I use.

    The only things you may need to purchase to get started are a PIC programmer of some sort, a PC with USB and some PICs. I currently use a Microchip PICKit 2 programmer which costs about £33 + VAT from Farnell (this includes a demo board with a PIC to get you going). This programmer works with Flash programmable PICs (most of the current devices) and is an 'in-circuit' programmer - you need to fit a 5 pin programming connector on any board you make. There are lots of suitable alternative programmers available.

    I am building a Revell Gato with a friend and it will be run by a PIC18F4620. The program is still under development (and will probably always be!) but so far it reads in 6 channels (PWM) from the TX, scales and limits the servo outputs and then sends modified PWM pulses to the servos. The motor control is via the PIC's own PWM and a MOSFET bridge. The PIC will respond to 'emergencies' such as loss of RC signal and low battery by surfacing in a pre-programmed manner. It has ADC inputs for pressure transducers, battery voltage etc..

    The choice between assembler and C is a personal one. I used to avoid C because I believed that high level languages must be slower in real time applications but I'm impressed with the efficiency of the C compiler. It is easy to check the speed in the development software and identify any timing problems before trying it on a real PIC.

    David has included some assembler, so here is some C code for comparison.

    // Use Timer 1 overflow to detect if radio link fails
    // At 20MHz PIC clock and 8 prescale, overflow takes about 105ms
    TMR1H = 0;
    TMR1L = 0;
    T1CONbits.TMR1ON = 1; // Start Timer 1 (to watch for loss of RC signal)
    // Enable interrupts
    INTCONbits.GIEH = 1;
    INTCONbits.PEIE = 1;

    // get the pulse widths putting channels 1,2,4 and 6 in T_In[0-3]
    while (_Sync==0);
    while (_Sync==1);
    while (_Data == 0) // Get Channel 1 rudder 1.8us loop at 20MHz - 555 for 1ms
    {
    T_In[0]++;
    }
    while (_Data == 1) // Get Channel 2 bow
    {
    T_In[2]++;
    }
    while (_Data == 0) // Get Channel 3 speed
    {
    T_In[4]++;
    }
    while (_Data == 1) // Get Channel 4 pump
    {
    T_In[5]++;
    }
    while (_Data == 0) // Get Channel 5 occlude
    {
    T_In[3]++;
    }
    while (_Data == 1) // Get Channel 6 stern
    {
    T_In[1]++;
    }

    There are two inputs from the receiver to the PIC and these are _Sync and _Data. I use the inverse of the first channel output pulse as _Sync and combine (with one CD4002 chip) all the servo outputs on an alternate +- basis to generate a serial data line _Data which has all the channel timing information in alternate ‘0’ and ‘1’ pulses.

    The above code waits for _Sync line to be 0 (start of first servo pulse) then measures the times of the alternate 0s and 1s to get all the pulse widths.

    The Futaba R147F which I am using gives no output when the radio signal disappears. That causes a Timer1 overflow and the interrupt sets everything to surface the boat (hopefully).

    I will probably turn on the PIC watchdog timer to guard against PIC problems. If that causes a reset, it will also cause the boat to surface, as would a low battery condition.


    I'd be happy to help anyone get started with PICs. They are great fun and there are plenty potential frustrations to keep up your interest!








    david f
    First Officer
    First Officer

    Posts: 1213
    Join date: 2010-11-10
    Age: 64
    Location: Cumbria

    Re: PIC microprocessors

    Post  david f on Mon Apr 18, 2011 2:13 pm

    I'm impressed with the C code, Harry.

    Any teaching source for C you would recommend?


    David

    Harry.Brash

    Posts: 3
    Join date: 2011-04-12

    Re PICs

    Post  Harry.Brash on Tue Apr 19, 2011 9:47 am

    David, the version of C used in Microchip C18 is very close to the original C language, with some additions to handle the PIC hardware. It is not an object orientated language like C++ or C# which are more powerful but more complex.

    There are lots of books and on-line tutorials available for C but probably the best introduction is to look at some relevant programs. Unlike assembler, C programs use a lot of recognisable words so it is immediately ‘readable’ to some extent.

    If you download and install the free Microchip C18 compiler, there are examples included which you can look at and try.

    There is also a lot of help provided by Microchip through the C18 documentation. The information on the libraries of functions is very helpful and there is immediate help available if you install the free Microchip development system.

    Harry

    david f
    First Officer
    First Officer

    Posts: 1213
    Join date: 2010-11-10
    Age: 64
    Location: Cumbria

    Re: PIC microprocessors

    Post  david f on Tue Apr 19, 2011 12:49 pm

    Thanks for the tips on C.

    I must admit I have a book on C++ vitually unopened on my bookshelf so I am pleased to know that C is more accessible.

    The following is for anyone interested in the Assembler route. I cut my teeth on David Tait's excellent website which is still preserved (and still useful - I think.) See below:

    http://www.nomad.ee/PIC/links.html

    The best book I used to get me started is "An Introduction to PIC Microcontrollers " by R A Penfold published by Babani Electronics.
    The PIC mentioned are now obsolete but it is a good introduction and I still pick it up sometimes to look up one of the Assembler instructions (even though there are only 35!) I see it is for sale secondhand on Amazon at a wallet breaking £3.75!

    For more modern sources I would recommend buying a copy of Everyday Practical Electronics and looking up some of the excellent tutorials on PICs available in there.

    I too use the excellent Microchip PICKit2 programmer.

    Thanks for sharing your approach, Harry.I will have a go at C.

    David

    david f
    First Officer
    First Officer

    Posts: 1213
    Join date: 2010-11-10
    Age: 64
    Location: Cumbria

    Re: PIC microprocessors

    Post  david f on Fri May 27, 2011 1:21 pm

    Hi Harry,
    I've just been looking for the C compiler option on the Microchip device website and the only "free" one listed seems to be for C18 PIC devices (I tend to use the cheap and simple 12F devices.)

    Have I got this right - is the free one only available for the more sophisticated PICs??

    david f
    First Officer
    First Officer

    Posts: 1213
    Join date: 2010-11-10
    Age: 64
    Location: Cumbria

    Re: PIC microprocessors

    Post  david f on Tue Jun 07, 2011 9:51 am

    Answering my own question - the High Tech C compilers work with 12F PICs and are free on the Microchip site as the "Lite" versions. The Quickstart description is good but so far I haven't even got the very small demo program to work in C. Why does nothing ever work first time?!

    david f
    First Officer
    First Officer

    Posts: 1213
    Join date: 2010-11-10
    Age: 64
    Location: Cumbria

    Re: PIC microprocessors

    Post  david f on Wed Jun 08, 2011 8:54 am

    Progress so far.(And my code is working.)!
    The free Picclite compiler is optimised well for the useful little 12F629 PIC.

    david f
    First Officer
    First Officer

    Posts: 1213
    Join date: 2010-11-10
    Age: 64
    Location: Cumbria

    Re: PIC microprocessors

    Post  david f on Thu Jun 23, 2011 12:18 pm

    Well many thanks to Harry and his pm's but I've decided to go back to Assembler for the 12F pics!

    I was hoping that C was going to be more universal than it appears to be. There are a lot of different C compilers out there many of which require a fair amount of money.

    So I think I would recommend anyone starting out (particularly with the bigger PICs such as the 18F) to start with C. It is well (and cheaply) supported by Microchip.

    I will carry on with Assembler for the smaller PICs and thanks to Harry for the advice.

    david f
    First Officer
    First Officer

    Posts: 1213
    Join date: 2010-11-10
    Age: 64
    Location: Cumbria

    Re: PIC microprocessors

    Post  david f on Sat Jul 16, 2011 1:06 pm

    A very useful (Dutch but in English!) site on PIcs:

    http://www.voti.nl/pic/index.html

      Similar topics

      -

      Current date/time is Mon Sep 01, 2014 7:29 pm