Posts Tagged “C30”

Continuing with Chapter 8, the next file is the one referred to in Figure 8:5 Improved code example for flashing LED. It takes the previous one and modifies it using macro’s. Well we sort of used macros in the last one to break out the Explorer 16 version and the original. This file is called “ledflash” and it’s also in the chapter 8 folder.

You will see what the author had in mind. The use of macro’s certainly makes it easier to see what is going on. Here is the modified code that enables the Explorer 16 to run this. Don’t forget to define EXPLORER16_100P.

#if defined(EXPLORER16_100P)
#define CONFIG_LED1() CONFIG_RA6_AS_DIG_OUTPUT()
#define LED1 _LATA6 //_LATA6 is port register for RA6
#warning Were using the explorer16
#else // not Explorer16
#define CONFIG_LED1() CONFIG_RB15_AS_DIG_OD_OUTPUT()
#define LED1 _LATB15 //_LATB15 is port register for RB15
#warning Were not using the explorer16 because EXPLORER16_100P not defined
#endif // if defined EXPLORER16_100P
int main(void) {
  configClock();
  /********** GPIO config **********/
  CONFIG_LED1();
  LED1 = 0;
  while (1) {
    DELAY_MS(250); //delay long enough to see LED blink
    LED1 = !LED1; // Toggle LED
  } // end while (1)
}

This time all the configuration changes needed are done in the short macro section while leaving the actual code identical effectively creating a hardware abstraction layer between the code and the hardware.

Ok so now time to build. Here is a check list to follow for these examples.

  1. Define EXPLORER16_100P (Project – Build Options – Project. Then the MPLAB C30 tab. Add the preprocessor macro.
  2. Remove the linker script from the workspace files viewer under “Linker Scripts” (dont add anything, MPLAB will manage it for you)
  3. Change from DEBUG to RELEASE in the drop down at top of MPLAB screen
  4. Select your programmer (Programmer – Select Programmer). I use the ICD2
  5. CTRL-F10 to build all
  6. When you get the BUILD SUCCEDED message, program the chip (Programmer – Program)
  7. Click ok the the ICDWarn0046 warning
  8. disconnect the ICD2 when the programmer has completed.

You should now see the LED connected to RA6 (second from left) flash at about 1 per second.

One thing I do want to point out, I am not going to teach you about programming and the PIC etc. The intent of this blog is share my adventures and if you happen to learn something from that, well that’s good. The other thing I’m going to recommend is to buy the book referred to here. See the books web site here. This is also the web site where you will find all the original code the book discusses and what I refer to here.

Comments No Comments »

Well I’m going to run with the PIC24 for the time being and use the Microcontrollers – From Assembly Language to C Using the PIC24 Family book, but jumping to chapter 8 as the assembly language is not what I want at the moment. It’s learning about C and the PIC24. Anyway, as I’m using the Explorer 16 board, I’m going to share the differences in the sample code that should apply. This is the first installment for chapter 8.

My first observation is the author hasn’t identified the file names of the examples in his book and as his book doesn’t include the full text of the samples, it’s a bit of guess work to find them. So I’ll help you along your way. The first example on page 258 is called “ledflash_nomacros” and it’s in the chap8 folder.

One thing you need to do with all the examples when using the Explorer16 board is define the macro EXPLORER16_100P either in the include/pic24_all.h file or it must be added to the MPLAB project (Use Project->Build Options-> Project, click on the MPLAB C30 tab, and in Macro Definitions click ‘Add’, and add EXPLORER16_100P). Where it makes sense, I’m going to use the macro definition to keep the original code in tact while adding specific changes for the Explorer 16 board.

Note: the PIM I’m using is the PIC24FJ128GA010 100pin PIM (Microchip part number MA240011).

So here is the source for main() for this exercise. The rest of the file should remain the same.

void a_delay(void) {
  uint16 u16_i,u16_k;
  // change count values to alter delay
  for (u16_k=1800; --u16_k;) {
    for (u16_i = 1200 ; --u16_i ; );
  }
}
int main(void) {
  configClock();    //clock configuration
  /********** GPIO config **********/
#if defined(EXPLORER16_100P)
// For the explorer 16, we're not going to use the open drain as the LED is
// wired from ground through a current limit resistor direct to pin 91 of
// the PIC24FJ128GA010
  _TRISA6 = 0;         //Config RB15 as output
  _LATA6 = 0;          //RB15 initially low
  while (1) {           //infinite while loop
    a_delay();          //call delay function
    _LATA6 = !_LATA6;  //Toggle LED attached to RB15
  } // end while (1)
#warning using Explorer16 code
#else // not EXPLORER16_100P
#ifdef _ODB15          //PIC24F CPU header files define this instead of ODCB15
  _ODB15 = 1;          //enable open drain
#else
  _ODCB15 = 1;          //enable open drain
#endif
  _TRISB15 = 0;         //Config RB15 as output
  _LATB15 = 0;          //RB15 initially low
  while (1) {           //infinite while loop
    a_delay();          //call delay function
    _LATB15 = !_LATB15;  //Toggle LED attached to RB15
  } // end while (1)
#warning NOT using Explorer16 code
#endif  //#if defined(EXPLORER16_100P)
}

When you open the project file (.mcp) for this exercise you will notice the linker script in the linker scripts section of the files explorer. You need to delete this one. No need to add another one as MPLAB will pick up the right one automatically if you have used the default install.

You will notice in this code segment I’ve added a couple of warning messages for the compiler to see what parts of the code it compiles. You can have them in or take them out. They are there just to make sure the compiler is doing what is expected.

Now if you try and use DEBUG as per the default project, you will see a number of failures and the program wont run. The first will be a pop up message:

  • ICDWarn0046:  Because clock switching is enabled, MPLAB ICD 2 requires the user to cycle target power after a program operation.

And then a message in the output window:

  • ICD0083: Debug:  Unable to enter debug mode.  Please double click this message for more information.

I chose to ignore and change to a RELEASE build to see it working. Do this by following these steps:

  1. Change the DEBUG drop down to RELEASE
  2. Select a programmer (Programmer – Select Programmer). I’m using the MPLAB ICD2 so the rest talks about what happens with the ICD2.
  3. Rebuild all (Ctrl+F10)
  4. When you get the BUILD SUCCEEDED message, program the device (Programmer – Program). You will get the ICDWarn0046 message, just click OK.
  5. When you get the “MPLAB ICD 2 ready for next operation”, disconnect the ICD2 and watch your program run. The LED second from left will flash at about once per second.

Comments No Comments »