Xpress board: Obstacle avoiding robot with XC8 code


XC8 code for the obstacle avoiding robot

The code for the robot is written using MPLAB Xpress IDE and XC8 compiler, with the PIC16F18855 being configured using Microchip Code Configurator. Then, the main code relies on functions generated by MCC.

This article is more than two years old and might contain obsolete information; it is still kept here for informational purposes.

The following configuration settings were done using Microchip Code Configurator:

  • Clock source HFINTOSC, clock frequency 4MHz, clock divider set to 1. This leads to a main clock frequency of 4MHZ.
  • Timer4 is used for PWM6, Timer6 is used for PWM7. Note that these two timers are identical, so we make the same settings for both timers
  • Timer settings:
    • Clock source FOSC/4
    • Prescaler 1:1
    • Postscaler 1:1
    • Period 1:250 us
    • Ext reset source T4CKIPPS pin
  • PWM settings
    • PWM duty 0%
    • PWM polarity: active high
    • PWM resolution is 9 bits given the current main clock / timer settings
  • A/D convertor is set to operate in basic mode
    • Clock source is FRC
    • Alignment right
    • Positive reference Vdd
    • Negative reference Vss
    • “Enable continuous operation” is unchecked
  • In pin module
    • PIN RB0 is set as ANx (analog input)
    • Pins RC5 and RC7 are set as outputs (they will control the direction of the motors)
    • Pins RC2 and RC4 are set as PWM

Below there are some screen captures from MCC:


Please observe that PWM resolution is 9 bits, so valid settings for the duty cycle are between 0x00 and 0x01FF.

The code

Main code is as follows:

Note: if the robot first moves backwards you just have to reverse the output pins on the DRV8835.

As usual, the complete MPLABX package can be downloaded here.

And.. action!


1 2 3

Comments are closed.