.Example Project Files

These example files can be used as the starting point for a new project in MPLAB.  Use the project wizard, select the C30 compiler and add these files into the project. PIC24FJ256GB106 main.h – Generic project header file main.c – main C file main.h – header file for main.c

Read More

Assembler

Notes Bit Numbers Use # in front of the number, so bit 0 is "#0" Special Function Registers You can use SFR's in the assembler directly instead of including via an input or output.  Doing this is faster as the compiler won't move the SFR via W0 and instead will access it directly. Multiple Assembler Statements Use "/n" to […]

Read More

AtoD

PIC24FJ256GB106 Auto Sampling //—– SETUP THE A TO D PINS —– //AN0, AN1, AN11 analog //Auto scan of inputs AD1PCFGL = 0xF7FC; //Set the analog pins (0 = analog) AD1PCFGH = 0xFFFF; AD1CON2 = 0x0408; AD1CON3 = 0x1F05; AD1CHS = 0x0000; AD1CSSL = 0x0803; //Set the inputs to scan AD1CON1 = 0x80E4; //AD1CON2 SMPI (interrupts […]

Read More

Constant Pointers In Program Memory

Constants In Program Memory Issues Although PIC24 has 3 bytes per instruction, you can only store data in 2 of them. Therefore program memory usage is 3x actaul data size. C30 uses 16 bits for pointers. By default it expects all constants to be in 32K of program memory address space. This is a limitation […]

Read More

I2C

  //—– SETUP I2C 1 —– //Used for: EEprom w_temp = I2C1RCV; I2C1ADD = 0; I2C1BRG = 395; //157=100KHz@16MIPS, 395=100KHz@40MIPS (typically 100kHz, 400kHz or 1MHz) I2C1STAT = 0; I2C1CON = 0; I2C1CONbits.I2CEN = 1; //Enable I2C          

Read More

IO

IO Defines //Output define examples #define MY_OUTPUT _LATD0 #define MY_OUTPUT(state) _LATF0 = state //<<<This method better for compatibility with with other compilers #define MY_OUTPUT(state) LATCbits.LATC1 = state //Input define examples #define MY_INPUT _RF2 //Port define examples #define DATA_BUS_IP PORTD #define DATA_BUS_OP(data) LATD = data #define DATA_BUS_TRIS(state) TRISD = state Peripheral Pin Select //—– INPUTS —– […]

Read More

Locating things in Program memory

Locating things in Pogram memory //—– CONSTANTS LOCATED IN PROGRAM MEMORY —– #ifdef FLASH_FIRMWARE_VALUES_LOCATION #ifndef __DEBUG //Don't do in devel mode to reduce the programming time if these are being located at the end of the memory space const unsigned int __attribute__ ((space(psv), address (FLASH_FIRMWARE_VALUES_LOCATION))) firmware_upload_markers [2] = {OUR_PRODUCT_ID, OUR_FIRMWARE_REVISION}; #endif #endif //A function int […]

Read More

Peripheral Libraries

Peripheral Library Documentation You need to go to the library files themselves for the documentation – its not in MPLAB help: C:\Program Files (x86)\microchip\MPLAB C30\docs\periph_lib\

Read More

PWM

PIC24FJ256GB106 //—– SETUP TIMER 2 —– //Used for: PWM T2CON = 0x0000; //16 bit, 1:1 prescale TMR2 = 0; PR2 = 0x0fff; //<This sets the PWM resolution //—– OC1 —– //Used for: Intensity PWM OC1CON1 = 0x0000; //Turn off Output Compare 1 Module OC1R = 0x0000; //Initial PWM period OC1RS = 0x0000; //Set PWM period […]

Read More