PDA

View Full Version : connect two pics



soki
- 19th September 2008, 13:34
Hi to the forum!

I already searched the forum about how to connect two pics, i found that many ppl suggesting using serin2 and serout2 commands.

Iam builiding a robot for avoiding obstacles. One PIC16f877a is used for collecting data(actually the voltage is changing) with a/d channels from a sharp IR sensor and display them on a lcd, I want when the sensor gets a minimal voltage value (indicating the robot is close to an obstacle) then the second PIC 16f84 become active (with a l293d chip) to make the dc motors moves the way I want.

Is there a simpler way to just make the second PIC enable for some minutes to just run the program with the dc motors, and then become inactive again without serial communication?

I know the simplier solution is to put everything on PIC 16f877a but for some strange reason the big PICs dont work well with l293d and dc motors only the smaller ones like pic16f84 or pic16f628 do thats why i need 2 pics.

Any help will be very much appreciated.

Thank you very much.

mackrackit
- 19th September 2008, 13:41
SERIN or SERIN2 is about as simple as it gets.

Why do you not want to use it?

Jumper
- 19th September 2008, 13:48
Hi,

You might want to find out why the big PIC doesnt want to work properly. Do you have a nice and clean supply voltage (or do you get spikes when you start and stop the DC motors)?

Do you have enough power at all?

Do you have capacitors close the the power pins of the pic?

Do you have brown out enabled?

PCB or a ball of soldered wires?

X-tal, RC or external oscillator?

I would reccomend you to try to solve this issue and then you would get a much easier project. There must be a reason for this so I think we keep the 2 pic design as Plan Z.


What do you mean with "it doen't work well"?
/me

soki
- 19th September 2008, 14:43
Thank you for your replies.

When I say it doesnt work well I mean when I make a program for big PICs and I want for example rotate one dc motor right then pause 2 sec then rotate left pause 3 sec etc, it just dont work or work wrong, for example it will rotate only right and then stop working at all or if its a big loop it maybe stop in the middle of the program.With small pics it works like champion.


Are there some examples with serial communication? I tried to find the "sixpics" project someone mention but there isn't in melabs program examples.

Thank you very much.

skimask
- 19th September 2008, 14:49
When I say it doesnt work well I mean when I make a program for big PICs and I want for example rotate one dc motor right then pause 2 sec then rotate left pause 3 sec etc, it just dont work or work wrong, for example it will rotate only right and then stop working at all or if its a big loop it maybe stop in the middle of the program.With small pics it works like champion.
Quite frankly, to me, that just doesn't make any sense. But, I'll bet, I'd just bet, if, and only if, you posted the code that you used in both PICs, the one that worked and the one that failed, you might, just might, get a quick answer as to why one works and the other one seems to fail.
But that would be just too easy...


Are there some examples with serial communication? I tried to find the "sixpics" project someone mention but there isn't in melabs program examples.
Ya know what works for me?
Typing 'Serial Communication between 2 PICs' in the Search box.
Crazy how that works ain't it?

mackrackit
- 19th September 2008, 14:55
I agree with Jumper, find the problem.
Maybe post the problem code??

SERIAL-- really simple example . Much more to add, but the basic idea.
SEND


SEROUT2, PORTX.X, MODE,[DEC 9]


RECEIVE


SERIN2, PORTX.X, MODE,[DEC VAR]
IF VAR = 9 THEN LABELX

Jumper
- 19th September 2008, 15:05
If you have connected anything on PORTA and you dont get the results you want.... this one I leave for Melanie to answer if needed :-)

Show us your HW setup and some code, it should not be rocket science to rotate a DC motor with the 877A.

So dont give into Plan Z just yet.

/me

soki
- 19th September 2008, 15:07
The program I put in both small and big pics are the same,not different programs! On small pics it work on big not well! :)

skimask
- 19th September 2008, 15:10
The program I put in both small and big pics are the same,not different programs! On small pics it work on big not well! :)

And there are hardware differences between the two PICs...
Is there some reason why we shouldn't see your program? State secret or something?

Jumper
- 19th September 2008, 15:31
The bigger PIC has tons of nice functions and these needs to be disabled/enabled/taken care of/set up/deactivated......etc

So tell us what you have connected where and all your regiter settings.. TRIS ADCON etc if you think your code it too big/secret to post here.

But if you insist of doing a 2 pic project feel free. And if you do that.. why use serial communication when it should be enough to use 2 pins as input on the small pic... as long pin-xx is high rotate left, if pin-yy is high rotate right...

but... naaah.... if you already have code working for a small pic... what PIC is that? It cant be a 16F84 since that has no A/D....

the plot thickens

/me

soki
- 19th September 2008, 16:31
You guys are great and have humor!

Thank you for your answers!

How wrong I can be with that simple program? :p

TRISB = 0 ' PORTB is output
PORTB.0 = 1 'enable1 of L293d is always high so input 1 and input 2 are available
start:
PORTB.1 = 1 'input1 of l293d goes high, so output 1 goes high so turns left
PORTB.2 = 0 'input2 of l293d goes low, so output 2 goes low so turns left
pause 2000
PORTB.1 = 0 'the opposite, turns right
PORTB.2 = 1
pause 3000
PORTB.1 = 0 'stop the dc motors
PORTB.2 = 0
goto start 'loop

skimask
- 19th September 2008, 16:33
So what's the problem?

soki
- 19th September 2008, 16:43
And the schematic, the l293d is also connected right(ground,vs,vss) but not shown on this schematic

soki
- 19th September 2008, 16:45
So what's the problem?

not work well the last time I checked, and believe me I tried many times.

Jumper
- 19th September 2008, 16:54
You say you have 2 pics, one small and one big and then you use the same code in both. In the small pic everything is dandy but with the 877A it is not working properly.

the code.... I agree.. what can go wrong... so the answer is probably not in the lines you posted.

What FQ do your run the pics on, and where do you get this one from?
Maybe DEFINE OSC XX in the beginning could help ....

Configurations bits... a long story.. and the bigger the pic.... the longer the story.. the best way to set configuration bits is by adding them to the code. How this is done depends if you use MPASM or not. Search the forum and add these bits into the top of your program... These have a significant effect on the pics behaviour.

OSC XT HS RC LP
Watchdog on off
Brown out or not
etc etc.. add all config bits you have for the device in the code

all these you can find in the datasheet....


And you still didnt answer how you power the pic and if you have caps close to the power pins.



/me

soki
- 19th September 2008, 16:59
I will try again today to connect directly l293d with the pic1678777a, the available ports left are portC and portD, which one to use? Should I take care extra something? I can give seperate voltage to l293d from a different +5V source. :)

Jumper
- 19th September 2008, 16:59
OK,

more questions... your 9V battery. What kind is that? Is that the normal small flat one or something with some real juice in?

How many mA does the motor take in start up current?

Nothing wrong with PORTB, this is a different problem.. but please power the L293D separetly. Add config bits to make sure at least brown out off and watchdog off.

/me

soki
- 19th September 2008, 17:01
You say you have 2 pics, one small and one big and then you use the same code in both. In the small pic everything is dandy but with the 877A it is not working properly.

the code.... I agree.. what can go wrong... so the answer is probably not in the lines you posted.

What FQ do your run the pics on, and where do you get this one from?
Maybe DEFINE OSC XX in the beginning could help ....

Configurations bits... a long story.. and the bigger the pic.... the longer the story.. the best way to set configuration bits is by adding them to the code. How this is done depends if you use MPASM or not. Search the forum and add these bits into the top of your program... These have a significant effect on the pics behaviour.

OSC XT HS RC LP
Watchdog on off
Brown out or not
etc etc.. add all config bits you have for the device in the code

all these you can find in the datasheet....


And you still didnt answer how you power the pic and if you have caps close to the power pins.



/me

See the schematic attachment about how I power the PIC. :) I will try those configurations bits! Thanks!

Jumper
- 19th September 2008, 17:23
Try to add caps (2* 0.1 uF) close to the power pins on the pic (one on each side).

and was it a regular 9V battery or something more powerful?

/me

skimask
- 19th September 2008, 17:52
Oh ya...
If that's a regular ol' 9v battery (little flat one like Jumper said), that isn't going to work well at all, at least not for very long.
Dropping the operating frequency of the PIC might help.
And of course, as Jumper said, put extra cap's everywhere.
Your PIC16F877A is probably brown-out resetting every time you start moving a motor (or maybe one motor or both or whatever). The smaller PICs might be a bit less sensitive to power dropoffs, or maybe they're LF versions whereas the '877A isn't an LF version.
Who knows...
One thing I do know is that the schematic shown above could use a bit of redesign to make it more reliable.

soki
- 20th September 2008, 16:26
Help guys Iam starting loose hair for this project :p

I think the problem must be the normal 9V battery, what I can use as alternative? Will be an alcaline 9V battery or something else be better choice. I notice also in LCD screen somehow the 2 power sources (9V battery and the 4x1.5V AA batteries are interfacing) maybe is the L293D drive chip to blame and try another one?

I also replace resonator 4mhz with crystal 4mhz with 33pf capacitors so can be more accurate. I also defined osc 4 in program. I also use pbp247 compiler and mpasmwin520 assempler(this is from Compile and Program Options) and I use an usb programmer I bought on ebay with the following settings when I program the chip the watchdog is disabled (see the first attachment).

I connected l293d chip successful with RC ports so to have only one chip, but my problem is that after some loops the circuit stop working!

See the complete schematic in the second attachment. Please help me to do it more reliable!

The code of the program is:


DEFINE OSC 4
DEFINE LCD_DREG PORTB ' LCD Data bits on PORTB
DEFINE LCD_DBIT 0 ' PORTB starting address
DEFINE LCD_RSREG PORTB ' LCD RS bit on PORTB
DEFINE LCD_RSBIT 5 ' LCD RS bit address
DEFINE LCD_EREG PORTB ' LCD E bit on PORTB
DEFINE LCD_EBIT 4 ' LCD E bit address
DEFINE LCD_BITS 4 ' LCD in 4-bit mode
DEFINE LCD_LINES 2 ' LCD has 2 rows
'
' Define A/D converter parameters
'
DEFINE ADC_BITS 8 ' A/D number of bits
DEFINE ADC_CLOCK 3 ' Use A/D internal RC clock
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in us
Res Var Word ' A/D converter result
Volts1 Var Word ' First part of result in mV
Volts2 Var Word ' Second part of result in mV
'
' Constants
'
Conv1 Con 19 ' 5000/256 = 19.53, this is the decimal part
Conv2 Con 53 ' This is the fractional part
TRISA = 1 ' RA0 (AN0) is input
TRISB = 0 ' PORTB connected with lcd is output
TRISC = 0 ' PORTC connected with l293d is output
PORTC.0 = 1 ' l293d enable1 high
PORTC.3 = 1 ' l293d enable2 high
PAUSE 1500 ' Wait 1.5sec for LCD to initialize
'
' Initialize the A/D converter
'
ADCON1 = 0 ' Make AN0 to AN4 as analog inputs,
' make reference voltage = VDD
ADCON0 = %11000001 ' A/D clock is internal RC, select channel AN0
' Turn on A/D converter
LCDOUT $FE, 1 ' Clear LCD
AGAIN:
'
' Start A/D conversion
'
ADCIN 0, Res ' Read Channel 0 data
Volts1 = Res * Conv1 ' Multiply by 19
Volts2 = Res * Conv2 ' Multiply by 53
Volts2 = Volts2 / 100
Volts1 = Volts1 + Volts2 ' Result in mV
IF Volts1 > 1800 THEN DOKIM
LCDOUT $FE,2,"V = ",DEC4 Volts1 ' Display result
PAUSE 1000 ' Wait 1 second
GOTO AGAIN ' Repeat
DOKIM: ' move one dc motor for testing
PORTC.1 = 1
PORTC.2 = 0
PAUSE 3000
PORTC.1 = 0
PORTC.2 = 1
PAUSE 3000
PORTC.1 = 0
PORTC.2 = 0
GOTO AGAIN
END

skimask
- 20th September 2008, 16:46
-Get rid of that 9v battery and make up another battery pack. Get a bunch of NiMH AA's and make another battery pack, anything but that stupid 9v battery.

-Put decoupling cap's across Vdd and Vss on the PIC.

-Turn off the brown out detect on the PIC in the config fuse options.

-4Mhz osc - accuracy isn't going to help here. As long as it runs, it should be good.

-USB programmer from eBay - spend the money and buy a PICKIT2. You'll be happier in the long run.

-One power supply for the PIC, one power supply for the motors - good idea.... Separate grounds, not a good idea. Connect the grounds together.

-Put low value inline resistors on the lines between the PIC and the L293D.

soki
- 20th September 2008, 17:36
Thank you skimask for your suggestions. I will try them! Thanx again! :)

Archangel
- 20th September 2008, 21:35
Hello soki,
Read and understand this thread:
http://www.picbasic.co.uk/forum/showthread.php?t=543
I think life will get easier for you if you do.

soki
- 20th September 2008, 22:13
Thanx Joe, I will read it :)

soki
- 21st September 2008, 07:11
Ok I changed the battery pack and already work well!

For inline resistors 500 ohm will be ok?

soki
- 21st September 2008, 12:35
Ok guys it finally works! I replace 9V battery with 6xAA battery pack all circuit use that power source, I put 500 ohm inline resistors between PIC and L293D and a 0.1uF decoupling capacitor across Vdd and Vss.

Thank you all for your great help! :)

skimask
- 22nd September 2008, 04:12
Ok I changed the battery pack and already work well!
Now you know all about internal impedance of a battery....


For inline resistors 500 ohm will be ok?
For situations like this, start high and work down, until something either works reliably and/or blows up.

soki
- 23rd September 2008, 06:29
Now you know all about internal impedance of a battery....


For situations like this, start high and work down, until something either works reliably and/or blows up.

Thanks God it works fine from the start and nothing blowed up! :)