Microcontrollers: From Assembly Language to C Using the PIC24 Family Chapter 8 Example 6 & 7

Hi again. Onto the next example, this time on page 295 (figure 8.28). The code for this one is called ‘ledtoggle_nofsm’ and it’s in the chap8 folder.

Now that we’ve defined EXPLORER16_100P in the pic24_all.h file, here is a new checklist to use:

  1. Double click on the project file for the exercise your wanting to do. (Make sure you don’t already have MPLAB open or it will complain)
  2. Set the processor type in the menu item Configure – Select Device. The default PIC24 that comes with the Explorer16 is PIC24FJ128GA010.
  3. Remove the linker script from the workspace files viewer under Linker Scripts (don’t add anything, MPLAB will manage it for you)
  4. Change from DEBUG to RELEASE in the drop down at top of MPLAB screen
  5. Select your programmer (Programmer – Select Programmer). I use the ICD2
  6. Make required changes to the code (see below for this exercise)
  7. CTRL-F10 to build all
  8. When you get the BUILD SUCCEDED message, program the chip (Programmer – Program)
  9. Click ok the the ICDWarn0046 warning (unless you’ve got sick of this message and clicked the “don’t show me again” like I did)
  10. disconnect the ICD2 when the programmer has completed.

Here is the modification needed for this one to work. Shown here is the code between the #include directive and the start of the main() function.

You should see the very left LED blinking. When you press the right of the 4 buttons, the second from left LED should toggle (light up if it’s the first press). Keep pressing and releasing and observe what happens. You will notice the LED state doesn’t change until the button is released.

#include "pic24_all.h"

/** \file
A program that toggles an LED whenever a pushbutton switch is pressed
and released. Does not use a finite statement approach.
*/

#if defined(EXPLORER16_100P)

/// LED1
#define CONFIG_LED1() CONFIG_RA6_AS_DIG_OUTPUT()
#define LED1  _LATA6  //_LATA6 is port register for RA6
#warning Were using the explorer16 version of the code
/// Switch1 configuration
inline void CONFIG_SW1()  {
  CONFIG_RD13_AS_DIG_INPUT();  //use RD13 for switch input
  ENABLE_RD13_PULLUP(); //enable the pullup 
}
#define SW1 _RD13 //switch state

#else  // not Explorer16

/// LED1
#define CONFIG_LED1() CONFIG_RB14_AS_DIG_OUTPUT()
#define LED1  _LATB14  //led1 state
#warning If your using an Explorer16, this program wont work
/// Switch1 configuration
inline void CONFIG_SW1()  {
  CONFIG_RB13_AS_DIG_INPUT(); //use RB13 for switch input
  ENABLE_RB13_PULLUP(); //enable the pullup 
}
#define SW1 _RB13 //switch state

#endif  // if defined EXPLORER16_100P

#define SW1_PRESSED() SW1==0  //switch test
#define SW1_RELEASED()  SW1==1  //switch test

int main (void) {
.....

Example 7 which is Figure 8.30 on page 298 is called ledtoggle and is in the chap8 folder. It’s the same changes as example 6 above except the original code has some extra bits that example 6 didn’t have.

#include "pic24_all.h"

/** \file
A program that uses a finite state machine approach for
toggling an LED whenever a pushbutton switch is pressed
and released. Demonstrates the use of debounce delays when
polling a switch input.
*/

#if defined(EXPLORER16_100P)

/// LED1
#define CONFIG_LED1() CONFIG_RA6_AS_DIG_OUTPUT()
#define LED1  _LATA6    //_LATA6 is port register for RA6
#warning Were using the explorer16 version of the code

/// Switch1 configuration
inline void CONFIG_SW1()  {
  CONFIG_RD13_AS_DIG_INPUT();     //use RD13 for switch input
  ENABLE_RD13_PULLUP();           //enable the pullup
}
#define SW1              _RD13       //switch state

#else  // not Explorer16

/// LED1
#define CONFIG_LED1() CONFIG_RB14_AS_DIG_OUTPUT()
#define LED1  _LATB14     //led1 state
#warning If your using an Explorer16, this program wont work

/// Switch1 configuration
inline void CONFIG_SW1()  {
  CONFIG_RB13_AS_DIG_INPUT();     //use RB13 for switch input
  ENABLE_RB13_PULLUP();           //enable the pullup
}
#define SW1              _RB13       //switch state

#endif  // if defined EXPLORER16_100P

#define SW1_PRESSED()   SW1==0  //switch test
#define SW1_RELEASED()  SW1==1  //switch test

typedef enum  {
.....