Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Embedded System Programming in Slot Machine | ETEE 3285, Exams of Electrical and Electronics Engineering

Material Type: Exam; Professor: Kuyath; Class: Assembly-Language Programming; Subject: Electrical Engineering Tech; University: University of North Carolina - Charlotte; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 07/28/2009

koofers-user-d9j
koofers-user-d9j 🇺🇸

4

(1)

10 documents

1 / 58

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Embedded Systems Programming
1
ETEE 3285
Topic 6: Slot Machine Example
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a

Partial preview of the text

Download Embedded System Programming in Slot Machine | ETEE 3285 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

Embedded Systems Programming

1

ETEE 3285

Topic 6: Slot Machine Example

Agenda

 We will work through a complete example

 Use CodeVision and AVR Studio

 Discuss a few ‘creative’ instructions

 Discuss #define and #include

 Read and write from/to ports

 Use some of the control statements learned in

the last topic

2

Initialization

4

// "Slot Machine" -- The Mini-Exercise

// Phase 1..

#include <Mega8515.h> /* processor specific information */

#define xtal 4000000L /* quartz crystal frequency [Hz] */

#define baud 9600 /* Baud rate */

#include <stdio.h> /* this gets the printf() definition */

// Global Variable Declarations..

char first,second,third; // Columns in the slot machine.

char seed; // Number to form a seed for random #s.

char count; // General purpose counter.

int delay; // A variable for delay time.

void main(void) {

PORTA = 0xFF; // Initialize the I/O ports

DDRA = 0x00; // port A all inputs

DDRB = 0x02; // port B.1 is an output (light)

//Initialize the UART control register

//RX & TX enabled, 8 data bits

UCSRB=0x18;

// initialize the UART's baud rate

UBRRL=xtal/16/baud-1;

The #include commands instruct the compiler to include these files as part of the overall program when compiling The Mega8515.h file includes all of the definitions for labels as well as port addresses. The stdio.h file includes functions such as printf and scanf

Initialization

5

// "Slot Machine" -- The Mini-Exercise

// Phase 1..

#include <Mega8515.h> /* processor specific information */

#define xtal 4000000L /* quartz crystal frequency [Hz] */

#define baud 9600 /* Baud rate */

#include <stdio.h> /* this gets the printf() definition */

// Global Variable Declarations..

char first,second,third; // Columns in the slot machine.

char seed; // Number to form a seed for random #s.

char count; // General purpose counter.

int delay; // A variable for delay time.

void main(void) {

PORTA = 0xFF; // Initialize the I/O ports

DDRA = 0x00; // port A all inputs

DDRB = 0x02; // port B.1 is an output (light)

//Initialize the UART control register

//RX & TX enabled, 8 data bits

UCSRB=0x18;

// initialize the UART's baud rate

UBRRL=xtal/16/baud-1;

For the Atmega128, we have to change this to Mega128 – but, the Atmega128 does not have the UART initialized below

Initialization

7

// "Slot Machine" -- The Mini-Exercise

// Phase 1..

#include <Mega8515.h> /* processor specific information */

#define xtal 4000000L /* quartz crystal frequency [Hz] */

#define baud 9600 /* Baud rate */

#include <stdio.h> /* this gets the printf() definition */

// Global Variable Declarations..

char first,second,third; // Columns in the slot machine.

char seed; // Number to form a seed for random #s.

char count; // General purpose counter.

int delay; // A variable for delay time.

void main(void) {

PORTA = 0xFF; // Initialize the I/O ports

DDRA = 0x00; // port A all inputs

DDRB = 0x02; // port B.1 is an output (light)

//Initialize the UART control register

//RX & TX enabled, 8 data bits

UCSRB=0x18;

// initialize the UART's baud rate

UBRRL=xtal/16/baud-1;

The #define commands instructs the compiler to equate these values with the labels “baud” and “xtal”. They are used in the program

Initialization

8

// "Slot Machine" -- The Mini-Exercise

// Phase 1..

#include <Mega8515.h> /* processor specific information */

#define xtal 4000000L /* quartz crystal frequency [Hz] */

#define baud 9600 /* Baud rate */

#include <stdio.h> /* this gets the printf() definition */

// Global Variable Declarations..

char first,second,third; // Columns in the slot machine.

char seed; // Number to form a seed for random #s.

char count; // General purpose counter.

int delay; // A variable for delay time.

void main(void) {

PORTA = 0xFF; // Initialize the I/O ports

DDRA = 0x00; // port A all inputs

DDRB = 0x02; // port B.1 is an output (light)

//Initialize the UART control register

//RX & TX enabled, 8 data bits

UCSRB=0x18;

// initialize the UART's baud rate

UBRRL=xtal/16/baud-1;

These variables are global because they are declared outside the main() function first, second, third, seed, and count are declared as char (8-bit unsigned integers). delay is declared as an int , a 16- bit unsigned integer

Initialization

10

// "Slot Machine" -- The Mini-Exercise

// Phase 1..

#include <Mega8515.h> /* processor specific information */

#define xtal 4000000L /* quartz crystal frequency [Hz] */

#define baud 9600 /* Baud rate */

#include <stdio.h> /* this gets the printf() definition */

// Global Variable Declarations..

char first,second,third; // Columns in the slot machine.

char seed; // Number to form a seed for random #s.

char count; // General purpose counter.

int delay; // A variable for delay time.

void main(void) {

PORTA = 0xFF; // Initialize the I/O ports

DDRA = 0x00; // port A all inputs

DDRB = 0x02; // port B.1 is an output (light)

//Initialize the UART control register

//RX & TX enabled, 8 data bits

UCSRB=0x18;

// initialize the UART's baud rate

UBRRL=xtal/16/baud-1;

It first initializes Port A and Port B

Initialization

11

// "Slot Machine" -- The Mini-Exercise

// Phase 1..

#include <Mega8515.h> /* processor specific information */

#define xtal 4000000L /* quartz crystal frequency [Hz] */

#define baud 9600 /* Baud rate */

#include <stdio.h> /* this gets the printf() definition */

// Global Variable Declarations..

char first,second,third; // Columns in the slot machine.

char seed; // Number to form a seed for random #s.

char count; // General purpose counter.

int delay; // A variable for delay time.

void main(void) {

PORTA = 0xFF; // Initialize the I/O ports

DDRA = 0x00; // port A all inputs

DDRB = 0x02; // port B.1 is an output (light)

//Initialize the UART control register

//RX & TX enabled, 8 data bits

UCSRB=0x18;

// initialize the UART's baud rate

UBRRL=xtal/16/baud-1;

It first initializes Port A and Port B and then initializes the UART

Main Program

13 The main program is contained in the while (1) { } loop structure: it mean run forever while (1) { // do forever.. while (PINA.0) // Wait for a button and seed++; // let the counter roll over and // over to form a seed. first = second = third = seed; // preload the columns do { // mix up the numbers // while waiting for button release. first ^= seed>>1; // Exclusive ORing in the moving seed second^= seed>>2; // can really stir up the numbers. third ^= seed>>3; seed++; // keep rolling over the seed pattern } while (PINA.0 == 0); // while the button is pressed for (count = 0; count < 5; count++) { // flash light when done.. for (delay = 0; delay < 10000; delay++) ; // just count up and wait PORTB.1 = 0; // turn the LED on.. for (delay = 0; delay < 10000; delay++) ; PORTB.1 = 1; // turn the LED off.. } first &= 3; // limit number to values from 0 to 3 second &= 3; third &= 3; printf("--> %d, %d ,%d <--\n",first, second, third); // show the values.. // determine a payout.. if((first == 3) && (second == 3) && (third == 3)) printf("Paid out: JACKPOT!!\n"); // all "cherries" else if((first == 3) || (second == 3) || (third == 3)) printf("Paid out: One Dime\n"); // if any are "cherries".. else if((first == second) && (second == third)) printf("Paid out: One Nickle\n"); // if three are alike, of any kind.. else printf("Paid out: ZERO\n"); // otherwise -- you lose.. } } This brace goes with main ( )

Main Program

14 This while loop is made up of these two instructions while (1) { // do forever.. while (PINA.0) // Wait for a button and seed++; // let the counter roll over and // over to form a seed. first = second = third = seed; // preload the columns do { // mix up the numbers // while waiting for button release. first ^= seed>>1; // Exclusive ORing in the moving seed second^= seed>>2; // can really stir up the numbers. third ^= seed>>3; seed++; // keep rolling over the seed pattern } while (PINA.0 == 0); // while the button is pressed for (count = 0; count < 5; count++) { // flash light when done.. for (delay = 0; delay < 10000; delay++) ; // just count up and wait PORTB.1 = 0; // turn the LED on.. for (delay = 0; delay < 10000; delay++) ; PORTB.1 = 1; // turn the LED off.. } first &= 3; // limit number to values from 0 to 3 second &= 3; third &= 3; printf("--> %d, %d ,%d <--\n",first, second, third); // show the values.. // determine a payout.. if((first == 3) && (second == 3) && (third == 3)) printf("Paid out: JACKPOT!!\n"); // all "cherries" else if((first == 3) || (second == 3) || (third == 3)) printf("Paid out: One Dime\n"); // if any are "cherries".. else if((first == second) && (second == third)) printf("Paid out: One Nickle\n"); // if three are alike, of any kind.. else printf("Paid out: ZERO\n"); // otherwise -- you lose.. } }

Main Program

16 When the button is pushed, the program will execute this assignment statement……. while (1) { // do forever.. while (PINA.0) // Wait for a button and seed++; // let the counter roll over and // over to form a seed. first = second = third = seed; // preload the columns do { // mix up the numbers // while waiting for button release. first ^= seed>>1; // Exclusive ORing in the moving seed second^= seed>>2; // can really stir up the numbers. third ^= seed>>3; seed++; // keep rolling over the seed pattern } while (PINA.0 == 0); // while the button is pressed for (count = 0; count < 5; count++) { // flash light when done.. for (delay = 0; delay < 10000; delay++) ; // just count up and wait PORTB.1 = 0; // turn the LED on.. for (delay = 0; delay < 10000; delay++) ; PORTB.1 = 1; // turn the LED off.. } first &= 3; // limit number to values from 0 to 3 second &= 3; third &= 3; printf("--> %d, %d ,%d <--\n",first, second, third); // show the values.. // determine a payout.. if((first == 3) && (second == 3) && (third == 3)) printf("Paid out: JACKPOT!!\n"); // all "cherries" else if((first == 3) || (second == 3) || (third == 3)) printf("Paid out: One Dime\n"); // if any are "cherries".. else if((first == second) && (second == third)) printf("Paid out: One Nickle\n"); // if three are alike, of any kind.. else printf("Paid out: ZERO\n"); // otherwise -- you lose.. } }

Main Program

17 Then the for loops are executed to blink the LED. PORTB.1 =0, instructs the program to make pin 1 of PORTB low, turning the LED on. PORTB.1 =1, instructs the program to make pin 1 of PORTB high, turning the LED off. while (1) { // do forever.. while (PINA.0) // Wait for a button and seed++; // let the counter roll over and // over to form a seed. first = second = third = seed; // preload the columns do { // mix up the numbers // while waiting for button release. first ^= seed>>1; // Exclusive ORing in the moving seed second^= seed>>2; // can really stir up the numbers. third ^= seed>>3; seed++; // keep rolling over the seed pattern } while (PINA.0 == 0); // while the button is pressed for (count = 0; count < 5; count++) { // flash light when done.. for (delay = 0; delay < 10000; delay++) ; // just count up and wait PORTB.1 = 0; // turn the LED on.. for (delay = 0; delay < 10000; delay++) ; PORTB.1 = 1; // turn the LED off.. } first &= 3; // limit number to values from 0 to 3 second &= 3; third &= 3; printf("--> %d, %d ,%d <--\n",first, second, third); // show the values.. // determine a payout.. if((first == 3) && (second == 3) && (third == 3)) printf("Paid out: JACKPOT!!\n"); // all "cherries" else if((first == 3) || (second == 3) || (third == 3)) printf("Paid out: One Dime\n"); // if any are "cherries".. else if((first == second) && (second == third)) printf("Paid out: One Nickle\n"); // if three are alike, of any kind.. else printf("Paid out: ZERO\n"); // otherwise -- you lose.. } }

Main Program

19 The remainder of this program will have no effect because we don’t have any output device connected (or simulated) while (1) { // do forever.. while (PINA.0) // Wait for a button and seed++; // let the counter roll over and // over to form a seed. first = second = third = seed; // preload the columns do { // mix up the numbers // while waiting for button release. first ^= seed>>1; // Exclusive ORing in the moving seed second^= seed>>2; // can really stir up the numbers. third ^= seed>>3; seed++; // keep rolling over the seed pattern } while (PINA.0 == 0); // while the button is pressed for (count = 0; count < 5; count++) { // flash light when done.. for (delay = 0; delay < 10000; delay++) ; // just count up and wait PORTB.1 = 0; // turn the LED on.. for (delay = 0; delay < 10000; delay++) ; PORTB.1 = 1; // turn the LED off.. } first &= 3; // limit number to values from 0 to 3 second &= 3; third &= 3; printf("--> %d, %d ,%d <--\n",first, second, third); // show the values.. // determine a payout.. if((first == 3) && (second == 3) && (third == 3)) printf("Paid out: JACKPOT!!\n"); // all "cherries" else if((first == 3) || (second == 3) || (third == 3)) printf("Paid out: One Dime\n"); // if any are "cherries".. else if((first == second) && (second == third)) printf("Paid out: One Nickle\n"); // if three are alike, of any kind.. else printf("Paid out: ZERO\n"); // otherwise -- you lose.. } }

Code Vision

20

 The program (Slot.c) was copied from the book

 It can be compiled by CodeVision and

simulated with AVR Studio

 It was typed in using WordPad

 So, it must be loaded into a CodeVision project

 The project must be created first

 Typically the wizard would be used, but this time we

won’t use it