View previous topic :: View next topic |
Author |
Message |
slipknotcc
Joined: 13 Feb 2010 Posts: 8
|
Controlling PWM with Potentiometer |
Posted: Sun Feb 14, 2010 10:55 pm |
|
|
Hey all,
I'm using the PIC 16f877A.
What I'm trying to do here is control the speed of a motor using PWM and a potentiometer hooked up to an A/D port. When I run the program the variable "pwm1" changes when I turn the potentiometer, but the speed of the motor does not.
What did I do wrong?
Thanks,
Cory
Code: |
void main()
{
int pwm1; // initialize variables to hold the analog value
setup_adc(ADC_CLOCK_INTERNAL); // Enable the A/D and use the internal clock
setup_adc_ports(ALL_ANALOG); // This is where you select analog inputs
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 254, 1);
while(1)
{
set_adc_channel(0);
delay_us(20);
pwm1 = read_adc();
delay_us(20);
set_pwm1_duty(pwm1);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 14, 2010 11:10 pm |
|
|
1. Post your #fuses and #use delay() statements.
2. Is this being tested in hardware or in Proteus ?
3. If it's hardware, did you build the board or did you buy it ?
If you bought it, what's the manufacturer and part number of the board ?
4. If possible post a link to the schematic of the board.
5. What's your compiler verison ? |
|
|
slipknotcc
Joined: 13 Feb 2010 Posts: 8
|
|
Posted: Sun Feb 14, 2010 11:18 pm |
|
|
1.
Code: |
#include <16f877A.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
|
2. Using hardware
3. Using just a protoboard, PIC, crystal, filter caps, and a ICD-U64 programmer, with a programming board created and provided by my instructor.
4.
5. PCWH Compiler version 4.101 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 14, 2010 11:27 pm |
|
|
Does the trimpot circuit look like this ? What's the value of the trimpot ?
It should be a maximum of 10K ohms.
Code: |
+5v
|
>
<
>------- To pin A0 on PIC
<
>
|
---
- GND
|
|
|
|
slipknotcc
Joined: 13 Feb 2010 Posts: 8
|
|
Posted: Sun Feb 14, 2010 11:41 pm |
|
|
yep looks just like that. Its a 1k pot though. Is that ok? I think I found the problem,
do i need
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 14, 2010 11:52 pm |
|
|
Quote: |
yep looks just like that. Its a 1k pot though. Is that ok?
|
A 1K trimpot will work.
Quote: | do i need
#device adc=8
|
You shouldn't need that, because the compiler defaults to using adc=8.
Quote: | setup_adc(ADC_CLOCK_INTERNAL);
|
Change this statement to use ADC_CLOCK_DIV_32. That's the correct
value for a 20 MHz oscillator. |
|
|
slipknotcc
Joined: 13 Feb 2010 Posts: 8
|
|
Posted: Mon Feb 15, 2010 2:42 am |
|
|
OK. I figured it out.... stupid mistakes....
Thanks For all the Help!,
Cory |
|
|
abraham90
Joined: 28 Sep 2010 Posts: 1 Location: Mexico
|
|
Posted: Tue Sep 28, 2010 7:08 pm |
|
|
Hi, I'm doing something similar to what you have done, I use the lm35 and amplify the voltage by 10 so when I have 25° I send 2.5 V to the pin A0.
The problem I'm having is that I can't see the PWM in Proteus, and with the adc I have no problem it works correctly.
Code: |
#include <16F877A.h>
#FUSES NOWDT,HS,NOPUT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use fast_io (C))
void main()
{
int8 a;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1|RTCC_8_BIT);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,124,1);
setup_ccp1(CCP_PWM);
SET_TRIS_A(0x3F);
SET_TRIS_C(0x00);
SET_TRIS_D(0X00);
While (TRUE)
{
delay_ms(100);
a = read_adc();
set_pwm1_duty(a);
delay_ms(100);
output_d(a); //i use this to see if the adc is working
}
}
|
Can you please tell me whats wrong. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|