18F4431 PWM question


Closed Thread
Results 1 to 3 of 3
  1. #1

    Default 18F4431 PWM question

    I am using a function generator for the input on this board. Based on the PWM, the 4431 will change its PWM channels too. The input signal is 100 hz. There is a 'pause' in the output side on PWM1,3,5,7. I am assuming I have something set up wrong and its not a hardware problem. I have tried this on four 4431's. The PWM will be fine then go high for an amount of time then go back to pulsing. If I am at 50% duty cycle the pause is the smallest. 20 and 80% is the worst. My function generator won't go lower or higher.

    Code:
    define CODE_SIZE 32
    define osc 20
    Include "modedefs.bas"	' Mode definitions for Serout
    Include "Elapsed-18.bas"
    OnOff1         var     PortE.1             'Input Signal from GPS to activate/deactivate row
    OnOff2         var     PortE.0             'Input Signal from GPS to activate/deactivate row  
    OnOff3         var     PortA.5             'Input Signal from GPS to activate/deactivate row
    OnOff4         var     PortB.5             'Input Signal from GPS to activate/deactivate row                                         
    
    Switch1        var     PortD.0             ''Output to 4066; activates Speed1
    Switch2        var     PortE.2             ''Output to 4066; activates Speed2
    Switch3        var     PortC.0             ''Output to 4066; activates Speed3
    Switch4        var     PortC.3             ''Output to 4066; activates Speed4
    
    MotorSpeed     var     PortC.1             'Input from 4066
    Population     var     PortC.2             'Input from GPS
    
    Enable1        var     PortD.1             ''Output Turns on Motor1 driver
    Enable2        var     PortD.2             ''Output Turns on Motor2 driver
    Enable3        var     PortD.3             ''Output Turns on Motor3 driver
    Enable4        var     PortD.4             ''Output Turns on Motor4 driver
    
    SinglePair     var     PortD.7             'Input Determines code path 
    Input1         var     PortB.0             'PWM0
    Input2         var     PortB.1             'PWM1
    Input3         var     PortB.2             'PWM2
    Input4         var     PortB.3             'PWM3
    
    LCD            var     PortD.5
    Duty1          Var     Word
    Duty2          Var     Word
    Duty3          Var     Word
    Duty4          Var     Word
    BCD1           var     byte
    BCD10          var     byte
    Rate           var     word
    DisplayTenths  var     word 
    MotorCount     var     byte              'Tracks motorspeed input
    Mod1           var     byte              'Offset tracker to correct motorspeed
    Mod2           Var     Byte              'Offset tracker to correct motorspeed
    Mod3           var     byte              'Offset tracker to correct motorspeed
    Mod4           Var     byte              'Offset tracker to correct motorspeed
    PosMin1        var     bit
    PosMin2        var     bit
    PosMin3        var     bit
    PosMin4        var     bit
    HighTime       var     word
    LowTime        var     word
    TotalTime      var     word
    Freq           var     word
    Prefix          con      $FE             ' needed before each command
    LcdCls          CON      $51             ' clear LCD (use PAUSE 5 after)
    LcdLine1        CON      $00             ' move cursor to line 1
    LcdLine2        con      $40             ' move curson to line 2
    LcdLine3        con      $14             ' move curson to line 3
    lcdLine4        con      $54             ' move curson to line 4
    LcdCol1         con      $00             ' move cursor to column 1
    LcdCol2         con      $07             ' move cursor to column 7
    Backlight       con      $53             ' Backlighting 1-8
    CursorPS        con      $45             'Cursor Position
    CursorOn        con      $47             'Cursof On
    CursorOff       con      $48             'Cursor Off
    
    INTCON = 0            'Interrupts off for now
    INTCON2.7 = 1         'Pull Ups disabled
    ADCON0 = 0            'A/D OFF
    ANSEL0 = %00000000    'All analogue channels to digital
    ANSEL1 = %00000000    'All analogue channels to digital
    TRISA = %111111       'PortA to inputs
    TRISB = %00010000     'PortB to outputs, except B5
    TRISC = %00000110     'PortC, Switch3 and Switch4 outputs; Motorspeed and Population inputs
    TRISD = %00000000     'PortD, Switch 1 output; Enable1-4
    TRISE = %011 
    PWMCON0=%01111111     'All odd PWMs enabled, all independent
    SSPCON=%00000000      'Disables Serial Port
    'DTCON = %00000101       ' ~500nS dead-time (for complementary outputs only)
    PTCON0 = %00000010      ' 1:1 postscale, Fosc/4 1:1 prescale, free running mode
    Freq=%11111111
    
    PTPERL =0'Freq.LowByte              
    PTPERH =1'Freq.HighByte            
    
    PWMCON1 = 1             ' updates enabled, overrides sync w/timebase
    PTCON1 = %11000000      ' PWM time base is ON, counts up  500hz
    FLTCONFIG = %00000010   ' enable fault A, cycle-by-cycle mode
    
    Serout2 LCD, 84, [Prefix,LcdCLS]
    pause 5
    
               
    Test: 
    Gosub BCD
    
    'Open Loop Motor Control
    If BCD1 = 0 then
      'gosub RowControl                 
      HIGH Enable1
      HIGH Enable2
      HIGH Enable3
      HIGH Enable4
      pulsin Population, 0, Rate
      Duty1 = (Rate*10)/492      '10%    489
      Duty2 = (Rate*10)/492      '20%    977
      Duty3 = (Rate*10)/492      '50%    2461
      Duty4 = (Rate*10)/492      '80%    3924
      Duty1 = Duty1*10
      Duty2 = Duty2*10
      Duty3 = Duty3*10
      Duty4 = Duty4*10 
      PDC0L = Duty4.LowByte
      PDC0H = Duty4.HighByte
      PDC1L = Duty2.LowByte
      PDC1H = Duty2.HighByte
      PDC2L = Duty3.LowByte
      PDC2H = Duty3.HighByte
      PDC3L = Duty1.LowByte
      PDC3H = Duty1.HighByte
      DisplayTenths=Duty1-(Duty1/10*10)
      SEROUT2 LCD,84, [Prefix,CursorPS,0, "Rate ", #Rate, " ", "Duty ", #Duty1/10, ".", #DisplayTenths, "%"]
    endif
    gosub TEst
    BCD:
    BCD1=PORTA
    BCD1=BCD1 & $0F
    BCD1=BCD1^ $0F 
    
    'BCD10=PORTC
    'BCD10=BCD10 & $F0
    'BCD10=BCD10^ $F0
    'BCD10 = BCD10 >>4
    
    BCD10=PORTC
    BCD10=BCD10 & $0F
    BCD10=BCD10^ $0F
    return

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: 18F4431 PWM question

    I am wondering if its more of a scope deal than code, lol. When I look at a long range of sample points I see a pause. If anyone sees anything wrong with the code I would appreciate any input.

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default Re: 18F4431 PWM question

    One immediate problem would be the gosub TEst after your IF block. Should be GOTO Test.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

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