Lab 3
Lab 3
• Program MCU
• avrdude.exe -c arduino -p m328p -P COM10 -b 19200 -v -v -v -v -U
flash:w:"$(ProjectDir)Debug\$(TargetName).hex ":i
• 8Hz internal crystal low fuses 0xf2
PREPARE ARDUINO AS ISP
PREPARE ARDUINO AS ISP
PREPARE ARDUINO AS ISP
DIGITAL READ
• PINx (Input Pins Address): Reads the state of the pins when they are configured as
inputs.
DIGITAL READ
• PINx (Input Pins Address): Reads the state of the pins when they are configured as
inputs.
DIGITAL READ
• PINx (Input Pins Address): Reads the state of the pins when they are configured as
inputs.
AVRDUDE COMMANDS
• Program MCU
• avrdude.exe -c arduino -p m328p -P COM10 -b 19200 -v -v -v -v -U
flash:w:"$(ProjectDir)Debug\$(TargetName).hex ":i
ATMEGA328P FUSE BITS OVERVIEW
• The ATmega328P has three sets of fuse bytes: Low Fuse Byte, High Fuse Byte, and Extended Fuse Byte.
Each byte consists of 8 bits, and each bit can be either programmed (0) or unprogrammed (1). The
configuration of these fuse bits determines the microcontroller's behavior. Below, we will explore the
different fuse bytes and their respective bits.
ATMEGA328P FUSE BITS OVERVIEW
These bits select the clock source for the microcontroller. The possible values are:
• 0000: External clock
• 0001: Low-frequency crystal
• 0010: Full-swing crystal oscillator
• 0011: Low-power crystal oscillator
• 0100: Internal RC oscillator (8 MHz)
• 0101: Internal RC oscillator (128 kHz)
• 0110: External RC oscillator
• 0111: Reserved
SUT1..0: STARTUP TIME
• These bits determine the delay time after the clock source is stabilized before the microcontroller starts
executing instructions. The values depend on the selected clock source. For instance:
• For a crystal oscillator:
• 00: 6 CK + 0 ms
• 01: 6 CK + 4.1 ms
• 10: 6 CK + 65 ms
• 11: Reserved
• If you set SUT1..0 to 01 with a crystal oscillator, the startup time will be 6 clock cycles plus 4.1 ms.
CKOUT: CLOCK OUTPUT
•0: Divide the clock by 8 (resulting in an 8 MHz internal clock running at 1 MHz).
•1: Do not divide the clock (the microcontroller runs at full clock speed).
EXAMPLE CONFIGURATION
• To configure the microcontroller to use an external 8 MHz crystal with a startup time of 65 ms and without
dividing the clock by 8, you can set:
• CKSEL3..0 = 0010
• SUT1..0 = 10
• CKDIV8 = 1
• The resulting Low Fuse Byte would be 0xC2 (11000010 in binary).
HIGH FUSE BYTE
• The High Fuse Byte controls features such as the bootloader, EEPROM preservation, and watchdog timer. The
default value is 0xD9. The bits are:
• BOOTRST: Boot Reset Vector
• BOOTSZ1..0: Boot Size
• EESAVE: EEPROM Save
• WDTON: Watchdog Timer Always On
• SPIEN: SPI Programming Enable
• DWEN: Debug Wire Enable
• RSTDISBL: External Reset Disable
BOOTLOADER
• BOOTRST: Boot Reset Vector
• 0: Start from the bootloader section after a reset.
• 1: Start from the main program after a reset.
• To configure the microcontroller to start from the main program, have a 512-word bootloader, preserve
EEPROM, and enable SPI programming, you can set:
• BOOTRST = 1
• BOOTSZ1..0 = 01
• EESAVE = 0
• SPIEN = 0
• The resulting High Fuse Byte would be 0xDA (11011010 in binary).
EXTENDED FUSE BYTE
• The Extended Fuse Byte primarily deals with brown-out detection settings. The default value is 0xFF. The bits
are:
• BODLEVEL2..0: Brown-out Detection Level
DDRx (Data Direction Register): Configures the data direction of the port pins. A '1' sets the pin
as an output, and a '0' sets it as an input.
PORTx (Data Register): When the pin is configured as an output, writing '1' to this register sets
the pin high, and '0' sets it low. When the pin is configured as an input, writing '1' enables the
pull-up resistor.
PINx (Input Pins Address): Reads the state of the pins when they are configured as inputs
UNDERSTANDING THE ATMEGA328P I/O PORTS
• DDRx (Data Direction Register): Configures the data direction of the port pins. A '1'
PIN DIRECTION SELECTION
sets the pin as an output, and a '0' sets it as an input.
• Example
• 00000001
• 00000010
• DDRx 0 0 0 1 0 0 0 0
Bit Shift (1<<1) 0 0 0 0 0 0 1 0
Set Bit DDRx | (1<<1) 0 0 0 1 0 0 1 0
Invert Bit Shifted ~(1<<1) 1 1 1 1 1 1 0 1
Clear Bit DDRx & ~(1<<1) 0 0 0 0 0 0 0 0
DIGITAL WIRTE
• PORTx (Data Register): When the pin is configured as an output, writing '1' to this
register sets the pin high, and '0' sets it low. When the pin is configured as an input,
writing '1' enables the pull-up resistor
EXAMPLE: BLINKING AN LED
• We'll start with a simple example of blinking an LED connected to pin PB0.
EXAMPLE: BLINKING AN LED
EXAMPLE: BLINKING AN LED
EXAMPLE: BLINKING AN LED
EXAMPLE: BLINKING AN LED
EXAMPLE: BLINKING AN LED
EXAMPLE: BLINKING AN LED
EXAMPLE: BLINKING AN LED
EXAMPLE: BLINKING AN LED
EXAMPLE: BLINKING AN LED
EXAMPLE: BLINKING AN LED
EXAMPLE: BLINKING AN LED
EXAMPLE: BLINKING AN LED
DIGITAL READ
• PINx (Input Pins Address): Reads the state of the pins when they are configured as
inputs.