Recommendations - Model Train Controller


Closed Thread
Results 1 to 40 of 102

Hybrid View

  1. #1
    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  

  2. #2
    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

  3. #3
    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

  4. #4
    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)

  5. #5
    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?

  6. #6
    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

  7. #7
    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.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by malc-c View Post
    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)
    Malcolm,

    I was using a 16F877A, I guess it was really comparing Apples to Oranges.

    I figured I better check with a 12F675, but didn't have one handy so I used a 12F683. It was close enough to figure out what was wrong.

    This get's everyone, so I should have seen it right away, but nope.

    CMCON = 7 ; disable the comparators
    ANSEL = 1 ; AN0 is analog, rest digital

    also, You have a PWM output on GPIO.0 ... The same pin as the POT is connected to on AN0. That'll cause problems with the A/D reading.

    Hope you're still interested?

    EDIT: 100hz is no problem. But you'll need a faster crystal/resonator
    INTRC won't cut it.

    HTH,
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Darrel,

    Sorry I missed that... I've remmed out the PWM line fro GPIO.0 and have got a reasonable result, especially for slow speed - The crawling speed of an 18 year old tank loco is really amaizing !!

    Here's the code

    Code:
    DEFINE OSC 20
    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  100                ; 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
    Using a close loop fedback output stage and a 12v regulated supply for the track as per the diagram attached, I get a nice smooth slow speed control. This is using the 12F675 with its internal osc.

    The loco does growl a bit when at really slow speed... and at maximum speed its fine for a goods train, but not an express. I susspect that using a higher track supply voltage will resolve the speed issue (my DVM reads 6.5v across the track at full speed), but wondered if there was anything I could do to reduce the growling at slow speed.

    Please bear in mind that I'm still somewhat new to PICs and programming.. so If I've still overlooked something in the settings then please advise what I did wrong and how to correct it.
    Attached Images Attached Images  

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


    Did you find this post helpful? Yes | No

    Default

    Hmm, I don't see the CMCON and ANSEL in your program.

    There's a R-M-W (Read-Modify-Write) problem when OUTPUTting to a pin set to Analog. It adds a high frequency component to the PWM signal.

    I don't know if that's what is causing the growling or not.

    6.5V at full speed? That doesn't sound right. The current is getting limited somewhere. Maybe if you increased the value of R4 ????
    <br>
    DT

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 : 0

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