PDA

View Full Version : Can a pic do several things at once?



karenhornby
- 10th April 2008, 17:03
I'm pretty new to pic programming and am still learing, I dont mind admitting I've got a LONG way to go yet
However what I'd like to know and I cant find an easy answer anywhere is:
Can you get a pic to do more than one thing at once?
What I've got in mind is Im using a pic to control solenoids in my car, measure and display temperature using a DS1820 and sending all the info to an lcd, but what I'd like to know is ...
Is it possible for the SAME pic to take a serial input, decode it and display the readings on 7 segment displays while doing all the above?

Put simply I'm trying to interface (once i figure out how properly) serial data from a GPS receiver to the pic, interrogate that data and when the NMEA sentence I'm looking for arrives, strip the SPEED from the NMEA sentence, convert it from Knots tro MPH and display it on a 2 7 segment LED display

Can I do all that with one PIC or would I be better off using a second pic and circuit for the GPS section of my project?

I need the speed reading to be taked once a second as its going to be used in my car as an accurate speedometer.
The Pic I'm using at the moment for the first half of the circuit is a 16F877A simply because thats what came with my easypic5 development board

mat janssen
- 10th April 2008, 17:26
Yes you can do all of that. When taking a 20Mhz xtal and that big pic. it's no problem. And all of that can be done in pbp and not 1 line in asm.

mackrackit
- 10th April 2008, 17:32
Can a pic do several things at once?
NO, even your PC on your desk can not.

That is where interrupts come in.

In you case an interrupt on the serial input should do the trick.

What happens with an interrupt, when an event takes place that is tied to the interrupt routine (internal or external) a "flag" is set. When the PIC or your PC gets time (finishes whatever it is doing ) it will then do the routine called by the interrupt. Then go back to normal operations until the next "flag" is set.

One PIC will work. If speed is a factor , Darrel's Instant Interrupts will be what you want.

First I would play with interrupts with an pin going high to be the trigger to get the feel of things.

A little of what the coding looks like for the above example.


ON INTERRUPT GOTO MYINT

INTCON = %10010000

'###################
'MAIN PART OF PROGRAM
'###################

DISABLE

MYINT:

IF PORTB.0 = 1 THEN

PC = PC + 1

WRITE 3,PC.BYTE0

WRITE 4,PC.BYTE1

READ 3,PCNT.BYTE0

READ 4,PCNT.BYTE1

pause 100

ELSE

PC = PC

ENDIF



INTCON.1 = 0

RESUME

ENABLE

'THE ABOVE WILL ALSO DEBOUNCE THE SWITCH

This is a snippet from one of my programs just so you can see what to do. It has nothing to do with your app. But it does show how to write to the EEPROM.

karenhornby
- 10th April 2008, 18:53
Time to learn about interupts I think ...
as well as how the serin command works
Oh just as a matter of interest, would a gps receiver that has a usb interface be using serial data or would it be some obscure data thing thats going to be hard to talk to?

mister_e
- 10th April 2008, 18:59
PIC are peripheral only, if you need to talk to another USB peripheral you need to use or a Microcontroller with USB OTG feature (some Microchip PIC32 now have it), or an external interface between your PIC and your GPS. Something like uALUSB, MAX3421 and so on. Maybe your GPS have another easier to use serial communication port?

mackrackit
- 10th April 2008, 19:02
I have not used GPS with a PIC myself... But serial data is pretty easy, USB can be a bit difficult.

Does your GPS plug into a serial or USB port?

karenhornby
- 10th April 2008, 22:30
Haven't bought the gps unit yet, but the one I'm probably going to buy is either bluetooth (too much work for me to learn how to use that with a pic) or usb as it's designed for use with a pda
I'm guessing it might be some kind of serial output using the usb connector, but it's unlikely i'll be that lucky.
According to the user manual, there are 3 types of cable/connector, USB, PS2 and RJ45 http://www.transystem.com.tw/pdf_gps/Manual_iGPS-M%20Pro_Rev%201.3.pdf

mister_e
- 10th April 2008, 22:37
There's probably reasons why they came with installation CDs...

I heard few times about Falcom modules.. never tried them though.
<hr>
EDIT: FSA02 seems to be a cute little one ;)

karenhornby
- 10th April 2008, 22:42
There's probably reasons why they came with installation CDs...

Well it's unlikely an installation cd would be much use to me the way I want to use it.....
And the USB version has no drivers on cd.

Anyway I've found a sual mini usb/bluetooth GPS receiver that uses the same chipset i wanted, and outputs all the NMEA sentences I would be interested in


Output terminal: Mini-USB (CMOS Level)
‧ NMEA protocol output : V 3.01
‧ Baud rate : 38400 bps
‧ Data bit : 8
‧ Parity : N
‧ Stop bit : 1
‧ Output format :
Standard : GPGGA (1time/1 sec), GPGSA (1 time/5 sec.), GPGSV (1time /5 sec.), GPRMC (1time /1 sec.), GPVTG (1 time/1 sec) Would this CMOS level interface directly to a pic with no other hardware needed so I could wait for the nmea sentence I wanted and then act upon that sentence?

In case anyone's interested... http://www.holux.com/JCore/en/products/products_spec.jsp?pno=227

mackrackit
- 10th April 2008, 23:18
If all you are after from the GPS is speed, one of these might do. I have not used them yet but I hear they are OK. Both are serial interfaced.

http://www.sparkfun.com/commerce/product_info.php?products_id=465

http://www.parallax.com/Store/Microcontrollers/BASICStampModules/tabid/134/txtSearch/gps/List/1/ProductID/396/Default.aspx?SortField=ProductName%2cProductName

dhouston
- 11th April 2008, 00:29
You need a USB host to receive data from a USB device. A PIC is not powerful enough to act as a USB host.

b1arrk5
- 11th April 2008, 01:05
Karen,
according to the link you provided, you could get RS-232 serial out of the gps unit using an RJ-45 connector. These are the same connectors used for ethernet connections, and you can purchase a cable that has this on one end and a nine pin serial plug on the other. You can make your own cable, but you need a tool to press the wires into the connector. You can also get PS-2 connectors, but they are harder to find and cost more. Of course you might find a broken PC keyboard, and take the cable off of it. Darrel's Instant Interrupts will be your best friend for this project.

Jerry.

Squibcakes
- 11th April 2008, 06:54
A cheaper option would be to use a hall sensor mounted on your drive shaft and have the pic count the rpm. Convert that to mph and voila! You have your speed.
(If you want to get even more fancy, you could then hook the pic into your throttle and hey presto!, you've just built a cruise control for your car. Lets see the GPS do that!!)

Good luck.

Squib

karenhornby
- 11th April 2008, 15:40
A cheaper option would be to use a hall sensor mounted on your drive shaft and have the pic count the rpm. Convert that to mph and voila! You have your speed.
(If you want to get even more fancy, you could then hook the pic into your throttle and hey presto!, you've just built a cruise control for your car. Lets see the GPS do that!!)

Good luck.

SquibI would have considered that but I've already got something similar, its my speedometer, my reason for wanting to do the above is the accuracy of speed derived from GPS signals, where using sensors on the driveshaft or anywhere else will never be anywhere near accurate due to tyyre sizes, and even the tyre pressure dropping would change the speed reading's accuracy thats why I ruled out sensors on the cars mechanical bits

Acetronics2
- 11th April 2008, 16:20
Hi, Karen

I think you could use one and only Pic ...

Let's see ...

Speed : you can't read digits @ more than 2 Hz ... and how often can you retrieve data from the GPS ???
DS1820 : Needs around 1 sec to make it's measure ... same comment for reading.

the PWM clock also can be used as the digits Multiplexing clock ...

Controling Solenoïds : aha ... that look dealing with a turboCharger wasteGate ... which is the PWM refreshing rate 40 Hz ? 100 Hz ? 200 Hz ?

No matter ... the PWM can Handle that with little trickery ( look at the recent convenient Thread on how to do that ... on MkBasic forum !!! )

Sooo, that lets plenty of time to make calculations on temp, GPS Data, Solenoid drive ...
just have a neat "scenario" ( Organigram ) and it's in the pocket !!!

Alain

PS: Mhhhh, Blondes put their hands under car hoods now ??? That's really THE news !!!

The Master
- 11th April 2008, 16:43
Just a thought. I have a satnav and it always says im going 5mph slower than the speedometer says. I also know that the speedometer says im going about 2mph slower than i really am so the satnav is about 7mph out. I know that in theory it should be incredibly accurate but it never seems to be. Might just be my satnav though.

Are you allowed to use GPS to calculate speed? What about if you lose the signal or something else goes wrong?

My personal choice would be checking how fast the axle is rotating although i wonder if its possible to do something similar to a laser mouse.

Acetronics2
- 11th April 2008, 17:16
Speed is just to show how much speed you have won with the brand new boost and injection programmings ...

no critical functions involved ...

what did you say about planet heating ??? .... ah, it's a transmission packet error !

Alain

mackrackit
- 11th April 2008, 17:24
Just for the heck of it.
Here is what we use for our ground applicators.
http://www.dickey-john.com/products/agriculture/ground-speed-sensing/radar-ii/
Doubt if it would work for Karen. Only goes to 66 mph.

Archangel
- 11th April 2008, 17:28
Hi Karen,
Call me old school, I believe your speedometer can be more accurate than a GPS bird 20 miles in space. Any decent speedometer shop should be able to put it right on, plus if the car is newer than the early eighties, you can access the VSS sensor for the pulses to operate the PIC.
JS

Squibcakes
- 11th April 2008, 21:53
I would have considered that but I've already got something similar, its my speedometer, my reason for wanting to do the above is the accuracy of speed derived from GPS signals, where using sensors on the driveshaft or anywhere else will never be anywhere near accurate due to tyyre sizes, and even the tyre pressure dropping would change the speed reading's accuracy thats why I ruled out sensors on the cars mechanical bits

Well thats your call, but as others have said, this approach would be more accuate than a gps. Whats more, the car needs to be moving to get an accurate speed (and direction). It would look a bit funny when your stopped at the traffic lights and your display shows you are moving at 1mph. I guess you need to take this into concideration and perhaps using a sensor on the shaft show zero speed when the car is stopped.

Oh yeah, and as far as nmea is concerned, the pic (speed) can handle it just fine untill the you start processing very long strings where you run out of memroy.

One problem I have had though is when sitting in a waitstring($GPVTG) waiting for that string to arrive, the pic can't do anything else. In those cases you could use a second pic just.... but then it all starts to get messy.

Thats my 2c worth.

Squib

squib

karenhornby
- 11th April 2008, 21:56
Hi Karen,
Call me old school, I believe your speedometer can be more accurate than a GPS bird 20 miles in space. Any decent speedometer shop should be able to put it right on, plus if the car is newer than the early eighties, you can access the VSS sensor for the pulses to operate the PIC.
JS


Just a thought. I have a satnav and it always says im going 5mph slower than the speedometer says. I also know that the speedometer says im going about 2mph slower than i really am so the satnav is about 7mph out. I know that in theory it should be incredibly accurate but it never seems to be. Might just be my satnav though.

Are you allowed to use GPS to calculate speed? What about if you lose the signal or something else goes wrong?

My personal choice would be checking how fast the axle is rotating although i wonder if its possible to do something similar to a laser mouse.


Hi, Karen

I think you could use one and only Pic ...

Let's see ...

Speed : you can't read digits @ more than 2 Hz ... and how often can you retrieve data from the GPS ???
DS1820 : Needs around 1 sec to make it's measure ... same comment for reading.

the PWM clock also can be used as the digits Multiplexing clock ...

Controling Solenoïds : aha ... that look dealing with a turboCharger wasteGate ... which is the PWM refreshing rate 40 Hz ? 100 Hz ? 200 Hz ?.

No matter ... the PWM can Handle that with little trickery ( look at the recent convenient Thread on how to do that ... on MkBasic forum !!! )

Sooo, that lets plenty of time to make calculations on temp, GPS Data, Solenoid drive ...
just have a neat "scenario" ( Organigram ) and it's in the pocket !!!

Alain

PS: Mhhhh, Blondes put their hands under car hoods now ??? That's really THE news !!! Why don't people read things fully and properly instead of just seeing the bits they want?
In the order highlighted (and not highlited) above...

GPS Speed accuracy:

speed measurement with accuracy approaching 0.01 knot (Knot is used by GPS satellites and has top be converted to MPH or KPH) is possible by using GPS Doppler data.
Way too complex to explain how the speed is calculated here in a few lines, but If you want to read up about it I suggest you start here http://nujournal.net/HighAccuracySpeed.pdf


HOW did you measure your speed?
and how did you work out your sat nav was 5mph out?
this is actually impossible once it has a lock on 3 or more satellites, again read the above article.
Also i suggest if your still not sure then have your sat nav compared against a police calibrated speedometer, or laser gun, they are calibrated.

Yes I am allowed to use GPS for speed, nothing in the rulebook says I cant, and besides If you had seen what I said earlier, I have a speedometer in the car already, the GSP speed will just be a backup so I can see A: my "REAL speed" and B: have a warning if I go over a preset limit (which I set in the program)

The GPS unit I will be using will be able to calculate data at a frequency rate of between 1 and 5hz, depending on how I program it.
Most standard "off the shelf" gps units output data at 1Hz.
The GPS will only be SENDING DATA to the pic, the pic will NOT be talking to the gps unit once it's set up.

As to update speed, on a bynote:
ALL the calculations regarding converting gps data to speed is already done in the GPS unit, the ONLY thing the pic will be doing in regard to that, is taking a serial output from the gps, looking for a certain data sentence, and just displaying the speed on a 7 segement display.
No major calculations in the pic, other than converting knots to MPH.

The DS1820? I dont see the relevance as it's only going to be measuring the outside temperature from behind the front bumper of the car and does not need to be highly accurate or update often.

Controlling Solenoids:
I dont see a problem at all with this, all I'm going to be doing is sending either 1 or 2 ports high (switching the solenoid) once a certain state has been reached, and they will STAY high untill I finish my journey or decide to turn them off before that.
There is no high speed switching, its only on once and thats it.
Turbo? it wont be going anywhere near the turbo at all, it was never mentioned.

I asked what to me seemed to be a simple question because I'm new to programming Pic's, in fact I only got the development board working this week, now to you "experts" it may have seemed a stupid question, but like I said in the beginning of this post, I just wanted a straight answer .. can it be done or do I need 2 pics.

Oh and for those that didn't see it earlier, i want to use GPS because of the accuracy, IF I was to use a sensor connected to the driveshaft or anything it's never going to be as accurate, and would need a mechanic to fit it and so on, the gps option I can program myself, so no other people needed to work on my car.

btw whats wrong with blondes working under car hoods?
Women can do almost anything these days, this isnt 1960 anymore.

Archangel
- 11th April 2008, 22:01
What truely amazes me is how people ask for help and opinions and then get pissed off when someone offers to help. Go help yourself!

JD123
- 11th April 2008, 22:05
Karen, don't be too hard on them. By nature, we (PIC'ers) want to fix every problem in the world, even if it's not broken. They meant well and I don't think anyone thinks your project is stupid. Chock it up to TGIF brain fog!

The Master
- 11th April 2008, 22:15
I compared my satnav against my speedometer and found it to be 5mph different. Unfortunately i dont have access to police speed checking devices unless i want to pay a fine. Come to think of it i cant remember how i worked out my speedometer was 2mph out. I do remember that it was about that though. Obviously it is just "about" i dont have a way of testing things accurately.

My satnav is usually connected to about 5 satellites at a time but i have noticed that even a little bad weather can cause problems. I dont think its that reliable and my satnav is supposed to be one of the best ones out.

Why exactly do you need something thats more accurate than a speedometer? I can understand if you would rather see a number instead of a dial but why go to the extreme of GPS?

I understand that wheel size and tyre pressure can affect your mph but i thought you should have it callibrated for different wheel sizes. as for tyre pressure i dont think it will affect it that much. Will your display have a decimal point? Can it display "10.54mph"? Unless you have a decimal point then i dont think it would make a difference

Melanie
- 11th April 2008, 22:20
Question : Can you get a pic to do more than one thing at once?

Definitive Answer : Yes and No (answers like this is a girl thing).

Explanation : The PIC can ONLY execute one program instruction at a time at any instant in time. So technically the answer is NO. However there are two riders to this...

1. Some PICs are packed full of additional hardware. Those hardware elements operate independently from the controlling processor (although they may be synchronised and share things like the clock). So, YES, you can SIMULTANEOUSLY perform an ADC operation, Hardware PWM, Comparator Function, Capture Compare, SERIAL I/O, Op-Amp operation etc etc (providing you have a PIC with the appropriate hardware built-in) whilst your software is doing something completely different AT THE SAME TIME.

2. Whilst I said that the PIC can only execute one instruction at any given instant (and this holds true) the definition of an 'instant' is something else altogether. If you now define 'an instant' to be 1mS (one millisecond), a 40MHz PIC can execute 10,000 instructions in that 'instant'. To the casual observer, the PIC can be handling multiple software tasks simultaneously, in truth though, it is still only doing ONE thing at a time (just happens to be doing it damn quick).

So, here is where you work out EXACTLY what you need your PIC to do, and what your particular definition of 'an instant' is going to be. That will define if you can get your single PIC to perform all the tasks you need it to in your available time slot.

karenhornby
- 11th April 2008, 22:27
Question : Can you get a pic to do more than one thing at once?

Definitive Answer : Yes and No (answers like this is a girl thing).

Explanation : The PIC can ONLY execute one program instruction at a time at any instant in time. So technically the answer is NO. However there are two riders to this...

1. The PICs are packed full of hardware. The hardware elements operate independently from the controlling processor (although they may be synchronised and share elements like the clock). So, YES, you can SIMULTANEOUSLY perform an ADC operation, Hardware PWM, Comparator Function, Capture Compare, SERIAL I/O, Op-Amp operation etc etc (providing you have a PIC with the appropriate hardware built-in) whilst your software is doing something completely different AT THE SAME TIME.

2. Whilst I said that the PIC can only execute one instruction at any given instant (and this holds true) the definition of an 'instant' is something else altogether. If you now define 'an instant' to be 1mS (one millisecond), a 40MHz PIC can execute 10,000 instructions in that 'instant'. To the casual observer, the PIC can be handling multiple software tasks simultaneously, in truth though, it is still only doing ONE thing at a time (just happens to be doing it damn quick).

So, here is where you work out EXACTLY what you need your PIC to do, and what your particular definition of 'an instant' is going to be. That will define if you can get your single PIC to perform all the tasks you need it to in your available time slot.

Thanks Melanie, that was exactly what I was looking for and it answered all my questions in one go :)

I realise I've got a lot of learning to do yet as regards and interupts and various other functions, but It's given me the urge to learn a lot more and work hard at ait till I get it doing exactly what I want,
I'm reasonably good with analogue electronice and originally I was just going to do something the old fashioned way, untill I realised that If I learnt how to program I can do loads more.

By the way, sorry to all concerned if my previous post seemed to be a bit "hostile" I was getting pretty wound up with everyone assuming things and not taking any notice of what I'd already said.
Don't worry though, I wont ask stupid questions again, I'll just sit and read from now on :)

mackrackit
- 11th April 2008, 22:58
I should probably keep quiet...but

I thought post #3 did a fair job at answering the question too. :)

And about the stupid questions and reading more.
If people would do that, then we would not need this forum. :D

Personally I am happy to see new people come in that want to learn and not just copy paste code.

As far as the accuracy of the GPS. The only way to be more accurate is to use a DGPS set up. Now that the WAAS system has been in place for a few years, I think most commercial products have that built in.

Bruce
- 11th April 2008, 23:22
A PIC can indeed do several things at once. It's just a matter of getting familiar with
hardware options the PIC you're using has. Like Melanie already said, firmware is going
to execute line-by-line, but built-in hardware goodies open up a whole new avenue to
real multi-tasking. You just need to get familiar with all your options.

And don't hold back on posting or asking questions here. That's what this forum is for.

I learn something new just about every time I login here just by reading & thinking about
some of the questions asked, so keep em coming. I still have a lot to learn myself..;o}

Archangel
- 12th April 2008, 00:04
By the way, sorry to all concerned if my previous post seemed to be a bit "hostile" I was getting pretty wound up with everyone assuming things and not taking any notice of what I'd already said.
Don't worry though, I wont ask stupid questions again, I'll just sit and read from now on :)
<font color=red> <h3><b>It did seem hostile, funny thing, big red letters are like that. . .</font color></b></h3>Apology accepted!
I cannot grasp what <b>Automotive</b> purpose requires a speed measurement which requires accuracy equaling two hundred thousandths of an inch per second (5mmps), weapons systems yes, cars, no.<br>Questions are seldom stupid or unwelcome, often they are well worn due to having been asked several times in the past, but not stupid. Please ask your questions, learn from those who exceed your personal knowledge<b> and</b> from those who do not.

Acetronics2
- 12th April 2008, 14:27
Hi, Karen

You seem tired ... and should rest some little time.

I was just explainng you HOW and WHY your different things could be done by the same Pic ...
By the way, add the regulating calculations to the duties the Pic will have to do ...

Will you use PID or Fuzzy to drive your Solenoïd ???

a little question :




This Doppler shift is directly
proportional to velocity of the receiver along
the direction to the satellite, regardless of the
distance to this satellite.



Must I think from that it can't work in a deep forest, a narrow mountain valley, or between high buidings, ???

Alain

PS: That's you who talked about "blondes" at first ... not me !

karenhornby
- 12th April 2008, 20:35
Hi, Karen

You seem tired ... and should rest some little time.

I was just explainng you HOW and WHY your different things could be done by the same Pic ...
By the way, add the regulating calculations to the duties the Pic will have to do ...

Will you use PID or Fuzzy to drive your Solenoïd ???

a little question :



Must I think from that it can't work in a deep forest, a narrow mountain valley, or between high buidings, ???

Alain

PS: That's you who talked about "blondes" at first ... not me !
LOL
thanks everyone :)
The need for 100% accuracy 24/7 isnt essential, I was actually thinking of having the display so I can see when I'm getting near the speed limit the cops WILL ticket me
and
Its mainly going to be there as a backup to the actual speedo (they have been known to break) and the other reason.. is the reason someone (wish I could remember who) when asked why he did something said:
because I can.
I just thought it would be a good way of learning how several part of the pic work, how to program the various bits, see it working everytime I drive, and more important,. anytime anyone asks about it I can not only say waht it was, but also say "I did it"
yes.. I admit i'll like boasting once its finished :)

enauman
- 10th May 2008, 00:09
If you are interested in accuracy, don't forget that most commercial GPS units don't display instantaneous velocity. Most of them average over a 5 second window. That is to say that what you see on the screen is the averave velocity of the last 5 seconds. The fastest it can update is once per second. If you really want accuracy and readability, make a speed pickup from a magnetic or optic sensor and put it on the drive shaft as was suggested. Then write the code to read it, translate it, and display it. If you want to split hairs, calibrate it through the operable speed range to compensate for tire expansion.

This is not PIC related, but it is an example of what can be done with a GPS head.
A while back the radio guy at work was walking all over the plant with a signal strength meter trying to map blind spots in our radio coverage. It was going to take him a month to get a minimal amount of data. I bought a Garmin GPS head with an RS232 output and fed it into the serial port on a laptop. I routed the signal strength output from one of the hand held radios into a DAQ board in the PCM-CIA slot and wrote some labview code. All he had to do was put the laptop, GPS head and radio into a car and drive around the plant. The code sampled position and signal strength and plotted a colored representation of the signal field overlaid on a map of the plant property. It took about 3 days to put it together and write the code and about 30 min for him to collect all the data needed. If you really want to interface the GPS with the PIC, I suggest you use a 16F876 and get a GPS head with an RS232 output. The NEMA format is real easy to handle.

Edward A. Nauman
Senior Staff Engineer

Lockheed Martin Aeronautics
"The Skunk Works"
Mail Stop 0157
1011 Lockheed Way
Palmdale, CA 93599
Voice: (661) 572-7350
Fax: (661) 572-3507
Cell: (661) 305 7636