I want to increase duty with switch1 one step at a time till it gets to 10
And also decrease the duty with switch2 till it's back to 0 but
Code:
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
Let me no if this looks right This don't increase duty by 1 it just jumps to 10
Code:
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
I set nothing to debounce the switches or slow down how fast they are read.
It sees the switch go active and rapidly rechecks it till it reaches 10. Just as you programed it
More like this
The basic is sure to have syntax errors but you get the idea.
Code:
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
I did not not check to see if you were reading the switches correctly.
Thanks I put a delay in but it wasn't working right so I took it out hoping some one would show me the error of my ways.
Code:
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
This works a simple as it gets pwm dimmer that can turn on a light bulb and dim it. Oh P.S I made the dec delay longer don't want it to jump to off.
Thanks 3v0 the code I posted work as I put the delay where you showed it
Code:
/ 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
Maybe I'm just unfamiliar with how the PIC and this BASIC programming actually execute but isn't this going to just ZING to 10 with a 10mS delay between increments?
Repeat
PWM.SetDutyPercent(Duty)
If sw0 = 1 Then
Inc(Duty)
DelayMS(10)
EndIf
Until Duty > 10
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
This will count up while the up button is pushed and down while the down button is pushed.
Code:
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
Are you using pull down resistors with the switches. With a pullup in place a pushed switch will read as a zero.
I took it that be80be wanted it to increment (or decrement) 1 time for each button push. Thats what I was describing. Increment (or decrement) ONE count and then stop incrementing (or decrementing) until the switch is NOT pressed. When the NOT pressed state was detected, then re-arm for another SINGLE INCREMENT(decrement).
As the code looks to me and 3v0's description is that "it will increment (or decrement) as long as the button is pushed"
And to me, it looks like if the button is pushed, the count would go from 0 to 10 in 100mS (10 times through the routine) But I am probably all wet.
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 in
Code:
Dec(Duty) //maybe like this
EndIf
DelayMS(20)
Until Duty = 0
and it works when you press the switch1 it turns the light on when you press switch2 it dimmes the light by how much you press the switch starting at 10 press 9 press 8
press 7 and so on. The doesn't do any change if you don't press the switch it stays where you stopped pressing
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
don't work will not even turn on the lamp. and my switches are low going high
3v0 don't get me wrong I really thank you for the help.
What get's me is that you can digg all over the net and find how to turn something on with a switch but it go's off when you let go or how to turn it on with one and off a second one but why not with just the same one. No one has it like that.
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
but it doesn't change it just turns it on or off. If you put it at the end it don't come on
{
*****************************************************************************
* 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