Microcomputers Notes

Jonathan Gan
7 min readApr 27, 2020

This semester Im taking Microcomputers and this is a study guide/cheat sheet I’m writing to help me better understand my notes and to help anyone else understand the topic who maybe in similar boat. The sections are broken up into an Introduction, followed by what each Exam will cover.

Textbook:

School: Florida Polytechnic University

Table of Contents:

i. Introduction

ii. Exam 1 Material

Exam 1 will cover Chapter 1–5

What to know about AVR 328 Board:

What is Assembly?:

Functions to know:

LDI

IN/OUT

STS

JMP

CBI

SBC

SBCI

ADD

SUBI

iii. Exam 1 Summary

Chapter 1 — 5

iv. Exam 2 Material

v. Exam 2 Summary

vi. Exam 3 Material

vii. Exam 3 Summary

Introduction:

What is a microcomputer?

Microcomputer vs microprocessor vs microcontroller?

What is an Arduino?

What is assembly?

What is a register

Exam 1:

Using assembly to control registers

Flags and how they work

  • BRNE checks for Z flag
  • Use DEC to change Z flags
  • 255 is the maximum number of loops
  • All registers keep a maximum of 255 bits
  • Create a nested loop to run a loop more than 255 times

ex:

This loops 9 times 100 more times

LDI R20, 100

L2: LDI R16,9

L1:ADD R16, 1

DEC R16

BRNE L2

Practice

This loops any number of times times until reaches a value of 27

LDI R20, 0

L1: ADD R20, R16←Number of times to loop the nested loop

LDI R17, 2 ← load 1 into register R16

ADD R16, R17← Loads 4 into register R15

LDI R17, 27 ← Adds 2 to R16

SUB R17, R16

BRCC L1

Stack, Push, Pop

Calling a function

Stack is a piece of ram (usually at the end of registers)

Stacks are architected as Last in first Out [LIFO]

Push Rr

[SP] =Rr ←save register value in stack pointer

SP = SP -1 ←decrement Stack pointer by 1

POP Rd

SP = SP + 1 ←Increment Stack pointer by 1

Rd = [SP] ←save stack pointer value in register

Push R20

Push R30

POP R16

Pointer:

An address saved in a variable

Stack Pointers are 16 bits in size; Two 8 registers →[SP Low & SP High]

How to setup stack pointer

  • You have to use a stack pointer with Push and Pop commands and it is essentially a place to store data but it is ordered based on the order it was pushed

General Purpose register: The structure of I/O pins

Setting the DDRP

Exam 1 Summary:

Overview:

Functions covered in assembly:

Exam 2:

Problem 1 (20 Points)

Write an assembly program to copy 100 bytes of data starting at RAM address $1FF to RAM locations starting at $2FC.

Problem 2 (20 Points)

Write an Assembly program that finds the position of the first low in an 8-bit data item and save

the position in R17. The data is scanned from D0 to D7. Consider the data item is $A7.

Problem 3 (20 points)

1. Write only ONE Assembly instruction to clear bit 4, 2, 0 in R16, without disturbing other bits.

(don’t use LDI)

2. Write only ONE Assembly instruction to set bit 4, 2, 0 in R16, without disturbing other bits.

(don’t use LDI)

3. Find the TCCR1A and TCCR1B values for CTC mode, no presacral, with the clock

coming from the AVR’s crystal

4. State the differences between Timer0 and Timer1.

Problem 4 (20 Points)

Write a C program to count up Port D from 0–99 continuously.

Problem 5 (20 Points)

Assume that XTAL = 16 MHZ. Find the values to be loaded in TCNT0 and OCR0A to generate a time delay

of 10 us in two modes:

a- using Normal mode, no prescalar mode.

b- using CTC mode, no prescalar mode.

Exam 3:

EEL 4746 Final Review Questions

Review: I/O registers: DDRx, PORTx, PINx

1. configure Port, specific bit as input/output

2. What is the maximum delay that can generated by our 16-bit Timer 1 with a prescaler value of 1024 and clock frequency of 16 MHz.

3. In which register can you find the global interrupt enable bit?

4. Where can you find the Interrupt Vector Table (IVT)?

5. When an interrupt is triggered, what register is placed on the stack? Why is this register saved?

6. What immediately happens when an interrupt occurs?

7. What is instruction is required to return from an Interrupt?

8. In what way is a ret instruction different from an reti instruction?

9. What is one of the last things and ISR does before it returns control to the interrupted program?

10. How many bits need to be set for a Timer/Counter 1 Overflow interrupt to be triggered?

11. What triggers a Timer/Counter 1 Overflow Interrupt?

12. Using a single bit instructions, disable all interrupts.

13. Write Assembly code to implement the following circuit.

14. Write an instruction to divide a signed number in register R22 by 2?

15. Write an instruction to multiply an unsigned number in register R22 by 2?

Steps in enabling an interrupt:

 Enable the general interrupt : set bit 7 (I) of the SREG to High.

 Enable the actual interrupt: set the interrupt bit of the corresponding register to High.

Timer interrupts: TIMSK0, TIMSK1, TIMSK2

External Interrupts: PD2, PD3

Pin change interrupt:

UART:

UDR = Data

Example 1:

sending character ‘G’ continuously (8 bits, 9600 baudrate, 16MHz)

#include <avr/io.h>

Int main (void)

{

UCSR0B = (1<<TXEN0)|(1<<UDRIE0);

UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);

UBRR0L = 103; //baud rate = 9600bps

Sei();

While (1);

Return 0;

}

ISR (USART_UDRE_vect)

{

UDR0=’G’;

}

Example 2:

Receiving bytes of data continuously (8 bits, 9600 baudrate, 16MHz)

#include <avr/io.h>

Int main (void) {

UCSR0B = (1<<RXEN0)|(1<<RXCIE0);

UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);

UBRR0L = 103; //baud rate = 9600bps

Sei();

While (1);

Return 0;

}

ISR (USART_RX_vect) {

PORTB=UDR0;

}

Previous Year Final

Question 1
We would like to design a multiplexer using ATmega328P. The multiplexer is functioning as follows:
PortD.0, PortD.1, and PortD.2 are inputs

PortD.3 is an output.

If the input PortD.2 is 0, then the output must be the same as the input PortD.0

If the input PortD.2 is 1, then the output must be the same as the input PortD.1

(a) [10 pts] Describe your method/idea without using assembly instructions.

Set the Data Direction register to input for Bits 0–2 in PORTD and set the Data direction register for PORTD.3 to output.

Whenever PortD.2 is 0 copy the data in PortD.0 to PortD.3.

Also if PortD.2 is 1 copy the data in PortD.1 to PortD.3.

To Check the status of PortD.2 use SBIC or SBIS.

(b) [40 pts] Implement your method/idea as an assembly program using assembly instructions (without changing the status of other pins on PortD).

LDI R16, 0x08

OUT DDRD, R16

SBIS PORTD, 2

RJMP LOW

RJMP HIGH

HIGH:SBIS PIND, O

RJMP LOOP

SBI PORTD, 3

RJMP END

LOOP:CBI PORTD, 3

RJMP END

LOW: SBIS PIND, 1

RJMP LOOP

SBI PORTD, 3

END: RJMP END

Question 2

We have an ATmega328P based system with 16MHz crystal. We would like to design a transmitter device for a communication system which supports only two different messages:
• To send a message meaning SLOWER, we send the character “S” or “s” over a serial connection
• To send a message meaning FASTER, we send the character “F” or “f” over a serial connection

We would like to use the serial port, PortD.1, to transmit these characters.

(a) [10 pts] Write a C subprogram to initialize the serial port and set the baud rate to 9600bps.

#include <avr/io.h>

Void setup(){

UCSROB = (1<<TXEN0);

UCSROC = (1<<UCSZ01)|(1<<UCSZ00);

UBRROL = 103; // BR=F/((UBRR+1)16)

// 9600=(16*10⁶)/((UBRR+1)16)

// UBRR=103.17

}

(b) [20 pts] Write a C program, which reads PortB.1 continuously and functions as follows:
If PortB.1 is 0 then send the message SLOWER, if PortB.1 is 1 then send the message FASTER.

void send(char c){

while(!(UCSR0A & (1<<UDRE0)));

UCSROC = (1<<UCSZ01)|(1<<UCSZ00);

UDR0 = C;

}

int main(){

setup();

while(1){

DDRD = 0xFF;

PORTD = UDR0;

DDRB = 0;

PINB = (1<<1);

if(PINB==0)

send(‘s’);

else

send(‘f’);

}

return 0;

}

Question 3

[20 pts] We would like to design a serial port receiver system (ATmega328P based with 16MHz crystal) which will use the serial port, PortD.0, to receive characters. Your design must be C based, and it should basically do the following:
If the received character is “S” or “s”, set PortB.2 to LOW
If the received character is “F” or “f”, set PortB.2 to HIGH
You can assume that the serial port is already initialized in receive mode with 9600 bps baud rate.

You don’t need the solution of Question 2 to be able to solve this question.

#include <avr/io.h>

void setup(){

UCSROB = (1<<TXEN0);

UCSROC = (1<<UCSZ01)|(1<<UCSZ00);

UBRROL = 103; // BR=F/((UBRR+1)16)

// 9600=(16*10⁶)/((UBRR+1)16)

// UBRR=103.17

}

int main(){

setup();

while(!(UCSR0A & (1<<RXC0))){

DDRD = 0xFF;

PORTD = UDR0;

if(PORTD==‘s’|| PORTD ==‘S’ )

PORTB = (0<<2);

else if (PORTD==‘f’ || PORTD==‘F’)

PORTB = ~(0<<2);

}

return 0;

}

Resources:

Thank you again for reading and enjoying this journey with me!

Buy me coffee? https://ko-fi.com/jonngan

--

--

Jonathan Gan

A Software Engineer who explores for clarity and observes for inspiration