|
|
View previous topic :: View next topic |
Author |
Message |
alpha
Joined: 05 Mar 2007 Posts: 1 Location: student
|
trouble with PIC18F452 |
Posted: Mon Mar 05, 2007 9:58 pm |
|
|
hi guys,
from past 7 days i am trying to run this code. it takes potentiometer input and gives pwm as output. for constant potentiometer input it should give constant pwm, but output keeps fluctuating. i am not able to figure out whats the problem with code...someone plzz help me
#define(__PCH__)
#include <18F452.h>
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay(clock = 8000000)
#use rs232(baud=38400, xmit=PIN_C6, rcv=PIN_C7)
#include <math.h>
#include <stdlib.h>
void main(){
signed int32 y1, y2, e1, e2, i, y3, e3, e4;
e1 = 0;
y1 = 0;
setup_adc_ports(RA0_ANALOG);
setup_adc(adc_clock_internal);
set_adc_channel( 0 );
while(TRUE){
e2 = read_adc() ;
e4 = e2 - e1;
e3 =( e4 << 7 ) + ( e4 << 6 ) + ( e4 << 3 );
y2 = ( (y1 << 6) + (y1 << 5) + (y1 << 2) ) + ( e2 << 2 ) + e3;
y2 = (y2 << 6) + (y2 << 5) - (y2 << 2) - (y2 << 1);
y2 = (y2 >> 6);
y3 = y2;
y3 = (y2<<2) + (y2<<1);
y1 = (y3 >> 10);
e1 = e2;
for(i=0;i<=y2;i++){
output_high(PIN_A1);
}
for(i=0;i<=y2;i++){
output_low(PIN_A1);
}
}
} |
|
|
Ttelmah Guest
|
|
Posted: Tue Mar 06, 2007 4:10 am |
|
|
First, don't ever define __PCH__.
This is a _system_ definition generated by the compiler. If you are using the PCH compiler, it will already be defined. Doing it manually, opens the possibility of getting it wrong for the compiler in use, and generating the wrong code...
Now, the next question is whether the value really is 'stable'. You will find, that unless the layout is very good, there _will_ be noise in the readings, and even with a perfect layout, at values between readings, there will still be jitter. Realistically, if the layout is good, using only 8bit ADC reading, the value should be stable, but 'no guarantees'.
Your code is too complex for a simple test. Take the reading, and output it. If it still changes, then you know where the problem is.
There are some lines that are 'silly', and may well reflect an error in your code. For instance, the lines immediately after the 'gap' in the code, have you setting Y3, equal to the value from y2, then setting it to y2*6. Obviously the first does nothing, but shows that there may well be something radically 'wrong' in the actual code. Sit down, and write out the arithmetic you actually want to perform, and put this as a comment on the lines. Doing this may be enough to find out what is happening. Also change the names to make the operation clearer. So:
Code: |
Current_val=read_adc(); //Get reading
delta=current_val-last_val; //Generate change from last reading
error=delta*200;
|
Does exactly what the first three lines currently do, and makes it much easier to 'see' where the values are coming from, and what they represent.
Best Wishes |
|
|
loem
Joined: 06 Mar 2007 Posts: 3
|
|
Posted: Tue Mar 06, 2007 8:41 am |
|
|
Try to put 0.1uF capacitor across analog input and ground.
The fluctuation may come from high-frequency noise
coupled from high-speed digital signals near by. |
|
|
|
|
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
|