Messing around with pwm and adc. If the ADC if it is equal
or more than stored value, I want to increase PWM
frequency 1 Hz. What is missing in this line?
IF ADC is => increase PWM frequency 1 hz
Chip is a PIC16f877 at 10Khz
Thanks dudes and dudettes (if present) Emmett
basic code what am I missing? IF ADC is => increase PWM
- HighFrequency
- Posts: 122
- Joined: Sun Apr 17, 2005 1:01 am
- Location: Victoria BC
- Contact:
- HighFrequency
- Posts: 122
- Joined: Sun Apr 17, 2005 1:01 am
- Location: Victoria BC
- Contact:
Would this help ?
This increases PWM up to its max over a period of 1 second.
Colin
Code: Select all
proc h_trip_check(ubyte rh, ubyte *h_trip_level)
ubyte duty_1
duty_1 = 0
if rh < h_trip_level[U_H] then
CCP1CON = 0x2C
while duty_1 < 32 do
duty_1 += 1
CCPR1L = duty_1
done
else
duty_1 = CCPR1L
if duty_1 !=0 then
duty_1 -= 1
CCPR1L = duty_1
if duty_1 == 0 then
CCP1CON = 0
endif
endif
endif
endproc
Colin
On a clear disk you can seek forever.
Re how much of a beginner are you
HighFrequency, I am hardware rich and logic poor. I've been messing around with electronics for 30 years or so and know enough to have built a fuel injector pulsing circuit that ran an old 67 mustang but it was "manually controlled" as in by varying the pulse width and frequency through some 555 timers. I know- very crude. As far as uP's I have several development boards and programmers but am trying to grasp the programming part and in my 50 year old brain it is challenging to say the least. I've been working on cars since I was 14 and have always graduated towards the computer side of things. I am not much to brag but I can fix whatever driveability concern that comes in the door just because I was around when the first computers started appearing on cars, then my wish finally came true when the first electronically fuel injected vehicle came in and I saw the computer controlled carbs go the way of the Dodo bird. Yes I'm that old. Anyhow any help would be appreciated greatly. Thank You for your time Emmett
........hmmmmmm ok well theres still a lot of code that we are not seeing and I'm not sure if its worth mentioning but ok... here we go.
The register specifically used to configure PWM are the
TRISC,
PR2,
CCP1CON,
CCP2CON,
CCPR1L,
CCPR2L
T2CON
There are two PWMs on PIC16F877. These modules are CCP1 and CCP2. pins 16 and 17
Now..
The TRISC register needsto be initialized so that RC1 et RC2 are output ports .....
bits 1 and 2 of TRISC set to 0.
CCP1CON and CCP2CON configure CCP1 and CCP2 modules.
A value of 12 configures these for PWM.
Timer2 is used by CCP1 and CCP2 modules for PWM.. Timer2 prescaler is configured using TCKPS0 / TCKPS1 of T2CON register. A value of 0 for these two configures a prescaler of 1. Bit TMR2ON must be initialized to 1 to start Timer2.
PR2 register controls the pulse frequency. The formula for the pulse period is:
(PR2 + 1) * 4 * TOSC * (Timer2 prescaler ).
Pulse frequency is 1 / period.
so....... an exampe
If PR2=255 and the PIC runs at 10 MHz.
1 / ((255 + 1)*4*1/10,000,000*1). = 9.765 kHz
CCPR1L / CCPR2L registers setup the duty cycle... which is relative to PR2. If PR2 is 10 and CCPR1 is 5, then the duty cycle is 50%..
did you write this entire code you posted?
what kind of output are youo getting?
The register specifically used to configure PWM are the
TRISC,
PR2,
CCP1CON,
CCP2CON,
CCPR1L,
CCPR2L
T2CON
There are two PWMs on PIC16F877. These modules are CCP1 and CCP2. pins 16 and 17
Now..
The TRISC register needsto be initialized so that RC1 et RC2 are output ports .....
bits 1 and 2 of TRISC set to 0.
CCP1CON and CCP2CON configure CCP1 and CCP2 modules.
A value of 12 configures these for PWM.
Timer2 is used by CCP1 and CCP2 modules for PWM.. Timer2 prescaler is configured using TCKPS0 / TCKPS1 of T2CON register. A value of 0 for these two configures a prescaler of 1. Bit TMR2ON must be initialized to 1 to start Timer2.
PR2 register controls the pulse frequency. The formula for the pulse period is:
(PR2 + 1) * 4 * TOSC * (Timer2 prescaler ).
Pulse frequency is 1 / period.
so....... an exampe
If PR2=255 and the PIC runs at 10 MHz.
1 / ((255 + 1)*4*1/10,000,000*1). = 9.765 kHz
CCPR1L / CCPR2L registers setup the duty cycle... which is relative to PR2. If PR2 is 10 and CCPR1 is 5, then the duty cycle is 50%..
did you write this entire code you posted?
what kind of output are youo getting?
CHBasic, Microbasic, Picbasic, Sorry I was out of town for a week with 1 hour of internet. Thank You
P.S. I got the code below from the MElabs forum and it is doing initially what I need but still needs to have the ability to sample an adc, run for 10 seconds then sample again and if it is higher than the previous adc sample then continue on to the next frequency. I believe it is in PicBasic language but it won't compile in the demo I have. Thanks again, Emmett
Begin:
PVAL = 1000 ' Start with 1kHz
For Duty = 64 to 250
HPWM 1,Duty,PVAL
PAUSE 100
NEXT Duty
Duty = 127 ' 50% Duty-cycle
For PVAL = 1000 to 10000 step 100
HPWM 1,Duty,PVAL
PAUSE 100
Next PVAL
GOTO Begin
END
P.S. I got the code below from the MElabs forum and it is doing initially what I need but still needs to have the ability to sample an adc, run for 10 seconds then sample again and if it is higher than the previous adc sample then continue on to the next frequency. I believe it is in PicBasic language but it won't compile in the demo I have. Thanks again, Emmett
Begin:
PVAL = 1000 ' Start with 1kHz
For Duty = 64 to 250
HPWM 1,Duty,PVAL
PAUSE 100
NEXT Duty
Duty = 127 ' 50% Duty-cycle
For PVAL = 1000 to 10000 step 100
HPWM 1,Duty,PVAL
PAUSE 100
Next PVAL
GOTO Begin
END
I have ordered PicBasicPro and have enough sample code on their site combined with what you'all have contributed, to more than do what I need. I'm not saying what I'll come up with will be efficient or the best way to do it but it will give me an understandable starting point that I can modify and experiment with by myself which, as untried as this is will be a good thing. I want to thank everyone so much for your valuable time and I hope you will consider helping this old fart out again some day:)
cwaugs i am sure you will be able to get it working with a little work .
Not sure about anyone else... I am no help with PicBasicPro. I am good with coding in C. Sounds like you have your PWM working.. all you need is to read the ADC and setup a couple "if" statements. Not hard..
Feel free to to stop by with any more questions.
Not sure about anyone else... I am no help with PicBasicPro. I am good with coding in C. Sounds like you have your PWM working.. all you need is to read the ADC and setup a couple "if" statements. Not hard..
Feel free to to stop by with any more questions.
Who is online
Users browsing this forum: No registered users and 62 guests