|
|
View previous topic :: View next topic |
Author |
Message |
andys
Joined: 23 Oct 2006 Posts: 175
|
frequency_shift |
Posted: Wed Jan 24, 2007 10:36 am |
|
|
I wrote this code and i want to but a (4 way)switch.For each step of the switch i want the PIC work with different frequency.I can't find the broplem.This is my code:
#include <18F4550.h>
#device ADC=16
#fuses XT,NOWDT,NOPROTECT,NOLVP,CCP2B3,CCP2C1
#use delay(clock=4000000)
void main()
{
long adc_result;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_16);
setup_ccp1(CCP_PWM); // to CCP1 doulevi san PWM
//setup_timer_2(T2_DIV_BY_1, 255,1);
while(1){
if (!input(PIN_A3)) {
setup_timer_2(T2_DIV_BY_1, 255,1);
set_adc_channel(0);
delay_ms(1);
adc_result=read_adc();
adc_result>>=6;
set_pwm1_duty(adc_result);
}
if (!input(PIN_A4)) {
setup_timer_2(T2_DIV_BY_4, 255,1);
set_adc_channel(0);
delay_ms(1);
adc_result=read_adc();
adc_result>>=6;
set_pwm1_duty(adc_result);
}
else if (!input(PIN_A5)) {
setup_timer_2(T2_DIV_BY_16, 255,1);
set_adc_channel(0);
delay_ms(1);
adc_result=read_adc();
adc_result>>=6;
set_pwm1_duty(adc_result);
}
else if (!input(PIN_E0)) {
setup_timer_2(T2_DISABLED , 255,1);
set_adc_channel(0);
delay_ms(1);
adc_result=read_adc();
adc_result>>=6;
set_pwm1_duty(adc_result);
}
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 24, 2007 10:37 pm |
|
|
Your code has several obvious bugs. If your code has obvious
bugs, people will tend to not respond to the post. They expect
you to spot the bugs.
Look at the first one:
Quote: |
#fuses XT,NOWDT,NOPROTECT,NOLVP,CCP2B3,CCP2C1
|
You have two fuses that configure the output pin for CCP2.
The first fuse selects pin B3 for the output. The 2nd fuse selects
pin C1 as the output. You can't have CCP2 going to two pins.
It must be one or the other. That's an obvious bug. When you
have two fuses that are in conflict, the compiler uses the last one.
In this case, the compiler will use CCP2C1. Also, it's really not
necessary to set the output pin for CCP2, since your code only
uses CCP1.
Let's look at the 2nd obvious bug.
Here you have configured all the A/D pins on the PIC as analog pins:
Quote: | setup_adc_ports(ALL_ANALOG);
|
Why is this a problem ? It's a problem because you're using several
of the analog pins as digital input pins. Example:
In each case below, you are trying to read digital i/o pins which you have
configured as "All Analog":
Quote: | if (!input(PIN_A3)) {
else if (!input(PIN_A5)) {
else if (!input(PIN_E0)) {
|
Each of those pins is an analog pin. They have "ANx" after the pin
name in the Device Overview section of the data sheet:
Quote: |
RA3/AN3/VREF+
RA5/AN4/SS/
RE0/AN5/CK1SPP
|
What does the PIC data sheet say about reading an i/o pin that's been
configured as "Analog". It says they:
This means that the following statements will always be
executed, because any input() operation will give a value of 0.
So your "if" statements below are the same as: if(!0)
That's the same as: if(1)
Quote: |
if (!input(PIN_A3)) {
else if (!input(PIN_A5))
else if (!input(PIN_E0)) {
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|