• No results found

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

N/A
N/A
Protected

Academic year: 2021

Share "Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example"

Copied!
58
0
0

Loading.... (view fulltext now)

Full text

(1)

1

ELET 3232

(2)

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

(3)

3 #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;

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.. }

(4)

Initialization

4

//

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

(5)

5

#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

(6)

Initialization

6

//

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

They will generate errors for the

128

(7)

7

#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

(8)

Initialization

8

//

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

(9)

9

#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;

This program has one function:

main(). It:

Accepts no inputs

and

(10)

Initialization

10

//

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

(11)

11

#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

(12)

Main Program

12

The main program is contained in

the

while (1) { }

loop structure: it

mean run forever

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..

}

(13)

13

The main program is contained in

the

while (1) { }

loop structure: it

mean run forever

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..

}

}

(14)

Main Program

14

This

while loop

is made up of

these two instructions

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.. }

(15)

15

This

while loop

is made up of

these two instructions. As long as

the button is not pushed

(connected to bit 0 of Port A),

PINA.0 will be 1 (or TRUE) and

seed will be incremented.

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.. }

(16)

Main Program

16

When the button is pushed, the

program will execute this

assignment statement…….

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.. }

(17)

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.

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.. }

(18)

Main Program

18

The program then ANDS each of

the variables with the constant

“3”. It ensures that the variables

have to be between 0 and 3.

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.. }

(19)

19

The remainder of this program

will have no effect because we

don’t have any output device

connected (or simulated)

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..

} }

(20)

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

(21)

21

Normally, the most

recent project worked

on would be loaded

In this case, no project

(22)

22

Choose NEW from

(23)

23

the File Menu

This dialog box will

open

(24)

24

Choose NEW from

the File Menu

This dialog box will

open

Choose project

You will be asked if

you want to use the

Wizard

(25)

25

to do many things

Specify AVR

Specify Clock speed

Configure ports

Provide project

information

Define interrupts and

timers

(26)

26

We will choose NO

(this time) because

most of these

parameters have

been specified in the

source file

(27)

27

(28)

28

The Configure

Project Box appears

Click Add

Choose source file

(I had typed it in in

WordPad and

stored it in this

directory)

(29)

29

Project Box appears

Click Add

The source file

appears as part of the

project

(30)

30

The Configure

Project Box appears

Click Add

The source file

appears as part of the

project

You may specify

(31)

31

Project Box appears

You can specify many

parameters

AVR

Clock speed

Memory model

RAM size

etc…….

(32)

32

The source file

(33)

33

appears

I assumed the file

was correct

(34)

34

The source file

appears

I assumed the file

was correct

Chose Build All

You’ll get

information about

your program

(35)

35

appears

I assumed the file

was correct

Chose Build All

You’ll get

information about

your program

And the assembler

(36)

36

(37)

37

And click on the

(38)

38

Click OK

And click on the

debugger

AVR Studio is

called (to run its

simulator)

(39)
(40)

40

Choose OPEN

And then choose

the *.cof file that

was created by

CodeVision

(41)

41

And then choose

the *.cof file that

was created by

CodeVision

Then AVR Studio

prompts you to

save its project file

(42)

42

Choose AVR and

(43)

43

AVR Simulator

Click finish

The simulator

(debugger) starts

automatically

(44)

44

You’ll want to watch

the variables and

registers

(how else would

you know if your

program is

(45)

45

(46)

46

From the VIEW Menu

choose Watch

The watch window

(47)

47

slots in the NAME

column

Type in the names

of the variables you

want to watch

(48)

48

You may also want to

watch the registers or

memory

Click on VIEW

(49)

49

(50)

50

You may also want to

view the ports

Click the + next to

I/O ATMEGA128

And then the + next

to PORTA and

PORTB

(51)

51

points (variables will

not be updated will

running the program)

(52)

52

(53)

53

program will run to the

1

st

break point

(54)

54

Click RUN and the

program will run to the

1

st

break point

The do/while loop will

continue as long as

PINA.0 =0

(55)

55

the values have

changed for the

variables

(56)

56

A few iterations later,

the values have

changed for the

variables

Set PINA.0 and the

(57)

57

test the output

For example:

Instead of printf () commands we could set

bits in PortB

Or declare a few other variables to set or

(58)

We worked through a complete example using

CodeVision and AVR Studio

Saw a few ‘creative’ instructions

ex: first ^=seed>>1;

Discussed and used #define and #include

Ports

Read ports

Wrote to ports

Used some of the control statements learned in

the last topic

References

Related documents

Conclusion: Eosinophilic inflammation was related to characteristics of asthma and sputum eosinophils. However, neutrophilic in- flammation reflected neither asthma features,

It is thus important that you make sure that you use any data about students, training and labour market needs into your quality assurance system and to then use it to ensure

The Proactive Community Supervision (PCS) strategy adopts the tenets of science- based research into reframing supervision services for offenders. The emphasis of the strategy is

Abstract — This paper describes the implementation of a GSM (Global System for Mobile) network using a USRP (Universal Software Radio Peripheral) module integrated with OpenBTS

Job Description Students with Federal Work-Study can do paid 'community service' work by becoming a tutor in either reading or math for at-risk children up to Grade Six at

A corporation would be liable for the acts of its Board of Directors and officers if the said acts were performed by them in accordance with powers granted to them under

Analizom upitnika upitnika UIQ-7 (utjecaj poremećaja funkcije mokraćnog mjehura na svakodnevni život) prije i nakon tri, šest i dvanaest mjeseci od operacijske

It was important to develop a resource that could be used in workplace training to teach occupational health and safety competencies while taking into account the language and