Sometimes in an Arduino project, especially projects tied to hardware, you’ll see a line accessing a variable that isn’t even declared anywhere in the code.
TCCR5A = 42;
Like…who is TCCR5A
? Nobody’s declared it anywhere, yet the program compiles fine (for certain boards). Meanwhile, I miss one variable declaration in my code and the compiler throws absolute fits! What’s going on?
A microcontroller doesn’t just run programs, it also talks to hardware like buttons, LEDs, weather sensors and small displays. So alongside the CPU, which does run programs, there sit additional silicon circuits called peripherals to assist with those other tasks.
The CPU needs a way to exchange data with those peripherals…read a button, or turn on an LED. To do this, peripherals are memory-mapped. They appear to the CPU just like part of main memory, like variables or arrays, but behind the scenes are actually connected to these peripheral functions and not RAM at all.
So, somewhere off in a header file that the Arduino compiler includes automatically, all of those peripheral registers are declared and assigned absolute addresses as if in memory. If you read from or write to those “variables,” you’re actually talking directly to the hardware!
TCCR5A
in this case is a timer control register, specific to some AVR microcontrollers…the ATmega1280 and ATmega2560 seen on Arduino Mega boards. This register’s used to set up timekeeping or PWM on certain pins.
And that’s the thing…these registers are extremely device-specific; you won’t find TCCR5A
on other chips. There may be some overlap within a few related chips of a family, but by and large this does not work when moving code between unrelated devices. If you see Arduino code doing this, it’s really getting into some low-level sorcery.
We can only scratch the surface of the topic here, just wanting to explain what these mystery variables are about. This guide about the TiCoServo library explains a bit further. If you get deep into the hardware side of things, understanding peripherals and navigating chip datasheets becomes a vital skill.
Text editor powered by tinymce.