Device = 18F1220
Clock = 8
Config OSC = INTIO2
Include "INTOSC8.bas"
Include "PWM0.bas" // import PWM module...
Include "Utils.bas"
Dim Duty As Word // local duty variable...
Dim sw0 as PORTA.0
Dim sw1 as PORTA.1
input (sw0)
input (sw1)
// main program...
If PWM.SetFreq(5000) Then
While true
Duty = 0
Repeat
PWM.SetDutyPercent(Duty)
Inc(Duty) // [COLOR="Red"]Here increase duty with switch1[/COLOR]
DelayMS(100)
Until Duty > 10
Repeat
PWM.SetDutyPercent(Duty)
Dec(Duty) //[COLOR="Red"]decrease the duty with switch2[/COLOR]
DelayMS(100)
Until Duty = 0
Wend
EndIf
Device = 18F1220
Clock = 8
Config OSC = INTIO2
Include "INTOSC8.bas"
Include "PW.bas" // import PWM module...
Include "Utils.bas"
Dim Duty As Word // local duty variable...
Dim sw0 As PORTA.0
Dim sw1 As PORTA.1
Input (sw0)
Input (sw1)
SetAllDigital
// main program...
If PWM.SetFreq(5000) Then
While true
Duty = 0
Repeat
PWM.SetDutyPercent(Duty)
If sw0 = 1 Then // [COLOR="Red"]maybe like this[/COLOR]
Inc(Duty)
EndIf
Until Duty > 10
Repeat
PWM.SetDutyPercent(Duty)
if sw1 = 1 then
Dec(Duty) //[COLOR="Red"]maybe like this[/COLOR]
endif
Until Duty = 0
Wend
EndIf
Duty =0;
While true
If ( (sw0 = 1) AND (Duty < 10) ) Then
Inc(Duty)
// delay here ,
EndIf
If ( (sw1 = 1) AND (Duty >0) ) Then
Dec(Duty)
// delay here ,
EndIf
PWM.SetDutyPercent(Duty)
WEND
Dim Duty As Word // local duty variable...
Dim sw0 As PORTA.0
Dim sw1 As PORTA.1
Input (sw0)
Input (sw1)
SetAllDigital
// main program...
If PWM.SetFreq(5000) Then
While true
Duty = 0
Repeat
PWM.SetDutyPercent(Duty)
If sw0 = 1 Then
Inc(Duty)
DelayMS(10)
EndIf
Until Duty > 10
Repeat
PWM.SetDutyPercent(Duty)
If sw1 = 1 Then
Dec(Duty) //maybe like this
EndIf
DelayMS(20)
Until Duty = 0
Wend
EndIf
Tell me about something I don't know Like a show how maybe toOr if you want one increment per press:
Detect switch ON
Enable incrementing
Increment count by one
Disable incrementing until switch is detected OFF
But maybe you just missed this part of the codeEnable incrementing
Increment count by one
Inc(Duty)
Dec(Duty)
Enable incrementing
Increment count by one
Disable incrementing until switch is detected OFF
/ main program...
If PWM.SetFreq(5000) Then
While true
Duty = 0
Repeat
PWM.SetDutyPercent(Duty)
If sw0 = 1 Then
Inc(Duty)
DelayMS(10)
EndIf
Until Duty = 10
Repeat
PWM.SetDutyPercent(Duty)
If sw1 = 1 Then
Dec(Duty) //maybe like this
EndIf
DelayMS(20)
Until Duty = 0
Wend
EndIf
PWM.SetDutyPercent(Duty) // set the duty to what you have set which was 0
If sw0 = 1 Then // read switch for press
Inc(Duty) // if switch was pressed increase duty by 1
DelayMS(10) // debounce switch
EndIf // if you get a 10 you skip next instruction
Until Duty > 10 // if not 10 keep checking
While true
If ( (sw0 = 1) And (Duty < 10) ) Then
Inc(Duty)
DelayMS(10)
EndIf
If ( (sw1 = 1) And (Duty >0) ) Then
Dec(Duty)
DelayMS(10)
EndIf
PWM.SetDutyPercent(Duty)
Wend
No I didn't ignore you I posted before you I tried your code it didn't work the code I posted I put the delay inYou ignored the code in my post !
This will count up while the up button is pushed and down while the down button is pushed.
Dec(Duty) //maybe like this
EndIf
DelayMS(20)
Until Duty = 0
don't work will not even turn on the lamp. and my switches are low going highCode:While true If ( (sw0 = 1) And (Duty < 10) ) Then Inc(Duty) DelayMS(10) EndIf If ( (sw1 = 1) And (Duty >0) ) Then Dec(Duty) DelayMS(10) EndIf PWM.SetDutyPercent(Duty) Wend
Duty = 0
While true
PWM.SetDutyPercent(Duty) // this is how John Barker he wrote it for swordfish shows to use it
If ( (sw0 = 1) And (Duty < 10) ) Then
Inc(Duty)
DelayMS(10)
EndIf
If ( (sw1 = 1) And (Duty >0) ) Then
Dec(Duty)
DelayMS(10)
EndIf
Wend
PWM.SetDutyPercent(Duty)
{
*****************************************************************************
* Name : PWM.BAS *
* Author : David John Barker & Warren Schroeder *
* Notice : Copyright (c) 2007 Mecanique *
* : All Rights Reserved *
* Date : 23/08/2007 *
* Version : 1.0 *
* Notes : 29/09/2007 - Added support for 2 PWM outputs *
* - Added FreqSetByTable() function *
* - PWM Table Writer Utility available at *
* http://circuit-ed.com/uplds/pwm_writer.exe *
*****************************************************************************
}
Module PWM
Dim
FMaxDuty As Word,
FTMR2ON As T2CON.2
#if _device in (18F1220, 18F1320)
Dim FPWM1Pin As PORTB.3 // RB3 connected to CCP1 module
#else
Dim FPWM1Pin As PORTC.2 // RC2 connected to CCP1 module
Dim FPWM2Pin As PORTC.1 // RC1 connected to CCP2 module
#endif
{
****************************************************************************
* Name : Start1 *
* Purpose : Start PWM Channel 1 *
****************************************************************************
}
Public Sub Start1()
CCP1CON = $0C
Output(FPWM1Pin)
FTMR2ON = 1
End Sub
{
****************************************************************************
* Name : Start2 *
* Purpose : Start PWM Channel 2 *
****************************************************************************
}
Public Sub Start2()
CCP2CON = $0C
Output(FPWM2Pin)
FTMR2ON = 1
End Sub
{
****************************************************************************
* Name : Stop1 *
* Purpose : Stop PWM Channel 1 *
****************************************************************************
}
Public Sub Stop1()
Input(FPWM1Pin)
CCP1CON = $00
End Sub
{
****************************************************************************
* Name : Stop2 *
* Purpose : Stop PWM Channel 2 *
****************************************************************************
}
Public Sub Stop2()
Input(FPWM2Pin)
CCP2CON = $00
End Sub
{
****************************************************************************
* Name : SetDuty1 *
* Purpose : The CCPR1L contains the eight MSbs And the CCP1CON<5:4> *
* : contains the two LSbs. This 10-Bit value is represented by *
* : CCPR1L:CCP1CON<5:4>. *
****************************************************************************
}
Public Sub SetDuty1(pDuty As Word)
CCP1CON.5 = pDuty.1
CCP1CON.4 = pDuty.0
CCPR1L = pDuty >> 2
End Sub
{
****************************************************************************
* Name : SetDuty2 *
* Purpose : The CCPR2L contains the eight MSbs And the CCP2CON<5:4> *
* : contains the two LSbs. This 10-Bit value is represented by *
* : CCPR2L:CCP2CON<5:4>. *
****************************************************************************
}
Public Sub SetDuty2(pDuty As Word)
CCP2CON.5 = pDuty.1
CCP2CON.4 = pDuty.0
CCPR2L = pDuty >> 2
End Sub
{
****************************************************************************
* Name : SetDuty1Percent *
* Purpose : Set the duty as a percentage for channel 1 *
****************************************************************************
}
Public Sub SetDuty1Percent(pPercent As Byte)
SetDuty1(FMaxDuty * pPercent / 100)
End Sub
{
****************************************************************************
* Name : SetDuty2Percent *
* Purpose : Set the duty as a percentage for channel 2 *
****************************************************************************
}
Public Sub SetDuty2Percent(pPercent As Byte)
SetDuty2(FMaxDuty * pPercent / 100)
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
SetFreq(5000)
Duty = 0
While true
PWM.SetDutyPercent(Duty) // this is how John Barker he wrote it for swordfish shows to use it
If ( (sw0 = 1) And (Duty < 10) ) Then
Inc(Duty)
DelayMS(10)
EndIf
If ( (sw1 = 1) And (Duty >0) ) Then
Dec(Duty)
DelayMS(10)
EndIf
Wend
EndIf
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?