PIC24 Tutorial

Your Ad Here

PIC24 Intro

The PIC24F is a very versatile piece of hardware. I use it at work all the time. Along with the MPLAB IDE and the free C30 compiler, these products from Microchip make for a powerful combination. You can pretty much do anything on the lower end of the frequency spectrum. I would like to share with the web, some of the intricacies of the microcontroller as I have become somewhat familiar with the chip. Most of these resources will apply to the other processors from Microchip 16-bit series. If you have any questions, feel free the comment or email. Come back to this page often as I will be adding features and code for all of the web to see.

1. Introduction
Welcome to 16 bits of… well not power… maybe versatility.

2. Getting Started
What is the PIC24, and how to start programming.

3. First Program
A journey of a thousand miles, starts with a single “step”.

4. Configuration
Different modes of the microcontroller, what do they all mean?

5. Inputs and Outputs
You and your microcontroller: the most basic way to talk.

6. Oscillator and Timing
To a microcontroller, nothing is constant.

Your Ad Here

7. Interrupts
Because we all have to stop once a in a while.

8. Timers
Getting the most out of time.

9.1. UART Setup
9.2. UART Usage
If it’s so universal, how come there’s so many shortcomings?

10.1 I2C Basics
10.2 I2C Basic Functions
10.3 I2C Advanced Functions
Finally, a serial interface everyone can appreciate.

11. ADC
Build a voltmeter out of a PIC.

12.1 SPI Basics
12.2 SPI Master usage
The “other” synchronous serial protocol

13. PWM DAC
Who says PWM has to be complicated?

14.1 USB, An Introduction
14.2 More on USB
14.3 Setting up the USB Framework
14.4 USB HID Joystick
14.5 USB Debugger – Protocol Design
14.6 USB Debugger – Implementation
14.7 Composite Device
Complex mess of a protocol

27 Responses to PIC24 Tutorial

  1. Ryan Tensmeyer says:

    First off, I love your site. It has been much more helpful than any other documentation I have found else where.

    I was wondering if you have an experience implementing the PMP feature. I have been unsucessful in my attempts and I am wondering if I am missing something. All I am trying to do is send out a byte of data and watch it in the simulator. I am trying to send out 8 bits of data at a time and I have done the following in my code:

    #define PMCON_VALUE 0xB280
    #define PMMODE_VALUE 0×0000
    #define PMAEN_VALUE 0×0000
    #define PMADDR_VALUE 0×0000
    #define PMINT_VALUE 0×0000
    PMPOpen(PMCON_VALUE, PMMODE_VALUE, PMAEN_VALUE, PMADDR_VALUE, PMINT_VALUE);
    PMPMasterWrite(0×55);

    I don’t know if there is something more that I need to do, it I have something configure incorrectly, etc. All I know is that I can’t get any of my data lines to change. Any direction that you could give me would be greatly appreciated.

    Thanks,
    Ryan

  2. jliu83 says:

    Hi Ryan,
    The PMP is a digital interface. As such, the io ports must be configured as digital using the AD1PCFG (or equivalent) register. That is all that I can think of that is the most common problem. Also a lot of the functions that come with the C30 compiler have loops in them that wait for certain things to occur. I haven’t taken a look at the PMPMasterWrite(), but I’m pretty sure that there are several loops that wait for the write/read strobe to be at their proper configuration (ie, high/low). If you’re having trouble, and the controller is not sending anything, the execution is most likely stuck in that loop. I would try to make sure that there’s nothing physically in that node that is forcing a high or low signal (i.e. another IC on the same copper trace that is forcing the trace to be pulled high, or grounded). This is especially important for the pins/traces that are used for protocol communications, such as the address and strobe lines because most of the code has loops that wait for a proper condition to occur on these protocol signals.

    Pseudo code in the PMPMasterWrite() probably looks like this:
    1. output address
    2. loop to wait until address pins are properly configured
    3. output strobe
    4. loop to wait until strobe pins are properly configured
    5. send data
    6. loop to wait until data is sent

    The loops are probably causing you trouble because they are waiting for something to occur. You need to look at the function itself, as well as your circuit and figure out what is wrong.

    Best of luck. The harder the problem, the better you’ll feel when you solve it. We’ve all been there.
    -J

  3. Igor says:

    Hello.

    First of all, great site, and explanations.

    Now, I see You have PWM section but nothing inside, so I was wondering if You could give me few directions on using PWM with pic24?

    This is the first time I’m using PIC24 (c30 compiler) within my student project and I was reading somethin about PWM but not quite understand it.

    It’ll be great if You could give some example on how using PWM@PIC24.
    (Also, is it possible to measure Duty Cycle of “input” PWM signal?)

    Thanks,
    Igor

  4. Joan says:

    Thanks again for the tutorial, how much till the ADC part is online? I can’t seem to make it work! :<

  5. TK says:

    Hey man, awesome tutorial. has been a great help.
    Could you please also do a tutorial on USB for us.

    Thanks, you are the man ! :)

  6. jliu83 says:

    Hmm… The USB standard is a little harder to understand, compared to the I2C or the UART. I don’t want to promise anything, but I’ll see if I can make an HID tutorial in the future.

  7. Maya says:

    Just wanted to thank you for the these tutorials ! it was a great help =3

    keep up the good work

  8. fizz3r says:

    hi man!! thanks for the great tutorial!! i was wondering if you can help me with a tutorial for PWM on the pic 24h. I want to use it for a student project and it’s my first time with the c30 compiler! Tanks

  9. Remco says:

    Hello jliu83,
    I’ve been reading your tutorial and I like it. Plain simple talk that one can understand. Thumbs up!
    I’m working on an altitudemeter using a PIC24HJ12GP201 microcontroller and Bosch BMP085 pressure sensor (I2C).
    Years ago I worked succesfully with a PIC16F84 but with these two I just can’t seem to get it to work.
    There are some things that I just don’t get. Mostly about the clock. It’s about selecting the right clock values (_FOSCSEL, _FOSC and OSCCON) and with I2C it’s the Baud Rate. I have tried your code snippets but couldn’t get it all together. Now I have used some routines from ‘Pic24 Support Llibraries’ but again… it’s not functioning. I connected all hardware correct and de PicKit2 is reading, writing and debugging with MPLAB works ok.

    Is there a possibility that you could take a look at my code and say if I made the right choices?
    I’ve prepared a workspace that doesn’t contain any other things than neccesary (buzzers and leds etc..).
    I think you can review it in a few minutes.

  10. Remco says:

    Got your email. You can remove it before spammers find it..
    I’ll mail you tomorrow.

  11. Remco says:

    Hey,
    After all help you’ve given a few months ago and with your tutorials I now have a nice program and working variometer! I’m very happy with it. Do you have any hints and tips about using memorybanks? I need an array of int but size 64 is too much. I really need some more. The compiler says it’s all ok but the program won;t start and MPLab gives me target halted. It’s kind of programming but I’ve read also about the special use of the data memory vs. program memory. Maybe a nice chapter ;)

  12. jliu83 says:

    I would recommend an SPI over SD card interface. It is cheap and relatively easy to implement. You’ll have to learn about file systems. Should be a lot of fun. Tell me how you decide to do the implementation when you get a chance.
    -J

  13. Remco says:

    I was thinking of and SD card indeed! But not for that, more for storing the telemertics of the variometer. Right now I have a variometer in my plane that ‘talks’ to a base station on the ground over 433MHz using manchester coding. Pretty cool stuff that was to digg in, yes it was! But for some calculations I need just a bit bigger array to get a more precise index of rising/decending per second. I’ll look in to it. You hear it when I have something interesting or if I’m stuck again.

  14. Nikolay says:

    Hello! It is very difficult at my age (I’m 62) remember the C language, which I wrote only 3 of the program in 1988. You could really help me, if indicated how I can get sample code from your textbook. In addition, in my kit MPLAB IDE no file pic.h
    I wish you continued success, Nikolay

  15. jliu83 says:

    Where specifically is the pic.h file listed? Which tutorial are you talking about? I don’t have a text book, I’m just writing this stuff for fun.

    -J

  16. spacemanspiff says:

    Hey J,

    I got a job in a small engineers office that is developing measurement systems. We’re working with Microchip products a lot and although I never lay hand on a microcontroller before, your site helped a lot to design a system including UART, I2C, File System, SD card and a lot more in 3 months of work. I ‘googled’ a lot, but your site is definately the best when it comes to giving an introduction to the PIC24 features. Without your page, the development would have taken significantly longer.
    Just wanted to let you know.
    Keep up the great work!

    spiff

  17. jliu83 says:

    Hi Spaceman,
    I’m glad I could help. I don’t get that many visitors, and I’m kind of running out of topics (other than putting up my own projects). As a user of the site, what would you like to see in future articles?

    Thanks,
    -J

  18. TAN THIAMHUAT says:

    your website is excellent to learn PIC24.

    Could you include an example (with code, inside Chapter 6) on how to use Timer1 and Low-Power Secondary Oscillator (32.768kHz crystal) to create precise delay (say 1 sec, 2 sec, 3 sec, etc) which we control? The example could for example show some output at Hyperterminal software via UART, and make use of the Real-Time Clock and Calendar (RTCC), to show the output at certain intervals controlled by that delay..

    thanks.

  19. Richard says:

    I would love it if you covered some CAN stuff available in the PIC24H series. That way we could do neat things with cars…

    Richard

  20. Neil says:

    I found great stuff on your site, and Thank you for doing this.

    It would be great if you could expand your ADC tutorial bit more to explain how to manipulate ADC registers to read multiple inputs using PIC24FJ processor.

    Thanks

  21. John says:

    Hi,

    May I know how to do a finite state machine for PIC UART?

  22. Peytonator says:

    Many thanks for this website.

    Please could I encourage you to complete the PWM tutorial! :)

  23. jliu83 says:

    @Peytonator. It’s in the works. I have the code and all prepped last year, but I haven’t had the time to write the article yet.

    -J

  24. jliu83 says:

    @John. Sorry for the late reply, I was at a conference. Which part of the FSM of a UART are you having trouble with?

    -J

  25. Ken says:

    Thanks for the great site! Lots of really good information. Very useful for the novice as well as the experienced PIC24 user.
    Sure would like to see a of couple chapters devoted to USB.
    One chapter for USB host mode. One chapter for USB device mode.
    Please consider.
    Thanks

  26. Sam says:

    I second the USB tutorial.

    I know how to use the FTDI chips but I would love a Microchip USB tutorial as I have a 24F development board sat here and the USB side of things is really confusing me..

    I will pay in gold and trinkets for a decent 24F usb tutorial.

  27. jliu83 says:

    Hahaha, I’ll hold you to that when I post the USB device tutorial.

    -J

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>