Best way to learn PICs?

KansaiRobot

Joined Jan 15, 2010
324
Thanks. I hope you can find it.
Ok, I found one of the ones I used to learn. the guy wrote some tutorials in assembly but I skipped them. I repeat it is not necessary. Just use the one in C:

http://www.pic18f.com/18f4550-c-tutorial/2009/11/16/tutorial-4-hello-world-program-in-c/

Dont worry, pics are not that complicated. the thing is that they got a lot of functions and huge data sheets but you got also a lot of forums and resources. That doesnt happen for example with the SuperH I am trying to program now...:(
 

Thread Starter

tracecom

Joined Apr 16, 2010
3,944
You might be better off to start with HI-TECH C.
Learning a new language takes a lot of patience and extra time. If this is a hobby thing, there is no golden rule that one must learn a particular dialect. If the C or assembler syntax just isn't sinking in, there are plenty of Basic compilers (including free and open source) out there that would do the job.
I know nothing about C and I know a little about Basic. Which would be easier to learn?
 

elec_mech

Joined Nov 12, 2008
1,500
Tracecom,

Here's my 2 cents . . .

If you used PICAXE, I assume you programmed in Basic, correct? I started with the Basic Stamp II which is very similar.

Before I begin, note there are levels to rate the various languages (assembly, C, and Basic in this case). The lower the level, the closer the code is to what the MCU actually uses which is machine code (all ones and zeros). Lower levels require more time to write because you have to tell the MCU exactly what to do. Example, create variable called temp, then assign temp to memory location 0x01, then set temp equal to 4, then move temp value into a working memory location to perform something, then move new value into temp, etc. The higher the level, the more work that is done by the software being used to turn the code into machine code. Example, create variable called temp, then set temp = 4, then add temp = temp + 1, etc. The higher the language, the less work and time you'll need to program because the program you're writing in takes care of the required transition to assembly. This is why C and Basic typically cost money while assembly is free.

Assembly is nice if you really want to get into the nuts and bolts of programming, but unnecessary and time-consuming whether you know it or not. It is considered a low-level language. It is free and great if you only need to do a couple of things or need extremely precise control or timing of a particular function.

Some might disagree with me, but C is a mid-level language. If you're planning to get a job with a big company, this is the way to go as it is the most widely used commercially. Like assembly, it gives you a lot of control but because of that requires some more time to write the code.

Basic is a high level language. I like this best because it is very intuitive, especially if you've used BSII or PICAXE before, requires less time to learn, and let's you focus more on what you want to do. I personally don't think there is much, if anything, that C or assembly can do that Basic can't. Again, some may disagree, but I've worked with all three, C and assembly sparingly, and Basic is by far the easiest and anything I did in C or assembly could have been done in Basic (and would have taken me less time to write).

Additionally, if you need to do something that can only be done or controlled in assembly, you can always add assembly code within C or Basic.

To that end, my vote is Basic. I recommend Chuck Hellebuyck's book(s). A quick look reveals a $75 book, but I need to look at what I have at home (I know I didn't spend that). If you opt for C, he has three books on that as well. He takes you step-by-step. I might suggest others once I get home and look.

Also, a great PicBasicPro compiler forum: http://www.picbasic.co.uk/forum/forumdisplay.php?f=4.

Microengineering Labs offers a good Basic compiler (not the cheapest though): http://www.pbp3.com/download.html

They might offer a limited capability one for free (check version 2.60 at the bottom of the downloads page).

Also check out MikroElectronika: http://www.mikroe.com/. They carry C and Basic compilers as well. They have AWESOME demo boards if you really get into it: http://www.mikroe.com/eng/categories/view/6/pic-development-tools/

The most difficult part starting out in PIC is defining the registers in my opinion. That takes a little time to study the datasheet, but it's not hard once you play with it some. The nice thing about BSII and PICAXE is they take care of it for you.

Hope this helps.
 

MrChips

Joined Oct 2, 2009
30,802
I have nothing against either C or BASIC. Both are High-Level Languages.
I depends on what you want to accomplish and how much effort you are willing to put into the learning process. There are simple platforms such as BASIC-STAMP and PICAXE where a lot is already done for you behind the scene.

If you have difficulty learning C then there is nothing wrong with going with BASIC if it gets the job done. It is a fun way to learn computer programming in a simple way. After you have gained experience in creating your own programming constructs you will find C not so intimidating.

Another option not to be overlooked is to try out the popular Arduino which is programmed in C but a lot of the hardware features are already programmed for you.
 

KansaiRobot

Joined Jan 15, 2010
324
I know nothing about C and I know a little about Basic. Which would be easier to learn?
C is elegant and powerful. It gives you complete control. Basic is totally off compared. But of course you can always find convoluted, difficult to understand C code written by some hack.

C all the way (if we have to choose between the two)

I leave you with some entertainment :D

http://www.youtube.com/watch?v=H4YRPdRXKFs
 

panic mode

Joined Oct 10, 2011
2,746
i use c because it works everywhere (any platform).
but .....
considering it is a high level language, it is a very ugly language... :cool:
 

KansaiRobot

Joined Jan 15, 2010
324
i use c because it works everywhere (any platform).
but .....
considering it is a high level language, it is a very ugly language... :cool:
well it is a matter of opinion... yeah it can become pretty ugly but that depends on the style of the programmer and his identation and writing style

personally I would hate to go back to the pascal days where you have to put "procedure" to everything. I prefer less words and more "{", "}" s. that gives a clearer apperance in comparison to other language full of "begin"s "end"s and letters letters and more letters:p
 

THE_RB

Joined Feb 11, 2008
5,438
C is elegant and powerful.
...
It's powerful, but is the complete opposite of elegant! C is ugly and convoluted and has some of the worst program flow control ever seen in computing langauges.

The program flow control was so bad that Kernigan and Ritchie were basically forced to add the "goto" command to C (and that clumsy kludge "case" statment) to stop everybody complaining.
 

Markd77

Joined Sep 7, 2009
2,806
I'm not a C programmer, although I do think there is a place for a high level language for PICs.
From what I've seen of C it isn't what I'd call elegant, eg:
C
Rich (BB code):
void main()
{
for (;;)
{ 
User code
} 
}
Assembler
Rich (BB code):
main
User code
goto main
 

KansaiRobot

Joined Jan 15, 2010
324
It's powerful, but is the complete opposite of elegant! C is ugly and convoluted and has some of the worst program flow control ever seen in computing langauges.

The program flow control was so bad that Kernigan and Ritchie were basically forced to add the "goto" command to C (and that clumsy kludge "case" statment) to stop everybody complaining.
I am sorry to disagree but in all the years I have been programming convoluted code in C, not even once I have ever felt the need to use "goto".

As my professor in college said "If you need goto, it means you are doing something wrong and must rethink your algorithm"

I find C elegant because it uses "{" "}" instead of clumsy words...
 

KansaiRobot

Joined Jan 15, 2010
324
I'm not a C programmer, although I do think there is a place for a high level language for PICs.
From what I've seen of C it isn't what I'd call elegant, eg:
C
Rich (BB code):
void main()
{
   for (;;)
     { 
       User code
      } 
}
Assembler
Rich (BB code):
main
User code
goto main
with identation it looks much more elegant hehehe
;)
 

John P

Joined Oct 14, 2008
2,026
No no no. That do-nothing for loop never looks elegant. Use while() instead. Loop while 1 is true, which it always is:
Rich (BB code):
void main()
{
   while(1)
   { 
      User code
   } 
}
Actually maybe it should be "Keep doing my code":
Rich (BB code):
void main()
{
    do
     { 
       User code
     } 
     while(1);
}
 

Rbeckett

Joined Sep 3, 2010
208
Tracecom,
Tou are a man after my own heart. I too have a couple of Picaxes and decided to go to "regular" MCU's from a mainstream manufacturer that are not pre-programmed. i ordered a bunch of cheap developement boards and an experiment board off of Ebay for pretty cheap. I am using the 40 pin 16f877a, and MPLAB X (the new release) I also ordered the C++ primer by Pratesh, and the C++ bible by Bjarne Stroudstrup ( Inventor of C++). Picaxe was fun and taught logical thinking, but just too slow for even a moderately complex program. The Picaxe code also took up valuable real estate on the chip too. So I am off too on C/C++. Since I have a minimum of 12 hrs a week to study I hope to "get it" fairly soon. Good luck and keep us informed on you progress
Wheelchair Bob
 

Thread Starter

tracecom

Joined Apr 16, 2010
3,944
Tracecom,
Tou are a man after my own heart. I too have a couple of Picaxes and decided to go to "regular" MCU's from a mainstream manufacturer that are not pre-programmed. i ordered a bunch of cheap developement boards and an experiment board off of Ebay for pretty cheap. I am using the 40 pin 16f877a, and MPLAB X (the new release) I also ordered the C++ primer by Pratesh, and the C++ bible by Bjarne Stroudstrup ( Inventor of C++). Picaxe was fun and taught logical thinking, but just too slow for even a moderately complex program. The Picaxe code also took up valuable real estate on the chip too. So I am off too on C/C++. Since I have a minimum of 12 hrs a week to study I hope to "get it" fairly soon. Good luck and keep us informed on you progress
Wheelchair Bob
Thanks for the encouragement. After briefly trying to understand C, I decided to build on what I already knew, and I bought PICBasic Pro Silver. It's due to arrive in the next few days, and I think I can "get it." I agree with your comments about the PICAXE; it was fun, but I want to step up to PICs. Time will tell if I have the ability.
 

elec_mech

Joined Nov 12, 2008
1,500
Tracecom,

I think you'll find you'll be up and running very quickly using PICBasicPro. I can send you some simple programs I've done (if I can find them - been awhile) if you'd like. As I said before, the trickiest part is setting up the registers, but that doesn't take long to understand. The rest should be nearly identical to PICAXE programming. Holler if you need help and good luck.
 

THE_RB

Joined Feb 11, 2008
5,438
I am sorry to disagree but in all the years I have been programming convoluted code in C, not even once I have ever felt the need to use "goto".
...
Yeah I've heard people say that too, but there are a lot of people not happy with the poor program flow control of C. I'm not the one that forced K&R to add goto to their language. ;)

...
As my professor in college said "If you need goto, it means you are doing something wrong and must rethink your algorithm"
...
And in his own narrowminded way he has really highlighted the main problem. In a large complex program if something is added or significantly changed it can damage the program flow, and in C many times the best fix is to re-write a whole heap of program flow, with all the ugly associated reformatting and rebracketing etc not to mention annoying workload.

As an example if you have this;
if(blah)
{
foo
foob
foogle
fobwobble
}
else
{
fee
feep
feebly
feezle
}

And later the project evolves and you may need to exit straight after foob, it becomes a matter of rewriting all the flow control. As the program flow gets larger and more nested it can force some nasty re-writes.

I don't use gotos much in C but if can be a very powerful addition to the language allowing multiple exit points from slow code all pointing to a single destination.

Another command K&R should have added is some type of break command to break from within an if or else statement.

...
I find C elegant because it uses "{" "}" instead of clumsy words...
Sure, I too like some of the neat and strong features of the bracketed flow control. And the indenting, although that is more style than the language itself.
 

KansaiRobot

Joined Jan 15, 2010
324
I am sorry but rewriting the flow control is a good thing!

what you are proposing is just adds gotos here and there when your needs changes and that makes spagetthi code which is difficult if not impossible to maintain for someone other than you! (and even you may find it difficult after a couple of years)

you dont need goto to exit after that function. Just rethink that part of the algorithm and write it accordingly

As I said I ve never ever used goto and dont even remember the sintax, but I can agree that C with gotos must be really ugly,. the elegance comes from not using them ;)
 

ErnieM

Joined Apr 24, 2011
8,377
I am sorry but rewriting the flow control is a good thing!

what you are proposing is just adds gotos here and there when your needs changes and that makes spagetthi code which is difficult if not impossible to maintain for someone other than you! (and even you may find it difficult after a couple of years)

you dont need goto to exit after that function. Just rethink that part of the algorithm and write it accordingly

As I said I ve never ever used goto and dont even remember the sintax, but I can agree that C with gotos must be really ugly,. the elegance comes from not using them ;)
QFT. When your if-else if-else structure becomes too convoluted to follow something cleaner may be required.
 
Top