5. ATmega168's EEPROM Addresses
This section details the mega168 settings that can be saved to EEPROM, the format of those settings, and the addresses to which they should be written. When writing to EEPROM, remember that an EEPROM memory address can be written thousands of times, but not infinitely many. Take great care when performing an EEPROM write from within a loop.
0: ADDR_INIT_CHECK — clear this byte and then reset the mega168 to restore it to factory default settings.
1: ADDR_M1_PWM_FREQUENCY — frequency + resolution of motor 1 PWM. Bits 7 – 3 are unused. Bit 2: set = 8-bit resolution, cleared = 7-bit resolution. Bits 1 & 0 enumerate the PWM clock’s prescaler value: 0 = prescaler 8, 1 = prescaler 64, 2 = prescaler 256, 3 = prescaler 1024. The frequency is calculated as 20 MHz / prescaler / 2bit-resolution and can be: 19.5 kHz, 9.77 kHz, 2.44 kHz, 1.22 kHz, 610 Hz, 305 Hz, 153 Hz, and 76.3 Hz. The default value for this byte is 4, which corresponds do a frequency of 9.77 kHz.
2: ADDR_M2_PWM_FREQUENCY — frequency + resolution of motor 2 PWM. Default value is 4.
3: ADDR_M1_CURRENT_SENSE — number of M1 ADC samples to average. The mega168 retains this many of the most recent ADC samples and keeps a running average of these values. This parameter must be a power of two and cannot exceed 128. Default value is 128.
4: ADDR_M2_CURRENT_SENSE — number of M2 ADC samples to average. This must be a power of two and cannot exceed 128. Default value is 128.
5: ADDR_M1_CURRENT_LIMIT — motor 1 current limit (0 = no limit). Default value is 0.
6: ADDR_M1_CL_P_CONST — motor 1 proportional current control constant P (7-bit value). If for a given motor the current exceeds the current limit (and the limit is not zero), one of the following will happen: (1) if P is zero, the motor will be shut off, or (2) if P is not zero, the pwm for that motor is adjusted according to PWM = PWM – P * (current – current limit). A P of zero will also cause the motor to shut off if it experiences a motor fault (otherwise the motor will attempt to recover from a fault). Default value is 5. This parameter must not be greater than 127.
7: ADDR_M2_CURRENT_LIMIT — motor 2 current limit (0 = no limit).
8: ADDR_M2_CL_P_CONST — motor 2 proportional current control constant P (7-bit value). Default value is 5. This parameter must not be greater than 127.
9: ADDR_M1_ACCELERATION — motor 1 acceleration; 0 = instantaneous (7-bit value). If a motor is accelerating, a tenth of this value will be added to it’s PWM every 10 ms (or, effectively, the PWM will increase by this much every 100 ms). Default value is 25.
10: ADDR_M2_ACCELERATION — motor 2 acceleration; 0 = instantaneous (7-bit value). Default value is 25.
11: ADDR_M1_BRAKE_DURATION — time (in 10 ms) motor 1 spends braking at full duty cycle on change of motor direction when issued an acceleration command (7-bit value). Default value is 10 (i.e. brake for 100 ms).
12: ADDR_M2_BRAKE_DURATION — time (in 10 ms) motor 2 spends braking at full duty cycle on change of motor direction when issued an acceleration command (7-bit value). Default value is 10.
13: ADDR_SERIAL_SETTINGS —
- bit 7: unused
- bit 6: normal UART functionality if cleared, enables permanent program mode (and overrides all other serial settings) if set
- bits 5&4: parity (0 = no parity, 1 = reserved, 2 = even parity, 3 = odd parity)
- bit 3: use one stop bit if cleared, two if set
- bit 2: unused
- bit 1: run at normal speed if cleared, at double speed if set
- bit 0: unused
Default value is 0 (normal UART functionality, no parity, one stop bit, normal speed). Character size is always 8-bit and cannot be set to anything else.
14: ADDR_SERIAL_UBRRH — UBRRH register (determines baud). Default value 0. UBRRH is a 3-bit value that combines with the 8-bit UBRRL to create an 11-bit UBRR value.
in normal mode:
baud = ( 20 MHz / 16 ) / ( UBRR + 1 ) = 1.25 MHz / ( UBRR + 1 ), so
UBRR = ( 1.25 MHz / baud ) – 1
in double-speed mode:
baud = ( 20 MHz / 8 ) / ( UBRR + 1 ) = 2.5 MHz / ( UBRR + 1 ), so
UBRR = ( 2.5 MHz / baud ) – 1
15: ADDR_SERIAL_UBRRL — UBRRL register (determines baud). Default value 10 (UBRR = 10 results in 115.2k baud with -1.4% error).
16: ADDR_SERIAL_READ_READY — the number of bytes the UART read buffer has when the mega168 sets the read-ready status flag. The parameter must be in the range of 1 – 32. If you set it to 32 you won’t get a read-ready flag until the read buffer is full. If you set it to 1, you will get a read-ready flag every time the mega168 receives a byte over the UART. Default value is 1.
17: ADDR_BUZZER_VOLUME — buzzer volume (must be a 4-bit value). The buzzer pwm duty cycle is set by generating a compare match at TOP >> (16 – volume). Therefore a volume of 15 results in a 50% duty cycle and a volume of 5 results in a .05% duty cycle. The volume control is somewhat crude, but it still lets you make the buzzer quieter if so desired. Volume must be in the range of 0 – 15. Default value is 12.
18: ADDR_STARTUP_MELODY — this parameter determines the melody that plays on startup/reset: 0 – 7 = melody of that number, 8 = silence (make no sound on startup), else chirp. Default value is 255 (chirp).
19: ADDR_NOTE_GAP — default duration in ms of silent pause inserted after each note (7-bit value). Default value is 5.
20: ADDR_SCK_DURATION — programmer SPI SCK setting (0 is fastest). This determines how quickly you can upload a hex file from the mega168 to the mega644 while programming. This parameter can be changed setting the ISP frequency in Main tab of AVR Studio’s AVRISP dialog window. Default value is 0.
21: ADDR_ISP_STATE — mega168 ISP state (in progmode or not). This is used internally by the mega168 to determine if the mega644 has potentially corrupted flash. If the ISP state ever disagrees with what the mega168 thinks it should be, the mega168 concludes something bad has happened during programming, such as a loss of power, and will not allow the mega644 to run. Instead it will alternately flash the red and yellow LEDs below the reset button until the mega644 is reprogrammed. You should never write to this EEPROM address.
22: ADDR_ISP_SW_MINOR — AVRISP software version major byte. Default value is 0x0A.
23: ADDR_ISP_SW_MAJOR — AVRISP software version minor byte. Default version is 0x02.
At the time of the X2’s release, the current AVR Studio AVRISP version is 2.0A. These bytes represent the version AVRISP version number the mega168 will transmit to AVR Studio when you attempt to program the mega644. If the two version don’t match, you will have to first cancel out of an “upgrade your firmware?” dialog box every time you try to program your mega644 (you cannot upgrade your programming firmware in this way, so this dialog is nothing more than an annoyance). As such, you might have a better programming experience if you set these version values to match the AVRISP version used by the AVR Studio you’re running.
24: ADDR_MELODY_START_PTR_MSBS — the mega168’s EEPROM is can be addressed by 9-bit values (512 bytes); this byte stores the ninth bits of the melody pointers according to the formula: bit i = bit 9 of melody i+1. Bit 9 of “melody 8” is really bit 9 of ADDR_MELODY7_END_PTR (melody 7 termination pointer).
25: ADDR_MELODY1_START_PTR — address of low byte of pointer to melody 1 start.
26: ADDR_MELODY2_START_PTR — address of low byte of pointer to melody 2 start.
27: ADDR_MELODY3_START_PTR — address of low byte of pointer to melody 3 start.
28: ADDR_MELODY4_START_PTR — address of low byte of pointer to melody 4 start.
29: ADDR_MELODY5_START_PTR — address of low byte of pointer to melody 5 start.
30: ADDR_MELODY6_START_PTR — address of low byte of pointer to melody 6 start.
31: ADDR_MELODY7_START_PTR — address of low byte of pointer to melody 7 start.
32: ADDR_MELODY7_END_PTR — address of low byte of pointer to the end of melody 7.
33: ADDR_MELODY0_START — address of 1st note of melody 0.
There is room in EEPROM for 159 notes. Notes are stored sequentially in three-byte blocks starting at EEPROM address 33.