Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Learning C for PIC's?

Status
Not open for further replies.
I am a beginner in the world of C and pretty new to the world of electronics. I am curious as to what would be the best resources/tools for me to learn C for PIC's? I have spoken with 3v0 and he has been very helpful, but wanted to get others opinions as well, as to maybe some books to purchase, online tutorials, documentation, etc.

I am having a hard time on finding a starting point of where to begin to learn. Thanks!
 
Quite honestly, 3v0 taught me everything I know about PICs. You want my opinion? I suggest listening to him :D

My first project was the classic LED flasher. It helps you learn how to set fuses, write basic code, read datasheets and other documentation, write code to the chip, and wire everything together to work in a circuit. My second project was an LED chaser, which is basically a more complicated version of the flasher, using 8 LEDs instead of one. Turn the leftmost LED on, leave it a second, turn it off and turn on the next one, leave it on, and continue this process. From there you can modify it to go back and forth (think Cylons from Battlestar Galactica) or come in from the sides and out from the center (I'm thinking KITT, from some episodes of Knight Rider). Once you've done that you can begin working on more complicated projects, for example use a switch to select different patterns you want displayed--change it every time the switch is pressed. Then go from there.

By that point you should be fairly familiar with how PICs are used and will be able to work on even more in-depth projects.

You've told me in chat you learn best working hands-on. This is a great way to do just that, but don't try jumping in to the complicated projects right from the start. Begin with basic ones. Even if they bore you, they will still teach you. I guarantee it.

So, first things first, what PIC have you decided to start with? That will ultimately determine the compiler you'll need and how it will be used.

Hope this helps.
Matt
 
Nigel did several PIC tutorials in ASM..... With these in mind I created C equivalent code to run on his tutorial boards.. Both links are in my signature....

I did a tutorial about creating a project in C within the MPLAB IDE environment..
https://www.electro-tech-online.com/articles/creating-projects-in-mplab-ide.582/

I have also done the "simple interrupts" article that has C and ASM side by side..
https://www.electro-tech-online.com/articles/simple-interrupts-on-mid-range-pics.640/

3V0 tutorials are definitely worth a visit.. ( I can't post the link as 3V0 has removed it from his signature )
 
I would learn the basics from online tutorials. The problem with beginner C books is that practically all of them teach programming in unix/linux environment.. how to read keyboard etc. That does not help you to get started with microcontrollers at all.
Problem with C books for microcontrollers is similar.. they are based on some particular chip or board, like Arduino etc. Hard to find a good C book that focuses solely on the programming language itself.
"OReilly - 21st Century C (2012)" is probably the most useful book I can recommend. And also "OReilly - Understanding and Using C Pointers (2013)".
 
Last edited:
also what I did when going from assembly to C was just to read the codes that people posted (comments helped) and read all the tutorials you can get your hands on, and start as stated by getting a led on, adding a switch to flash it, and so forth....Nigel's tutorials and Ians C conversion will help alot....
 
I did a bit of research on finding the best book for learning C and PICs and this one seems to be the general consensus, "Designing Embedded Systems with PIC Microcontrollers, Second Edition: Principles and Applications"

I have not seen that book before, but I can say that all books from Newnes I've read are really good. That book seems to cover couple of different PICs. That is a really good thing, but one thing that catched my eye is the 16F84 chip. I thought that was a really old chip, but the book is published in 2010. Is that a relevant chip to learn today? Is it good one to learn the basics of the mid-range PICs?
 
ok I may aswell rock the cart.

Have you done any other programming? there are more options than just c.

you could always start with a programming language that you are more familiar with.

Rich
 
Ya Der, you sent me that link a while back; I have been reading it since. Thanks, Good stuff!

In the googlymoogly tutorials that 3v0 lnked me to it suggest learning assembly language first then converting over to C. Thoughts? I am thinking about doing this just so I can understand how the code works with the actual device.
 
In the googlymoogly tutorials that 3v0 lnked me to it suggest learning assembly language first then converting over to C. Thoughts? I am thinking about doing this just so I can understand how the code works with the actual device.

A lot of people suggest learning assembly first "to teach you how the chips work", but I honestly think you could get by just fine simply by using the datasheets. I started learning Assembly before I started with C, and personally I feel it was a waste of a month. However, that is only my personal opinion. Your experience may be different, so I suppose it couldn't hurt to learn some. Just don't let it hold you back.
 
I don't think there is any advantage learning assembly first. If you program the chip rather than rely on libraries then you will be as knowledgeable as an assembly programmer.

One thing I found useful for trying out bits of code is http://www.compileonline.com/compile_c_online.php

Have fun,

Mike.
Edit, the example on that page is not very useful. Here's a little code to take a number and convert it to a string. On a pic you would change the printf to send to an LCD or serial port.
Code:
#include <stdio.h>
#include <string.h>

main(){
    int i=1,j=45678;            //initialize variables
    while(i<j)                  //find largest divider higher than J
        i*=10;                  //this is the only instruction in while loop
    i/=10;                      //don't need leading zero.
    while(i>0){                 //
        printf("%i",j/i);       //print 1 digit
        j-=j/i*i;               //remove digit from J
        i/=10;                  //prepare for next digit
    }                           //end while loop
    printf("\nDone.");
}
 
Last edited:
My personal opinion, It is not wastage time that we spend for Assembly few months, that will well helpful to understand core level operation of any micro controller I thought.

I started PIC from Mid-Range after huge experiment I transferred to PIC 18, while i am studding PIC 18 I have been thinking that I had been stated from PIC 18 but that is not truth

This is good book that I can recommend to you
https://bookmoving.com/book/solutio...bedded-systems-using-assembly-c-pic-_290.html
 
My personal opinion, It is not wastage time that we spend for Assembly few months, that will well helpful to understand core level operation of any micro controller I thought

How do you learn more by doing,
Code:
    movlw 7
    movwf CMCON
Rather than
Code:
    CMCON=7;
As I said, without libraries you learn the chip just as well in C as assembly.

Mike.
 
Micro said:
My personal opinion, It is not wastage time that we spend for Assembly few months, that will well helpful to understand core level operation of any micro controller I thought.

The truth here is, when you become proficient at C.. You can look to ASM when you really need to optimize...
Without that knowledge you remain in the hands of the compilers!!!

You do not need a working knowledge of ASM to learn C but syntax is syntax.... When you have learned one, the others are relatively the same..

Unfortunatley for me .. I first used C/C++ on the PC so the low level stuff, the magic bios and dos were masked from me... When moving to the pic I did everything in ASM so moving to C on a micro was a snap, but I'm really glad I did the ASM thing!!!!
 
In ASM

Code:
bsf       SSPCON2,SET
btfsc    SSPCON2,SET
goto      $ - 1
return

in C

Code:
void I2CStart(){

SET=1;
while(SET);

}

As per above, the ASM codes very sensible specially for beginners those who studding them self with data sheet
I think most optimize language in the world ASM but very long development cycle
However finally, I agreed, every one should start with ASM or not must finally engage with high level langue
 
I think most optimize language in the world ASM but very long development cycle

It is easy to optimize trivial small code sections, but when you need to write more complex programs with complex data structures and algorithms, then the design becomes more important than small details in the code.
 
For the beginning, I would start from what looks more appealing to you. Find a blinking LED samples in Assembler and C. Look at both codes and see what is more understandable for you and start from it. Try a simple project of yourself. Say, add a buttom that changes blinking frequency. You will need to do bouncing, auto-repeat etc. This will teach you conditional execution, loops, other basic stuff. The important thing is not to search Internet for ready solutions, but to do it on your own. That's how you start learning.

The worst mistake would be to get pieces of ready-made code and try to fit them together. This will not teach you anything.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top