close

ATmega 328p is a single-chip microcontroller. It is usually used in basic Arduino boards. For example, Arduino UNO, Arduino Pro Mini and Arduino Nano. Now, try to write an assembly language program for an ATmega328P. In this circuit, a DIP switch is connected to the PORT D and a LED is connected to the PORT B on ATmega 328p . Switch the bits from 4 to 0 and display on the bits 6 to 2 on the LEDs.

 

this is a common example in mechatronics. 

The solutions are given below.

 

LDI R20, OXFF

OUT DDRB, R20

OUT PORTB, R20

LDI R21, 0

OUT DDRD, R21

OUT PORTD, R20

L1: IN R16, ~PIN D (pull up)

LDI R17, R16

ADD R17, R16

ADD R17, R16

ADD R17, R16

ADD R17, R16

OUT PORTB, R17

JMP L1

 

I'm not so sure if this is the best way to solve this problem, but this is what I think through pure logic.

 

Another way to solve this problem is post by a student in the US through Youtube channel.

It is much better than mine, but I don't really get how it works.

 

LDI R16, 0

LDI R17, 0XFF

OUT DDRD, R17

OUT DDRB, R16

IN R17, PIND

LSL R17

LSL R17

OUT PORTD, R17

 

if you have a better and thorough explaination of this problem, please leave it in the comment box below. We would be honored to receive precious advises from you.

創作者介紹

Josephood7 發表在 痞客邦 留言(0) 人氣()