• AVR Freaks

16f88 PWM servo movement

Page: 12 > Showing page 1 of 2
Author
Grandpa Jay
Super Member
  • Total Posts : 279
  • Reward points : 0
  • Joined: 2006/11/21 17:08:59
  • Location: Fuquay Varina, NC
  • Status: offline
2014/08/30 07:07:31 (permalink)
0

16f88 PWM servo movement

I have a 16f88 operating model railroad crossing gates.  This was programmed back in 2006 With 0x51 PR2 period, 0x04 and 0x07 CCPR1L duty cycle.  I do not remember where these numbers came from.  This works fine but the movement is too fast.  To slow down the movement  I was thinking of entering a factor of 4 into the values.  That way I could step the duty cycle 12 positions.
Is this a good way or is there a better way?
Jay
#1

30 Replies Related Threads

    Grandpa Jay
    Super Member
    • Total Posts : 279
    • Reward points : 0
    • Joined: 2006/11/21 17:08:59
    • Location: Fuquay Varina, NC
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/30 07:49:25 (permalink)
    0
    That's not going to work.  The duty cycle is OK but the period would be wrong.  Do I need to change the clock speed?
    Jay
    #2
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/30 07:51:24 (permalink)
    0
    point is, "what is the factor of 4" ?
    0x51 * 4 won't fit in a 8bit register, to say one...

    GENOVA :D :D ! GODO
    #3
    Grandpa Jay
    Super Member
    • Total Posts : 279
    • Reward points : 0
    • Joined: 2006/11/21 17:08:59
    • Location: Fuquay Varina, NC
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/30 09:27:59 (permalink)
    0 (1)
    You are correct.  I'm thinking decimal.  The max. I could use would be 3 * 81 = 243.  That would be 9 steps in stead of 3 whitch would not be enough.
    Jay
    #4
    Ian.M
    Super Member
    • Total Posts : 13268
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/30 13:45:47 (permalink)
    +1 (1)
    You'd better tell us about the servo motor and what sort of control signal it needs.  Railway modelling has strange stuff like pulsed duty points motors that burn out if powered for more than 10 seconds . . . .

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #5
    NKurzman
    A Guy on the Net
    • Total Posts : 18041
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/30 21:53:18 (permalink)
    0
    0x04 and 0x07 CCPR1L duty cycle.  That is what you need to look at.
    The Frequency is unimportant here.
    If the Code switch between 4 and 7 that is the issue. You are seeing the max speed of the servo.
    You would need to change the rate at which the changes are set to the servo.
    Note there is 2 extra bits in the CCP control register that will give 10 bits.
    The Servo works by time you need to supply the Xtal Frequency if you want to change PR2.
     
     
    #6
    Grandpa Jay
    Super Member
    • Total Posts : 279
    • Reward points : 0
    • Joined: 2006/11/21 17:08:59
    • Location: Fuquay Varina, NC
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/31 11:01:23 (permalink)
    0
    If I decrement the CCPR1L with a delay between them.  I get into position with 3 steps.  I can change the frequency to 500000kHz and that would give me about 6 steps, better but not smooth.
    My question is; how do I make use of the 2 LSB that are in the CCP1CON. Do I have to set and clear the CCP1X and CCP1Y bits each time?  That would give me about 13 steps.
    Jay
     
    #7
    Ian.M
    Super Member
    • Total Posts : 13268
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/31 11:42:10 (permalink)
    +1 (1)
    Granpa Jay
    I can change the frequency to 500000kHz

    Really?  That's 500MHz. The fastest PIC32MZ is rated for 200MHz clock. . . . .
    If you are doing 500MHz PWM connected to a motor on a model railway layout, the FCC (or local equivalent) will be knocking on your door as you've just built a UHF broadband swept jammer! mr green

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #8
    Grandpa Jay
    Super Member
    • Total Posts : 279
    • Reward points : 0
    • Joined: 2006/11/21 17:08:59
    • Location: Fuquay Varina, NC
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/31 12:02:06 (permalink)
    0 (1)
    No its just 500kHz.  My error.
    #9
    Ian.M
    Super Member
    • Total Posts : 13268
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/31 12:39:39 (permalink)
    +1 (1)
    500KHz is still a ridiculously high PWM frequency for a motor control application.  Anything above 25KHz is far more usual (to keep the fundamental above the audible frequency range). 
     
    I asked you before for details of the motor or at least its control signal.  Perhaps you can provide them this time as it appears your 2006 algorithm is sub-optimal for smooth movement and needs a re-think.

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #10
    NKurzman
    A Guy on the Net
    • Total Posts : 18041
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/31 13:08:34 (permalink)
    0
    treat CCP1X and CCP1Y as the low bits and you will get the addition steps.
    You may need to wait for the timer interrupt to fire to avoid jitter if the update happens in the middle of an up date.
    The Servo reads Time not duty cycle.  The Pulse width should not change or the servo will not work.
    #11
    Grandpa Jay
    Super Member
    • Total Posts : 279
    • Reward points : 0
    • Joined: 2006/11/21 17:08:59
    • Location: Fuquay Varina, NC
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/31 13:53:38 (permalink)
    +1 (1)
    My understanding of model servo motors (Futaba S3003) are that a ~20ms period is primarily used to hold the position of the servo or repeat rate for the high pulse.  The high pulse width (1.0ms to 2.0ms) determine the position of the servo arm.   If a 1.0ms (~0 degrees)pulse width is followed later by a 2.0ms (~180 degrees) pulse width the transition time is based on the speed of the servo.  The only way that I can see to control this speed is to step the width of the high pulse from 1.0ms to 2.0ms.  I would like to do this in steps of 0.01ms or 0.05ms.  This may not be able using the PWM of the 16F88 with 10bit resolution.
    #12
    Ian.M
    Super Member
    • Total Posts : 13268
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/31 14:32:14 (permalink)
    +1 (1)
    *NOW* we've got there.  You are using a Futaba RC servo, not some wierd railway modlleer's motor.   Your understanding is correct and you need to generate a variable width pulse (nominally 1 to 2 ms) repeated every 20ms.   As the PIC16F88 internal oscillator can run at 8MHz giving a 2MHz instruction clock, you can actually use a CCP module to generate a suitable pulse with 0.5us  resolution.  However "set & forget" PWM mode is *NOT* suitable, and an ISR is required to set the time of the next edge.
     
    The trick is to use a free running Timer 1 and the CCP module in output compare mode.
    Start off with the CCPR registers zeroed and TMR1 at 0xFFF0 so it will roillover fairly soon after it is started.   Set CCP mode 1000 (Compare mode, set output on match).  
     
    At the next match CCP1 will go high.  In the CCP ISR, add the desired pulse width to the CCPR register pair and set CCP mode 1001 (Compare mode, clear output on match).  After the desired pulse duration, the ISR adds the remainder of the 20ms period (in terms of the timer 1 increment rate) to the CCPR register pair and sets CCP mode 1000 again.
     
    All you need to do in the main program is increment (or decrement) the variable containing the desired pulse width slowly between its value for the two end positions.
     
     

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #13
    Grandpa Jay
    Super Member
    • Total Posts : 279
    • Reward points : 0
    • Joined: 2006/11/21 17:08:59
    • Location: Fuquay Varina, NC
    • Status: offline
    Re: 16f88 PWM servo movement 2014/08/31 17:53:44 (permalink)
    0
    Thank You, that is a whole new feature that I am not familiar with.  I am using timer 0 interrupt to flash the lights.  This is going to take me awhile.
    Thank again.
    Jay 
    #14
    Grandpa Jay
    Super Member
    • Total Posts : 279
    • Reward points : 0
    • Joined: 2006/11/21 17:08:59
    • Location: Fuquay Varina, NC
    • Status: offline
    Re: 16f88 PWM servo movement 2014/09/01 14:02:24 (permalink)
    0
    Ian
    This is as far as I can get without a lot of help. I'm not sure that it is correct so far.
    1. What is the correct way to enter data into a 16bit register? I have tried two ways in setup compare with TMR1 and CCPR1 both build OK.
    2. How do you add the desired pulse width to the CCPR register and what would be a good starting value?
    3. Pos2 will be considered home position. When a train is coming the value will need to be decremented. I do not understand how this will be done. After the train passes the value will have to be incremented up to home value.
    4. Is the cycling between CCP mode 1000 and 1001 the length of the high pulse?
    5. The internal oscillator is set to 250kHz. How do you calculate the values needed for 1.7ms, 1.0ms and 20ms?
    6. Is the timer 1 turn over rate supposed to be 20ms?
    7. Is an 8bit variable used in the ISR enough? That would give me 255 steps.
     
    ;**********************************************************************
    ; Project name: New Gate CCP *
    ; Filename: New gate.asm *
    ; New Filemame New gate *
    ; Date: Sep 1, 2014 *
    ; Author: Jay Root *
    ;**********************************************************************
    ; Files required: 16F88.lkr *
    ;**********************************************************************
    ; Notes:Use timer 1 and CCP *
    ;**********************************************************************
    list p=16F88 ; list directive to define processor
    #include <p16F88.inc> ; processor specific variable definitions
    errorlevel -302 ; suppress message 302 from list file
    __CONFIG _CONFIG1, _CP_OFF & _CCP1_RB3 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_IO
    __CONFIG _CONFIG2, _IESO_OFF & _FCMEN_OFF
    ;***** VARIABLE DEFINITIONS
    INT_VAR UDATA_SHR 0x71
    w_temp RES 1 ; variable used for context saving
    status_temp RES 1 ; variable used for context saving
    pclath_temp RES 1 ; variable used for context saving
    Cnt RES 1 ;Delay Counters
    Cnt1 RES 1
    Cnt2 RES 1
    TempB RES 1 ;Temp storage for Port B
    PlayE EQU 6 ;RA6 output pulse, stop bell active low
    PlayL EQU 7 ;RA7 output pulse, start bell active low
    ;**********************************************************************
    RESET_VECTOR CODE 0x000 ;processor reset vector
    goto main ;go to beginning of program
    INT_VECTOR CODE 0x004 ;interrupt vector location
    movwf w_temp ;save off current W register contents
    movf STATUS,w ;move status register into W register
    movwf status_temp ;save off contents of STATUS register
    movf PCLATH,W ;move pclath register into W register
    movwf pclath_temp ;save off contents of PCLATH register
    ; isr code can go here or be located as a call subroutine elsewhere
    BTFSC INTCON,TMR0IF ;TMR0 Overflow Interrupt Flag bit
    CALL Flash ;Flash crossing lights
    CALL CCP ;Change gate position
    ; isr restore data
    movf pclath_temp,W ;retrieve copy of PCLATH register
    movwf PCLATH ;restore pre-isr PCLATH register contents
    movf status_temp,w ;retrieve copy of STATUS register
    movwf STATUS ;restore pre-isr STATUS register contents
    swapf w_temp,f
    swapf w_temp,w ;restore pre-isr W register contents
    retfie ;return from interrupt
    main ; remaining code goes here
    ;******************Initialise********************************
    ;Setup Port A, B, Timer 0,Osc freq & Comparator
    CLRF PORTA
    CLRF PORTB
    BANKSEL TRISA ;Go to Bank 1
    MOVLW 0x00 ;Set PortA to
    MOVWF ANSEL ;Digital I/O
    MOVLW b'00001111' ;RA0-3 Input,
    MOVWF TRISA ;RA4-7 Output
    MOVLW b'00000000' ;Set up Port B
    MOVWF TRISB ;for output
    MOVLW b'11000110' ;Set timer 0 to internal clock
    MOVWF OPTION_REG ;and prescaler to 128
    MOVLW b'00100000' ;Set internal oscillator
    MOVWF OSCCON ;to 250KHz
    BANKSEL PORTA ;Come back to Bank 0
    BSF PORTA,PlayE ;Disable play
    BSF PORTA,PlayL ;Disable continous loop
    MOVLW b'10101010' ;Set up
    MOVWF TempB ;for lights
    ;*************************************************************
    ;Setup compare
    BANKSEL CCP1CON
    MOVLW b'00001000' ;Compare mode, set output
    MOVWF CCP1CON ;on match (CCP1IF bit is set)
    MOVLW b'10000000' ;Enable interrupts
    MOVWF INTCON ;and clear flags
    CLRF TMR1L ;Clear Low byte, Ensures no rollover into TMR1H
    MOVLW 0xFF ;Value to load into TMR1H
    MOVWF TMR1H ;Write High byte
    MOVLW 0xF0 ;Value to load into TMR1L
    MOVWF TMR1H ;Write Low byte
    CLRF CCPR1 ;Zero the CCPR1L&H
    BSF PIE1,CCP1IE ;CCP1 Interrupt Enable bit
    ;Test for train*****************************************************
    CALL Pos2 ;Gates up
    Start
    BTFSC PORTA,3 ;Is train comming
    GOTO Start ;No, wait till train comes
    CALL Pos1 ;Yes, Gates down
    CALL Dly10 ;Allow for short trains
    No BTFSS PORTA,3 ;Is track clear
    GOTO No ;No, wait till train is gone
    CALL Pos2 ;Yes, Gates up
    GOTO Start ;Next train
    ;********************************************************************
    Pos1 ;Bring gate down, Start Flashers and Bell
    ;Short pulse to Servo
    ;*******MOVLW 4 ;~1mSec 0x04
    ;*******MOVWF CCPR1L ;Duty Cycle of PWM Period
    BCF PORTA,PlayL ;Start bell continous loop
    NOP ;active low
    BSF PORTA,PlayL ;Pulse only
    ;Start flasher counter
    BSF INTCON,TMR0IE ;Enable timer 0 interrupt
    RETURN
    ;**********************************************************************
    Pos2 ;Home Position gates up
    ;Long pulse to Servo
    BCF INTCON,TMR0IE ;Disable timer 0 interrupt
    CLRF PORTB ;Lights off
    BCF PORTA,PlayE ;Bell off
    NOP ;active low
    BSF PORTA,PlayE ;pulse only
    ;*******MOVLW 0x07 ;~1.7mSec 0x07
    ;*******MOVWF CCPR1L ;Duty Cycle of PWM Period
    RETURN
    ;***********************************************************************
    Flash ;Start lights flashing
    Banksel PORTB ;Go to bank 0
    COMF TempB,f
    MOVFW TempB
    MOVWF PORTB
    BCF INTCON,TMR0IF ;Clear interrupt flag
    RETURN
    ;********************************************************************
    CCP ;Adjust position THIS IS NOT RIGHT UNLESS I CAN INCLUDE IT TO
    ;TRANSION FROM POS 1 TO POSN 2 AND BACK
    BCF PIR1,CCP1IF ;A TMR1 register compare match occurred
    RETURN
    ;*********************************************************************;
    Dly10 ;Time ~10 Sec @ 250KHz
    MOVLW 0xFF
    MOVWF Cnt
    MOVLW 0x5F
    MOVWF Cnt1
    MOVLW 0x02
    MOVWF Cnt2
    CALL Timer
    RETURN
    Dly.5 ;Time ~.5 Sec @ 250KHz
    MOVLW 0xFF
    MOVWF Cnt
    MOVLW 0x12
    MOVWF Cnt1
    MOVLW 0x01
    MOVWF Cnt2
    CALL Timer
    RETURN
    Dly.1 ;Time ~0.1 Sec @ 250KHz
    MOVLW 0x7C
    MOVWF Cnt
    MOVLW 0x04
    MOVWF Cnt1
    MOVLW 0x01
    MOVWF Cnt2
    CALL Timer
    RETURN
    ;********************************************************************
    Timer ;Counter
    DECFSZ Cnt,f
    GOTO $+2
    DECFSZ Cnt1,f
    GOTO $+2
    DECFSZ Cnt2,f
    GOTO Timer
    RETURN
    ; initialize eeprom locations
    EE CODE 0x2100
    DE 0x00, 0x01, 0x02, 0x03

    END ; directive 'end of program'
     
     
    #15
    Ian.M
    Super Member
    • Total Posts : 13268
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: 16f88 PWM servo movement 2014/09/03 22:43:37 (permalink)
    +1 (1)
    Sorry, I couldn't be fagged to delouse your assembler code.  Instead I've written a complete PIC16F88 Futaba servo sweep example (ServoCCP.C) using the algorithm I described in C.  It will compile with any recent XC8 version (I used v1.32) in FREE mode and uses approximately 5% of the PIC's memory, so plenty of room left to add whatever lights, bells and whistles you want.
     
    If you still have questions or need help with an assembler version after studying and trying this one, I'll see what I can do, but you will have to develop the pulse ISR.  Its quite tricky to avoid using 16 bit maths, so at the very minimum you need to know how to add 16 bit numbers to update the CCPR register pair.
     
    The timer 1 rollover rate is *NOT* closely related to the 50Hz servo pulse rate.   My code has a rollover rate of about 15.3Hz, the only requirement being that it is less than 50Hz.
     
    HTH
    Ian.
     
    Edit: ServoCCP.c Bugfix- I forgot to set INTOSC to 4MHz.  Insert a new line:
    OSCCONbits.IRCF=0b110; // Fosc=4MHz

    before InitIO() in main().
     
    NecroEdit 2019: Now for extra goodness, I've attached the multiple servo version of this code here. With a single extra 74HC238 chip a PIC16F88 can handle eight servos concurrently. :)
     
    post edited by Ian.M - 2019/10/13 06:54:23

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #16
    Grandpa Jay
    Super Member
    • Total Posts : 279
    • Reward points : 0
    • Joined: 2006/11/21 17:08:59
    • Location: Fuquay Varina, NC
    • Status: offline
    Re: 16f88 PWM servo movement 2014/09/04 05:05:24 (permalink)
    0
    Ian
    Thank you, I will try to learn your "C" program.  That's a lot of set up (most of which I do not understand) for a little main function.  I have spent the last two days trying to get the gates to operate slowly using assembly code.  I have the servos working and am in the process of trying to add the flashing lights.  I am not going to use the ISR, as I think that this would inter fear with the servo timing .
    Jay
    #17
    Ian.M
    Super Member
    • Total Posts : 13268
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: 16f88 PWM servo movement 2014/09/04 06:24:29 (permalink)
    0
    Most of the setup is a framework to make adding other stuff later much easier.  e.g.

    // Fake shadow port structures to avoid RMW effect
    #define sPORTAbits (*(PORTAbits_t * volatile)&sPORTA)
    #define sPORTBbits (*(PORTBbits_t * volatile)&sPORTB)
     
    #define updPorts() (PORTA=sPORTA, PORTB=sPORTB)  //Update ports from shadows


    volatile near uint8_t sPORTA=0,sPORTB=0; // Port shadowing registers, in shared RAM for performance

    is all to implement port shadowing to avoid the RMW effect problem for when you add the flashing lights.  The CONFIG is a little verbose, but that's the format the MPLAB X Config code generator outputs + I added some conditional compilation to make it compatible with in-circuit debugging without having to change the source.
     
    For the rest of it, I just pulled out the I/O initialisation and CCP module + Timer 1 + interrupts setup into functions (subroutines) to keep the main program clean and easy to follow.
     
    There are some slightly tricky macros:

    #define usToTcy(x) ((unsigned long)((x)*(_XTAL_FREQ/4000000.0))) // time in us => Tcy
    #define msToTcy(x) ((unsigned long)((x)*(_XTAL_FREQ/4000.0))) // time in ms => Tcy
     

    that convert a time interval to an instruction cycle count at build time.
     
    N.B.  As they do floating point maths, it is critically important to NEVER pass a variable or C const to them as you want the compiler to do the maths on the PC with a literal number or expresson rather than bloating the PIC code with slow floating point routines.
     
    Their use enables  setting the servo pulse width in real units e.g.
    ATOMIC pulsewidth=msToTcy(1.7);

    will set a 1.7ms wide servo pulse and as long as you keep _XTAL_FREQ up to date, will do so no matter what you set the clock speed to.
     
    ATOMIC adds a sightly tricky wrapper:
    #define ATOMIC for(INTCONbits.GIE=0;!INTCONbits.GIE;INTCONbits.GIE=1)

    to the statement or {...} block following it to disable interrupts for that statement or block only.
    Its needed to avoid a servo glitch if the interrupt fires while the pulsewidth is being updated.  Otherwise without disabling interrupts during the update, it could get the low byte of one pulsewidth mixed with the high byte of another which could cause the next pulse to command a position up to 45 degrees out which, at best, would make the servo twitch a bit!
     
    Assuming you are using a ISR that resembles mine to handle the CCP pin toggling, the shortest possible servo pulse is 1ms, so you have at least 900us to do other things before its essential the interrupt be handled.   That's plenty of time to handle another section in the ISR to deal with light flashing, etc.   I'm even wondering if it will be possible to play back an 8Khz WAV file through the voltage reference DAC for crossing bell sound effects.  Trouble is an 8KHz sample rate only gives 250 instruction cycles at 8MHz Fosc for the whole ISR, and the bell effect sound is going to need to be packed into only 4K words of FLASH.

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #18
    Grandpa Jay
    Super Member
    • Total Posts : 279
    • Reward points : 0
    • Joined: 2006/11/21 17:08:59
    • Location: Fuquay Varina, NC
    • Status: offline
    Re: 16f88 PWM servo movement 2014/09/04 14:34:16 (permalink)
    0
    Ian
    I apologize for having you spend so much time.  I think that I have lost too many brain cells to even try to keep up with your programming skills.  My background is mechanical, electronics is just an interesting hobby along with garden railroading in G scale.  PIC chips are an off shot of that.  I have several small motor controllers and one stepper motor in my layout.  I have tinkered my way thou trial and error (not planing or programming) in assembly and have a crossing controller that works reasonably well using stepped delay counters for the high and low pulses.  The bell is a board that I found some time ago that allowed me to record an actual crossing bell, and play it back in a loop.
    Jay 
    #19
    Ian.M
    Super Member
    • Total Posts : 13268
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: 16f88 PWM servo movement 2014/09/04 17:05:10 (permalink)
    +3 (2)
    I hope it swept your actual servo correctly, limit to limit.  It had only been tested in the MPLAB 8 simulator and logic analyser as I was too tight for time to dig out an actual servo and breadboard it.
     
    Most of it was 'canned code' I'd already written for other projects.  The slowest part was simulating it and double checking the servo pulse width swept correctly even if I changed the clock speed.   I caught a bug in the 20ms period setting there and had to replace the version I'd just uploaded! sad (fortunately before anyone else had downloaded it).  
     
    The second slowest part was pulling in all the stuff I normally keep in separate headers and C files and ripping out board specific I/O setup stuff to end up with a single C file so I didn't intimidate you with a six or eight file project and have to make MPLAB 8 and MPLAB X versions.
     
    Actually implementing the algorithm was pretty quick.  I'd already run in in my head so knew exactly how it had to go together and that all critical timings had plenty of margin on them at my chosen clock speed.   The time to Tcy macros were shamelessly stolen from the compiler's pic.h where they were masquerading as the delay macros:  just file off the serial number_delay and give them a quick repaint rename and I had two brand spanking new macros ready to sell use! wink
     
    The great thing with doing it in C is I don't have to remember so many of the core architectural details but can instead concentrate on the overall picture and getting the best out of the peripherals.  This conserves my remaining brain cells by *NOT* having to worry if each instruction will smash the flag bit in STATUS I want to test next or trying to juggle too many variables with only one W register.  If you don't already have adequate knowledge of basic C, set aside some time to gain it - It will increase the complexity of projects that fall within your "comfort zone" by at least one order of magnitude.  Ask if you want suggestions for resources to help you learn C.
     
    Give some details of the lights you need to operate (e.g. their pattern and timing and pick which I/O pins you want to use), and I'll either fit in the time to reinstate and customise the LED flashing code I ripped out when I grabbed a 'F88 project framework to base the above demo on, or at least point you in the right direction to get it running yourself concurrent with the gate movement.

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5