.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. (more…)

Assembler

Notes

Bit Numbers

Use # in front of the number, so bit 0 is "#0"

(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 after # conversions) must >= the number of inputs being scanned otherwise the higher inputs will not be sampled (if >  the number of inputs being scanned then the scannign will repeat fillign up the buffer before the interrupt flag is set, se you can set to 2x the number of inputs being scanned to get 2 samples of all the inputs before an interrupt etc).
	IFS0bits.AD1IF = 0;		//Clear the interrput flag

 (more…)

Compiler Version

Viewing compiler version

Project > Select Language Toolsuite

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.

(more…)

I2C – dsPIC


//----- SETUP I2C 1 -----
//Used for: temperature sensor
I2C1CONbits.I2CEN = 0;
I2C1BRG = 93; //400kHz @ 40MHz Fcy
I2C1CONbits.I2CEN = 1;

 (more…)

I2c – PIC24

 


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

(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

 (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

 (more…)

Oscillator Configuration

PIC24HJXXXGPX06/X08/X10

//10MHz Osc
//= 80MHz with /2, x32, /2 PLL
//= 40MIPS
//= 25nS per instruction
//How it works:
//Input OSC = 10MHz (1.6 MHz to 16MHz required)
//10MHz / 2 = 5MHz (0.8 MHz to 8 MHz required)
//PLLFBD = 30 = x32 = 160MHz (100 MHz to 200 MHz required)
//CLKDIV 7:6 = 0 = \2 = 80MHz (12.5 MHz to 80 MHz requried, which generates device operating speeds of 6.25-40 MIPS).

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\

PWM

PIC24FJ256GB106


	//----- SETUP TIMER 2 -----
	//Used for: PWM
	T2CON = 0x0000;			//16 bit, 1:1 prescale
	TMR2 = 0;
	PR2 = 0x0fff;			//<This sets the PWM resolution

 (more…)

Reset Function

Soft Reset


asm("reset");

Reset Function

(more…)

SPI – dsPIC


//----- SETUP SPI 1 -----
//Used for: Flash memory IC
w_temp = SPI1BUF;
SPI1STAT = 0;
SPI1CON1 = 0x0320; //SPI in master mode
//Data is valid on the rising edge of the clock (Transmit occurs on transition from active to idle clock state)
//Clock low in idle bus state
SPI1CON1bits.PPRE1 = 1; //Prescallers 4:1 1:1 = 10MHz (max possible from this device)
SPI1CON1bits.PPRE0 = 0;
SPI1CON1bits.SPRE2 = 1;
SPI1CON1bits.SPRE1 = 1;
SPI1CON1bits.SPRE0 = 1;
SPI1CON2 = 0;
SPI1STATbits.SPIEN = 1; //Enable the port

SPI – PIC24HJ

SPI Master


	//----- SETUP SPI 1 -----
	//Used for: 
	w_temp = SPI1BUF;
	SPI1STAT = 0;
	SPI1CON1 = 0b0000001100100001;	//SPI in master mode (SPI1STATbits.SPIEN must be 0 to write to this register)
									//Data is valid on the rising edge of the clock (Transmit occurs on transition from active to idle clock state)
									//Clock low in idle bus state
	SPI1CON1bits.PPRE1 = 1;			//Prescallers 4:1 1:1 = 10MHz (max possible from this device)
	SPI1CON1bits.PPRE0 = 0;
	SPI1CON1bits.SPRE2 = 1;
	SPI1CON1bits.SPRE1 = 1;
	SPI1CON1bits.SPRE0 = 1;
	SPI1CON2 = 0;
	SPI1STATbits.SPIEN = 1;			//Enable the port

(more…)

UART

PIC24 UART Full Duplex

https://github.com/ibexuk/C_Communications_UART_PIC24_Full_Duplex

(more…)