PDA

View Full Version : Strange behaviour from PIC16F877 on TMR0



mikebar
- 13th August 2006, 15:34
Good morning to everyhome.
I'm a little bit desperate because I have a very strange behaviour from a project of mine that is controlling a DC 24V motor with a single channel hall-effects encoder on the shaft.
What the project do, is open a pliers to a specified position and then come back. I've connected the open collector output of the single channel encoder of the motor to a 2.2Kohm pull-up resistor and then to a NOT gate made with a BC547 transistor and a couple of resistors. From this NOT gate, directly to a spare quad-or gate 7432. The output of the OR gate is applied to the RA4 (Timer0) input of the PIC 16F877, running at 4MHz with two 22pf capacitors on the crystal.
The RA4 pin is set to be incremented from an external source.

The 24V power needed for the motor is completely ground separated from the power that run all the logic. The motor is drived by one L298N dual H-Bridge with flyback fast diodes.
I wrote the firmware that poll the TMR0 register and if I find a value greater than zero this value will be accumulated in a word sized variable.
When this value reach a thereshold, the motor have to stop.

All the registers related to the TMR0 seems to be setup correctly, like this:
OPTION_REG.7 = 0 'Pull-up resistor set to ON over the PortB
OPTION_REG.6 = 1 'Rising Edge detection on PortB.0
OPTION_REG.5 = 1 'Set TMR0 to be incremented from an outside source
OPTION_REG.4 = 1 'TMR0 will be incremented from a high-to-low (falling edge signal transition)
OPTION_REG.3 = 1 'Assign the Prescaler to the WatchDogTimer instead of TMR0
OPTION_REG.2 = 0 'The bit from 2 to 0 define the Prescaler's reduction ratio
OPTION_REG.1 = 0 'in this case it is 1:1 of the WDT rate
OPTION_REG.0 = 0

'ADCON1 register setting (Datasheet page 112)
ADCON1.3=0 'Set the Port A in DIGITAL mode (bits from 3 to 0 of the ADCON1 register)
ADCON1.2=1
ADCON1.1=1
ADCON1.0=1

'INTCON register setting (Datasheet page 20)
INTCON.7 = 0 'Disable the Global Interrupt flag (GIE)

INTCON.1 = 0 'Clear the bit before enabling the interrupt on RB0
'If INTCON.1=1 mean that an external interrupts occurred on RB0.
'The INTCON.1 bit have to be cleared by software (INTF)
INTCON.4 = 1 'Enable external interrupts on RB0 (Pin n.6) (INTE)

INTCON.2 = 0 'Clear the bit before enabling the interrupt on TMR0 overflow.
'If INTCON.2=1 mean that TMR0 is in overflow.
'The INTCON.2 bit have to be cleared by software (TMR0IF)

INTCON.5 = 1 'Enable Overflow Interrupt on TMR0 (T0IE)

What is happening is that this circuit work MOST OF THE TIME!
But sometime happen that the motor NEVER STOP and then I will unplug the motor leads to stop it. Then, if I have a look to the variable that have to accumulate the value she show me only small values. I mean, normal values are around 500 pulses, instead I found 7 or 10 or 100...

This is let me fool and this is a project kill behaviour.
I've also putted some filter out of the encoder output made by a diode followed by a resistor, capacitor and a zener diode of 4,7V but this not helped me.
I've many 100nF capacitors all around the board and very close to the PIC, but nothing. I've also 330microF close to the PIC.
MCLR is connected by a 4,7K resistor to the Vcc.
All the unused PIC pins are set as output and left unconnected.
I've also replaced the motor with the encoder with another one of the same type, but nothing.

Seems that the PIC is NOT resetting itself. Simply seems that he ignore the incoming pulses on RA4. So, I've modified the source code to flash a led each time that a values greater than zero is found on the TMR0 register.

What is happening is that the Led still correctly flashing but seems that the variable is screwed up.
The program is quite long and seems that I'm experiencing this behaviour after I made some modifications that have made the program longer than before.
Then I've moved this variable to Bank0:
BeackEncPos VAR WORD Bank0 'Accumulate the encoder position
BeackFinalPos VAR WORD Bank0 'This is the thereshold that have to be compared with the BeackEncPos variable

This pop up another question: how many word sized variables can I store in the Bank0?
Currently I managed to have in the bank0 3 byte sized variables, 3 bit sized variables and 8 word sized varibles. Maybe I'm running out of this space and I don't know...

I'm sure that I've not touched the place where the counting is made and also I'm sure that I've not used some variables that is shared between the previous version and the new one.

Worst of all, is that I start having this behaviour 2 nights ago and after many cuts & corrections on the board (add some cap more etc), firmware upgrading etc. at the end the last night this robot was working very well for the whole night and made hundreds of movements. All very well done. Then I switched off and today in the morning, only after few movements, I'm experiencing the same behaviour I had 2 nights ago. :-(

I really don't know why and where this erratic behaviour is coming from!

Any suggestions?
Please, somebody with high knowledge like the peoples that write on this forum, can save me from the third no sleeping night?

Sorry for the long post.
Thank a lot to everyone.

Mike.

mister_e
- 13th August 2006, 20:10
Woohaa i try to figure out but there's too much 'maybe this or that' in my mind.
Can you post your whole code and schematic?

mikebar
- 14th August 2006, 06:34
Woohaa i try to figure out but there's too much 'maybe this or that' in my mind.
Can you post your whole code and schematic?

Thank you, mister_e.
Tonight I've found that absolutely there is not the software that cause this so strange behaviour. Since the board is under control of another one that drive 2 bigger motors in PWM (24Vdc, anytime separated from the power of the logic but this 24Vdc are the same that power the tiny motors where I loose pulses), I've found the following:
If I let one of this bigger motor to use a little bit more power (just because I'm trying to move the shaft with my hand and the PID controllers try to oppose to my strenght) THEN the strange behaviour start happening. If I turn off one of these motors, the "strangeness" do not happen.
Seems that there is some kind of disturbance that is picked-up by the micro.
I've putted so many caps and grounded any unused pin as you can see... what can I do more?
Also, when I start missing pulses on TMR0 (RA4) seems that I pick-up many other "ghost" pulses on RB0...

Thank you for your time.

mister_e
- 14th August 2006, 17:01
OR your power supply is not good enough to provide more power. Assuming a spike when you need more power, the voltage may decrease enough at the input of the 7805 to make this one unable to provide the stable 5Volt at the output. If the output of the regulator drop under the Brown Out voltage of your PIC, it may badly reset the PIC and send the PC to an unwanted program location. Hence do some strange behaviours.

How about if you use another transformer (let's say wall wart) to drive your PIC?

I'll have a look to your schematic.. maybe i could find something in.

mikebar
- 15th August 2006, 14:16
Thank you mister_e for the answer.
I forgot to mention that I'm using a commercial switching power supply for the logic that is able to deliver 4A at 12V. Then I slow down from 12V to 5 with the LM7805. The power supply is really bigger than my needs.
But let me explain what I've discovered next:
The board that is "misunderstunding" the pulses is inside a big metal case. This big metal case is connected to the earth ground throught the switching power supply, BUT not to the GND of my boards.

Then, when I was making my tests, a personal computer inside this big case was running in the same time. Then I was experiencing the erratic behaviour as I told you.
I found that the personal computer inside the case was running by ANOTHER power extension that DOEN'T HAVE any earth socket!
Now, I'm running this PC with the earth ground connected and SEEMs that this erratic behaviour is disappeared.
I Hope...

There is a logic in this?

mister_e
- 15th August 2006, 18:21
Every missing earth or else ground could do something. Without seeing the whole it's a little bit hard but maybe you got one of the possible cause.

Ground bouncing, ground looping is sometimes hard to find... Well keep us posted.
Finger crossed!

mikebar
- 16th August 2006, 13:27
Oh boy...
MAYBE I've discovered WHY so strange behaviours...
I was supposing that my DC motors provider got me a couple of motor with filters capacitors inside (just like the invoice said).
Instead I've found that NO CAPACITORS was inside the motors.
Now I've mounted a couple of motors with capacitors inside and still seems that anything is working...
The motors without the filter capacitors was a couple of big one that are not in the schematic I've posted, and seems that the disturbances are coming from the board that drive in PWM locked anti-phase these 2 motors.

Anyway, Steve, some good suggestion on PCB design to avoid disturbance as much is possible?
I'm using for the PWM section two LMD18201.
These 2 drivers are very close to the regulator section...
There is a ground plane on both side of the PCB connected to the logic gnd.

Thank you.

mister_e
- 16th August 2006, 14:50
probably something like that
http://www.picbasic.co.uk/forum/showpost.php?p=23514&postcount=3

and keep driver far from the regulators. Well, my idea.

mikebar
- 16th August 2006, 20:28
Thank you Steve.
I had a look to the link you posted. Sound interesting, but exactly in which way you perform the points:

- i'll ensure the PIC Vdd line is correctly filtered
- i'll screen the PIC and controller section properly to avoid incoming noise and spikes
- i'll insert a bead on the PIC gnd line

Regards,
Mike.

Archangel
- 16th August 2006, 20:40
Mikebar
Just a side note of 2 more things to try.
Are your encoder lines shielded? And If strong magnetic field is near the hall effect device it might cause it to saturate, maybe move it away from the motor to the other end of driven shaft?

Archangel
- 16th August 2006, 22:56
I thought of one more thing, if your encoder wires are really long the capacitance of the cable may be rounding off the tops of the signal and the PIC just might not like that, in which case and amplifier may be required.
Good Luck
JS

mister_e
- 17th August 2006, 05:08
Maybe better if using opto-coupler instead... just too keep power and logic away. Harder to light a led than introducing a noise wich will be amplified later. Well that's what i did as now.



I had a look to the link you posted. Sound interesting, but exactly in which way you perform the points:

- i'll ensure the PIC Vdd line is correctly filtered

those usual 0.1uF and 10-47uF tantalum as close as possible of the PIC and as many 0.1uF as you have IC... one on each.. still as close as possible.

Bigger cap (47-220uF) + 0.1 uF close to the driver ics...


- i'll screen the PIC and controller section properly to avoid incoming noise and spikes

Proper ground plane design, ground routing, Power lines routing and size against regular signals. Maybe use a grounded metal cabinet to fit it.



- i'll insert a bead on the PIC gnd line


When i deal with multiple Gnds (analog, power, digital) i often add bead core on most of them, usually 1 on analog, 1 on digital.

Do a search for bead core, ground loop, ground plane, ground boucing and i'm sure you may learn a lot of it. It's long to explain and i'm sure i'll mess and miss something. There's pro and cons on each methods... same for ground plane. Sometimes they're usefull, sometimes not. If they're done correctly they're usefull, case not... it will cause you more bugs than benefits.

EDIT: oh i forgot... never leave unused pins floating... tie them somewhere, VDD, VSS via resistor or not. Leaving them floating but setting them to output do nothing good. At least never pass my tests in cars applications.

mikebar
- 17th August 2006, 16:41
Thank you very much Steve.
Except the suggestion about the bead core and the unused pin set as input and never let them floating, I'm already using all your other suggestions.
This mean that I'm in the correct road.

I will try the your other two suggestions.

Thank again you for your time.

Best regards,
Mike.

Archangel
- 17th August 2006, 19:04
I have one LAST Idea as to the source of this problem:
As I understand their operation, a hall Effect switch outputs 1/2 of it's supply voltage in the on condition, so 5v in 2.5 v out, That's dangerously close to the logic low threshold of TTL, so any voltage drop in supply or signal and POOF no encodeer operation. Check the logic high voltage out of the Hall effect sensor and see what it is.
JS

mikebar
- 18th August 2006, 12:14
I have one LAST Idea as to the source of this problem:
As I understand their operation, a hall Effect switch outputs 1/2 of it's supply voltage in the on condition, so 5v in 2.5 v out, That's dangerously close to the logic low threshold of TTL, so any voltage drop in supply or signal and POOF no encodeer operation. Check the logic high voltage out of the Hall effect sensor and see what it is.
JS

Yes, this could be. I've attached the schematics to this thread. As you can see around T2 there is a network made by a transistor. I think that also 2.5V are enough to switch the transistor on/off (really are needed only 0.7V) and is this transistor that drive (by another filter made by zener) the PIC pin RA4. I'm right or I'm missing/misunderstunding something?

Thank you.

Archangel
- 19th August 2006, 00:34
I've connected the open collector output of the single channel encoder of the motor to a 2.2Kohm pull-up resistor and then to a NOT gate made with a BC547 transistor and a couple of resistors. From this NOT gate, directly to a spare quad-or gate 7432. The output of the OR gate is applied to the RA4 (Timer0) input of the PIC 16F877, running at 4MHz with two 22pf capacitors on the crystal.
The RA4 pin is set to be incremented from an external source.

Mike.
HI MIKEBAR, Your schematic attachment did not come through, but I think I understand from your description how your circuit works. First off the old style TTL 7400 series chips while plentiful still really BLOW, they are lazy and usually do not switch cleanly, are severly noisey and cantankerous, and that's their good points. I would use a more modern IC, probably an inverter with schmitt triggers. make sure to use a pullup / pulldown on your RA4 depending on which level you have it setup to trigger on. I do not know where you live or how difficult parts are to get there, here goes some part numbers:
old style TTL:7413 dual 4 input NAND with schmitt trigger,74132 quad 2 input schmitt trigger NAND gates 7414 hex schmitt trigger inverter, Advanced low power shotkey:74ALS14 hex schmitt trigger inverter, etc etc the newer logic has 2 or 3 letters between the 74 and the next 2 or 3 numbers the letters define what improved device type they are I. E. LS = Low Power Shotkey,
ALS = Advanced LS, AS=Advanced Shotkey, hc/hct High speed cmos etc.

Bottom line . . make sure your sensor is switching cleanly your RA4 input and that you have a measurable voltage swing in that pin, and if you do and it still doesn't count, I believe your problem lies elsewhere, chip failure, software glitch . . .
Regards
JS

mikebar
- 19th August 2006, 00:56
Hi JS.
Thank you for your suggestions. I will try to use these parts. Did you think that the HC version of the gates are good enough?
I mean 74HC02, 74HC04 etc.

All the best.
Mike.

Archangel
- 19th August 2006, 01:28
Yes I do, but I think the key is to use something with a schmitt trigger, too often logic without them tends to languish in a state 1/2 way between high and low, and then they will operate intermittantly as power suppliy voltage, temp. and humidity, or will of the gods dictate.

Archangel
- 19th August 2006, 01:31
Before you go out and buy anything, try a low power LED on you input and visually see what happens when you turn the motor by hand.
JS