Recommendations - Model Train Controller


Closed Thread
Page 1 of 3 123 LastLast
Results 1 to 40 of 102
  1. #1
    malc-c's Avatar
    malc-c Guest

    Default Recommendations - Model Train Controller

    Hi guys and gals,

    Just want to pick your brains over a project I'm looking at starting. I want to build a controller for a club layout and thought that I could use a PIC rather than traditional descrete components.

    The controler will initially have direct control (ie no simulated coasting and breaking), so I thought of two possible ways the PIC could be used, and wondered if you had any comments.

    The first is to use A/D to read the voltage on one pin, and then output a DC voltage to a FET / power transistor to drive the motor from a 12v supply.

    The second uses the same A/D to read the voltage on a pin, but the output is PWM (or more exact PPM), which is then used to drive the FET or power transistor in the same way.

    The reason I wanted to use a PIC was to add things later such as auto station stops (triggere by a sensor on the track) and simulated inetia, and programming the pick to to that might be better than loads of high value capacitors and the like

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by malc-c View Post
    Hi guys and gals,

    Just want to pick your brains over a project I'm looking at starting. I want to build a controller for a club layout and thought that I could use a PIC rather than traditional descrete components.

    The controler will initially have direct control (ie no simulated coasting and breaking), so I thought of two possible ways the PIC could be used, and wondered if you had any comments.

    The first is to use A/D to read the voltage on one pin, and then output a DC voltage to a FET / power transistor to drive the motor from a 12v supply.

    The second uses the same A/D to read the voltage on a pin, but the output is PWM (or more exact PPM), which is then used to drive the FET or power transistor in the same way.

    The reason I wanted to use a PIC was to add things later such as auto station stops (triggere by a sensor on the track) and simulated inetia, and programming the pick to to that might be better than loads of high value capacitors and the like
    Not 100% sure what it's called, but have you ever heard of DMC control (again, not sure what it really is)?
    As far as I know, you put DC power to the track, superimpose a digital HF signal over the top of the DC power (AC riding on DC). Each 'unit' has a receiver waiting for some sort of encoded sequence, whatever. 1000 different ways of doing it I suppose. Not sure how I'd do it if I had to. Maybe even IR LEDs spaced out everywhere along the track and IR detectors in each 'unit' to detect various codes and commands...

  3. #3
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks for the input

    Yes I'm awear of the "digital revolution" that is now appearing in the model railway world, however its not a possibility here for several reasons.

    1) - its a club layout, so we need to use "traditional" controllers so that members can run their own locos, and most don't use DMC

    2) - we're modelling in N gauge, and most of the modules are too large to fit is the intended stock

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by malc-c View Post
    Thanks for the input

    Yes I'm awear of the "digital revolution" that is now appearing in the model railway world, however its not a possibility here for several reasons.

    1) - its a club layout, so we need to use "traditional" controllers so that members can run their own locos, and most don't use DMC

    2) - we're modelling in N gauge, and most of the modules are too large to fit is the intended stock
    So, you have member A, with his regular ol' analog controller (big transformer, big pot, reversing switch, etc) running his loco on the same length (electrically and physically) of track (only for arguments sake) as member B who has his own controller and his own loco...

    And you want them to be able to control their 2 loco's independently...using those same controllers so the members don't have to go out and buy completely new stuff, but instead add new stuff to the output of their old stuff to make their old things do new things...

    And be able to fit these controllers inside an N gauge loco...

    Woof! Ya don't ask for much do ya

    Or have I missed something...(I probably have). I've got a few ideas (as I'm sure others do), but each one of them would require some sort of PCB to be added to each loco so it could decode what it needs to do. A small PIC based decoder with a MOSFET using bidirectional PWM drive shouldn't take up that much space (maybe 1 or 2 sq.inches. at most). I've got a 3 channel driver board that'll handle a few amps on each channel, has a 16F628A onboard along with a few other things and it's almost small enough to fit inside an N gauge loco. Take off those 'other things', redesign the board and it should fit...but again, I've probably missed something...

  5. #5
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post

    Or have I missed something...(I probably have).
    - yeah you've gone a little off the rails

    So, you have member A, with his regular ol' analog controller (big transformer, big pot, reversing switch, etc) running his loco on the same length (electrically and physically) of track (only for arguments sake) as member B who has his own controller and his own loco...
    No - the member will not be asked to bring their own controllers. The idea is that the club layout will have its own control panel with its own controllers, and as 96% of the membership don't have DCC type control the decision was taken to build "traditional" means of control so that on a club night any member can bring their own locos along and "play trains"

    As we need the option to control four trains at one time, I thought that the use of a PIC could reduce the amount of descrete components required (or use 2 pics if there aren't any that have 4 A/D ports - have yet to check the Microchip site) - especially when the controller gets developed a stage further and we build in delays to represent coasting, and simulated acceleration.

    Hope that makes it clearer

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink Good Engineering Exercise

    Hi, Malc

    Here you have THE Thing you look for ...

    just to translate from "Old" logical circuits to PIC ...

    All functions to program are clearly shown.

    Alain
    Attached Images Attached Images
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  7. #7
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Nice !

    I was hoping for something with a component count in single figures

    Seriously though, thanks for the drawing - I'll have a study in detail later

  8. #8
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default the idea

    hopefully the attached image will try to get over my idea. By adjusting VR1 the resulting value is used to adjust the PPM output on RB0 which (via the power tranny) is fed to the tracks.
    Attached Images Attached Images  

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I would use PNP over NPN for that... Depending the current needed, Some P-Channel mosfet.
    Last edited by mister_e; - 9th February 2007 at 14:51.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  10. #10
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Cool

    Driving a '3055 straight from the PIC port ...

    Interesting idea ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  11. #11
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    The diagram was for example only, I intend to use FETs for the driver (They work fine driven directly from a PIC) - If I was going to use a 3055 I would use a GP NPN tranny and connect the two in an emitter follower fashion.

    I'm really after your comments on using a PIC's A/D to control the output PW modulation..

  12. #12
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Default Think simple ...

    Hi, Malc

    Why not simply use RCTime to read your pots ...

    That would allow "simple" pics like '84 or '628 ... even 648 !!! ... just because of the pincount !!!

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  13. #13
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Alain,

    One step ahead of you... I'm using a 16F828a and set up a 100K lin pot and 0.1uf capacitor, and a simple bit of code.. but can't get it to work (just slung it together in the past 5 mins)

    The code compiles, but I don't get any PWM on the output (using a LED for test purposes at the moment - no FET connected.)

    10k between MCLR and +5v, input from the pot to A2

    Here's the code

    Code:
    ;************ set up PIC *****************
    PORTA = 0                        ' declare port level BEFORE port direction = safe power-on
    CMCON = 7                        ' PortA Digital inputs
    CCP1CON = 0                      ' PWM off
    VRCON = 0                        ' Voltage reference disabled
    OPTION_REG.7 =    0
    
    
    TRISA=%11101111                  'set PORTA as all input apart from A4
    TRISB=%00000000                  'set PORTB as all output
    
    @RC_OSC_NOCLKOUT 
    @WDT_ON
    @PWRT_ON
    @MCLR_OFF
    @BOD_ON
    @LVP_OFF
    @CPD_OFF
    @PROTECT_OFF
    
    ;************* set up varibles ************
        
    D var byte                          ;used to store the result of the pot on port A2
    scale var byte                      ;used in the POT command
    Scale = 254                         ;used to set range 
                                                                
    ;************* main program ****************
    Main:                         
    Pot PORTA.2,scale,D                 ;used to read value from pot
    pwm PORTB.4,D,1
    Goto main
    The main loop is self explanatory, port A2 is checked and the result is placed in the variable D, this is used to set the width of the PWM sent out on pin B4

    Any ideas why I can't get it to work
    Last edited by malc-c; - 10th February 2007 at 15:44.

  14. #14
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Malc,
    Your configuration fuse don't do anything. The way you do it, it actually do nothing else than define some Labels.

    Code:
            '
            '       PIC Configuration fuses
            '       =======================
                    '    _BODEN_ON 
                    '    _BODEN_OFF
                    '    _CP_ALL   
                    '    _CP_75    
                    '    _CP_50    
                    '    _CP_OFF   
                    '    _DATA_CP_ON 
                    '    _DATA_CP_OFF
                    '    _PWRTE_OFF  
                    '    _PWRTE_ON   
                    '    _WDT_ON     
                    '    _WDT_OFF    
                    '    _LVP_ON     
                    '    _LVP_OFF    
                    '    _MCLRE_ON   
                    '    _MCLRE_OFF  
                    '    _ER_OSC_CLKOUT 
                    '    _ER_OSC_NOCLKOUT 
                    '    _INTRC_OSC_CLKOUT 
                    '    _INTRC_OSC_NOCLKOUT 
                    '    _EXTCLK_OSC         
                    '    _LP_OSC             
                    '    _XT_OSC             
                    '    _HS_OSC 
            @ __CONFIG  _INTRC_OSC_NOCLKOUT & _MCLRE_OFF  &  _LVP_OFF & _WDT_ON & _PWRTE_ON  & _BODEN_ON
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  15. #15
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Steve,

    The code was ripped from a previous bit of code that worked fine - I just changed the main part of the program and deleted all that wasn't required, so I can't see why it wouldn't work.

    Its always the fuses that catch me out !!

  16. #16
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Steve,

    If I remember correctly, if I leave out any config settings it takes whats listed in the INC file ??

    If I delete my listings and add your last line it errors "overwriting previous settings" - (I'm using MPSAM) - If I leave the listings as

    Code:
    @RC_OSC_NOCLKOUT 
    @WDT_ON
    @PWRT_ON
    @MCLR_OFF
    @BOD_ON
    @LVP_OFF
    @CPD_OFF
    @PROTECT_OFF
    it compiles fine

  17. #17
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Sure it compile fine but it simply don't do anything.

    You could also Type
    Code:
    @SantaClausIsComingToTown_ON
    And MPASM won't yell, it just define a LABEL

    Code:
    @SpinHere
    @        GOTO SpinHere
    Code:
    @MCLR_OFF
    @        Goto MCLR_OFF
    also work

    Have a look at the following...
    http://www.picbasic.co.uk/forum/show...75&postcount=5
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  18. #18
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by malc-c View Post
    Alain,

    One step ahead of you... I'm using a 16F828a and set up a 100K lin pot and 0.1uf capacitor, and a simple bit of code.. but can't get it to work (just slung it together in the past 5 mins)

    pwm PORTB.4,D,1
    4

    Any ideas why I can't get it to work
    Hi, Malc

    1) May be you should try the HPWM function ... i've noticed it worked much better for motor PWM ... on PORTB.3 instead of B.4 ( ! )

    2) Try RCTime ...instead of this stupid POT ... linearity will be far better !!!

    AND KEEP WITHIN THE MANUAL VALUES ( 5 -50 k ; 0.1µF ) IF STILL USING POT !!!

    heuuuu ... your scale value (254 ) is a bit surprising ...

    Alain
    Last edited by Acetronics2; - 10th February 2007 at 18:35.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  19. #19
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink The next step ...

    Hi, Malc

    When your 1 Channel PWM will work ...

    Have a look there, then :

    http://www.pbpgroup.com/modules/wfse...p?articleid=12

    for implementing the 4 Channels ...

    Alain
    Last edited by Acetronics2; - 11th February 2007 at 11:17.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  20. #20
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks for the link, and really interesting site.

    I'm going to browse the forum to sort out my basic config problems ( Its often months between projects and I forget how to do the simple stuff ) That was the one thing I liked about JAL, you simply added something like #include 16F628a.inc at the start of the code and that took care of all the config settings.

    Thanks guys for your input - much head scratching to do !

  21. #21
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Ok I now realise where I went wrong.. since I last used PBP I re-built the PC and thus the changes made to the 16F628a inc file had been lost.. I followed Steves advice and now have a working device..sorta !

    Just need to tweek the scale so that I get a full duty cycle from 0 to 255

  22. #22
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default Change of tactics

    Ok I got nowhere with that POT command, so jump in the deep end and try using ADC.

    I've used a 12F675 with the following code. As I've never used ADC before and still find datasheets confusing I just cut and pasted examples from the PBP manual for ADC.

    Code:
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
    
    TRISIO.2 = 0   'Set GPIO.2 to output.
    ANSEL.2 = 0    'Set GPIO.2 to digital		
    GPIO.0 = 1      'gpio.0 set to input
    VRCON = 0        ' Voltage reference disabled
    OPTION_REG.7 =    0
    
    DEFINE ADC_BITS 8	' Set number of bits in result
    DEFINE ADC_CLOCK 3	' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50	' Set sampling time in microseconds
    
    led var GPIO.2
    i var byte
    D var byte
    
    main:
    ADCIN 0, D                                   
        Pwm GPIO.2,D,1
    goto main
    To my suprise, the code compiled and works, well sort off !

    using a 10K pot connected between +5v and GND with the wiper connected to GPIO 0, my DVM reads between +0.14v and +5 on GPIO 2 when the pot is turned through its range - connecting an LED to GPIO 2 confirms that something is working as its brightness can be controlled via the POT... so far so good

    I then tested the FET by connecting a 10K pot between gnd and +5v with the wiper going to the gate, the track between +12v and FET and the FET to GND. Adjusting the pot produced some degree of control over a loco... so I moved the link from the pots wiper and connected it to GPIO 2. However the DVM reads the same vdd of the 12v rail regardless of the setting of the POT.

    I can only assume that its something to do with the PWM output from the PIC that is turning on the FET continiously ?? anyone have any suggestions ?? Can a PIC have DAC so I could convert the resulting digital value to a voltage that would be used to drive the FET (or whatever output stage I use).

    I want to stick with a PIC for the control as it would be easier to use simple for next loops / or counters for programmable stops, acceleration, etc

    Comments welcome as always

  23. #23
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    i think it's more a matter how this PWM function is made...i'll do some test here first. How many different channel you need?
    Which other PIC you have on hand?
    Last edited by mister_e; - 11th February 2007 at 16:41.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  24. #24
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    LOL - Steve, I sence you see I'm strugling and a bit out of my depth

    I would like to use 4 channels in total so I can control 4 trains all independantly, but I'm not bothered if I need to use two chips.

    I have at my disposal the following PICs:

    16F84A
    16F873A
    16F628A
    16F676
    16F688
    16F877A
    16F684
    18F2550
    12F675
    18F4550

    Thanks

  25. #25
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    i think it's more a matter how this PWM function is made...i'll do some test here first.
    Steve, I think you might be right. I used a winscope (soundcard) and captured the output at different settings of the POT - it don't look much like a PPM signal to me
    Attached Images Attached Images   

  26. #26
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink

    Hi, Malc

    Your scoping confirms what I thought and read ...

    The pot function charges the capacitor, and then drags little amounts of current ( in coulombs ...), verifies if the pin low thresold is reached, takes another quantity of current, verifies, and so on ...

    That's why you get such garbage !!!

    Now, the result is a count of the little quantities ... and that explains why you must use the indicated values and not others, even the RC value remains constant.

    Alain

    PS: and what about a scale value of ... 64 ???
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  27. #27
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Alain,

    The traces were from the 12f675 that used A/D to read the voltage from a 10K pot (as detailed in post #22).

    Code:
    ADCIN 0, D                                   
        Pwm GPIO.2,D,1
    goto main
    I was expecting a traditional square wave type signal, with varying width of pulse, not the noisy signal the PIC is outputing.... The manual shows additional components for analogue output (resistor / capacitor), maybe I should try using that to get a decent signal out ?

  28. #28
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Talking The Bass beat Filter II ???

    Huh !!!

    The manual clearly tells Pwm outputs garbage ... and needs a RC or more sophisticated filter.

    Now just keep in mind PWM is only good to get some "analogic" output from the PIC.

    Another STAMP " Not so good a function " survivor.

    Now, if you want to keep the PWM function ... just compare a sawtooth to this "analogic" voltage ( once filtered ) and you'll get a neat "real PWM" at the output of the comparators.

    a quad LM 339 sould do that job very honestly ...

    But we remember DARREL's slow PWM is the shiny solution to what you want to do.

    Ah, last point ... and not least ! you'll discover the motors have a low PWM ratio thresold not to make some noise and unwanted heating while not turning. one more thing to add !!!

    Alain
    Last edited by Acetronics2; - 12th February 2007 at 10:13.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  29. #29
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Yeah, I re-read the manual and noted that it stated the PWM signal is not tidy. Tried the 10K / 1uF as suggested in the manual


    But that simply smoothed the output to a constant voltage level, regardless of what setting the pot was turned to.

    I spent a lot of time browsing the web last night and most brushed motor control using PICs involved using H-Bridges, which may be impractical for this purpose...

    I'll have a more indepth look of that PBgroup website and see if there is anything I can use - Thanks

  30. #30
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Alain,

    I'm getting way out of my depth here. I downloaded the three files from Darrel's web site (http://www.darreltaylor.com/DT_INTS-14/SPWM.html) and copied the sample code for the LEDs. Darrel stated on the PBgroup that the code Works on just about any chip. 12F, 16F, 18F, but if I try compiling with MCS I get errors, regardless of the target pic selected.

    ERROR: variable wsave3 position request 416 beyond RAM_END 335

    I've never used other include files etc before, but if I understand his site correctly it should be a simple matter of downloading and saving the three files to the PBP folder, copy and pasting the sample code into MCS and then compiling ?

    Any ideas ?

  31. #31
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink The long and winding road ...

    YESS

    Big Idea !!! just comment the faulty line ... as this location only exist in elephant memory Pics !!!

    also ... as stated in one of Darrel's further replies !!!

    Alain

    find it ... in the beginning of " DT int 14 .bas "

    ' --- IF any of these three lines cause an error ?? ----------------------------
    ' Comment them out to fix the problem ----
    ' -- It depends on which Chip you are using, as to which variables are needed --
    wsave1 var byte $A0 SYSTEM ' location for W if in bank1
    wsave2 var byte $120 SYSTEM ' location for W if in bank2
    wsave3 var byte $1A0 SYSTEM ' location for W if in bank3
    ' ------------------------------------------------------------------------------

    YOU DO NOT HAVE BANK3 in your Pic ... isn't it ???
    Last edited by Acetronics2; - 12th February 2007 at 12:51.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  32. #32
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Alain, thanks for the life saving ring... its keeping me afloat

    OK I have something that works... but its not as smooth as the PWM command in PBP.

    This is the code, based on the sample taken from Darrel's website. Using a 12F675
    Code:
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
    
    DEFINE OSC 4
    CLEAR
    
    DEFINE ADC_BITS 8	' Set number of bits in result
    DEFINE ADC_CLOCK 3	' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50	' Set sampling time in microseconds
    
    INCLUDE "DT_INTS-14.bas"            ; Base Interrupt System
    INCLUDE "SPWM_INT.bas"              ; Software PWM module
    
    DEFINE SPWM_FREQ  40                ; SPWM Frequency
    DEFINE SPWM_RES   256               ; SPWM Resolution
    
    D var byte
    
    DutyVars   VAR BYTE[3]              ; DutyCycle Variables
      DutyVar1 VAR DutyVars[0]          ; group them in an array for easy access
      DutyVar2 VAR DutyVars[1]          ; with FOR loops etc.
      DutyVar3 VAR DutyVars[2]
    
    ASM
    SPWM_LIST  macro                    ; Define Pin's to use for SPWM
         SPWM_PIN  GPIO, 0, _DutyVar1  ; and the associated DutyCycle variables
         SPWM_PIN  GPIO, 1, _DutyVar2  ; Notice the underscore before variables
         SPWM_PIN  GPIO, 2, _DutyVar3
      endm
      SPWM_INIT  SPWM_LIST              ; Initialize the Pins
    ENDASM
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,  SPWMhandler,  ASM,  yes
        endm
        INT_CREATE                      ; Creates the interrupt processor
    ENDASM
    
    @ INT_ENABLE  TMR1_INT              ; enable Timer 1 interrupts
    
    ;_____________________________________________________________________________
    
    Main:                               
     ADCIN 0, D 
        DutyVar3 = D   
    GOTO Main
    The output is quite pulsed, and moving the pot produces rather stepped increase in brightness rather than a smooth increase. Also the range of the brightness is limited to 2/3 the travel of the POT (unlike when I used the PWM command when it was right across from one end to the other)

    The waveform is different to that previously found when using the PWM command, as can be seen by the two images attached (one low, one high)

    I assume the PWM settings in Darrels code can be changed by altering the values of
    Code:
    DEFINE SPWM_FREQ  40                ; SPWM Frequency
    DEFINE SPWM_RES   256               ; SPWM Resolution
    I'll play with the values and see if anything improves - Thanks for your help on this

    EDIT:
    Changing the OSC to 20 and freq to 100 gives less flicker, but the transitions are still not smooth... I also think that I need to come up with some driver circuit for the FET or feed the PIC outout into a Darlington pair arrangement with a power tranny reather than a FET
    Attached Images Attached Images   
    Last edited by malc-c; - 12th February 2007 at 14:10.

  33. #33
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink

    Hi, Malc ...

    Think you change your Duty VERY frequently ( it's only a train ...not a jetfighter !!! ) ... so you could insert some PAUSE 100 - 300.

    Secondly, you also could change DUTY ... only if the POT result changes.( a little Hysteresis w/ ADC could be useful ??? )

    Third, you could insert a smooth duty transition ...

    like that :


    ....

    Dutyvar3 = 0

    Main:
    ADCIN 0, D

    IF D <> DutyVar3 THEN

    IF Dutyvar3 > D then Dtemp = Dtemp - 1 '( 1, 5, 10 ... as INERTIA needed ! )
    IF Dutyvar3 < D then Dtemp = Dtemp + 1

    DutyVar3 = Dtemp


    ENDIF


    PAUSE 200 '( to be trimmed ... 200 for 1 Channel, 100 for 2
    Channels ...)

    GOTO Main

    ... or something like that !!!

    Alain
    Last edited by Acetronics2; - 12th February 2007 at 14:28. Reason: skipped PAUSE ...
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  34. #34
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default better result

    In a bid to get rid of the strange noise shown on the traces in previous posts I've tried using the pulseout command.

    Whilst its still not perfect as the pusle width can go down to zero, the resulting squarewave that the PIC outputs is a lot cleaner as can be seen by the attached image

    I'm going to get some power transistors tomorrow and develop the output stage based on a few examples I found in an old book I used some 25 years ago and see how it performs.

    Here's the code if anyone is interested.

    Code:
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
    
    TRISIO.2 = 0   'Set GPIO.2 to output.
    ANSEL.2 = 0    'Set GPIO.2 to digital		
    GPIO.0 = 1      'gpio.0 set to input
    VRCON = 0        ' Voltage reference disabled
    OPTION_REG.7 =    0
    
    DEFINE ADC_BITS 8	' Set number of bits in result
    DEFINE ADC_CLOCK 3	' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50	' Set sampling time in microseconds
    
    D var byte
    
    main:
    ADCIN 0, D 
    PULSOUT GPIO.2 ,D                                  
    goto main
    Attached Images Attached Images  

  35. #35
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Guys, thanks for your input to this idea, however I think I'll opt for a controller using descrete parts afterall. I played about with a few transistors and the FET and managed to get some control over a loco... the drawback was that the armature sang due to the frequency of the pulses !!

    Mind you I've learnt a bit more of PBP along the way, so it hasn't all been a waste of time

  36. #36
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Malcolm,

    Don't know anything about trains, but I can say with somewhat good confidence, that the waveforms you were getting are due to the Soundcard Oscilloscope.

    Sound cards have a capacitively coupled input, And most have a minimum of around 20hz that they can see. That's a 20hz sine wave, not a squarewave. When you changed to pulsout, you can see on your picture that the frequency went up to almost 2khz, so it was able to at least see the waveform, even though still inaccurately.

    I ran the program here (although on a different chip) and got a nice clean 40hz PWM.

    It looks like you've given up on the PIC idea already so I won't bore you anymore.

    But if you're going to build it out of transistors, this might help.
    Various Transistor Throttles
    http://home.cogeco.ca/~rpaisley4/Throttles.html

    regards,
    DT

  37. #37
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Darrel,

    Thanks for the advice, never thought that the scope / soundcard software would cause that issue...

    I've not given up entirely on using a PIC, as I feel they offer a lot more and simpler than using descrete components. I'll keep plugging away and see what I come up with.

    I assume that using a 100 Hz frequency for the PWM would be ideal... what PIC did you use, and is having an internal clock an issue (I'm using a 12F675 for experimential use)

  38. #38
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by malc-c View Post
    Darrel,

    Thanks for the advice, never thought that the scope / soundcard software would cause that issue...

    I've not given up entirely on using a PIC, as I feel they offer a lot more and simpler than using descrete components. I'll keep plugging away and see what I come up with.

    I assume that using a 100 Hz frequency for the PWM would be ideal... what PIC did you use, and is having an internal clock an issue (I'm using a 12F675 for experimential use)
    I've ran a heater blower motor on a car at ~4khz and unless I strained to hear it, there wasn't much to hear. I know it's a different motor, but still, quite a current draw and I switched the power with MOSFETs. Initially, I ran it with somewhere around 62hz...lots of noise! Both audible and almost a vibration...not to mention the noise coming thru the stereo (which may have just been a bad ground somewhere).
    The 12F675 can run 4mhz on the internal clock. You should be able to easy incorporate the TMR0 interrupt and make a software PWM output that'll run at 3.9khz (which should be plenty high enough) and use the A/D input to set the duty cycle on that PWM. I suppose the easy way to do it would be to take the 10bit A/D input, divide it by 4, giving you 8 bits and use that as the basis for the software PWM.
    Are you familiar with doing software PWM based on interrupts?

  39. #39
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    The 12F675 can run 4mhz on the internal clock. You should be able to easy incorporate the TMR0 interrupt and make a software PWM output that'll run at 3.9khz (which should be plenty high enough) and use the A/D input to set the duty cycle on that PWM. ... Are you familiar with doing software PWM based on interrupts?
    I'd be interested to know how you could do that.
    And since Malcolm was attempting to use MY software PWM routines based on interrupts, I guess you could say I'm familiar with them.

    Let's see, 4 channels @ 3.9khz with 8-bit resolution.

    That'll take (3900 *256) = 998,400 interrupts per second.
    With a PIC running at 4mhz, it'll execute 1,000,000 intructions per second.
    That leaves, 1.0016... instructions per interrupt.

    You wouldn't have a code example for that would you?
    <br>
    DT

  40. #40
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    I'd be interested to know how you could do that.
    And since Malcolm was attempting to use MY software PWM routines based on interrupts, I guess you could say I'm familiar with them.

    Let's see, 4 channels @ 3.9khz with 8-bit resolution.

    That'll take (3900 *256) = 998,400 interrupts per second.
    With a PIC running at 4mhz, it'll execute 1,000,000 intructions per second.
    That leaves, 1.0016... instructions per interrupt.

    You wouldn't have a code example for that would you?
    <br>
    Well, yes, I've seen your PWM routines, and fine routines they are, as are most of possibly 100% of the items you post...........ppppfffffffttttttt (that's me fizzling out)

    AND I had a brain fart! I had a whole paragraph typed out explaining my position when I realized I forgot the final divide by 256 for the software PWM. Yep, 15.25hz is about the top for 4mhz. That is unless you start sacrificing resolution for speed...but it still doesn't get you up into the khz range, which is what I did to get a 4khz PWM signal in the blower motor project...then again, I only needed slow, fast and a few speeds in between for that one.

Similar Threads

  1. Microstepper controller
    By aratti in forum Schematics
    Replies: 14
    Last Post: - 3rd January 2015, 16:52
  2. problem with the GSM controller
    By Dariolo in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th May 2009, 20:33
  3. Replies: 2
    Last Post: - 14th July 2008, 22:11
  4. Model Train controller - revisited
    By malc-c in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 8th May 2007, 09:40
  5. Dedicated LCD Controller question
    By chuckles in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 27th February 2006, 14:44

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts