// ******************************************************************************************* // // // Author: Roman Lysecky // Date: November 18, 2010 // // Basic set of software routines for accessing the ADLX345 acclerometer using an SPI // interface. // // NOTE: This library of routine is incompelte, and only supports a small subset of // commands. // // ******************************************************************************************* // #include "p24fj64ga002.h" #include "libpic30.h" #include "stdio.h" // ******************************************************************************************* // // Configuration bits for CONFIG1 and CONFIG2. // // Make sure "Configuration Bits set in code." option is checked in MPLAB. // This option can be set by selecting "Configuration Bits..." under the Configure // menu in MPLAB. // // These settings are appropriate for debugging the PIC microcontroller. If you need to // program the PIC for standalone operation, change the COE_ON option to COE_OFF. _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_ON & COE_OFF & ICS_PGx1 & FWDTEN_OFF & WINDIS_OFF & FWPSA_PR128 & WDTPS_PS32768 ) _CONFIG2( IESO_OFF & SOSCSEL_SOSC & WUTSEL_LEG & FNOSC_PRIPLL & FCKSM_CSDCMD & OSCIOFNC_OFF & IOL1WAY_OFF & I2C1SEL_PRI & POSCMOD_XT ) // ******************************************************************************************* // // Allocates an array of integers in program memory that we will use to persistenyly store data // even in the baence of power to the PIC. The space(prog) indicates the aray wil be located in // program memory. The size of the aray should be equal to one FLASH page, as we can only update // one FLASH_PAGE at a time with the PIC24F. In addition, the aligned(_FLASH_PAGE*2) // ensure the array is properly aligned at a FLASH_PAGE boundary. int __attribute__((space(prog), aligned(_FLASH_PAGE*2))) flash_data[_FLASH_PAGE]; // ******************************************************************************************* // #define XTFREQ 7372800 // On-board Crystal frequency #define PLLMODE 4 // On-chip PLL setting (Fosc) #define FCY (XTFREQ*PLLMODE)/2 // Instruction Cycle Frequency (Fosc/2) // ******************************************************************************************* // #define BAUDRATE 115200 #define BRGVAL ((FCY/BAUDRATE)/16)-1 // ******************************************************************************************* // volatile int updateCount = 0; // ******************************************************************************************* // int main(void) { int count = 0; // local array (i.e. array stored in data memory) used for data storage with a size equal // to one FLASH page (i.e. _FLASH_PAGE integers). int local_data[_FLASH_PAGE]; // Data structure that acts as a pointer to a program address. _prog_addressT flash_data_p; // ******************************************************************************* // // UART1 is used debugging through printf's // ******************************************************************************* // RPINR18bits.U1RXR = 9; // UART1 RX connected to RP9 RPOR4bits.RP8R = 3; // UART1 TX connected to RP8 U1BRG = BRGVAL; // Set UART1's baud rate generator register U1MODE = 0x8000; // UART enabled, 8-bit, 1 stop bit, no parity U1STA = 0x0440; // UART TX enabled, U1RXIF set when any character received // ******************************************************************************* // _TRISB5 = 1; _CN27IE = 1; _CNIF = 0; _CNIE = 1; // ******************************************************************************* // // Get program address for flash_data array. _init_prog_address(flash_data_p, flash_data); // Copy data from program memory to the local data array. The size of the transfer // for one flash page is _FLASH_PAGE*2 (i.e. specified in bytes not 16-bit words). _memcpy_p2d16(local_data, flash_data_p, _FLASH_PAGE*2); // Retrieve the last count value, and print the count to the UART count = local_data[0]; printf("init count = %i\r\n", count); // ******************************************************************************* // while(1) { if( updateCount == 1 ) { updateCount = 0; count++; printf("%i\r\n", count); // Write the update count value to the local data array. local_data[0] = count; // Erase the FLASH page where we will store the data in program memory. // NOTE: You must erase the FLASH page before writing. _erase_flash(flash_data_p); // Write the local data array to the program memory. _write_flash16(flash_data_p, local_data); } } return 0; } // ******************************************************************************************* // // Change Notifcation interrupt use to detect if SW5 (connected to RB5) of the Starter Board // is pressed, and set the updateCount variable used within the main execution loop. // ******************************************************************************************* // void _ISR _CNInterrupt(void) { _CNIF = 0; // Clear the change notification interrupt flag // If button pressed, set updateCount variable. if ( _RB5 == 0 ) { updateCount = 1; } } // ******************************************************************************************* //