microcontroller part 2

123
https://www.facebook.com/groups/embedded.system.KS/ Embedded System PART 2 ENG.KEROLES SHENOUDA 1

Upload: keroles-karam

Post on 21-Jan-2018

1.134 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/

Embedded System

PART 2ENG.KEROLES SHENOUDA

1

Page 2: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Index (1) GPIO/DIO/IO Module

Atmel-8155-8-bit-Microcontroller-AVR-ATmega32A_Datasheet

DIO_LAB1

DIO_LAB2

DIO_Project1:LED Matrix 8*8

quiz1: 7- segment “assembly Code”

DIO_LAB3:Two- digital Display Module DIO_Project2Seven Segment Digital Clock

GPIO Driver

References

2

Page 3: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

What will get on Embedded System

AVR Microcontroller

AVR Interfacing

ARM SOC

ARM Tiva C

3

Page 4: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Get Started With Eclipse and AVR

Install AVR-GCC and AVR Dude

https://sourceforge.net/projects/winavr/files/

4

Page 5: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

install the avr-eclipse plugin.5

Page 6: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

install the avr-eclipse plugin.

This opens the screen to specify the plug-in to install.

Here you just enter the update URL of AVR-Eclipse

(http://avr-eclipse.sourceforge.net/updatesite/), press

enter and select the AVR Eclipse Plugin:

After you click Next you will be asked a lot of question, if you

accept licenses or if you trust unsigned software – to install

answer them all with yes. When the installation is finished Eclipse

wants to restart, you should do it.

6

Page 7: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Create the first Project7

Page 8: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Create the first Project 8

Page 9: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

install GNU Win32 project

http://gnuwin32.sourceforge.net/packages/make.htm

9

Page 10: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

A quick change of PATH demonstrated this would allow a

successful compile from the command line and so I made the equivalent change in Eclipse:

1) Go to Window->Preferences and then to the AVR->Paths section. Here select the entry for GNU make

10

Page 12: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Build Done12

Page 13: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Open Proteus13

Page 14: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Open Design then point to AMIT KIT14

Page 15: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

We will work on ATmega Evaluation KIT REV5

Modeled on Proteus15

Page 16: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

16

Page 17: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Enable the Led0,1,2 17

Page 18: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Simulate18

Page 19: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Debug 19

Page 20: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/

GPIO/DIO/IO

20

Page 21: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

GPIO / DIO / IO

GPIO is a digital Module, responsible on control all the PINS on the SOC

It is called

GPIO : General Purpose Input/Output

DIO : Directive input/Output

IO : input/Output

I will describe the GPIO on a General Concept and general Capabilities And Features.Then will read the Atmega32 GPIO.And on the future you will read the TivaC GPIO by yourself

21

Page 22: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CPU

RAM

ROM

Interrupt Controller

UART

CAN

ADC

GPIO

Uart_TX

CAN_TX

Analog_ip

Registers

Controller MUX

Externalinterrupt

IRQ = specific NumberAccording to the SOC Specs

Irq number

PORT

PIN

22

Page 23: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CPU

RAM

ROM

Interrupt Controller

UART

CAN

ADC

GPIO

Uart_TX

CAN_TX

Analog_ip

Registers

Controller MUX

Externalinterrupt

IRQ = specific NumberAccording to the SOC Specs

Irq number

PIN

Each PINCan be configured as

Alternative Signal GPIO SignalLike in this example

UART_TXCAN_TXAnlog_ip

23

Page 24: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CPU

RAM

ROM

Interrupt Controller

UART

CAN

ADC

GPIO

Uart_TX

CAN_TX

Analog_ip

Registers

Controller MUX

Externalinterrupt

IRQ = specific NumberAccording to the SOC Specs

Irq number

PIN = UART_TX

Each PINCan be configured as

Alternative Signal GPIO SignalLike in this example

UART_TXCAN_TXAnlog_ip

You Can Configure PIN to be alternative Signal

Then Write on the MUX register to choose the

UART_TX

24

Page 25: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CPU

RAM

ROM

Interrupt Controller

UART

CAN

ADC

GPIO

Uart_TX

CAN_TX

Analog_ip

Registers

Controller MUX

Externalinterrupt

IRQ = specific NumberAccording to the SOC Specs

Irq number

PIN = CAN_TX

Each PINCan be configured as

Alternative Signal GPIO SignalLike in this example

UART_TXCAN_TXAnlog_ip

You Can Configure PIN to be alternative Signal

Then Write on the MUX register to choose the

CAN_TX

25

Page 26: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CPU

RAM

ROM

Interrupt Controller

UART

CAN

ADC

GPIO

Uart_TX

CAN_TX

Analog_ip

Registers

Controller MUX

Externalinterrupt

IRQ = specific NumberAccording to the SOC Specs

Irq number

PIN = Anlog_ip

Each PINCan be configured as

Alternative Signal GPIO SignalLike in this example

UART_TXCAN_TXAnlog_ip

You Can Configure PIN to be alternative Signal

Then Write on the MUX register to choose the

Anlog_ip

26

Page 27: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CPU

RAM

ROM

Interrupt Controller

UART

CAN

ADC

GPIO

Uart_TX

CAN_TX

Analog_ip

Registers

Controller MUX

Externalinterrupt

IRQ = specific NumberAccording to the SOC Specs

Irq number

PIN

Each PINCan be configured as

Alternative Signal GPIO Signal

InputOutput

Normal Input

External Interrupt

GPIO Signal

Externalinterrupt

27

Page 28: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CPU

RAM

ROM

Interrupt Controller

UART

CAN

ADC

GPIO

Uart_TX

CAN_TX

Analog_ip

Registers

Controller MUX

Externalinterrupt

IRQ = specific NumberAccording to the SOC Specs

Irq number

PIN = Externalinterrupt

Each PINCan be configured as

Alternative Signal GPIO Signal

InputOutput

Normal Input

External Interrupt

GPIO Signal

Externalinterrupt

You Can Configure PIN to be GPIO Signal, Input and

External interrupt

28

Page 29: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CPU

RAM

ROM

Interrupt Controller

UART

CAN

ADC

GPIO

Uart_TX

CAN_TX

Analog_ip

Registers

Controller MUX

Externalinterrupt

IRQ = specific NumberAccording to the SOC Specs

Irq number

PIN = INPUT

Each PINCan be configured as

Alternative Signal GPIO Signal

InputOutput

Normal Input

External Interrupt

GPIO Signal

Externalinterrupt

You Can Configure PIN to be GPIO Signal, Normal InputThe CPU will read the GPIO

Data Register

29

Page 30: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CPU

RAM

ROM

Interrupt Controller

UART

CAN

ADC

GPIO

Uart_TX

CAN_TX

Analog_ip

Registers

Controller MUX

Externalinterrupt

IRQ = specific NumberAccording to the SOC Specs

Irq number

PIN = OUTPUT

Each PINCan be configured as

Alternative Signal GPIO Signal

InputOutput

Normal Input

External Interrupt

GPIO Signal

Externalinterrupt

You Can Configure PIN to be GPIO Signal, Output

The CPU will write the GPIO Data Register

30

Page 31: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/

GPIO/DIO/IOFOR ATMEGA32

31

Page 32: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Pin out & Descriptions

These pins are used to connect

external crystal or RC oscillator

Provides supply voltage to the

chip. It should be connected to +5

Supply voltage for ADC and portA.

Connect it to VCC

Clears all the registers and

restart the execution of

program

Reference voltage for ADC

Port APort B

Port DPort C

32

Page 33: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Pin out & DescriptionsMega32/Mega16

(XCK/T0) PB0

(T1) PB1

(INT2/AIN0) PB2

(OC0/AIN1) PB3

(SS) PB4

(MOSI) PB5

(MISO) PB6

(SCK) PB7

RESET

VCC

XTAL2

GND

XTAL1

(RXD) PD0

(TXD) PD1

(INT0) PD2

(INT1) PD3

(OC1B) PD4

(OC1A) PD5

(ICP) PD6

PA0 (ADC0)

PA1 (ADC1)

PA2 (ADC2)

PA3 (ADC3)

PA4 (ADC4)

PA5 (ADC5)

PA6 (ADC6)

PA7 (ADC7)

AREF

AGND

PC7 (TOSC2)

AVCC

PC6 (TOSC1)

PC5 (TDI)

PC4 (TDO)

PC3 (TMS)

PC2 (TCK)

PC1 (SDA)

PC0 (SCL)

PD7 (OC2)

DDRAPORTA

PINA

DD

RB

PIN

B

PO

RT

B

DDRCPORTC

PINC

33

Page 34: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Pin out & Descriptions34

Page 35: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Atmel-8155-8-bit-Microcontroller-

AVR-ATmega32A_Datasheet

35

Page 36: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Atmel-8155-8-bit-Microcontroller-

AVR-ATmega32A_Datasheet

36

Page 37: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Atmel-8155-8-bit-Microcontroller-

AVR-ATmega32A_Datasheet

37

Page 38: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Atmel-8155-8-bit-Microcontroller-

AVR-ATmega32A_Datasheet

38

Page 39: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Atmel-8155-8-bit-Microcontroller-

AVR-ATmega32A_Datasheet

39

Page 40: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Atmel-8155-8-bit-Microcontroller-

AVR-ATmega32A_Datasheet

40

Page 41: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Atmel-8155-8-bit-Microcontroller-

AVR-ATmega32A_Datasheet

41

Page 42: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

HAND

WRITING

42

Page 43: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/

HAND WRITING

43

Page 44: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Elementary I/O: LEDs

• LEDs must be connected the correct way round,the diagram may be labeled a or + for anode andk or - for cathode (yes, it really is k, not c, forcathode!). The cathode is the short lead andthere may be a slight flat on the body of roundLEDs. If you can see inside the LED the cathode isthe larger electrode (but this is not an officialidentification method).

• LEDs can be damaged by heat when soldering,but the risk is small unless you are very slow. Nospecial precautions are needed for solderingmost LEDs.

• LEDs are available in red, orange, amber, yellow,green, blue and white. Blue and white LEDs aremuch more expensive than the other colors. Thecolor of an LED is determined by thesemiconductor material, not by the coloring ofthe 'package' (the plastic body).

An LED is a semiconductor device that converts electrical energy directly into adiscrete color of light

44

Page 45: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Elementary I/O: LEDs• Never connect an LED directly to a battery or power supply! It will be

destroyed almost instantly because too much current will pass through andburn it out.

• LEDs must have a resistor in series to limit the current to a safe value, for quicktesting purposes a 1k resistor is suitable for most LEDs if your supply voltage is12V or less.

• The resistor value, R is given by:

• Estimate 1.5 - 2 V voltage drop (VL)

• Typically draws 10-20 mA (I)

Unless you know what are you doing!

45

Page 46: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

ATMega32 Elementary I/O: LEDs

+5VR

output pin1

+3.2V

No current

Currentlight

Estimate R =voltagecurrent

=5 – 1.8 – 0.4

13 x 10 -3 = 215 Ω ~ 220 Ωeasier to get

LED

no light+5V

+5VR

output pin0

LED

+0.4V

Turning on an LED

Setting the pin to high will not turn ON the LED

Setting the pin to low will turn ON the LED

46

Page 47: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_LAB1

Write A embedded C Code

To toggle 3 Led on sequential way

Then start a tick(pulse) by Buzzer then

Rerun again

Port D pin 5,6,7 as output (LED)Port D pin 4 (Buzzer)

step1

step2

step3

step4avr/io.h and util/delay.h

47

Page 48: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_LAB1 Solution: Create

ATMEGA32_registers.h to be independent on

avr/io.h

48

Page 49: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_LAB1 Solution:

write the Code 49

Page 50: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_LAB1 Solution:

write the Code 50

Page 51: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

It will be helpful if you learned to

Create the ATMEGA32_Registers.h

for All Atmega32 Registers

51

Page 52: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Project1:

LED Matrix 8*8

52

Page 53: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Project1

Seven Segment Digital Clock Write a program for a 2-D Led array

(1*2 LCD) using all GPIO pins in Atmeg32, that acts like a real screen. The Screen is divided into two (8*8) frames (characters), and write a two functions that takes a character and print it for each frame. And simulate the project using proteus. the following figure shows one frame

53

Page 54: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Project1

Seven Segment Digital Clock Say if we want to turn on LED D10 in

the matrix, we need to power the PIN14 of module and ground the PIN3 on module. With this the D10 will turn ON. This is shown in the figure below. This should be first check for MATRIX to know everything in order.

54

Page 55: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Project1

Seven Segment Digital Clock Say if we want to turn on D1, we

need to power PIN9 of matrix and ground the PIN13. With that LED D1 will glow. The current direction for this case is shown in below figure.

55

Page 56: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Project1

Seven Segment Digital Clock Now for the tricky part, consider we

want to turn on both D1 and D10 at a time. So we power both PIN9, PIN14 and ground both PIN13, PIN3. With that we will have D2 and D9 ON along with D1 and D10. It’s because they share common terminals. So if we want to turn LEDs along the diagonal, we will be forced to turn ON all the LEDs along the way. This is shown in below figure.

56

Page 57: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Project1

Seven Segment Digital Clock So to eliminate this problem we will turn only one led on at a

time. Say at t=0m SEC, LED D1 is tuned ON. At t = 1m SEC, LED D1 is tuned OFF and LED D2 is turned ON. Again at t=2 m SEC, LED D2 is turned OFF and LED D1 is turned ON. This goes on.

Now the trick is, the human eye cannot capture a frequency more than 30 HZ. That is if a LED goes ON and OFF continuously at a rate of 30HZ or more. The eye sees the LED as continuously ON. However this is not the case. The LED will be constantly turning ON and OFF. This technique is called multiplexing.

By using multiplexing, we will turn only one row at a time, and there will be cycling around the 8 rows continuously. This visualized as a completely turned ON matrix for a naked eye.

Multiplexing With very speed

To render Each row

57

Page 58: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Solution For Led Matrix

Video

The Video also on https://drive.google.com/open?id=0B7kUsgpvTWFLVnNYSnJ2RWpHc0E

58

Page 59: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

59

Page 60: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

For Example if we want to display

character E

First we will make PORTC= 0x80 (c1=1 (0b1000 0000))

PORTC Select

PORTD = 0xff (0b1111 1111)

PO

RT

D

60

Page 61: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

For Example if we want to display

character E

First we will make PORTC= 0x40 (c2=1 (0b0100 0000))

PORTC Select

PORTD = 0xff (0b1111 1111)

PO

RT

D

61

Page 62: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

For Example if we want to display

character E

First we will make PORTC= 0x20 (c3=1 (0b0010 0000))

PORTC Select

PORTD = 0x0 (0b0000 0000)

PO

RT

D

62

Page 63: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

For Example if we want to display

character E

First we will make PORTC= 0x10 (c4=1 (0b0001 0000))

PORTC Select

PORTD = 0x36 (0b00110110)

PO

RT

D

63

Page 64: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

For Example if we want to display

character E

First we will make PORTC= 0x8 (c5=1 (0b0000 1000))

PORTC Select

PORTD = 0x36 (0b00110110)

PO

RT

D

64

Page 65: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

For Example if we want to display

character E

First we will make PORTC= 0x4 (c6=1 (0b0000 0100))

PORTC Select

PORTD = 0x36 (0b00110110)

PO

RT

D

65

Page 66: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

For Example if we want to display

character E

First we will make PORTC= 0x2 (c7=1 (0b0000 0010))

PORTC Select

PORTD = 0xff(0b1111 1111)

PO

RT

D

66

Page 67: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

For Example if we want to display

character E

First we will make PORTC= 0x2 (c8=1 (0b0000 0010))

PORTC Select

PORTD = 0xff(0b1111 1111)

PO

RT

D

67

Page 68: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

PORTC Select

Multiplexing With very speed

To render Each Column

Make your eyes see the character

Appear like this

68

Page 69: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Each character have 8 valuesFor eight columns

69

Page 70: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

70

Page 71: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Led 2 1 0

write embedded C program “up/down binary Counter”

By three leds and two push button

01

7……

sw0sw1

DIO_Quiz1 71

Page 72: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

I/O: 7-Segment Display

There are applications where you need to display a decimal

digit using a seven segment LED display.

The display could represent e.g. the number of times a

switch was pressed.

Digits 0-9 and hex A-F can be displayed by giving the

proper 7-segment codes

q g f e d c b a q g f e d c b a

0 0 1 1 1 1 1 1 8 1 1 1 1 1 1 1

1 0 0 0 0 1 1 0 9 1 1 0 0 1 1 1

2 1 0 1 1 0 1 1 A 1 1 1 0 1 1 1

3 1 0 0 1 1 1 1 b 0 0 1 1 1 1 1

4 1 1 0 0 1 1 0 C 0 1 1 1 0 0 1

5 1 1 0 1 1 0 1 d 1 0 1 1 1 1 0

6 1 1 1 1 1 0 1 E 1 1 1 1 0 0 1

7 0 0 0 0 1 1 1 F 1 1 1 0 0 0 1

72

Page 73: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

I/O: 7-Segment Display

Common-anode :

requires VCC

LED ON when Output is LOW.

Common-cathode :

NO VCC ,

LED ON when Output is HIGH.

TTL and CMOS devices are

normally not used to drive the

common-cathode display directly

because of current (mA)

requirement. A buffer circuit is

used between the decoder chips

and common-cathode display

73

Page 74: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

I/O: 7-Segment Display 74

Page 75: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

I/O: 7-Segment Display

A 7-Segment LED could be attached as shown:

75

Page 76: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

I/O: 7-Segment Display

Or:

76

Page 77: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Lab2 :

A 7-segment is connected to PORTA.

Write an assembly/embedded C Code

to Display 1 on the 7-segment.

ATmega32

8PORTC

0

1

2

3

56

4

77

Page 78: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Lab2 Solution: assembly

A 7-segment is connected to PORTA. Display 1 onthe 7-segment.

ATmega32

8PORTC

0

1

2

3

56

4

1 1 1 1 1 1 1 1

0 0 0 0 0 1 1 0

DDRC

:PORTC:

;; DIO_lab2.asm;; Author : kkhalil;; Replace with your application code

LDI R20,0x06 ;R20 = 00000110 (binary)OUT PORTC,R20 ;PORTC = R20LDI R20,0xFF ;R20 = 11111111 (binary)OUT DDRC,R20 ;DDRC = R20 ;Delay 1;cycle latency

L1: RJMP L1

78

Page 79: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Lab3 :Two- digital Display Module

7 Segment Display

DATA LINES:

- 7SEG_APORTC .4

- 7SEG_BPORTC .5

- 7SEG_CPORTC .6

- 7SEG_DPORTC .7

DECIMAL POINT:

- 7SEG_DPPORTB .0

ENABLE LINES

- 7SEG_EN1PORTC .2

- 7SEG_EN2PORTC .3

The trick is, the human eye cannot capture a frequency

more than 30 HZ. That is if a LED goes ON and OFF

continuously at a rate of 30HZ or more. The eye sees the

LED as continuously ON. However this is not the case.

The LED will be constantly turning ON and OFF. This

technique is called multiplexing.

79

Page 80: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Lab3 :Two- digital Display

Module

Write an embedded C Code to “up Counter” from 0 to 99.

80

Page 81: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Lab3 :Two- digital Display

Module

Write an embedded C Code to “up Counter” from 0 to 99.

81

Page 82: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Lab3 :Two- digital Display

Module

Write an embedded C Code to “up Counter” from 0 to 99.

82

Page 83: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Lab3 Solution: Write an embedded C Code to

“up Counter” from 0 to 99.

83

Page 84: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/

How To Write Driver

For GPIO/DIO/IO

FOR ATMEGA32

84

Page 85: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Create Eclipse Project

DriverCreate your own files,

Don’t depend on anything

Be Professional ;)

Write your application here

85

Page 86: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Mydelay.h86

Page 87: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Data_Types.h 87

Page 88: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Atmega32_rgisters.hget all the registers absolute address from SPECS

88

Page 89: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Atmega32_rgisters.hget all the registers absolute address from SPECS

89

Page 90: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

For the GPIO Driver Create two Files GPIO.h & GPIO.c

GPIO.c

GPIO.h #include “GPIO.h”

90

Page 91: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

GPIO.h91

Page 92: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

GPIO.h92

Page 93: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

GPIO.c93

Page 94: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

GPIO.c94

Page 95: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

GPIO.c95

Page 96: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

GPIO.c96

Page 97: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

GPIO.c97

Page 98: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

GPIO.c98

Page 99: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Now Write a simple application To toggle led0 which connected to

D5 Main.c

GPIO.C

ATMEGA32

GPIOD5

99

Page 100: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

CPU

RAM

ROM

Interrupt Controller

UART

CAN

ADC

GPIO

Uart_TX

CAN_TX

Analog_ip

Registers

Controller MUX

Externalinterrupt

IRQ = specific NumberAccording to the SOC Specs

Irq number

D5Toggle Led

.txt(main.c/GPIO.c)

Stack/Heap/Data_Memory

100

Page 101: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

main.c101

Page 102: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Simulate on ISIS102

Page 103: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Kindly you can find the Source code in

https://github.com/keroles/Embedded_System/tree/master/Atmega32_Drivers/ATMEGA32_Drivers

103

Page 104: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

How To Debug the Code on ISIS ?

104

Page 105: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

105

Page 106: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

.elf image contains debug information

.hex not contain Any debug information

The compiler can generate different types of files, according to what you need. On a PC, the compiler generates an executable file with the extension ”.exe”. However, microcontrollers need a different, dedicated format. For our microcontroller we will be using HEX (extension ”.hex”) files. Other possible formats generated by the compiler are: .elf, .map, .eeprom

106

Page 107: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

AVR Tool-Chain provide

AVR Libc represents the AVR library provided by Atmel. It defines all the

register names (together with their bits), as well as other useful functions.

GCC represents the GNU Compiler Collection. Together with the GNU

binutils it generates the HEX files.

AVR-GDB, AVaRICE, ISIS and SimulAVR can help simulate / debug the

behavior of your microcontroller application on a PC.

107

Page 108: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

Simulator versus emulator

An emulator only mimics the observable behaviour of a target, whereas a simulator also models the internal state of the target. In this respect, emulators are faster, but they might also be prone to behaviours / errors that the original target would not manifest.

What is a debugger?

The purpose of a debugger is to allow you to see what is going on “inside” another program while it executes - or what another program was doing at the moment it crashed

108

Page 109: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

How To Debug the Code on ISIS ?

First Go to eclipse project

settingsAnd enable the debug informationAs shown

109

Page 110: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

How To Debug the Code on ISIS ?

First Go to eclipse project

settingsAnd enable the debug informationAs shown

110

Page 111: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

How To Debug the Code on ISIS ?

Second Build

the Project

111

Page 112: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

How To Debug the Code on ISIS ?

Third Copy all the Build output files and all the source code which

inside the eclipse project to the ISIS Project folder beside your

Prject.DSN

112

Page 113: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

How To Debug the Code on ISIS ?

Fourth open ISIS Project

and open the Atmega32 IC and pass the elf image toProgram file

113

Page 114: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

How To Debug the Code on ISIS ?

FIFTH run the simulation and press

pause then Open the AVR source code window as shown

114

Page 115: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

How To Debug the Code on ISIS ? 115

Page 116: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

116

Page 117: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Project2:

Seven Segment Digital Clock

117

Page 118: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Project2

Seven Segment Digital Clock Our digital clock operates as follows. When the circuit is powered up the clock

starts at "00:00:00" for "HH:MM:SS". There are two push-button switches used to set the time. Switch SW1 is for setting the minutes, when this button is pressed the minutes in increase until it reaches 59 then reset and start counting from 0. Switch SW2 is for setting the hours.

All seven segment displays are driven by the same port (PortB). The microcontroller through controls from PortC indicate which seven segment display each digit is displayed on. Note that these Seven Segment Displays are common cathode display.

118

Page 119: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Project2

Seven Segment

Digital Clock

119

Page 120: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

DIO_Project2

Seven Segment Digital Clock

120

Page 121: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

GPIO Project 3

Assignment

Microcontroller

sessionWrite code to make the following

functionality:

121

Page 122: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

References

https://docs.google.com/viewer?a=v&pid=sites&srcid=ZmtlLnV0bS5teXxyaWR6dWFuLXMtd2Vic2l0ZXxneDo2ODU0NzlkM2JkOTg4MjRk

http://www.avrprojects.net/index.php/avr-projects/sensors/38-humidity-and-temperature-sensor-dht11?showall=&start=1

http://www.cse.wustl.edu/~lu/cse467s/slides/dsp.pdf

http://www.avr-tutorials.com/

Microprocessor: ATmega32 (SEE3223-10) http://ridzuan.fke.utm.my/microprocessor-atmega32-see3223-10

http://circuitdigest.com/article/what-is-the-difference-between-microprocessor-and-microcontroller

http://cs4hs.cs.pub.ro/wiki/roboticsisfun/chapter2/ch2_7_programming_a_microcontroller

122

Page 123: Microcontroller part 2

https://www.facebook.com/groups/embedded.system.KS/ https://www.facebook.com/groups/embedded.system.KS/

123