0% found this document useful (0 votes)
12 views51 pages

Lab 3

Biomedical engineering Embedded system
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views51 pages

Lab 3

Biomedical engineering Embedded system
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

LAB 2 GETTING STARTED AND DIGITAL OUTPUT

EMBEDDED SYSTEMS AND INTERFACING LAB

BY: ENG . MOHAMMED ABDALNASSER ALZAGHIR


773215130
771311599
COMMANDS
• Check MCU connection
• avrdude.exe -c arduino -P COM10 -b 19200 -p m328p

• Set MCU Fuses


• avrdude.exe -c arduino -p m328p -P COM10 -b 19200 -U lfuse:w:0xff:m -U
hfuse:w:0xDF:m -U efuse:w:0xF9:m

• 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

• Check MCU connection


• avrdude.exe -c arduino -P COM10 -b 19200 -p m328p

• Set MCU Fuses


• avrdude.exe -c arduino -p m328p -P COM10 -b 19200 -U lfuse:w:0xff:m -U hfuse:w:0xDF:m -U
efuse:w:0xF9:m

• 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

• Low Fuse Byte


• The Low Fuse Byte primarily controls the clock source and startup time. The default value for this byte is 0x62.
The bits are:
• CKSEL3..0: Clock Source Selection
• SUT1..0: Startup Time
• CKOUT: Clock Output
• CKDIV8: Divide Clock by 8
• CKBODLEVEL: Brown-out Detection Level
CKSEL3..0: CLOCK SOURCE SELECTION

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: Output the system clock on the CLKO pin.


•1: Disable clock output.
CKDIV8: DIVIDE CLOCK BY 8

•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.

• BOOTSZ1..0: Boot Size


• These bits define the size of the bootloader section:
• 00: 256 words
• 01: 512 words
• 10: 1024 words
• 11: 2048 words
• For instance, setting BOOTSZ1..0 to 10 means a bootloader size of 1024 words.
• WDTON: Watchdog Timer Always On
• 0: Watchdog Timer is always on.
• 1: Watchdog Timer is under software control.
• SPIEN: SPI Programming Enable
• 0: Enable SPI programming.
• 1: Disable SPI programming.
EXAMPLE CONFIGURATION

• 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

• BODLEVEL2..0: Brown-out Detection Level


• These bits set the voltage level for brown-out detection:
• 000: 4.3V
• 001: 2.7V
• 010: 1.8V
• 111: Disabled
• For example, setting BODLEVEL2..0 to 001 will enable brown-out detection at 2.7V.
EXAMPLE CONFIGURATION

• To set the brown-out detection level to 2.7V, you can set:


• BODLEVEL2..0 = 001
• The resulting Extended Fuse Byte would be 0xFD (11111101 in binary).
PRACTICAL EXAMPLES
• Example 1: Configuring for an External 16 MHz • High Fuse Byte:
Crystal • BOOTRST = 1 (Start from main program)
• Let's configure the ATmega328P to use an external • BOOTSZ1..0 = 10 (1024 words)
16 MHz crystal with a startup time of 65 ms, no clock
• EESAVE = 0 (Preserve EEPROM)
division, start from the main program, a 1024-word
bootloader, preserve EEPROM during chip erase, • SPIEN = 0 (Enable SPI programming)
enable SPI programming, and a brown-out detection • Result: 0xDA
level of 2.7V.
• Extended Fuse Byte:
• Low Fuse Byte:
• BODLEVEL2..0 = 001 (2.7V)
• CKSEL3..0 = 1111 (16 MHz full-swing crystal)
• Result: 0xFD
• SUT1..0 = 10 (65 ms)
• CKDIV8 = 1 (No division)
• Result: 0xFF
PRACTICAL EXAMPLES
• Example 1: Configuring for an External 16 MHz • High Fuse Byte:
Crystal • BOOTRST = 1 (Start from main program)
• Let's configure the ATmega328P to use an external • BOOTSZ1..0 = 10 (1024 words)
16 MHz crystal with a startup time of 65 ms, no clock
• EESAVE = 0 (Preserve EEPROM)
division, start from the main program, a 1024-word
bootloader, preserve EEPROM during chip erase, • SPIEN = 0 (Enable SPI programming)
enable SPI programming, and a brown-out detection • Result: 0xDA
level of 2.7V.
• Extended Fuse Byte:
• Low Fuse Byte:
• BODLEVEL2..0 = 001 (2.7V)
• CKSEL3..0 = 1111 (16 MHz full-swing crystal)
• Result: 0xFD
• SUT1..0 = 10 (65 ms)
• CKDIV8 = 1 (No division)
• Result: 0xFF
UNDERSTANDING THE ATMEGA328P I/O PORTS
The ATmega328P has three main I/O ports: PORTB, PORTC, and PORTD. Each port is associated
with three registers:

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

• DDRB0 as output pin is DDRB |= (1 << PORTB1) ; PORTB1 is equal to 1

• 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.

You might also like