me again


Closed Thread
Results 1 to 6 of 6

Thread: me again

  1. #1
    Join Date
    Mar 2006
    Posts
    24

    Unhappy me again

    Sorry to keep bothering you all but can anyone tell me how to get both HPWM channels working on a 16F877.
    I get a pwm signal out of one channel but not the other. I have tried the declares listed below but no good.

    My code is here.

    @ Device pic16F877A, WDT_OFF, BOD_OFF, DEBUG_OFF , LVP_OFF, PROTECT_OFF, CPD_OFF, XT_OSC
    include "Interrupter.bas"

    Define LCD_DREG PORTD
    Define LCD_DBIT 4
    Define LCD_RSREG PORTC
    Define LCD_RSBIT 7
    Define LCD_EREG PORTC
    Define LCD_EBIT 6

    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

    'DEFINE CCP1_REG PORTC 'Hpwm 1 pin port
    'DEFINE CCP1_BIT 2 'Hpwm 1 pin bit
    'DEFINE CCP2_REG PORTC 'Hpwm 2 pin port
    'DEFINE CCP2_BIT 1 'Hpwm 2 pin bit
    'DEFINE HPWM1_TIMER 1
    'DEFINE HPWM2_TIMER 2

    TRISC = %00000000

    DUTY VAR WORD

    T2CON=%00000100
    PR2=1
    DUTY=4
    CCP1CON=%00001100 ' PWM MODE
    CCP1CON.5=DUTY.1
    CCP1CON.4=DUTY.0
    CCPR1L=DUTY>>2



    PlugOutput var portd.2
    volts var word[2] 'to recieve the A2D value
    PlugOn var byte
    plugon = 0
    low PlugOutput
    INTCON.5 = 0
    t1con.0 = 0
    low portd.3
    low portc.1
    low portc.2


    pause 500

    main:
    pause 100
    lcdout $FE,1,"PWM Test:" ,#volts / 10, "." , #volts // 100
    lcdout $FE, $C0, "Val = ", " : ", dec2 i_sec

    'Run the glow plug for a set time
    if portc.4 = 1 then
    plugon = 1
    i_sec = 0
    endif

    if plugon = 1 then
    INTCON.5 = 1
    t1con.0 = 1
    endif

    if i_sec >= 10 then
    intcon.5 = 0
    t1con.0 = 0
    plugon = 0
    i_sec = 0
    low PlugOutput
    low portd.3
    endif
    call SetVoltage1
    call SetVoltage2
    call SetVoltage3
    'pause 100
    goto main

    SetVoltage1: '5vDc out put control (Gyro and Tail servo)
    input porte.2
    ADCON1 = 2 ' PORTA is analog
    pause 50
    ADCIN 7, volts ' Read channel 7 to variable Volts
    volts = (volts * 1000)/((256 / 5)*10)
    'if volts > ReqVolts1 then duty1 = duty1 - 1
    'if volts < ReqVolts1 then duty1 = duty1 + 1
    HPWM 1, 25, 200 'Channel1, duty cycle%, 2kHz
    return

    SetVoltage2: '5 to 7 vDc out put control (RX and other servos)
    input porte.1
    ADCON1 = 2
    pause 50
    ADCIN 6, volts
    volts = (volts * 1000)/((256 / 5)*10)
    'if volts > ReqVolts2 then duty2 = duty2 - 1
    'if volts < ReqVolts2 then duty2 = duty2 + 1
    HPWM 2, 50, 200
    return

    SetVoltage3: '1.2 vDc out put plug
    input porte.0
    ADCON1 = 2
    pause 50
    ADCIN 5, volts
    volts = (volts * 1000)/((256 / 5)*10)
    'if volts > PlugVolts then duty3 = duty3 - 1
    'if volts < PlugVolts then duty3 = duty3 + 1
    return

  2. #2
    Join Date
    Mar 2006
    Posts
    7


    Did you find this post helpful? Yes | No

    Default PWM on 2 channels

    Hi,
    The two PWM channels on the 16F877 share the same timer, which is timer 2. The two PWM channels must have the same period. They can have different duty cycles. You can check the data sheet to see if the 16F877A has the same restrictions about the timer.

    Thanks,

    Keith

  3. #3
    Join Date
    Mar 2006
    Posts
    24


    Did you find this post helpful? Yes | No

    Cool I thought they did

    I have the same frequency for both dont I?
    Have I missed something, I have been looking at the same thing for too long now and could miss something realy obvious.

  4. #4
    Join Date
    Mar 2006
    Posts
    7


    Did you find this post helpful? Yes | No

    Default Timers

    Hi,
    I think you are using the same frequency, but more than that you have to use exactly the same timer.

    'DEFINE HPWM1_TIMER 1
    'DEFINE HPWM2_TIMER 2

    I think that you should use:
    'DEFINE HPWM1_TIMER 2
    'DEFINE HPWM2_TIMER 2

    but I am not sure, having never used PICBASIC to control PWM's

    Thanks, Keith

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


    Did you find this post helpful? Yes | No

    Default


    Code:
    HPWM 1, 25, 200 'Channel1, duty cycle%, 2kHz
    try with
    Code:
    HPWM 1, 25, 2000 'Channel1, duty cycle%, 2kHz
    Code:
            @  __CONFIG _XT_OSC & _LVP_OFF
    
    PORTC=0
    TRISC=0
    
    duty var byte
    DEFINE HPWM2_TMR 2
    
    Start:
        for duty =0 to 255
            hpwm 1,(255-DUTY),2000
            hpwm 2,DUTY,2000
            pause 10
            next
        goto start
    Last edited by mister_e; - 1st March 2007 at 17:59.
    Steve

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

  6. #6
    Join Date
    Mar 2006
    Posts
    24


    Did you find this post helpful? Yes | No

    Smile Again Thanks

    Once again mate,
    Thanks for the help

    This is what the help file says in the package I have been using

    DEFINE HPWM2_TIMER 1 'Hpwm 2 timer select
    DEFINE HPWM3_TIMER 1 'Hpwm 3 timer select

    Example

    HPWM 1,127,1000 ' Send a 50% duty cycle PWM signal at 1kHz
    HPWM 1,64,200 ' Send a 25% duty cycle PWM signal at 2kHz

    See Also

    PWM

    Copyright © 2001 microEngineering Labs, Inc.
    All rights reserved.


    That is where the 200 came from. I thought it didnt look right at the time but didnt have enough experience to challenge it.

    Anyway. It all works now.

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