Jump to content


Photo

Strange Pic Behavior


5 replies to this topic

#1 mityeltu

mityeltu

    Regular

  • EstablishedMember
  • Pip
  • 81 posts
  • Gender:Male
  • Location:Tennessee
  • Interests:Communications

Posted 05 January 2012 - 12:06 AM

I have a problem that I cannot seem to fix. Granted, I think this is more of a physical issue rather than a programming issue, but who better to ask than a bunch of people who use these things on a regular basis.

Here's what happens. After applying 5.5v batery power to the circuit, which consists of the mcu, a button, a PING ultrasonic sensor and 3 leds I get superfluous signals to the PING unit. There is a little light on it that lets me know when it is firing and the thing flickers for a while then stops. If I touch the mcu, it flickers. SDomtimes if I move my hand too close, it flickers. I have checked vdd and vss for problems - nothing there. I have checked all connections and found nothing inconsistent. Any thoughts on what is causing this issue?

My code is here:

#include <system.h>
#include <eeprom.h>
#pragma DATA _CONFIG1, _INTOSCIO & _WDT_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_ON & _LVP_OFF & _DEBUG_OFF
#pragma DATA _CONFIG2, _BOR21V & _WRT_OFF
/*
This program written for PIC16F887 with internal 8MHz clock
portb will be used to change pwm value
port b0 will increase the pwm dc, portb1 will decrease the dc
portd.0 is ultrasonic input and output
*/
void setup(void);
void interrupt(void);
void pulse(void);
char flag;
short echo;
char pb; // this variable is only for IOC
void main()
{
setup();
while(1)
{
if (test_bit(portd,0))
{
delay_ms(20);
while (test_bit(portd,0))
{}
pulse();
}
}
}/////////////////////////////////////////////////////////////////////////END MAIN

void pulse()
{
/*
This section uses the Parallax PING sensor
This module utikizes a single pin for start pulse and timing pulse
a 5 us pulse is used to start the ping process. There is a delay of between
500 and 750 us to allow setting the port for input and timing.
Timer 1 is used for timing of the pulse
portb must be read in order to keep the interrupt from always being set
*/
tmr1h = 0x00;
tmr1l = 0x00;
clear_bit(iocb,0); // clear ioc flag
clear_bit(intcon,1); // clear ext int flag
clear_bit(intcon,0); // make sure ioc b0 flag is not already set
clear_bit(trisb,0); // set b0 for output
set_bit(portb,0);
delay_us(5); // starts timing delay
clear_bit(portb,0);
pb = portb;
clear_bit(iocb,0); // clear ioc flag
clear_bit(intcon,1); // clear ext int flag
set_bit(trisb,0); // rest b0 for input
set_bit(iocb,0); // set portb,0 as int on change
}//////////////////////////////////////////////////////////////////////////END PULSE

void setup()
{
osccon = 0x71; // internal 8MHz
clear_bit(intcon,0);
intcon = 0xc8; // ioc en

trisa = 0x00;
trisb = 0xff;
trisc = 0x00;
trisd = 0xff;
trise = 0x00;

porta = 0x00;
portc = 0x00;
portd = 0x00;
porte = 0x00;

ansel = 0x00; // porta digital
anselh = 0x00; // portb digital

adcon0 = 0x00; // a/d off

tmr1h = 0x00;
tmr1l = 0x00;
t1con = 0x00;

flag = 0x00;
}/////////////////////////////////////////////////////////////////////////END SETUP


void interrupt()
{
clear_bit(intcon,7);
clear_bit(intcon,6);

if (test_bit(intcon,0)) // ioc b
{
clear_bit(intcon,0);
if (test_bit(flag,0))
{
clear_bit(t1con,0);
pb = portb;
clear_bit(porte,0);
clear_bit(flag,0);
eeprom_write(0x00, tmr1h);
delay_ms(20);
eeprom_write(0x01, tmr1l);
MAKESHORT(echo,tmr1l,tmr1h);
}
else
{
pb = portb;
set_bit(porte,0);
set_bit(flag,0);
set_bit(t1con,0);
}
}
clear_bit(intcon,0);
set_bit(intcon,7);
set_bit(intcon,6);
}/////////////////////////////////////////////////////////////////////END INTERRUPT

#2 kenn

kenn

    Regular

  • EstablishedMember
  • Pip
  • 89 posts
  • Location:Toronto, ON

Posted 05 January 2012 - 02:24 AM

Ping ))) sensor manual

After confirming that the sensor is connected correctly and is getting 5v, my next suggestion would be to put an oscilloscope into use to confirm that you're getting a good +5v trigger pulse of approx 5 microseconds to the PING sensor, and with a reflecting surface about 16" from the sensor, you should see an echo pulse of about 2 to 3 milliseconds long (corresponding to a path of approx 3 ft)

You apparently haven't put any time into the pulse() function or the main loop to "rest" the sensor between cycles. I suggest adding a delay of 250 milliseconds or more after the call to pulse() in your main loop, just to ensure you've got a safe repeatable interval for triggering and measuring.

Edited by kenn, 05 January 2012 - 02:25 AM.


#3 mityeltu

mityeltu

    Regular

  • EstablishedMember
  • Pip
  • 81 posts
  • Gender:Male
  • Location:Tennessee
  • Interests:Communications

Posted 05 January 2012 - 10:11 PM

The time between pulses is determined by a button on pin D0. The only time I should be getting the signal to the sensor is after the button is released. Oscilloscop confirms I am getting flat 5.2v. No dips dring button operation. unable to monitor puse using scope, but have ultrasonic sound frequency devider and am able to hear it audibly. Echo produces input as expected on pin B0 after pulse is initiated. However, when the mcu is behaving erratically, I am getting multiple spurrious signals and echoes with no detectable change in source voltage. Note, I never have to push the button to cause a firing of the PING sensor. When the PING is firing, I am getting the start signal from the mcu, that is I am able to detect the high output that "appears" to correspond to the led on the PING sensor lighting. It's so fast at tims I am unable to be certain, but either way, there is a signal from the mcu and the PING fires as it is supposed to.

#4 trossin

trossin

    Super Enthusiast

  • Moderator
  • PipPipPip
  • 243 posts
  • Gender:Male
  • Location:Colorado

Posted 06 January 2012 - 05:10 PM

Could it be that you have some unused inputs to your PIC "floating in the wind" and acting like antennas? Make sure that all unused pins are programmed up as outputs or connect them to ground. Maybe your switch is connected to an input so that when the button is pressed the input is grounded but when it is not it is just floating? Make sure you have a 1K to 47K resister to VDD on your D0 pin.

#5 mityeltu

mityeltu

    Regular

  • EstablishedMember
  • Pip
  • 81 posts
  • Gender:Male
  • Location:Tennessee
  • Interests:Communications

Posted 27 November 2013 - 05:44 PM

I may have found the problem. I know it's been a really long time. I gave up on this microcontroller stuff for a long time because my circuits either didn't work or worked strangely. That is, they would exhibit this same strange behavior on an aperiodic basis. I could never reproduce it, but it would happen for no apparent reason.

 

I think the reason in my power supply. I use a small 5v 3A power supply I boulght from mouser. Plent of power for all my apps. I notiiced this strange behavior again and tried to diagnose it. In the past, the real problems have been with motors and other switching devices (such as the ping sensor above). I tried looking on a scope, but didn't find anything. Still a little odd, but placing a .1uf cap across my power supply fixed my issues. I don't have a complete resaon why, but the fact that it stopped immediately after I plcaed the cap and hasn't done it again since, tells me that was the problem.

 

I hope this helps someone else.



#6 JorgeF

JorgeF

    Super Enthusiast

  • EstablishedMember
  • PipPipPip
  • 274 posts
  • Gender:Male
  • Location:ES @ Europe, third rock from the Sun

Posted 28 November 2013 - 11:02 PM

Hi

 

Is this an extra capacitor, or was it missing betwen the power pins of the PIC?

 

If it is an extr cap, then your power lines are too long or the power supply has vary poor regulation.

If you are trying to make a PIC work without the specified (by Microchip) decoupling cap at its power pins, than it was a serious hardware error.

 

 

Best regards

Jorge





Reply to this topic



  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users