Migrating to BoostC from mikroC

Thread Starter

T.Jackson

Joined Nov 22, 2011
328
I am looking for some help with migrating from mikroC to BoostC. If some experts out there can help me translate the following code portion below into BoostC I would really appreciate it.

Rich (BB code):
/*
 * PROJECT:
      DIY Keypad-based car engine security system ...
      Copyright ©2010 Trent Jackson all rights reserved

 * MCU:
      PIC16F628a @4MHz
*/
 
// :: Global scope variables :: //

unsigned short scan = 0;
unsigned short userCode[6];
unsigned short user[6];
unsigned short keyDown = 0;
unsigned short active = 1;
unsigned short tmrLED = 0;
unsigned short progMode = 0;
unsigned short codeCorrect = 0;
unsigned short hashPressed = 0;
unsigned short ukeyPointer = 0;
unsigned short Pinattempts = 0;
unsigned short i = 0;
unsigned short j = 0;

unsigned int timeout = 0;
unsigned int tmrIgnit = 0;

// :: Defines :: //

// Keypad rows ...
#define rowA PORTB.F0
#define rowB PORTB.F1
#define rowC PORTB.F2
#define rowD PORTB.F3

// Keypad columns
#define colA PORTB.F4
#define colB PORTB.F5
#define colC PORTB.F6

// LEDs ...
#define LEDa PORTA.F3
#define LEDb PORTB.F7

// Relays
#define relayA PORTA.F0
#define relayB PORTA.F1

// Ignition
#define ignit PORTA.F4

void interrupt()
{
  tmrLED++;
  tmrIgnit++;
  TMR0   = 96;
  INTCON = 0x20;
}

void flashLEDa()
{
   LEDa = ~LEDa;
}

void flashLEDb()
{
   LEDb = ~LEDb;
}

void playValidSND()
{
   for (i = 0; i < 5; i++)
   {
      Sound_Play(2000, 25);
      Delay_ms(100);
   }
}

void playErrorSND()
{
   for (i = 0; i < 5; i++)
   {
      Sound_Play(250, 50);
      Delay_ms(100);
   }
}

void rstUser()
{
   ukeyPointer = 0;
   codeCorrect = 0;
   hashPressed = 0;

   for (i = 0; i < 5; i++)
   {
      user = 255;
   }
}

unsigned short getKey()
{
/*
                      :: Multiplex 4x3 keypad function ::

   1. Cycle through rows and check for corresponding column being set ...
   2. Return the key if col is set (active high)
   3. Protocol implementation(s) / limitation(s):

                                    a. No simultaneous key sequences
                                    b. Key detect on release ...
                                    c. Very responsive with zero bounce
*/
      switch (scan)
      {
         case 0: // :: First col (1, 4, 7, *) :: //

            // Set cols
            colA = 1;
            colB = 0;
            colC = 0;

            // 5mS delay allow port to settle
            Delay_ms(5);

            // Check for button press ...
            if (rowA == 1)
            {
               return  1;
            }
            else if (rowB == 1)
            {
               return  4;
            }
            else if (rowC == 1)
            {
               return  7;
            }
            else if (rowD == 1)
            {
               return  54;
            }
            break;

         case 1:

            // Set cols
            colA = 0;
            colB = 1;
            colC = 0;

            // 5mS delay allow port to settle
            Delay_ms(5);

            // Check for button press ...
            if (rowA == 1)
            {
               return  2;
            }
            else if (rowB == 1)
            {
               return  5;
            }
            else if (rowC == 1)
            {
               return  8;
            }
            else if (rowD == 1)
            {
               return  0;
            }
            break;

         case 2:

            // Set cols
            colA = 0;
            colB = 0;
            colC = 1;

            // 5mS delay allow port to settle
            Delay_ms(5);

            // Check for button press ...
            if (rowA == 1)
            {
               return  3;
            }
            else if (rowB == 1)
            {
               return  6;
            }
            else if (rowC == 1)
            {
               return  9;
            }
            else if (rowD == 1)
            {
               return  55;
            }
            break;


      }

   // Inc to next col ...
   scan ++;

   // Reset after last row
   if (scan == 3)
   {
      scan = 0;
   }

   // Nothing pressed, 255 denotes this
   return 255;
}


Cont ...
 

Thread Starter

T.Jackson

Joined Nov 22, 2011
328
Rich (BB code):
void main()
{
  CMCON = 7;          // Disable analog comparators
  OPTION_REG = 0x84;  // Assign prescaler to TMR0
  TMR0  = 96;         // Timer0 initial value
  INTCON = 0xA0;      // Enable TMRO interrupt
  TRISA = 0x10;       // PortA as output ...
  PORTA = 0x00;       // Init port, all pins low
  TRISB = 0x0F;       // 4 inputs & 4 outputs

  // Using snd lib for short beep on key press
  //Sound_Init(&PORTA, 2);

  // Leds both off
  LEDa = 0;
  LEDb = 0;

  // Relays off
  relayA = 0;
  relayB = 0;

  // Cols off ...
  colA = 0;
  colB = 0;
  colC = 0;

  // Null user vars
  rstUser();
  
  // Fetch 4-digit code from EEPROM
  for (i = 0; i < 4; i++)
  {
     Delay_ms(50);
     userCode = EEPROM_Read(i);
  }

  // Load default code
  if(userCode[0] == 0xFF)
  {
     userCode[0] = 1;
     userCode[1] = 2;
     userCode[2] = 4;
     userCode[3] = 8;
  }

  // :: Infinite program loop :: //
  
  while(1)
  {
      // :: Armed flash red deterrent led @~2Hz :: //
      
      if (pinAttempts < 3)
      {
         if (active == 1)
         {
            if (tmrLED >= 100)
            {
               flashLEDa();
               tmrLED = 0;
            }
         }
         else // Not armed
         {
            if (progMode == 1)
            {
               if (tmrLED >= 100)
               {
                  flashLEDb();
                  tmrLED = 0;
               }
            }
            else if (ignit == 1)
            {
               LEDb = 1;
               relayA = 1;
               relayB = 1;
               tmrIgnit = 0;
            }
            else // Grn led + rlys off when no ignition
            {
               if (tmrIgnit == 1000)
               {
                  LEDb = 0;
                  relayA = 0;
                  relayB = 0;
                  tmrIgnit = 0;
               }
            }
         }
      
         // :: Fetch keys :: //
   
         if (keyDown != 1)
         {
            user[ukeyPointer] = getKey();

            if(user[ukeyPointer] != 255)
            {
               // Flag set ...
               keyDown = 1;
            
               // Play tone
               Sound_Play(888, 25);
            
               // 50mS debounce
               Delay_ms(50);
            
               // Inc pos in array
               ukeyPointer++;

               // Reset after 5 keys
               if (ukeyPointer == 5)
               {
                  ukeyPointer = 0;
               }
            }
         }
         else // Wait for key release
         {
            if (rowA == 0)
            {
               if (rowB == 0)
               {
                  if (rowC == 0)
                  {
                     if (rowD == 0)
                     {
                        // Flag off ...
                        keyDown = 0;
                     }
                  }
               }
            }
         }
      
         // :: See if user has entered in the right pin :: //
      
         if(user[0] == userCode[0])
         {
            if(user[1] == userCode[1])
            {
               if(user[2] == userCode[2])
               {
                  if(user[3] == userCode[3])
                  {
                     codeCorrect = 1;
                     pinAttempts = 0;
                  }
               }
            }
         }

         // :: Detect if hash key has been pressed :: //

         for (i = 0; i < 5; i++)
         {
            if (user == 55)
            {
               hashPressed = 1;
               break;
            }
         }
      
         // :: Hash to arm / disarm system :: //
      
         if (progMode == 0)
         {
            if (hashPressed == 1)
            {
               if (active == 1)
               {
                  if (codeCorrect == 1)
                  {
                     LEDa = 0;
                     active = 0;
                     playValidSND();
                  }
                  else
                  {
                     playErrorSND();
                     pinAttempts ++;
                  }
               }
               else // ARM if vehicle ignition is off
               {
                  if (ignit != 1)
                  {
                     LEDb = 0;
                     active = 1;
                     relayA = 0;
                     relayB = 0;
                  }
               }
               rstUser();
            }

            // :: Star key after entering in pin to set new pin :: //
      
            else if (user[4] == 54)
            {
               if (codeCorrect == 1)
               {
                  if (active == 0)
                  {
                     if (ignit == 1)
                     {
                        progMode = 1;
                     }
                  }
               }
               else
               {
                  pinAttempts ++;
               }
               rstUser();
            }
         }
         else  // :: User is programming in an new pin code :: //
         {
            if (ukeyPointer == 4)
            {
         
               // Save code to eeprom ...
               for (i = 0; i < 4; i++)
               {
                  userCode = user;
                  EEPROM_Write(i, userCode);
                  Delay_ms(20);
               }
            
               // Reset & bail
               progMode = 0;
               rstUser();
            }
         }
      }
      else // 3 incorrect pin attempts in a row (lock out system for a few mins)
      {
      
         if (timeout == 0)
         {
            LEDa = 0;
            LEDb = 0;
         }
         
         if (timeout < 50)
         {
            playErrorSND();
            flashLEDa();
            flashLEDb();
            timeout ++;
         }
         else
         {
            timeout = 0;
            pinAttempts = 0;
            LEDa = 0;
            LEDb = 0;
         }
      }
   }
}
 

thatoneguy

Joined Feb 19, 2009
6,359
Change your PORTA.3 to porta.3 (basically all ports are lowercase) for all your port aliases. All the port names and special names are lower case.

delay_ms() is lowercase, only accepts up to 255 mS delay, differs on capitalized first letter in Mikro

Unsure on EEPROM Read/Setup, that's a bit different, but the rest is ANSI C, so it ports over well.

The PDF Manual and examples that come with it cover just about everything, i2c, LCD, EEPROM, etc.

example:
Rich (BB code):
    //Configure port B
    trisb = 0x0F;
    //Configure port C
    trisc = 0x00;
    
    //Initialize port B
    portb = 0x00;
    //Initialize port C
    portc = 0x00;
    
    osccon=0x70;  // 8Mhz Internal Oscillator
    cm1con0 = 0x70; //disable comparators
    cm2con0 = 0x70; // both of em
    ansel=0x0; // Disable ADC
    anselh=0x0; // All 12 of them
 

thatoneguy

Joined Feb 19, 2009
6,359
post the code, though I can tell you ahead of time that most functions are the same, just with lower case, and the special function/port names are the same, just lower case

LCD and EEPROM commands are slightly different.

I do wish they'd come up with "standard" standard libraries for uC compilers.
 

Thread Starter

T.Jackson

Joined Nov 22, 2011
328
Any tips that you can offer with BoostC?

Can I time code in it like I can do with mikroC, which has a stopwatch with a uS readout?
 

thatoneguy

Joined Feb 19, 2009
6,359
There is a profiler in the debugger that lets you see what takes how many ticks after writing, as well as how much time each function takes, etc.

The time a function can take varies based on what the inputs are. For fastest running use unsigned char for values 0-255 instead of unsigned int.

Avoid using signed math anywhere (negative numbers) if you can avoid it. (declare everything unsigned, and as short as possible)

Don't do much inside interrupts other than set variables that will be worked on in the main loop, only do high priority stuff in the interrupt() routine.
 

Thread Starter

T.Jackson

Joined Nov 22, 2011
328
Thanks for the advice.

Question: I just changed all of my variables to unsigned char in my composite video program and did a time test. I am still getting the same times from point a to point b.
 

thatoneguy

Joined Feb 19, 2009
6,359
Compilers with optimization turned on will switch for you, but in the eval versions of some (HiTech is one), they disable all optimizations.

Boost C and Mikro C only disable advanced optimizations, they just put a code limit in. For the price, BoostC can't be beat at $50 for a full compiler with RealTimeOS libraries, which might be useful in your project, assign the redraw to the highest priority task, and in another task do your other stuff. Goto http://www.sourceboost.com/Products/NovoRtos/Overview.html for an idea. You get a limited version of the RTOS with the $49 Full/no limit license of the compiler (assuming you don't use it for commercial gain). If used for commercial gain, then the full price applies, which you should be able to afford if using it commercially.

Use a PIC that can have INTOSC PLL'd to 32 or 48Mhz, and you'd have plenty of power for a pong game and maybe even an NTSC color test at low res.
 

thatoneguy

Joined Feb 19, 2009
6,359
Not right now. It doesn't really offer much of anything over Boost C dollar for dollar.

Microchip has acquired them, so the new compiler coming out may be a good deal cheaper, they may be realizing they are losing a lot in the marketplace due to Arduino and actually make a decent compiler for low cost, but who knows.
 

Thread Starter

T.Jackson

Joined Nov 22, 2011
328
I came close to laying down a deposit on one that I saw on ebay. Two grand with a few hundred deposit would have put it on hold for me.

My gut told me no though.
 

thatoneguy

Joined Feb 19, 2009
6,359
There is a gcc for PICs, but it doesn't support many controllers, has limited libraries, and the rest of the issues that go with early projects.

gcc for AVR was built up by the huge user group the Arduino brought in. Hopefully, the PIC gcc will get more of a boost, it has at least on the 16/32/dsp side, just not the 8 bit side.

All the major companies that use PIC in the business have pretty much already purchased their compilers, so now it is down to hobbyists, which is a numerically large group, provided the price point is right. Boost C is at the limit of the price point for most, even the free version isn't very limited, not many people make projects that need over 2k of code, a good number of common PICs don't even have 2k of storage.

The simulator with plugin pack is very good, not ISIS level, but very good and accurate, meaning, if it doesn't work in the sim, it isn't going to work on the board.
 

Thread Starter

T.Jackson

Joined Nov 22, 2011
328
I agree that 2K is a lot if you half know what you're doing and have a valid project. Of course 2K of flash memory won't last long if all you're set out to do is display different texts on an LCD.
 

Thread Starter

T.Jackson

Joined Nov 22, 2011
328
How slow is it to read in data that is stored in flash and load it into an array that's in ram?

I figure that with my monochrome video project I will have at least 1K of memory remaining, so wondering about the possibility of storing some ascii characters in flash.
 

thatoneguy

Joined Feb 19, 2009
6,359
Reading Flash/Program Memory is very quick, basically standard clock speed to print out a block of data declared in code, move character to w, print w, repeat, so it will go fairly quickly. On Enhanced Midrange PIC16 series(16F1827, 16F690) and the 18F series, the process is single cycle, as the data doesn't need to traverse through w.

If you are storing it in EEPROM onbaord, it's a bit slower, but not crippling slow. Fetching data from on-chip EEPROM is single instruction after 5 instruction setup (8 instructions per word/2 bytes of EEPROM to RAM)

Writing back to program memory (PIC18 series and Enhanced Midrange PIC16 series) is slower. But if you wanted to change what the strings in firm coded memory were, it's possible.

Enhanced Mid Range PIC16 and PIC18 have:
• C Compiler Optimized Architecture
• Up to 8 Kbytes Linear Program Memory Addressing
• Linear Data Random Access Memory Addressing
• Interrupt Capability with Automatic Context Saving
• Direct, Indirect and Relative Addressing modes:
- Two full 16-bit File Select Registers (FSRs)
- FSRs can read program and data memory
 
Last edited:

thatoneguy

Joined Feb 19, 2009
6,359
I just copied and pasted from a datasheet

But that one is detailed in the next few.

Pointers allowed, "flat" memory access, etc. All are inherent methods of C programs to handle data, passing by reference, reading until NULL from pointer with destination RAM or another indirect pointer.

No excessive bank switching to read more data like the other PIC10/12/16 series, and w can contain a pointer to another memory address that is to be used, rather than needing to contain the value itself.
 

thatoneguy

Joined Feb 19, 2009
6,359
Three of the major hardware improvements/peripherals that weren't brought "back down" from PIC18 to PIC16E are the 8x8 hardware multiply, 12 bit ADC, or USB, though it's made up for by ECCP PWM, programmable Bandgap Vref, and the other features listed above.

Otherwise, the major differences between PIC18 and PIC16 were the "Sticking points" of memory access listed above, so Microchip added them and made the PIC16E Enhanced Mid Range Microcontroller. The PIC 16F1827 is a great example of an 18 pin PIC with more peripherals than pins. Great "general purpose" uC, as long as you don't need USB, it has everything but the kitchen sink built in. Sample some now, 3 for free through microchipdirect.com if you have a business, or are a dedicated hobbyist with a business name and address. Samples take about a month to arrive, and most are SMD, but there are through hole available on their newer releases, such as the 1827

The complete compare Checklist for PIC10 Baseline to PIC18 High performance 8 bit uCs, including what "Enhanced Mid-Range" is is on Page 3 of the Summer 2010 8 Bit Microcontroller Brochure
 
Last edited:
Top