Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
pic24f [2019/02/28 18:58] paul created |
pic24f [2019/03/31 14:50] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PIC24 ====== | + | ====== |
+ | |||
+ | The PIC24F is a versital microprocessor with lots of nice peripherals such as | ||
+ | DAC, multiple UARTS and PWM. I used it for the [[ltp_lidar|LTP Lidar]]. | ||
+ | |||
+ | ===== Documentation Notes ===== | ||
+ | |||
+ | The PIC24 from Microchip has two types of documentation. The first is the device | ||
+ | specific datasheet and the second is a general set of documentation for the PIC24 | ||
+ | family of chips. Sometimes, especially when looking for a detailed explanation of how | ||
+ | a peripheral works, the general family documentation is more helpful. | ||
+ | |||
+ | ===== General ===== | ||
+ | Every project has '' | ||
+ | '' | ||
+ | |||
+ | There used to be compiler libraries for the peripherals of the PIC24 but they | ||
+ | have become depracated | ||
+ | (([[http:// | ||
+ | PIC24 MCU & dsPIC DSC Peripheral Library]])). Instead perhiphal functions are | ||
+ | generated by MCC (MPLAB Code Configurator). | ||
+ | I2C and the MCC I2C commands | ||
+ | [[http:// | ||
+ | |||
+ | ===== Delay ===== | ||
+ | You can use the delay functions of the xc16 compiler but you have to define the | ||
+ | following before including the right header file. It is this: | ||
+ | |||
+ | <code c> | ||
+ | #define FOSC (32000000ULL) | ||
+ | #define FCY | ||
+ | |||
+ | #include < | ||
+ | </ | ||
+ | |||
+ | Then you can use: | ||
+ | <code c> | ||
+ | __delay_us() | ||
+ | __delay_ms() | ||
+ | </ | ||
+ | |||
+ | ===== PWM ===== | ||
+ | |||
+ | Getting PWM to work on the PIC24 is tricky. We make use of the '' | ||
+ | that has a tremendous amount of functionality which also makes it tricky to use. | ||
+ | |||
+ | ==== Capture/ | ||
+ | |||
+ | The PIC24 has a periphal group called the '' | ||
+ | data sheet here has specific register information | ||
+ | [[http:// | ||
+ | |||
+ | This document gives a detailed description of this peripheral group [[http:// | ||
+ | |||
+ | Go to Section 64.7 of the PIC24 Family document. | ||
+ | |||
+ | To make a simple PWM signal use the '' | ||
+ | **Dual Edge Mode**. The **Dual Edge Mode** allows us to reset the '' | ||
+ | pulse width and along with the 16 bit timer length. | ||
+ | |||
+ | We use dual edge buffered '' | ||
+ | buffered part is that so that we can update the PWM values while the PWM is | ||
+ | running, and the peripheral takes care of loading the next values at the end of | ||
+ | a cycle. | ||
+ | |||
+ | ===== SPI ===== | ||
+ | |||
+ | I learned a lot about SPI in the LTP Lidar project because it didn't work right | ||
+ | off the bat. It is not that complicated and the easy setup in MCC (MPLAB Code | ||
+ | Configurator) gives you all the stuff you need. You just use the | ||
+ | '' | ||
+ | |||
+ | Some notes: | ||
+ | * Make sure you have the SPI mode selected correctly!! | ||
+ | * Always break out the SPI bus SCK, MISO, MOSI and SS pins to probe test points [[https:// | ||
+ | |||
+ | ===== UART ===== | ||
+ | |||
+ | To use the UART you can set it up on the the MCC (MPLAB Code Configurator). | ||
+ | |||
+ | Check the box that diverts '' | ||
+ | '' | ||
+ | byte function we get from the configurator. | ||
+ | |||
+ | To use '' | ||
+ | |||
+ | Read about the xc16 '' | ||
+ | |||
+ | To use the MCC generated '' | ||
+ | '' | ||
+ | |||
+ | The PIC24F only has a 4 byte serial buffer. Getting the pic to do a serial read | ||
+ | requires me to get out of my comfort zone and work with interrupts. I need to set up | ||
+ | an interrupt every time there is a byte and add it to my running buffer. This really | ||
+ | shouldn' | ||
+ | |||
+ | I need to set the '' | ||
+ | is 1 or more characters in the buffer. | ||
+ | |||
+ | There is information about the UART in both the PIC24F Family Reference manual and also | ||
+ | the PIC24FV16KM204 Family Reference Manual [[https:// | ||
+ | |||
+ | This is all forcing me to learn about interrupt functions. Reading about it in | ||
+ | Chapter 7 of the C30 Compiler User's Guide [[http:// | ||
+ | |||
+ | I think I can use a macro for the UART Rx interrupt. See Section 8.3.4 in the | ||
+ | Interrupts manual | ||
+ | [[http:// | ||
+ | |||
+ | Im trying to find the correct preproccessor macro for the ISR of the UARTRx | ||
+ | interrupt. In doing so, I read this in the C Compiler Manual: | ||
+ | |||
+ | < | ||
+ | If an interrupt handler does not require any of the optional parameters of the | ||
+ | interrupt attribute, then a simplified syntax may be used. The | ||
+ | following macros are defined in the device-specific header files: | ||
+ | </ | ||
+ | |||
+ | I found out I can just use the autogenerated UART handler instead of writing my | ||
+ | own ISR. Which may be worse than writing my own ISR because there is a lot of | ||
+ | bullshit with it. |