Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example



Similar documents
How to use AVR Studio for Assembler Programming

An Introduction to MPLAB Integrated Development Environment

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

AVR033: Getting Started with the CodeVisionAVR C Compiler. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

MSP-EXP430G2 LaunchPad Workshop

Lecture 22: C Programming 4 Embedded Systems

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Using the HCS12 Serial Monitor on Wytec Dragon-12 boards. Using Motorola s HCS12 Serial Monitor on Wytec s Dragon-12 boards

SKP16C62P Tutorial 1 Software Development Process using HEW. Renesas Technology America Inc.

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

ET-BASE AVR ATmega64/128

Interrupts and the Timer Overflow Interrupts Huang Sections What Happens When You Reset the HCS12?

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

Quick Start Tutorial. Using the TASKING* Software Development Tools with the Intel 8x930 Family Evaluation Board

Getting Started with Embedded System Development using MicroBlaze processor & Spartan-3A FPGAs. MicroBlaze

AVR030: Getting Started with IAR Embedded Workbench for Atmel AVR. 8-bit Microcontrollers. Application Note. Features.

PC Base Adapter Daughter Card UART GPIO. Figure 1. ToolStick Development Platform Block Diagram

AVR Butterfly Training. Atmel Norway, AVR Applications Group

Lab Experiment 1: The LPC 2148 Education Board

13 Managing Devices. Your computer is an assembly of many components from different manufacturers. LESSON OBJECTIVES

PIC Programming in Assembly. (

AVR Timer/Counter. Prof Prabhat Ranjan DA-IICT, Gandhinagar

Project Manager Editor & Debugger

Application Note: AN00141 xcore-xa - Application Development

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

Sources: On the Web: Slides will be available on:

EXERCISE 3: String Variables and ASCII Code

An Introduction To Simple Scheduling (Primarily targeted at Arduino Platform)

Serial Communications

Programming PIC Microcontrollers in PicBasic Pro Lesson 1 Cornerstone Electronics Technology and Robotics II

Mixing C and assembly language programs Copyright 2007 William Barnekow <barnekow@msoe.edu> All Rights Reserved

Informatica e Sistemi in Tempo Reale

Assignment 09. Problem statement : Write a Embedded C program to switch-on/switch-off LED.

Hi-Speed USB Flash Disk User s Manual Guide

C Programming Structure of a C18 Program

Display Message on Notice Board using GSM

CP Lab 2: Writing programs for simple arithmetic problems

EE8205: Embedded Computer System Electrical and Computer Engineering, Ryerson University. Multitasking ARM-Applications with uvision and RTX

Install MS SQL Server 2012 Express Edition

Accurate Measurement of the Mains Electricity Frequency

INTRODUCTION TO SERIAL ARM

ENGI E1112 Departmental Project Report: Computer Science/Computer Engineering

Animated Lighting Software Overview

Yun Shield Quick Start Guide VERSION: 1.0 Version Description Date 1.0 Release 2014-Jul-08 Yun Shield Quick Start Guide 1 / 14

Setting Up a Windows Virtual Machine for SANS FOR526

Start A New Project with Keil Microcontroller Development Kit Version 5 and Freescale FRDM-KL25Z

QUICK START GUIDE. SG2 Client - Programming Software SG2 Series Programmable Logic Relay

Interface and Simulation of a LCD Text Display

Jump-Start Tutorial For ProcessModel

Software development and debugging for NXP ARM7 MCUs

Definitions and Documents

DsPIC HOW-TO GUIDE Creating & Debugging a Project in MPLAB

USBSPYDER08 Discovery Kit for Freescale MC9RS08KA, MC9S08QD and MC9S08QG Microcontrollers User s Manual

BitLocker To Go User Guide

8-bit Microcontroller. Application Note. AVR286: LIN Firmware Base for LIN/UART Controller. LIN Features. 1. Atmel LIN/UART Controller

Arduino project. Arduino board. Serial transmission

by NetMedia, Inc. All rights reserved. Basic Express, BasicX, BX-01, BX-24 and BX-35 are trademarks of NetMedia, Inc.

EVAL-UFDC-1/UFDC-1M-16

Control III Programming in C (small PLC)

AVR317: Using the Master SPI Mode of the USART module. 8-bit Microcontrollers. Application Note. Features. Introduction

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Introduction to the use of the environment of Microsoft Visual Studio 2008

8051 MICROCONTROLLER COURSE

Using the RDTSC Instruction for Performance Monitoring

Tutorial for MPLAB Starter Kit for PIC18F

Freescale Semiconductor, I

Connecting your Omega/BetaPAT PLUS to a PC via a USB

Computer Organization and Components

How to design and implement firmware for embedded systems

Introduction to LogixPro - Lab

APPLICATION NOTE. Atmel AT02607: Wireless Product Development Using Atmel Studio and ASF. Atmel MCU Wireless. Description.

Setting up Windows Phone 8 environment in VMWare

2.0 Command and Data Handling Subsystem

ezsystem elab16m Project 1F: Alarm System (Full Project description)

3. Programming the STM32F4-Discovery

Using Windows Task Scheduler instead of the Backup Express Scheduler

Lab 1 Course Guideline and Review

BioWin Network Installation

Dual Ports Serial PC Card User Manual

Speed Based on Volume Values & Assignment (Part 1)

ELEG3924 Microprocessor Ch.7 Programming In C

LPR for Windows 95/98/Me/2000/XP TCP/IP Printing User s Guide. Rev. 03 (November, 2001)

Chapter 28: Expanding Web Studio

After: bmotorreflected[port2]= 1; //Flip port2 s direction

Keil C51 Cross Compiler

Stage One - Applying For an Assent Remote Access Login

Chapter 5 Real time clock by John Leung

AN1229. Class B Safety Software Library for PIC MCUs and dspic DSCs OVERVIEW OF THE IEC STANDARD INTRODUCTION

Introduction. - Please be sure to read and understand Precautions and Introductions in CX-Simulator Operation Manual and

EMBEDDED C USING CODEWARRIOR Getting Started Manual

Chapter 13. PIC Family Microcontroller

PM1122 INT DIGITAL INTERFACE REMOTE

How to configure the DBxtra Report Web Service on IIS (Internet Information Server)

SPI. Overview and Use of the PICmicro Serial Peripheral Interface. Getting Started: SPI

Configuration Guide. Remote Backups How-To Guide. Overview

Software Toolbox License Transfer Guide. TOP Server OPC Server

Cloud Services ADM. Agent Deployment Guide

Transcription:

Microcontroller Systems ELET 3232 Topic 8: Slot Machine Example 1

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

Slot Machine // // "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; DDRA = 0x00; DDRB = 0x02; // Initialize the I/O ports // port A all inputs // 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; 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.. } } 3

Initialization 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 // // "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; DDRA = 0x00; DDRB = 0x02; //Initialize the UART control register //RX & TX enabled, 8 data bits // Initialize the I/O ports // port A all inputs // port B.1 is an output (light) UCSRB=0x18; // initialize the UART's baud rate UBRRL=xtal/16/baud-1; 4

Initialization For the Atmega128, we have to change this to Mega128 but, the Atmega128 does not have the UART initialized below // // "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; DDRA = 0x00; DDRB = 0x02; // Initialize the I/O ports // port A all inputs // 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; 5

Initialization 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 // // "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; DDRA = 0x00; DDRB = 0x02; // Initialize the I/O ports // port A all inputs // 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; 6

Initialization The #define commands instructs the compiler to equate these values with the labels baud and xtal. They are used in the program // // "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; DDRA = 0x00; DDRB = 0x02; // Initialize the I/O ports // port A all inputs // 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; 7

Initialization 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 // // "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; DDRA = 0x00; DDRB = 0x02; //Initialize the UART control register //RX & TX enabled, 8 data bits // Initialize the I/O ports // port A all inputs // port B.1 is an output (light) UCSRB=0x18; // initialize the UART's baud rate UBRRL=xtal/16/baud-1; 8

Initialization This program has one function: main(). It: Accepts no inputs and Returns no values // // "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; DDRA = 0x00; DDRB = 0x02; // Initialize the I/O ports // port A all inputs // 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; 9

Initialization It first initializes Port A and Port B // // "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; DDRA = 0x00; DDRB = 0x02; // Initialize the I/O ports // port A all inputs // 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; 10

Initialization It first initializes Port A and Port B and then initializes the UART // // "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; DDRA = 0x00; DDRB = 0x02; // Initialize the I/O ports // port A all inputs // 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; 11

Main Program 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.. } } 12

Main Program 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 This brace goes with main ( ) 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

Main Program 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.. } } 14

Main Program 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. 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.. } } 15

Main Program 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.. } } 16

Main Program 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.. } } 17

Main Program The program then ANDS each of the variables with the constant 3. It ensures that the variables have to be between 0 and 3. 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.. } } 18

Main Program 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.. } } 19

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 20

Open CodeVision Normally, the most recent project worked on would be loaded In this case, no project had been opened 21

Choose NEW from the File Menu 22

Choose NEW from the File Menu This dialog box will open Choose project 23

Choose NEW from the File Menu This dialog box will open Choose project You will be asked if you want to use the Wizard Normally, click YES 24

The Wizard allows you to do many things Specify AVR Specify Clock speed Configure ports Provide project information Define interrupts and timers etc.. 25

We will choose NO (this time) because most of these parameters have been specified in the source file 26

The Configure Project Box appears 27

The Configure Project Box appears Click Add Choose source file (I had typed it in in WordPad and stored it in this directory) Click OPEN 28

The Configure Project Box appears Click Add The source file appears as part of the project 29

The Configure Project Box appears Click Add The source file appears as part of the project You may specify output directories 30

The Configure Project Box appears You can specify many parameters AVR Clock speed Memory model RAM size etc. 31

The source file appears 32

The source file appears I assumed the file was correct Chose Build All 33

The source file appears I assumed the file was correct Chose Build All You ll get information about your program 34

The source file appears I assumed the file was correct Chose Build All You ll get information about your program And the assembler output 35

Click OK 36

Click OK And click on the debugger.. 37

Click OK And click on the debugger AVR Studio is called (to run its simulator) 38

Choose OPEN 39

Choose OPEN And then choose the *.cof file that was created by CodeVision 40

Choose OPEN And then choose the *.cof file that was created by CodeVision Then AVR Studio prompts you to save its project file Click SAVE 41

Choose AVR and AVR Simulator 42

Choose AVR and AVR Simulator Click finish The simulator (debugger) starts automatically 43

You ll want to watch the variables and registers (how else would you know if your program is working) 44

From the VIEW Menu choose Watch 45

From the VIEW Menu choose Watch The watch window opens 46

Double click on the slots in the NAME column Type in the names of the variables you want to watch 47

You may also want to watch the registers or memory Click on VIEW registers 48

The register window opens 49

You may also want to view the ports Click the + next to I/O ATMEGA128 And then the + next to PORTA and PORTB 50

Finally, set break points (variables will not be updated will running the program) 51

Click RUN. 52

Click RUN and the program will run to the 1 st break point 53

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 54

A few iterations later, the values have changed for the variables 55

A few iterations later, the values have changed for the variables Set PINA.0 and the program will continue 56

We could also insert some other instructions to test the output For example: Instead of printf () commands we could set bits in PortB Or declare a few other variables to set or clear under specific circumstances 57

Summary 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 58