Pic Firmware

Forums:

Some questions concerning the pic firmware ..

Compilation on ubuntu works fine although it seems to be a windows
build because of a missing path in the <configurations.xml>:

<property key="tracecontrol.tracefile" value="C:\FRL\FRL Projects\NaffBorg\servo_and_usm\UltraBorg.X\defmplabxtrace.log"/>

Why is in the MPLABX Project
the PIC16F1828
choosen?

Is the code for a PIC16F1824 exactly the same?

Is the HEX File you push on the chip
done with the optimized XC8 compiler version?

Because with the free XC8 they say
"you could have produced up to 60% smaller and 400% faster code."

thanks for some info.
Bruce

piborg's picture

You are correct, the final production build was done on a Windows machine.

The PIC fitted to the UltraBorg is a PIC16F1828, not a PIC16F1824.
The two PICs are basically the same, however the PIC16F1824 only has 14 pins, the PIC16F1828 has 20.
The extra pins provide some additional GPIO pins, which we needed for the UltraBorg design.

The builds have been performed with the free compiler.
We decided to do this so anyone who wants to modify the code can start from exactly the same build as we have.

On your UltraBorg Specification page you write about a PIC16F1824-I/SL
circuit. So possibly this needs an update.

Cheers.

piborg's picture

Ah, that is a mistake, thanks for letting us know :)

We use the PIC16F1824-I/SL on the PicoBorg Reverse rather than the UltraBorg.

I have corrected the UltraBorg specification page to list the correct PIC now.

Hello and thanks for the clarification,

now that I study the PicCode some questions rise about it.

You write that there is 30Hz/60Hz/120Hz Servo operation possible but preconfigured to 30Hz.

I can not directly find where in the PIC code these operation modes are configured.

SERVO_PULSE_CLOCK and SET_SERVO_SCLOCK seem the closest matches but
it does not pop up in my mind how these could be used to switch to i.e. 120Hz.

No where I cand find Hz.

Also the Ultrasound connectors fire continuously right?
There is no command to disable one connector for a while. Or is there?

So how or where would the servo frequency possibly be changed?

Thanks for some advice.

Greets
Andreas

piborg's picture

Changing the servo frequency involves a few code changes.
I will go through the changes needed below, they are not too obvious.

You are correct, the ultrasonic sensors are all triggered at a regular interval.
There is no command to disable any of the connectors.

The triggering and reading of the ultrasonic sensors is all handled by the ReadAllUltrasonics function in
main.c.

Changing the servo frequency

The servo output is generated from a clock signal output by the PIC.
This signal is linked to the internal clock.
To change it we need to alter a few places in the code.

The main change is to the ConfigureOscillator function in system.c.
We need the clock to be at one of these speeds:

  • 8 MHz → 30 Hz servo operation
    void ConfigureOscillator(void) {
    	// 8 MHz
    	OSCCONbits.SCS = 0b10;	    // Set the clock to the internal oscillator
        OSCCONbits.IRCF = 0b1110;	// Select the 8 MHz postscaler
        OSCCONbits.SPLLEN = 0;		// Disable the 4x PLL (overriden in CONFIG2 by PLLEN = ON)
    }
    	
  • 16 MHz → 60 Hz servo operation
    void ConfigureOscillator(void) {
    	// 16 MHz
    	OSCCONbits.SCS = 0b10;	    // Set the clock to the internal oscillator
        OSCCONbits.IRCF = 0b1111;	// Select the 16 MHz postscaler
        OSCCONbits.SPLLEN = 0;		// Disable the 4x PLL (overriden in CONFIG2 by PLLEN = ON)
    }
    	
  • 32 MHz → 120 Hz servo operation
    void ConfigureOscillator(void) {
    	// 32 MHz
    	OSCCONbits.SCS = 0b10;	    // Set the clock to the internal oscillator
        OSCCONbits.IRCF = 0b1110;	// Select the 8 MHz postscaler
        OSCCONbits.SPLLEN = 1;		// Enable the 4x PLL (overriden in CONFIG2 by PLLEN = ON)
    }
    	

We also change the PLLEN setting in configuration_bits.c.

  • 8 or 16 MHz → 30 or 60 Hz servo operation
    #pragma config PLLEN = OFF // PLL Disable (4x PLL disabled)
  • 32 MHz → 120 Hz servo operation
    #pragma config PLLEN = ON // PLL Enable (4x PLL enabled)

At this point the servos will operate at the correct speed, however the ultrasonic reading rate may be too fast.
We can fix this by changing the main function in main.c slightly.

First add a counter variable inside main:

void main(void) {
	int usmTriggerCount = 0;

	// Configure the oscillator for the device
	ConfigureOscillator();

Now modify the ultrasonic reading section to increase and check this counter:

        // Get the ultrasonic readings
		if (PIR3bits.TMR4IF) {
			// Clear the interrupt flag
			PIR3bits.TMR4IF = 0;

			++usmTriggerCount;
			if (usmTriggerCount >= 1) {
				// Read all of the ultrasonics and trigger the next set of readings
				ReadAllUltrasonics();
				usmTriggerCount = 0;
			}
		}

Finally you will need to change the counter limit depending on the servo frequency:

  • 8 MHz → 30 Hz servo operation
    if (usmTriggerCount >= 1) {
  • 16 MHz → 60 Hz servo operation
    if (usmTriggerCount >= 2) {
  • 32 MHz → 120 Hz servo operation
    if (usmTriggerCount >= 4) {

Is it possible to reach 20KHz PWM frequency by reprogramming the PIC, or is it even possible after board initialisation to modify the frequency parameter through python (for driving a BLDC motor with integrated ESC)

piborg's picture

Unfortunately no. While the code could be changed to get higher frequencies, 20 KHz is beyond what UltraBorg would be capable of.

Subscribe to Comments for &quot;Pic Firmware&quot;