I've been trying to use a pic 18f26k80 with pwm on 2 outputs and I'm struggling
id like to get a pwm output on pin 13(PORTC.2/ccp2) and pin25 (PORTB.4/eccp1)
and i can only seem to get a pwm output on PORTB.4.
I've been hunting through the data sheets and just cant seem to get it working.
Code: Select all
Device = 18F26k80
Clock = 64
Config FOSC = HS2 'HS2 = set to high speed osc
Config PLLCFG = ON
Config SOSCSEL = DIG 'set c.0 and c.1 to digital
Include "HPWM2.bas"
Include "utils.bas"
Dim led As PORTC.0 // this is pin 7 board needs changing !
CCPTMRS = %00000000
Utils.SetAllDigital()
CM1CON = %00000100
CM2CON = %00000100
ANCON0 = %00000101
ANCON1 = %00000000
TRISA = %11110101
TRISB = %11111111
TRISC = %10111000
{ T1CON.3 = 0
T3CON.3 = 0 }
ADCON1 = %01000000
PWM2.SetFreq(10000)
PWM2.Start1
PWM2.Start2
PWM2.SetDuty1(50)
PWM2.SetDuty2(60)
While true
DelayMS(100)
Toggle(led)
Wend
End
Code: Select all
Module PWM2
Device = 18F26k80
Dim
FMaxDuty As Word,
FTMR2ON As T2CON.2
Dim FPWM1Pin As PORTB.4
Dim FPWM2Pin As PORTC.2
Public Sub Start1()
CCP1CON = $0C
Output(FPWM1Pin)
FTMR2ON = 1
End Sub
Public Sub Start2()
CCP2CON = $0C
Output(FPWM2Pin)
FTMR2ON = 1
End Sub
Public Sub SetDuty1(pDuty As Word)
CCP1CON.5 = pDuty.1
CCP1CON.4 = pDuty.0
CCPR1L = pDuty >> 2
End Sub
Public Sub SetDuty2(pDuty As Word)
CCP2CON.5 = pDuty.1
CCP2CON.4 = pDuty.0
CCPR2L = pDuty >> 2
End Sub
{
****************************************************************************
* Name : MaxDuty *
* Purpose : *
****************************************************************************
}
Public Inline Function MaxDuty() As FMaxDuty
End Function
{
****************************************************************************
* Name : SetFreq *
* Purpose : *
* Notes : Initializes Freq settings *
* : Resets Duty Cycle To 0 *
* : Requires PWM.Start afterward if PWM is not running *
****************************************************************************
}
Public Function SetFreq(pFrequency As LongWord) As Boolean
Const Fosc As LongWord = _clock * 1000000
Dim Prescale As Byte
Dim PR2Value, PRConst As Word
// loop through all the valid prescalers...
PRConst = Fosc / pFrequency / 4
Prescale = 1
Result = false
Repeat
PR2Value = PRConst / Prescale - 1 // calculate a PR2 value
If PR2Value < 256 Then // if it is a valid value, then...
FMaxDuty = (PR2Value + 1) * 4 // determine maximum duty...
PR2 = PR2Value // initialise PR2
Select Prescale // configure T2CON prescale
Case 1 : Prescale = %00000000 // prescale 1
Case 4 : Prescale = %00000001 // prescale 4
Case 16 : Prescale = %00000011 // prescale 16
End Select
SetDuty1(0) // output = 0V
SetDuty2(0) //
T2CON = (T2CON And 252) Or Prescale // load prescaler value
Result = true // function return true (success)
Exit // exit the sub
EndIf
Prescale = Prescale * 4
Until Prescale > 16
End Function
{
****************************************************************************
* Name : SetFreqByTable *
* Purpose : Change PWM frequency settings by indexing table *
* Notes : Initializes Freq settings *
* : Resets Duty Cycle To 0 *
* : Requires PWM.Start afterward if PWM is not running *
****************************************************************************
}
Public Sub SetFreqByTable(pIndex As Byte)
Const pwmtbl(4) As Byte = (255,1,127,3)
// (Fosc=48) 11719; 5859;
Dim prv As Byte
pindex = pindex * 2 // table offset value
prv = pwmtbl(pindex) // get new PR2 value from array for frequency value
FMaxDuty = (prv + 1) * 4 // maximum duty cycle resolution based on PR2 value
PR2 = prv // load PR2
SetDuty1(0) // output = 0V
SetDuty2(0) //
T2CON = (T2CON And 252) Or pwmtbl(pindex+1) // load prescaler value from array
End Sub
// initialise the module
FMaxDuty = 0
thanks in advance
Richard