|
|
View previous topic :: View next topic |
Author |
Message |
smahighl Guest
|
Pins raising together |
Posted: Wed May 31, 2006 8:56 am |
|
|
Hi
I try to read a phototransistor into RB1 and RB2 on my PIC18F4620, with this program:
Code: |
// Read Photorecoptor value
unsigned int8 read_photo_level(int Nr) {
switch (Nr) {
case 1:
set_adc_channel(10); //channel RB1, Photo1
delay_us(50);
return read_adc();
break;
case 2:
set_adc_channel(8); //channel RB2, Photo2
delay_us(50);
return read_adc();
break;
}
}
#int_timer3
void timer3_isr()
{
if (pwm_counter>=100) pwm_counter=0; // pwm_counter between 0 and 100
switch (direction) {
case 0: // left
if (set_pwm_value>=pwm_counter)
output_low(PIN_BIRD1B);
else
output_float(PIN_BIRD1B);
break;
case 1: // right
if (set_pwm_value>=pwm_counter)
output_low(PIN_BIRD1A);
else
output_float(PIN_BIRD1A);
break;
}
pwm_counter++;
set_timer3(0xFFFF-50); //
}
void init()
{
// Setup peripherals
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);
// DIV_2 is really not enough. DIV_4 just too fast. From DIV_8 and more it's OK.
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32); // @ 32MHz|T1_DIV_BY_32, it has a resolution of 4us and will overflow every 262ms
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // Used with anemometer. Timer 1 is 16-bit. @ 32MHz|T1_DIV_BY_8, it will overflow every 65.5ms.
setup_timer_2(T2_DIV_BY_1,255,1); // Timer2 is used by the CCP1 for the motor PWM.
// The cycle time will be (1/clock)*4*t2div*(period+1) -> (1/20MHz)*4*1*255 = 31.875us or 31.4khz
// period=255 determines when the clock value is reset (and gives max values possible)
// postscale=1 -> timer resets n times before an interrupt
setup_timer_3(T3_INTERNAL|T3_DIV_BY_8); // Used for BIRDS PWM. Timer 3 is 16-bit. @ 32MHz, it will overflow every 65.5ms..
setup_ccp1(CCP_PWM); // CCP1 is connected to RC2 (Mot1) and uses timer2
setup_ccp2(CCP_CAPTURE_RE); // CCP2 is connected to RC1 (Anemometer) and captures timer1
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
// Setup PIC I/O ports; 1 indicates the pin is input and 0 output.
set_tris_a(0b00111111);
set_tris_b(0b11110111);
set_tris_c(0b10000010);
set_tris_d(0b11111111);
set_tris_e(0b100);
// Setup interrupts.
enable_interrupts(INT_TIMER3);
enable_interrupts(global);
delay_ms(100);
}
void main() {
init();
while(true) {
battery=read_battery_level();
led_on_led1();
VALUE_PHOTO1=read_photo_level(1); // read photo 1 (RB1)
VALUE_PHOTO2=read_photo_level(2); // read photo 2 (RB2)
difference=VALUE_PHOTO1-VALUE_PHOTO2;
if (difference<0) { // more light on photo 1
led_off_led2();
led_on_led0();
fraction= 1 - VALUE_PHOTO1/VALUE_PHOTO2;
direction=1; // move BIRD1A
delay_ms(300); //
set_pwm_value=30 * fraction;
}
else {if (difference>0) { // more light on photo 2
led_off_led2();
led_on_led0();
fraction= 1 - VALUE_PHOTO2/VALUE_PHOTO1;
direction=0; // move BIRD1B
delay_ms(300);
set_pwm_value=30 * fraction;
}
else
set_pwm_value=0; // move none
}
}
|
first of all I do not understand why PINRB1 is 150 when nothing is connected and PINB2 only 100.
When connecting the phototransistor to PINB1 it behaves corresponding to the light but the value of PINRB2 raises also, (not propotional). Connecting with PINRB2, it behaves corresponding to light but the value of PINRB1 stays constant at 150 (as it should).
(It is the same with PIN RA0 and RB4 instead of RB2)
Does somebody understand what is wrong?
I'm sorry for this very 'debutant' question, but I can't figure it out for quite some while now.....
Thank you very much
-mirko |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed May 31, 2006 12:29 pm |
|
|
An open A/D pin can easily drift to strange voltages. I am not familliar with your chip but if it has one capacitive A/D connected through a mux to several pins, then the A/D can be charged by a valid input, and when the mux switches to an open input the charge on the A/D actually imposes part of its voltage on the mux channel. This gives a reading for the open channel that is a function of the valid channel voltage and other factors like time and leakage current. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
smahighl Guest
|
|
Posted: Wed May 31, 2006 1:20 pm |
|
|
....ok, but the pins are quite far away from each other, and the exactly same happens with another pin, but only from one to the other but not the other way around... So I assume it rather being related to software or PIC-internal-something....
Did I use the set_adc_channels etc. correctly....?
-thank you very much!
(btw: I wanted to put the RE0 as inputs, but read this shall not be done because of PSPMode....) |
|
|
Ttelmah Guest
|
|
Posted: Wed May 31, 2006 2:46 pm |
|
|
You don't show your 'led_off', and 'led_on' codes?.
Now, you are manually setting TRIS, but don't show a '#use fast_io' statement. Without this for each port, the default 'standard_io' mode will remain selected, which will override your TRIS settings. A 'classic', would then be that if you do any 'port I/O' (as opposed to 'bit I/O') operations on the port with the ADC connections, the TRIS will get overridden...
Beware, that if these are photo sensors,the 'odds' are that the impedances involved will be high. As such a buffer op-amp, will be _essential_ to drive the ADC inputs. The ADC, requires a source impedance of 2.5KR or lower, to give acceptable results.
Best Wishes |
|
|
smahighl Guest
|
|
Posted: Wed Jun 07, 2006 3:03 am |
|
|
...thank you all very much for your help.
After all it seems that it was because of the open input pin...
The rest seems to work...
best, and thank you again
-mirko
|
|
|
|
|
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
|