0% found this document useful (0 votes)
37 views18 pages

L01 Embedded 22

Here are the answers to the review questions: 1. Unsigned char: 0 to 255. Signed char: -128 to 127. 2. Unsigned int: 0 to 65,535. Signed int: -32,768 to 32,767. 3. Unsigned char or unsigned int. 4. False. 5. Factors that can affect the delay size are: 1) The crystal frequency connected to the microcontroller. 2) The compiler used to compile the C program.

Uploaded by

king
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)
37 views18 pages

L01 Embedded 22

Here are the answers to the review questions: 1. Unsigned char: 0 to 255. Signed char: -128 to 127. 2. Unsigned int: 0 to 65,535. Signed int: -32,768 to 32,767. 3. Unsigned char or unsigned int. 4. False. 5. Factors that can affect the delay size are: 1) The crystal frequency connected to the microcontroller. 2) The compiler used to compile the C program.

Uploaded by

king
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/ 18

CPE 411

Embedded System

Lecture 1
AVR PROGRAMMING IN C

Dr. Manhal Alhilali Google Classroom Telegram


[email protected] Class code: nbzgksj https://t.me/+JXFdroSkUPtlZmFl

Previously

• History of embedded systems

• Get to Know Arduino

• Microcontroller: ATmega328

• The Arduino IDE,

• The Arduino board uses The AVR

family of 8-bit microcontroller

devices from Atmel and designed

as a self-contained circuit board

with easy-to-use connections,

2
1. The AVR microcontroller AVR Features

• The AVR is an 8-bit RISC single-chip


microcontroller with Harvard
architecture
• comes with some standard features
such as on-chip program (code) ROM,
data RAM, data EEPROM, timers and
I/O ports.
• AVRs have some additional features
like ADC, PWM, and different kinds
ofserial interface such as USART, SPI,
I2C (TWI), CAN, USB, and so on.

1. The AVR microcontroller Simplified View of an AVR Microcontroller

4
1. The AVR microcontroller AVR family groups: Classic, Mega, Tiny, and special purpose.

1. The AVR microcontroller Other microcontrollers

6
Objective

You are expected to: Why program the AVR in C?


• Examine C data types for the AVR • Compilers produce hex files that we
• Code C programs for time delay and download into the Flash of the
microcontroller.
I/O operations • The size of the hex file produced by the
• Code C programs for I/O bit compiler is one of the main concerns of
microcontroller programmers.
manipulation • Assembly language produces a hex file that
• Code C programs for logic and is much smaller than C.
• Why writing programs in C instead of
arithmetic operations Code C Assembly?
programs for ASCII and BCD data • It is easier and less time consuming to
write in C than in Assembly. C is easier
conversion to modify and update.
• Code C programs for binary (hex) to • You can use code available in function
libraries.
decimal conversion • C code is portable to other
• Code C programs for data serialization microcontrollers with little or no
modification.
• Code C programs for EEPROM access
7

2. DATA TYPES AND TIME DELAYS IN C

Unsigned Char
Using int in place of
char can lead to the
need for more memory
space. Which is limited
in AVR

C compilers use the


signed char as the
default unless we put
the keyword unsigned in
front of the char

We can also use the


unsigned char data type
Data Direction Register
for a string of ASCII
characters, including
extended ASCII
characters.

9
2. DATA TYPES AND TIME DELAYS IN C Unsigned Char

10

2. DATA TYPES AND TIME DELAYS IN C Unsigned Char

11
2. DATA TYPES AND TIME DELAYS IN C Signed Char
The signed char is an 8-bit data type that uses the most significant bit (D7 of D7-D0) to
represent the - or +value. (-128 to + 127)

FCH = 1111 1011


0000 0100

FDH = 1111 1100


0000 0011

FEH = 1111 1101


0000 0010

12

2. DATA TYPES AND TIME DELAYS IN C Unsigned/ signed Int

• The unsigned int is a 16-bit data type that takes a value in the range of 0 to 65.535
(0000-FFFFH).
• Unsigned int is used to define 16-bit variables such as memory addresses. It is also used
to set counter values of more than 256.
• Signed int
is a 16-bit
data type
that uses
the most
significant
bit (D15 of
DI5-D0) to
represent
the - or
+value. (-
32,768 to
+32,767).

13
2. DATA TYPES AND TIME DELAYS IN C Time Delay
Using a simple for loop
Factors that can affect the accuracy of the delay:
1. The crystal frequency connected to the XTAL1-XTAL2 input. The duration of the clock
period for the instruction cycle is a function of this crystal frequency.
2. The compiler used to compile the C program (might effect or ignore the loop) .

For that when we


use a loop to write
time delays for C,
we must use the
oscilloscope to
measure the exact
duration.

14

2. DATA TYPES AND TIME DELAYS IN C Time Delay


Using predefined C functions
Such as _delay_ms( ) and _delay _us( ) defined in delay.h in WinAVR or
delay_ms( ) and delay_us( ) defined in delay h in CodeVision.

15
? Review Questions

1. Give the magnitude of the unsigned char and signed char data types.

2. Give the magnitude of the unsigned int and signed int data types.

3. If we are declaring a variable for a person's age, we should use the

_____ data type.

4. True or false. Using predefined functions of compilers to create a time

delay is not recommended if you want your code to be portable to other

compilers.

5. Give two factors that can affect the delay size.

16

3. I/O PROGRAMMING IN C
All port registers of the AVR are both byte acces- sible and bit accessible.

Byte size I/O


To access a PORT register as a byte, we use the PORTx
We access the data direction registers in the same way, using DDRx
To access a PIN register as a byte, we use the PINx

17
3. I/O PROGRAMMING IN C +

18

3. I/O PROGRAMMING IN C +

Bit size I/O:


The I/O ports of ATmega32 are bit-accessible. But some AVR C compilers do not support
this feature.

19
4. LOGIC OPERATIONS IN C Bit-wise operators in C

logical operators AND (&&), OR (I), NOT (!)


the bit-wise operators AND (&), OR (I), EX-OR (^), inverter(~), shift right (>>), and left (<<).

20

4. LOGIC OPERATIONS IN C +

21
4. LOGIC OPERATIONS IN C +

22

4. LOGIC OPERATIONS IN C +

23
4. LOGIC OPERATIONS IN C +

24

4. LOGIC OPERATIONS IN C +

25
4. LOGIC OPERATIONS IN C +

26

4. LOGIC OPERATIONS IN C +

27
4. LOGIC OPERATIONS IN C +

28

4. LOGIC OPERATIONS IN C +

29
4. LOGIC OPERATIONS IN C +
Disassembly of Part (a)

Examine the Assembly


output for parts (a) and
(b). You will notice that
Disassembly of Part (b) the generated codes
are the same because
they do exactly the
same thing.

30

4. LOGIC OPERATIONS IN C

Compound assignment operators in C:


To reduce coding (typing) we can use compound statements for bit-wise operators in C.

Bit-wise shift operation in C

31
4. LOGIC OPERATIONS IN C Bit-wise shift operation and bit manipulation

32

4. LOGIC OPERATIONS IN C +

33
4. LOGIC OPERATIONS IN C +

34

4. LOGIC OPERATIONS IN C +

35
4. LOGIC OPERATIONS IN C +

Notice that to
generate
more
complicated
numbers, we
can OR two
simpler
numbers. So
we can simply
write
(1<<7)|(1<<4)
.

36

? Review Questions

37

You might also like