1 Assembly Programming through Arduino G V V Sharma Contents 1 Components 1 2 Seven Segment Display 1 2.1 Hardware Setup....... 1 2.2 Software Setup........ 2 2.3 Controlling the Display... 2 3 Display Decoder 3 3.1 Driving the Segments.... 3 4 Boolean Operations 4 Component Value Quantity Breadboard 1 Resistor 220Ω 1 Arduino Uno 1 Seven Segment Common 1 Display Anode Decoder 7447 1 Flip Flop 7474 2 Jumper Wires 20 TABLE 1.0.1 5 Combinational Logic 5 6 Decade Counter through Arduino 5 Abstract This manual is a beginner s guide to assembly programming using the arduino. The instruction set of the ATMEGA328P IC, which is the arduino microcontroller, is used for building a decade counter by programming in assembly language. This manual can also be used for a first course on microprocessor architecture. 1 Components The components required for this manual are listed in Table 1.0.1. 2.1 Hardware Setup 2 Seven Segment Display The breadboard can be divided into 5 segments. In each of the green segements, the pins are internally connected so as to have the same voltage. Similarly, in the central segments, the pins in each column are internally connected in the same fashion as the blue columns. Problem 2.1. Plug the display to the breadboard in Fig. 2.1 *The author is with the Department of Electrical Engineering, Indian Institute of Technology, Hyderabad 502285 India e-mail: gadepall@iith.ac.in. Fig. 2.1 The seven segment display in Fig. 2.2 has eight pins, a, b, c, d, e, f, g and dot that take an active LOW input, i.e. the LED will glow only if the input is connected to ground. Each of these pins is connected to an LED segment. The dot pin is reserved for the LED. Problem 2.2. Connect one end of the resistor to the COM pin of the display and the other end to an extreme pin of the breadboard. The Arduino Uno has some ground pins, analog input pins A0-A3 and digital pins D1-D13 that can be used for both input as well as output. It also has two power pins that can generate 3.3V and 5V. In the following exercises, only the GND, 5V and digital pins will be used. Problem 2.3. Connect the 5V pin of the arduino to an extreme pin that is in the same segment as the 1K resistor pin.
2 g f COM a b a Problem 2.9. Open geany and type the following code. Save the file as hello.asm ; h e l l o ; u s i n g assembly l a n g u a g e f o r t u r n i n g LED on f g b. i n c l u d e / home/ alarm/ m328pdef. i n c e d c dot l d i r16, 0 b00100000 o u t DDRB, r16 l d i r17, 0 b00000000 o u t PortB, r17 e d COM c dot Fig. 2.2 Problem 2.4. Connect the GND pin of the arduino to the opposite extreme pin of the breadboard Problem 2.5. Connect the Arduino to the computer. Problem 2.6. Connect the dot pin of the display to a pin in the same segment as the GND pin. What do you observe? 2.2 Software Setup Problem 2.7. Install AVRA and AVRDUDE on your Linux system through the following commands sudo apt g e t i n s t a l l a v r a avrdude %F i n d i n g t h e p o r t sudo dmesg grep t t y %The o u t p u t w i l l be something l i k e [ 6. 1 5 3 3 6 2 ] cdc acm 1 1. 2 : 1. 0 : ttyacm0 : USB ACM d e v i c e %and your p o r t number i s ttyacm0 Problem 2.8. Download the m328pdef.inc file from h t t p :// t l c. i i t h. ac. i n/ img/m328pdef. i n c and copy into the directory home (/home/user/) directory Problem 2.10. With hello.asm open in geany, go to Build Set Build Commands Compile and enter a v r a %f Then go to Build Set Build Commands Execute and enter avrdude p atmega328p c a r d u i n o P / dev/ ttyacm0 b 115200 U f l a s h :w:%e. hex Problem 2.11. Change/home/linaro/m328Pdef.inc in hello.asm to /home/username/m328pdef.inc, where username is your linux login name. 2.3 Controlling the Display Problem 2.12. Now connect the dot pin of the display to pin 13 of the arduino. Use F8 to compile hello.asm and F5 to execute. Problem 2.13. Turn the LED off by modifying hello.asm. Problem 2.14. Complete Table 2.14.1 for all the digital pins using Fig. 2.14 Port Pin Digital Pin PD2 2 PB5 13 TABLE 2.14.1
3 PD2 PD3 PD4 PD5 PD6 PD7 PB0 decimal a b c d e f g 0 0 1 0 0 1 0 2 TABLE 2.16.1 Fig. 2.14 Problem 2.15. Connect the a g pins of the display to the 2-8 pins of the arduino. Run the following code. ; u s i n g assembly l a n g u a g e f o r ; d i s p l a y i n g number on ; seven segment d i s p l a y. i n c l u d e / home/ l i n a r o/ m328pdef. i n c ; C o n f i g u r i n g p i n s 2 7 of Arduino ; as o u t p u t l d i r16, 0 b11111100 o u t DDRD, r16 ; C o n f i g u r i n g p i n 8 of Arduino ; as o u t p u t l d i r16, 0 b00000001 o u t DDRB, r16 ; W r i t i n g t h e number 2 on t h e ; seven segment d i s p l a y l d i r17, 0 b10010000 o u t PortD, r17 l d i r17, 0 b00000000 o u t PortB, r17 Problem 2.16. The output of Problem 2.15 can be explained by Table 2.16.1 below. Complete Table 2.16.1 for all numbers between 0-9. Use this information to display the numbers from 0-9. 3 Display Decoder The 7447 IC helps in displaying decimal numbers on the seven segment display. The ā f, pins of the 7447 IC are connected to the a f pins of the display. V cc should be connected to a 5V power source. The input pins of the decoder are A,B,C and D, with A being the lowest significant bit (LSB) and D being the most significant bit (MSB). For example, the number 5 is visible on the display when the A,B,C and D inputs are the following. V CC 16 f 15 D C B A Decimal 0 1 0 1 5 ḡ 14 ā 13 7447 1 2 3 4 5 6 7 8 B C D A GND b 12 Fig. 3.0 Problem 3.1. Connect the 7447 IC decoder ā ḡ pins to the a g pins of the display respectively. Problem 3.2. Connect the V cc and GND pins of the decoder to the 5V supply and GND pins of the breadboard. 3.1 Driving the Segments Problem 3.3. Connect the A-D pins of the 7447 IC to the pins D2-D5 of the Arduino. Problem 3.4. Type the following code and execute. What do you observe? ; d r i v i n g t h e 7447 d e c o d e r c 11 d 10 ē 9
4. i n c l u d e / home/ alarm/ m328pdef. i n c l d i r16, 0 b00111100 ; i d e n t i f y i n g o u t p u t p i n s 2, 3, 4, 5 o u t DDRD, r16 ; d e c l a r i n g p i n s as o u t p u t l d i r16, 0 b00010100 ; l o a d i n g t h e number 5 i n b i n a r y o u t PORTD, r16 ; w r i t i n g o u t p u t t o p i n s 2, 3, 4, 5 rjmp S t a r t Problem 3.5. Now generate the numbers 0-9 by modifying the above program. 4 Boolean Operations Test all the following using the 7447 IC Problem 4.1. Verify the AND,OR and XOR operations in assembly. Solution: ; l o g i c a l AND, OR and XOR o p e r a t i o n s ; o u t p u t d i s p l a y e d u s i n g 7447 IC. i n c l u d e / home/ alarm/ m328pdef. i n c l d i r16, 0 b00111100 ; i d e n t i f y i n g o u t p u t p i n s 2, 3, 4, 5 o u t DDRD, r16 ; d e c l a r i n g p i n s as o u t p u t l d i r16, 0 b00000000 ; i n i t i a l i z i n g W l d i r17, 0 b00000001 ; i n i t i a l i z i n g X ; l o g i c a l AND ; and r16, r17 ;W AND X ; l o g i c a l OR ; or r16, r17 ;W OR X ; l o g i c a l XOR e o r r16, r17 ;X XOR X ; f o l l o w i n g code i s f o r d i s p l a y i n g o u t p u t ; s h i f t i n g LSB i n r16 t o 2nd p o s i t i o n l d i r20, 0 b00000010 ; c o u n t e r = 2 r c a l l loopw ; c a l l i n g t h e loopw r o u t i n e o u t PORTD, r16 ; w r i t i n g o u t p u t t o p i n s 2, 3, 4, 5 ; loop f o r b i t s h i f t i n g loopw : l s l r16 ; l e f t s h i f t dec r20 ; c o u n t e r brne loopw ; i f c o u n t e r!= 0 Problem 4.2. Write a routine for finding the complement of a number. Solution: ; program t o complement a b o o l e a n. i n c l u d e / home/ alarm/ m328pdef. i n c l d i r16, 0 b00111100 ; i d e n t i f y i n g o u t p u t p i n s 2, 3, 4, 5 o u t DDRD, r16 ; d e c l a r i n g p i n s as o u t p u t l d i r16, 0 b00000000 ;A=1
5 r c a l l comp ; j u m p t i n g t o comp r o u t i n e below ; f o l l o w i n g code i s f o r d i s p l a y i n g o u t p u t ; s h i f t i n g LSB i n r16 t o 2nd p o s i t i o n l d i r20, 0 b00000010 ; c o u n t e r = 2 r c a l l loopw ; c a l l i n g t h e loopw r o u t i n e o u t PORTD, r16 ; w r i t i n g o u t p u t t o p i n s 2, 3, 4, 5 ; loop f o r b i t s h i f t i n g loopw : l s l r16 ; l e f t s h i f t dec r20 ; c o u n t e r brne loopw ; i f c o u n t e r!= 0 ; comp r o u t i n e b e g i n s comp : mov r0, r16 ; u s i n g r0 f o r c o m p u t a t i o n s l d i r16, 0 b00000001 ; l o a d i n g 1 e o r r16, r0 ;A =A XOR 1 ; u r n i n g from comp 5 Combinational Logic Problem 5.1. In the truth table in Table 5.2.1, W, X, Y, Z are the inputs and A, B, C, D are the outputs. This table represents the system that increments the numbers 0-8 by 1 and resets the number 9 to 0 Using K-maps, write the boolean logic functions for A, B, C in terms of W, X, Y, Z. Z Y X W D C B A 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 1 0 0 1 1 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 1 1 0 0 1 1 0 0 1 1 1 0 1 1 1 1 0 0 0 1 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0 TABLE 5.2.1 Solution: The desired equations are A=W (5.1.1) B=WX Z + W X (5.1.2) C= WXY + X Y+ W Y (5.1.3) D=WXY+ W Z (5.1.4) Problem 5.2. Write an assembly program for implementing Table 5.2.1 and verify if your logic is correct by observing the output on the seven segment display. 6 Decade Counter through Arduino Problem 6.1. Use the following code for LED blinking. You will have to connect pin 13 to the LED on the seven segment display. The blinking delay that you obtain is 1 second.. i n c l u d e / home/ alarm/ m328pdef. i n c ; works as 1 second d e l a y on a r d u i n o : 16 MHz (16 x10 ˆ 6 ) s b i DDRB, 5 ; s e t p i n 13 as o u t p u t p i n (DDRB p i n 5) l d i r16, 0 b00000101 ; t h e l a s t 3 b i t s d e f i n e t h e p r e s c a l e r, 101 => d i v i s i o n by 1024 o u t TCCR0B, r16 ; p r e s c a l a r used = 1024. So new f r e q. of c l o c k c y c l e = (16 MHz / 1024) = 16 khz
6 c l r r18 ; o u t p u t b i t s. we a r e only i n t e r e s t e d i n b i t 6 from t h e r i g h t. D2-D5 pins as Arduino input. V CC 14 13 D2 12 CLK2 11 10 Q2 9 8 l d i r20, 0 b00100000 ; i n i t i a l i z i n g 7474 loop : e o r r18, r20 ; change t h e o u t p u t of LED o u t PORTB, r18 l d i r19, 0 b01000000 ; t i m e s t o run t h e loop = 64 f o r 1 second d e l a y r c a l l PAUSE ; c a l l t h e PAUSE l a b e l rjmp loop PAUSE : ; t h i s i s d e l a y ( f u n c t i o n ) l p 2 : ; loop r u n s 64 t i m e s IN r16, TIFR0 ; t i f r i s t i m e r i n t e r u p t f l a g (8 b i t t i m e r r u n s 256 t i m e s ) l d i r17, 0 b00000010 AND r16, r17 ; need second b i t BREQ PAUSE OUT TIFR0, r17 ; s e t t i f r f l a g high dec r19 brne l p 2 1 2 3 4 5 6 7 D1 CLK1 Q1 GND Fig. 6.2 Problem 6.3. Connect the Q pins to IC 7447 Decoder as input pins. Connect the 7447 IC to the seven segment display. Problem 6.4. Connect the D6-D9 pins of the arduino to the D input pins of two 7474 ICs. Use the D6-D9 pins as Arduino output. Problem 6.5. Connect pin 13 of the Arduino to the CLK inputs of both the 7474 ICs. Problem 6.6. Using the logic for the counting decoder in assembly in Section 5, implement the decade counter using flip-flops. ; p r e s c a l a r l o o p i t e r a t i o n s t i m e r d u r a t i o n = 16 m i l l i o n c y c l e s ; 1 6 m i l l i o n c y c l e s a t 16 MHz = 1 second The 7474 IC in Fig. 6.2 has two D flip flops. The D pins denote the input and the Q pins denote the output. CLK denotes the clock input. Problem 6.2. Connect the D2-D5 pins of the arduino to the Q pins of the two 7474 ICs. Use the