-
Notifications
You must be signed in to change notification settings - Fork 65
AVR: Customizing
µCNC for AVR can be configured/customized to fit different AVR powered boards other than Arduino UNO
Jump to section
µ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.
This is similar to the general customization instructions
All input pins can have a weak pull-up activated to drive them high if unconnected. This is similar to the general customization instructions
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.
For Arduino MEGA the pin mapping can be checked here.
- 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
- 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
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)
This is similar to the general customization instructions
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.
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);
This is similar to the general customization instructions
This is similar to the general customization instructions
µCNC is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. µCNC is distributed WITHOUT ANY WARRANTY.
Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
µCNC Wiki
- Home
- Basic user guide
- Porting µCNC and adding custom HAL
- Customizing the HAL file
- Adding custom Tools and Modules to µCNC
- FAQ
µCNC for ALL MCU
µCNC for AVR
µCNC for STM32F1 and STM32F4
µCNC for SAMD21
µCNC for ESP8266
µCNC for ESP32
µCNC for NXP LPC176x
µCNC for NXP RP2040