AVR: Customizing - Paciente8159/uCNC GitHub Wiki

µCNC for AVR can be configured/customized to fit different AVR powered boards other than Arduino UNO

Jump to section

Customizing boardmap

µCNC for AVR can be customized either using the Web Config Tool or by manually modifing the source code files. For the latest method most configurations and pin assigning should be done in the corresponding boardmap file inside the uCNC/src/hal/boards/avr/ directory and then the respective board file.

Taking the boardmap_uno.h file inside src\hal\boards\avr folder, has an example lets walk through each section of the file.

Configure IO pin

This is similar to the general customization instructions

Input pin options

Weak input pull-ups

All input pins can have a weak pull-up activated to drive them high if unconnected. This is similar to the general customization instructions

External interrupt feature

All pins all repeatedly read by a soft polling routine. But for special function pins an interrupt driven event to force a reading and respective action can be also activated. This causes the response to be immediate and not depend on the pin reading routine cycle. AVR has two types of input interrupts. The interrupt on change feature that can be configure up to 3 ports and also an external interrupt feature assigned to several pins on different ports (check the MCU datasheet for specifications). For Arduino UNO the pin mapping can be checked here.

Arduino UNO pin mapping

For Arduino MEGA the pin mapping can be checked here.

Arduino Mega pin mapping

Interrupt on change feature

  1. The interrupt on change feature can be configured up to 3 ports on AVR (PCINT0-7, PCINT8-15 and PCINT16-23). For this you need to map in the HAL the PCINT#_PORT (# from 0-2) to the respective port. PCINT0_PORT handles (PCINT0-7), PCINT1_PORT handles (PCINT8-15) and PCINT2_PORT handles (PCINT16-23). For example:
//On Arduino UNO port B has PCINT0-7 interrupt on change feature so HAL's PCINT0_PORT must be correctly mapped to this port
#define PCINT0_PORT B
  1. Now you can activate the interrupt on change feature on any pin of that port.
//activate interrupt on change feature on LIMIT_X that is tight to IO pin B3
#define LIMIT_X_ISR 0 //PCINT<0> ISR

External interrupt feature

To activate an INT# interrupt on an input pin just define the ISR to be the negative value of the (# + 1). For example (from boardmap file for RAMPS) to active INT5 interrupt feature available on pin E5 that is assigned to LIMIT_X just declare:

//activate external interrupt feature on LIMIT_X that is tight to IO pin E5
#define LIMIT_X_ISR -6 //INT<5> ISR => -(5 + 1)

Configure communications

This is similar to the general customization instructions

Configure pwm clock

To configure the pwm clock the used OCR# register and timer must be supplied. To find the used OCR# register and timer we must check the MCU datasheet. Looking at the pin mapping shown above for Arduino UNO and knowing that Grbl uses pin 11 (pin B3) has the spindle pwm control we can see that the pwm on that pin is generated via OC2A which in turn translates to Timer 2 OCR register A.

#define PWM0_OCR A
#define PWM0_TIMER 2

Just a quick note. Later the PWM0 is assigned to SPINDLE in the config.h but this the side of the code connection to the HAL.

Configure analog channel

Although not used anywhere inside µCNC reading analog pins is possible. Beside configuring the pin like an input pin, one more definition is needed to configure the AVR analog reading. That definition is the channel of the internal ADC for the conversion. For example on Arduino UNO the A4 (Analog channel 4) is on pin C4. To configure it as µCNC's ANALOG0 input add the following code to the boardmap file:

#define ANALOG0_BIT 4
#define ANALOG0_PORT C
#define ANALOG0_CHANNEL 4 //AVR channel 4

To make a read just do

uint8_t value = mcu_get_analog(ANALOG0);

Configure step generator timer

This is similar to the general customization instructions

Configure servo pins

This is similar to the general customization instructions

⚠️ **GitHub.com Fallback** ⚠️