PDA

View Full Version : Discussion Some PIC-based circuits and C


David T
Apr 17, 2007, 03:42 PM
Hi folk. I've been teaching myself C and building a few PIC based devices. My first are (1) an individual cell low voltage cutoff (LVC) circuit for 3 lipos, (2) a 3/4 cell lipo tester to reveal whether cells are in balance under load and at rest, and (3) another 'servo tester'. Both the LVC and lithium tester devices can log events to aid analysis. http://www.flyelectric.ukgateway.net/pic2.htm

I found it quite hard not having programmed before so I've posted many of the smaller files I wrote along with some tips to help others get started.
Cheers, David.

MatC
Apr 20, 2007, 11:58 AM
Thanks for sharing :)

IceWind
Apr 20, 2007, 01:18 PM
That is great.
I wanted to learn some pic programming myself, what do you advice to read?

David T
Apr 20, 2007, 02:15 PM
I found it hard to find simple examples to help me learn the basics. So I have created the following page. This will give you a feel for what's involved.
http://www.flyelectric.ukgateway.net/pic1.htm

rcBluebird
Apr 21, 2007, 04:58 AM
Go to www.microchip.com for more information.

Download MLAB IDE for free, and search for PIC16F84A to start with the assembly code. You can also download the student edition of Microchip C-compiler for free (MCC18) to start with C-language.

I started with Kit-81 http://robot.ee.hacettepe.edu.tr/forum/files/33_K81-f84%20programmer.PDF and built the kit myself.

PIC tutorials are widely available on the web. Search it and enjoy.

I hope it helps.

IceWind
Apr 22, 2007, 04:08 PM
Thanks David T and rcBluebird.

I've done some pic programing and really simple assemblies using a JDM programmer. But i don't know what the code does, i just program and that's it.

I think simple things can be archived using simple pic's.

I'm going to read the info shared and try to acquire one of those kits.

Thanks again.

meteor
Apr 26, 2007, 08:12 AM
Although C is a useful high-level language, it is not really very appropriate for PIC development. (the PIC instruction set and available program space are tiny, and C is complete overkill for these purposes)

Custom languages like JAL and customized BASIC are more appropriate for the PIC environment, and are easier to learn to boot!

C is a semi-standard with PIC's simply because of the familiarity of the environment by code developers, not because it's the best language!


I have used JAL for many robot and display projects, and it's easy to lean and understand for beginners.

There's a great set of tutorials here: http://members.home.nl/b.vandam/lonely/index.html


If you want to start at the bottom and work your way up (the best way actually!), there's a really good set of PIC assembler tutorials and hardware design here: http://www.winpicprog.co.uk/pic_tutorial.htm



Good luck!

AndyKunz
Apr 26, 2007, 09:28 AM
I guess the past ten years of my life have been a waste - I've been using Hitech C almost exclusively for that time (since before it was released).

You just need to use the right tool for the job. So far, JAL and BASIC are not appropriate for anything I do. Things I do range from ESCs (www.rc-hydros.com) to model sub stuff (SubTech) to motion control to web servers - all of them running PICs. There's probably one of my products feeding the power at your local cell tower.

Andy

alexcmag
Apr 26, 2007, 01:57 PM
I agree with Andy.

My first payment for a development job was at first year on high school, 20 years ago. The card puncher used on previous entry exams broke and since it was from an old mainframe computer there was nobody in our country to fix it. The school used a optical mark reader from another school to read tests and needed to extract selected answers from a bit map. Each row contained 1 bit for each possible answer of 4 questions (A..E).

Someday I was learned some trigonometric functions at math class and was testing it at the lab with an old Turbo Pascal compiler, using the functions to draw a wire frame 3d box that appears to rotate around itself since I make it to refresh at 20fps.

The TI manager, that was also a teacher, was walking at the lab, looked it and asked if I could write a program to translate some bit information into a text file with the test number and a letter (A-E) to each answer, with X if there is more then 1 bit and blank if none.

So I made a small program in Pascal and it worked pretty well. The code was 44 times faster then the COBOL code they wrote to read it, the time needed to process all tests drop from the 336hs they had estimated (2 weeks 24x7) to less then 8 hours, making it possible to release results in time.

I usually write programs in C to PICs with some assembly when needed. This is enough to make some critical tasks like modulating 38Khz or 40Khz waves to remote controls or sonar, to read servo pulses while watch serial input and many other things I can't do with other languages.

Of course for simpler programs that are not so time-based most languages would be enough.

For PC programs I don't like to use C/C++ because most times thare is so much code for simple things, most codes are in Delphi with a SQL server, but sometimes just a script do the job, so why bother with compiling and user interface...

I started to use PIC because after years without developing electronic circuits I needed to develop and build hundreds of 8-key custom ATM keyboards in one week, the nearest suppliers for programmer boards and MCUs only had PIC, so I needed to learn to program it in one day, make the PCB and firmware in two, order hundreds of PCBs to a local supplier, solder the components and deploy.

Now I'm starting to test other MCUs from Holtek (cheaper), Atmel (more powerful) and others, to have more choices when need.

meteor
Apr 30, 2007, 10:39 AM
The point was that C, although useful, creates an unnecessarily steep a learning curve for the beginner with PIC's.

C features a lot of unintuitive structure and syntax, that may be important for large-scale projects, but simply confuse a beginner, and can get in the way of a successful simple program.

A non-technical, non-programmer, beginner would have much more fun and success with other languages, simply due to the easier comprehension of what's going on, rather than simply parroting existing C code without a clue to what is actually going on!


If a beginner desired to continue their PIC development into commercial or employment opportunities, then learning C in the long term would probably be a wise decision. But this scenario represents a tiny minority of PIC experimenters...


Again, simply put, C is simply NOT the "best" language for beginner PIC development.

Almost any compiler can do a given task, particularly in the tiny scope of PIC applications and hardware realities.

As is always the case, assembler is arguably the "best" for ANY task, and with PICs, assembler is actually a somewhat practical choice due to the small scope of the applications, and tiny memory size. (programs are measured in tens of Kbytes, not tens of megabytes!)


Basically, it's a matter of personal preference, and market familiarity. (C, in general, holds huge weight in the developer community, it's very hard to resist such inertia...)

westfw
May 06, 2007, 09:51 AM
The point was that C, although useful, creates an unnecessarily steep a learning curve for the beginner with PIC's.Recently I've been playing with something called Arduino (http://www.arduino.cc); currently this is for Atmel AVRs, but there isn't any reason it couldn't be done with PICs instead. The point is that it wraps a simplified runtime environment and VERY simplified Development environment around "C" (quite "basic-stamp"-like) to make it easier for beginners to use...

MatC
May 07, 2007, 07:05 PM
Without wishing to get too religious about this, C is:
- Most portable option, across procs and dev environments.
- Harder in the short term.
- Less to learn in the long term - C will work on everything from a PIC12F675 to a supercomputer.
The downside is that C is great if you know it already, but hard to learn from the resources I've seen. The documentation lists datatypes and C info and such, but there's not really a how-to guide from scratch. I might write one sometime.

JF1980
Jun 03, 2007, 07:50 PM
This is a great thread and just what I was looking for; thanks David.

I had a little experience with PIC's when I was younger and really wanted to work on some R/C PIC based projects. Of course ten years ago I didn't have enough pocket money to buy all of the gizmos as well as models so it was put on hold. Now I have the money but the time is hard to find.

I've just brought a PIC project board which has lots of bells and whistles (http://www.mikroelektronika.co.yu/en/tools/easypic4/) but obviously before I can play with them I need to get back to the stage of being able to flash an LED :) I aim to learn C to program the PIC rather than going back to assembler. I've no doubt learning C will be a challenge (I've only seriously programmed php in the past) but it makes the most sense. Taking the extra time will mean I am not locked in to PIC chips in the future.

I'm not sure which compiler to use or where to start with learning C. I want to use a mainstream compiler so that I can find code samples and tutorials. I suppose for now I could use the free versions of Hi-tech C, CCS or mikroC (http://www.mikroelektronika.co.yu/en/compilers/mikroc/pic/). I don't know which one to choose! Any advice? I would really have liked to code in MPLAB but I could leave it for another environment if need be. If I used mikroC I would be able to use the boards debugger.

I had started to read the C tutorial on this site (http://www.eskimo.com/~scs/cclass/notes/top.html) and have gotten to chapter 6 but I don't want to continue following it if there is something out there more applicable to learning C with the aim of programming PICs. If I follow the FED tutorial (http://www.dhmicro.com/prod_fedc.html) will it be directly applicable to the aforementioned compilers?

David T
Jun 04, 2007, 05:26 AM
I've read the K&R book and have also stumbled across the the link you provide. Appears to be solid C advice but as you say not Pic-specific. 'C for Pic' books are available if you want to go that route. The FED guide you mention is outstanding for Pic development with any C compiler.

Simple Assembler examples are more common than C. I found most C examples to be quite hard for a novice to understand. On the other hand, once you start grasping the language, C code written for one compiler starts becoming easier to translate into your own programs with another compiler. So I think finding simple examples with explanations is more useful for a beginner than examples for 'your' compiler.

Because this is all so hard! and since I'm finding more and more great things I can do with Pics, I have written up many of my learnings along the way. I have posted the programs I wrote as I learned the basics and tried to include explanations for things. See the link in post #4 ('pic1'). I can't guarantee that my advice is perfect but it should get you started quickly.

Pic chips have different features so as you become familiar with these you will concentrate on a few but want some variety in your 'suite'. In our RC hobby and particularly with electric flight, I think ADC's are the most powerful measuring feature. For instance they are used to measure voltage, current, temperature, etc. So I use the 12F683 which has 4 ADCs in an 8pin package, 16F688 (8 ADC's 14pin) and 16F687 (12 ADCs 20pin). I've only found two C compilers whose free version supports most Pics and their other restrictions (eg: code size) allow them still to be useful (CC5X and BoostC).

Compilers seem to be priced for commercial use and don't usually cater for hobby pricing. So the free versions are important because most of us cannot afford the licensed versions particularly when just starting out and you don't know if you will sustain your interest. The BoostC pricing model does accommodate hobby budgets should you need to upgrade. The free version of BoostC allows double the code size and produces more compact code than the free version of CC5X. BoostC has an active forum. I started with CCX5 but am doing all my new work in BoostC. Both work well and the coding differences are at the foot of my 'pic1' link. BoostC available here: http://www.sourceboost.com/home.html

I've not tried simulation yet. I have found that every time you want to do something different there is a whole raft of learning to be done. So simulation has not been a priority yet. I try to start with simple versions of something, hence the small examples on my site. When a more complex program does not work right I 'comment out' sections of code to simplify it. I add temporary statements to switch LEDs on and off to confirm actions. I often write register values to EEPROM so I can see exactly what they are doing. BoostC has it's own IDE with some simulation capability. Both BoostC and CC5X can be used as plugins to MPLabs.

Hope this helps budding C programmers, David.