Time I learn C - advice

Discussion in 'Embedded Systems and Microcontrollers' started by wannaBinventor, Sep 28, 2010.

  1. wannaBinventor

    Thread Starter Member

    Apr 8, 2010
    179
    4
    I think I've spent long enough diddy-dallying with PIC assembly. It seems that as I get into more advanced things, much of the sample code is only offered in PIC C. I've tried, unsuccessfully, to figure out what is going on in some C code and port it over to assembly, but it seems like I always mess something up.

    Can someone recommend a good ONLINE C tutorial aimed at teaching people how to program a PIC?

    Thanks
     
  2. PRS

    Well-Known Member

    Aug 24, 2008
    989
    35
    I don't know of an online source, but used texts are really cheap at such sites as Abe's books. I recently got a fat book on C and C++ for 2 dollars plus 4 dollars postage. It came right to my door. This book is either new or very little used! Though it's copyright 1998, C hasn't changed since then.

    Another source is Microchip. They give free C compilers away and they provide PDF files describing them for free. These are specifically intended to support their PICs. Google Microchip and look around. You'll find something.
     
  3. sceadwian

    New Member

    Jun 1, 2009
    499
    37
    LCCWIN32 is a good C primer, micro controller C and ANSI/computer C is not the same thing though. Basic C syntax funtions and program flow understand can come from understanding an ANSI C based programming language like LCCWin32, it's free and well documented, there are many others out there.

    It might be better if you posted the C code you were trying to port of to ASM and explain where your problems were, the main thing C has going for it is the math and protocol libraries that are very difficult to program in ASM.
     
  4. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    I also can't recommend any online tutorials. Most of the people I've worked with taught themselves C (as I did) and the usual technique was to do it from a book. I don't remember the book I used, but it wasn't anything special. I recommend going to Amazon and searching for "teach yourself C programming" and you'll find a few books. I like to find the ones that are used and listed at $0.01 with $4 shipping; you can get some good deals this way. If you're going to be doing a lot of C programming, it wouldn't hurt to have a copy of K & R (Kernighan and Ritchie).

    If you use a PC as your computer, you can find free compilers to use, so don't bother buying anything yet -- something like gcc on Linux/Windows/Mac will do everything you need to help you learn the basics. If you're really dedicated, you'll also get a book like Sedgewick's "Algorithms in C" (I can't remember the exact title and I'm too lazy to go find it) and also learn some data structures and algorithms stuff to help you learn C.
     
  5. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    If you buy a compiler, it'll come with a manual. If you get something free (or stolen? Oh say it won't be) on the Internet, then it's much more dubious. But for microprocessor programming, you can't just expect to use standard C, because there are all the features of the processor to be concerned about.

    I'm a PIC guy myself, but if I were starting out I might go for the AVR processor just because there's a free compiler for it. However, what's the documentation for that compiler like? That is the question. I paid good money for my compiler (I have the CCS one, now many versions out of date) and it came with a manual that's been good enough.
     
  6. sceadwian

    New Member

    Jun 1, 2009
    499
    37
  7. Nanophotonics

    Active Member

    Apr 2, 2009
    365
    3
    There are a variety of C languages out there. It depends which one/s you want to use.
     
  8. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    Learn the core C concepts first, possibly by the mechanisms I gave in my previous post. You don't need to mess with any specific microcontroller or embedded systems compiler -- just use "regular" compilers like gcc, bcc, cl, etc. Later, when you want to work on embedded systems, then you'll get into all the nitty-gritty details related to the hardware and processor architecture.

    More importantly, this learning process needs to teach you about the mechanics of designing, writing, and debugging programs. You need to learn things like how to debug without using a debugger, reading the assembly output of the compiler to see what's really going on (or documenting a compiler bug :p), etc. I'm a big fan of the "debuggers are evil" mentality, which was promulgated by my software mentor. He was right, as every debugger I've ever used has failed me at some point. I've worked on embedded systems where you simply had no debugger or ICE to help you figure out what was going wrong -- and you would have killed to have a simple printf to help you (this happened on the boot-up of a famous product most of you have probably used -- and we had to debug it by pure logic, as there simply was no possibility of any output in the code we were debugging).

    Now, for you dogmatics that foam at the mouth when (you think) I said you shouldn't work with a debugger, that's not what I said. I'm simply recommending that you learn how to debug your programs when a debugger isn't available -- as it surely will happen sometime. On an even higher level, the goal is to learn how to design programs that don't have bugs in the first place -- but that's a dissertation, not a forum post!
     
  9. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Yes, that is the moment you start to write your own debugger. A blank display is a great stimulus.
     
  10. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    This book really helped me out a lot.
    http://www.amazon.com/Beginning-Nov...sr_1_sc_2?ie=UTF8&qid=1285788558&sr=8-2-spell

    Also I don't know who makes em but I stumbled into 2 different C video series. From starting out they were very helpful to watch. I couldn't say I learned C by it. But once I read the books the video seemed to help it all come together.

    Also if your a true beginner with hardware I can't reccomend a good cheap dev board to start out with enough. Just so you can see how everything is connected and working. Its much easier to debug something you know works other them messing with code for 12 hours to realize you missed a pull down resistor somewhere. Each chip will have mandatory electronics hooked up to it. Then you need to add everything else you need to do with your project. So if you get the dev board you will know how the crystal works, how the MCLR works, etc.. People here tend to get a little ahead of themselves because they've been doing it so long, and don't realize some of us starting out have trouble telling the difference between a byte, a bit, and a nibble.
     
    Last edited: Sep 29, 2010
  11. wannaBinventor

    Thread Starter Member

    Apr 8, 2010
    179
    4
    Thanks for all the advice guy. I did order a book. I should have just gotten the K&R, but I've been dumping a crapload of money into this hobby......errrrr..... invention research lately, so I just found one that reviewed okay for .01 + 4 bucks shipping.

    I've glanced at some online stuff, but it all seems incomplete and lacking, so hopefully the book will do the job.

    For whatever reason, assembly seems much more intuitive to me, however cumbersome.

    One of the things I guess I don't understand about C are the libraries. I'm use to doing everything in detail, but it seems like C pulls some stuff out of nowhere.

    For instance.....

    Code ( (Unknown Language)):
    1.  
    2. void SPIWrite(BYTE data)
    3. {
    4.     BYTE i;
    5.    
    6.     #if defined(HARDWARE_SPI)
    7.         PIR1bits.SSP1IF = 0;
    8.         i = SSP1BUF;
    9.         SSP1BUF = data;
    10.         while(PIR1bits.SSP1IF == 0){}
    11.     #else
    12.    
    13.         SPI_SDO = 0;
    14.         SPI_SCK = 0;
    15.        
    16.         for(i = 0; i < 8; i++)
    17.         {
    18.             SPI_SDO = ((data & (0x80 >> i)) > 0) ? 1:0;
    19.             SPI_SCK = 1;
    20.             SPI_SCK = 0;  
    21.         }  
    22.         SPI_SDO = 0;  
    23.     #endif
    24.  
    Outside of not understanding the details like ANDing data with 0x80 and i equalling SSP1BUF, I don't understand how the SPIWrite (Byte Data) function works. I looked at the # include commands and looked at the header files and what not but the best I could come up wtih was

    Code ( (Unknown Language)):
    1.  
    2. #define HARDWARE_SPI
    3.  
    I guess I don't understand how that does everything when in assembly I have to do a few things to initialize and use the SPI.
     
  12. sceadwian

    New Member

    Jun 1, 2009
    499
    37
    It sounds to me like you need to learn basic boolean algebra.
     
  13. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    Assembly you have to do everything at a bit level. Litterally code everything. With C much of the code is done for you in libraries. Also the code itself is much easier to read and understand then ASM. If your wanting to go with a higher powered chip I recommend Flying the Pic24. Its based on the Explorer16 which is a 16/32 bit pic dev kit and it even has a programmer on it. All the code examples in the book are for the EXP16 so this would be a great place to start. If your wanting to do 8 bit stuff I hear good things about the Mikroe BigPic5/6. I know at first it looks way more complex then ASM but once you learn the basics its really far far easier. Also read your datasheet. This will help you with all those words you don't know. Many of them are just turning on and off bits in the register.
     
  14. Potato Pudding

    Well-Known Member

    Jun 11, 2010
    684
    92
  15. Potato Pudding

    Well-Known Member

    Jun 11, 2010
    684
    92
    Also Make certain you download and try Notepad++
     
  16. ELECTRONERD

    Senior Member

    May 26, 2009
    1,146
    16
    When I first started in PIC programming with the C language I first read a few C books to understand the general syntax. After that, I got my hands dirty and started contriving various projects to exercise my C programming skills. There's one thing that knowledge can't teach you, and that's experience. I used to program with the Boe-Bot in PBASIC, and they have exercises and projects in there for each chapter. So you can follow along and attempt each one, except in the C language; if you need help, simply ask on the forum. If you can't find a Boe-Bot manual there are plenty of ideas and tutorials online. I use the PICKit2 with the PIC18F1320, which is a simple PIC to get started on. Here's some links to help you find ideas:

    http://membres.multimania.fr/liesha... Exploring the PIC32 - Lucio Di, Elseiver.pdf

    http://padabum.com/data/Микроконтро...ial PIC microcontroller (S. Katzen, 2000).pdf

    http://leehow.blogspot.com/

    http://203.172.182.172/~chirayut/Picbasipro_30project.pdf

    http://www.palgrave.com/pdfs/0333929942.pdf

    http://www.electronicspk.com/store/catalog/documents/manual_pic_lab_II.pdf

    http://highered.mcgraw-hill.com/sites/dl/free/0072402415/55328/alc_ch07.pdf

    http://falleaf.net/download/Microch... Your Personal Introductory Course 3rd Ed.pdf

    http://ti.tuwien.ac.at/ecs/teaching/courses/mclu/theory-material/Microcontroller.pdf

    http://old.disco.unimib.it/informaticaindustriale/PDF_Utili/The PIC Microcontroller Book.pdf

    http://www.alexsb.org/ebooks/Getting started with PIC microcontrollers.pdf

    http://www.best-microcontroller-projects.com/support-files/pic-micro-c-course-reference-manual.pdf
     
Loading...