| PIC24 Support Libraries
    | 
#include "pic24_util.h"#include "pic24_serial.h"#include "pic24_clockfreq.h"#include "pic24_ports.h"#include "pic24_delay.h"#include "pic24_unittest.h"#include <stdio.h>Go to the source code of this file.
| Functions | |
| void _ISR | _DefaultInterrupt (void) | 
| void | reportError (const char *sz_errorMessage) | 
| uint32_t | readProgramMemory (uint32_t u32_address) | 
| void | checkDeviceAndRevision (void) | 
| void | checkOscOption (void) | 
| void | printResetCause (void) | 
| void | configBasic (const char *sz_helloMsg) | 
| uint32_t | roundFloatToUint32 (float f_x) | 
| uint16_t | roundFloatToUint16 (float f_x) | 
| uint16_t | compute_brg (uint32_t u32_fcy, uint16_t u16_brgh, uint32_t u32_baudrate) | 
| Variables | |
| static _PERSISTENT const char * | sz_lastError | 
| _PERSISTENT const char * | sz_lastTimeoutError | 
| static uint16_t | u16_INTTREGlast | 
| Heartbeat | |
| These routines provide heartbeat support by blinking a LED on a regular basis. See doHeartbeat() for more information. | |
| #define | MS_PER_HEARTBEAT (10) | 
| #define | HEARTBEAT_MAX (CYCLES_PER_MS * MS_PER_HEARTBEAT) | 
| The approximate half period, in processor cycles, of the heartbeat. | |
| _PERSISTENT uint32_t | u32_heartbeatCount | 
| void | configHeartbeat (void) | 
| void | doHeartbeat (void) | 
| void | toggleHeartbeat (void) | 
This file contains implementations for functions prototyped in pic24_util.h.
Definition in file pic24_util.c.
| #define MS_PER_HEARTBEAT (10) | 
When u32_heartbeatCount reaches this maximum, the heartbeat LED is toggled by doHeartbeat().
Definition at line 78 of file pic24_util.c.
| void _ISR _DefaultInterrupt | ( | void | ) | 
Provide a default interrupt handler which records what interrupt was not handled then resets the chip. Typically, a call to printResetCause during chip startup will then print the error.
Definition at line 162 of file pic24_util.c.
| void checkDeviceAndRevision | ( | void | ) | 
Determines the device and revision of the PIC this program is executing on. This information is then output via the default UART. A warning message is issued if this program was not compiled for the chip it is running on.
Definition at line 208 of file pic24_util.c.
Referenced by printResetCause().
| void checkOscOption | ( | void | ) | 
Reports the oscillator currently in use to the default serial port. See FNOSC_SEL for a table of which chips support which clocks.
Definition at line 280 of file pic24_util.c.
Referenced by printResetCause().
| uint16_t compute_brg | ( | uint32_t | u32_fcy, | 
| uint16_t | u16_brgh, | ||
| uint32_t | u32_baudrate | ||
| ) | 
Choose UART baud rate, based on u32_fcy. NOTE: Be careful about using BRGH=1 - this uses only four clock periods to sample each bit and can be very intolerant of baud rate % error - you may see framing errors. BRGH is selected via the DEFAULT_BRGH1 define above.
Definition at line 474 of file pic24_util.c.
Referenced by compute_brg(), and configUART1().
| void configBasic | ( | const char * | sz_helloMsg | ) | 
Perform basic chip configuration:
| sz_helloMsg | Hello message to print. | 
Definition at line 434 of file pic24_util.c.
| void configHeartbeat | ( | void | ) | 
Configures a GPIO pin for use with the heartbeat and sets up the heartbeat counter.
Definition at line 89 of file pic24_util.c.
Referenced by configBasic().
| void doHeartbeat | ( | void | ) | 
This heartbeat function should be called repeatedly in any sort of blocking wait loop. It will periodically toggle an LED after HEARTBEAT_MAX increments.
Definition at line 104 of file pic24_util.c.
Referenced by checkRxErrorUART1(), configUART1(), doErasePageFlash(), doWriteRowFlash(), inChar1(), outChar1(), WAIT_UNTIL_CONVERSION_COMPLETE_ADC1(), and WAIT_UNTIL_TRANSMIT_COMPLETE_UART1().
| void printResetCause | ( | void | ) | 
Determines and prints the cause of a CPU reset. This should be called when the chip first starts up. For an example, see the configBasic function.
Definition at line 329 of file pic24_util.c.
Referenced by configBasic().
| uint32_t readProgramMemory | ( | uint32_t | u32_address | ) | 
Reads a 24-bit program memory word at the given address.
| u32_address | Address of program memory to read. | 
Definition at line 196 of file pic24_util.c.
Referenced by checkDeviceAndRevision().
| void reportError | ( | const char * | sz_errorMessage | ) | 
Report a critical error by recording a message in sz_lastError then resetting the chip, assuming hat printResetCause will be called during chip initialization.
| sz_errorMessage | Error message to report. | 
Definition at line 183 of file pic24_util.c.
Referenced by _DefaultInterrupt(), checkOscOption(), checkRxErrorUART1(), configUART1(), and picAssert().
| uint16_t roundFloatToUint16 | ( | float | f_x | ) | 
Round a floating-point number to the nearest integer.
| f_x | Floating-point value to round | 
Definition at line 460 of file pic24_util.c.
Referenced by compute_brg(), msToU16Ticks(), and usToU16Ticks().
| uint32_t roundFloatToUint32 | ( | float | f_x | ) | 
Round a floating-point number to the nearest integer.
| f_x | Floating-point value to round | 
Definition at line 448 of file pic24_util.c.
Referenced by ticksToMs(), ticksToNs(), ticksToUs(), and usToU32Ticks().
| void toggleHeartbeat | ( | void | ) | 
A function which toggles the heartbeat LED.
Definition at line 116 of file pic24_util.c.
Referenced by doHeartbeat().
| 
 | static | 
Persistent storage for an error message, typically set by reportError and reported at reset by printResetCause.
Definition at line 129 of file pic24_util.c.
Referenced by printResetCause(), and reportError().
| _PERSISTENT const char* sz_lastTimeoutError | 
Persistent storage for a timeout error, to be reported if a watchdog reset occurs.
Definition at line 134 of file pic24_util.c.
Referenced by convertADC1(), getI2C1(), printResetCause(), putI2C1(), putNoAckCheckI2C1(), rstartI2C1(), startI2C1(), and stopI2C1().
| 
 | static | 
Store a copy of the INTTREG register as a bitfield. This is not defined for all PICs, so work around with an #ifdef of ILR, one of the bitfields in this register. This is _PERSISTENT so that it survives the resets which occurs immeidately after the default interrupt handler _DefaultInterrupt copies INTTREG to this variable.
Definition at line 154 of file pic24_util.c.
Referenced by _DefaultInterrupt(), and printResetCause().
| _PERSISTENT uint32_t u32_heartbeatCount | 
The current heartbeat count. When this value reaches HEARTBEAT_MAX, the heartbeat LED is toggled by doHeartbeat().
Definition at line 71 of file pic24_util.c.
Referenced by configHeartbeat(), delayAndUpdateHeartbeatCount(), and doHeartbeat().
 1.8.13
 1.8.13