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>
#define xtal 4000000L
#define baud 9600
#include <stdio.h>
do {
/* processor specific information */
/* quartz crystal frequency [Hz] */
/* Baud rate */
/* 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;
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
// 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;
// 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;
second &= 3;
third &= 3;
// limit number to values from 0 to 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>
#define xtal 4000000L
#define baud 9600
#include <stdio.h>
/* processor specific information */
/* quartz crystal frequency [Hz] */
/* Baud rate */
/* 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;
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>
#define xtal 4000000L
#define baud 9600
#include <stdio.h>
/* processor specific information */
/* quartz crystal frequency [Hz] */
/* Baud rate */
/* 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>
#define xtal 4000000L
#define baud 9600
#include <stdio.h>
/* processor specific information */
/* quartz crystal frequency [Hz] */
/* Baud rate */
/* 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>
#define xtal 4000000L
#define baud 9600
#include <stdio.h>
/* processor specific information */
/* quartz crystal frequency [Hz] */
/* Baud rate */
/* 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 16bit unsigned integer
//
// "Slot Machine" -- The Mini-Exercise
//
//
Phase 1..
#include <Mega8515.h>
#define xtal 4000000L
#define baud 9600
#include <stdio.h>
/* processor specific information */
/* quartz crystal frequency [Hz] */
/* Baud rate */
/* 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;
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>
#define xtal 4000000L
#define baud 9600
#include <stdio.h>
/* processor specific information */
/* quartz crystal frequency [Hz] */
/* Baud rate */
/* 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>
#define xtal 4000000L
#define baud 9600
#include <stdio.h>
/* processor specific information */
/* quartz crystal frequency [Hz] */
/* Baud rate */
/* 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>
#define xtal 4000000L
#define baud 9600
#include <stdio.h>
/* processor specific information */
/* quartz crystal frequency [Hz] */
/* Baud rate */
/* 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
while (1) {
while (PINA.0)
seed++;
Main Program
The main program is contained in
the while (1) { } loop structure: it
mean run forever
// do forever..
// Wait for a button and
// let the counter roll over and
// over to form a seed.
first = second = third = seed; // preload the columns
do {
first ^= seed>>1;
second^= seed>>2;
third ^= seed>>3;
seed++;
} while (PINA.0 == 0);
// mix up the numbers
// while waiting for button release.
// Exclusive ORing in the moving seed
// can really stir up the numbers.
// keep rolling over the seed pattern
// 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
while (1) {
while (PINA.0)
seed++;
Main Program
The main program is contained in
the while (1) { } loop structure: it
mean run forever
// do forever..
// Wait for a button and
// let the counter roll over and
// over to form a seed.
first = second = third = seed; // preload the columns
do {
first ^= seed>>1;
second^= seed>>2;
third ^= seed>>3;
seed++;
} while (PINA.0 == 0);
// mix up the numbers
// while waiting for button release.
// Exclusive ORing in the moving seed
// can really stir up the numbers.
// keep rolling over the seed pattern
// 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;
This brace goes with main ( )
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
while (1) {
while (PINA.0)
seed++;
Main Program
This while loop is made up of
these two instructions
// do forever..
// Wait for a button and
// let the counter roll over and
// over to form a seed.
first = second = third = seed; // preload the columns
do {
first ^= seed>>1;
second^= seed>>2;
third ^= seed>>3;
seed++;
} while (PINA.0 == 0);
// mix up the numbers
// while waiting for button release.
// Exclusive ORing in the moving seed
// can really stir up the numbers.
// keep rolling over the seed pattern
// 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
while (1) {
while (PINA.0)
seed++;
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.
// do forever..
// Wait for a button and
// let the counter roll over and
// over to form a seed.
first = second = third = seed; // preload the columns
do {
first ^= seed>>1;
second^= seed>>2;
third ^= seed>>3;
seed++;
} while (PINA.0 == 0);
// mix up the numbers
// while waiting for button release.
// Exclusive ORing in the moving seed
// can really stir up the numbers.
// keep rolling over the seed pattern
// 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
while (1) {
while (PINA.0)
seed++;
Main Program
When the button is pushed, the
program will execute this
assignment statement…….
// do forever..
// Wait for a button and
// let the counter roll over and
// over to form a seed.
first = second = third = seed; // preload the columns
do {
first ^= seed>>1;
second^= seed>>2;
third ^= seed>>3;
seed++;
} while (PINA.0 == 0);
// mix up the numbers
// while waiting for button release.
// Exclusive ORing in the moving seed
// can really stir up the numbers.
// keep rolling over the seed pattern
// 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
while (1) {
while (PINA.0)
seed++;
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.
// do forever..
// Wait for a button and
// let the counter roll over and
// over to form a seed.
first = second = third = seed; // preload the columns
do {
first ^= seed>>1;
second^= seed>>2;
third ^= seed>>3;
seed++;
} while (PINA.0 == 0);
// mix up the numbers
// while waiting for button release.
// Exclusive ORing in the moving seed
// can really stir up the numbers.
// keep rolling over the seed pattern
// 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
while (1) {
while (PINA.0)
seed++;
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.
// do forever..
// Wait for a button and
// let the counter roll over and
// over to form a seed.
first = second = third = seed; // preload the columns
do {
first ^= seed>>1;
second^= seed>>2;
third ^= seed>>3;
seed++;
} while (PINA.0 == 0);
// mix up the numbers
// while waiting for button release.
// Exclusive ORing in the moving seed
// can really stir up the numbers.
// keep rolling over the seed pattern
// 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
while (1) {
while (PINA.0)
seed++;
Main Program
The remainder of this program
will have no effect because we
don’t have any output device
connected (or simulated)
// do forever..
// Wait for a button and
// let the counter roll over and
// over to form a seed.
first = second = third = seed; // preload the columns
do {
first ^= seed>>1;
second^= seed>>2;
third ^= seed>>3;
seed++;
} while (PINA.0 == 0);
// mix up the numbers
// while waiting for button release.
// Exclusive ORing in the moving seed
// can really stir up the numbers.
// keep rolling over the seed pattern
// 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
Code Vision
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
Code Vision
Open CodeVision
Normally, the most
recent project worked
on would be loaded
In this case, no project
had been opened
21
Code Vision
Choose NEW from
the File Menu
22
Code Vision
Choose NEW from
the File Menu
This dialog box will
open
Choose project
23
Code Vision
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
Code Vision
The Wizard allows you
to do many things
Specify AVR
Specify Clock speed
Configure ports
Provide project
information
Define interrupts and
timers
etc……..
25
Code Vision
We will choose NO
(this time) because
most of these
parameters have
been specified in the
source file
26
Code Vision
The Configure
Project Box appears
27
Code Vision
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
Code Vision
The Configure
Project Box appears
Click Add
The source file
appears as part of the
project
29
Code Vision
The Configure
Project Box appears
Click Add
The source file
appears as part of the
project
You may specify
output directories
30
Code Vision
The Configure
Project Box appears
You can specify many
parameters
AVR
Clock speed
Memory model
RAM size
etc…….
31
Code Vision
The source file
appears
32
Code Vision
The source file
appears
I assumed the file
was correct
Chose Build All
33
Code Vision
The source file
appears
I assumed the file
was correct
Chose Build All
You’ll get
information about
your program
34
Code Vision
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
Code Vision
Click OK
36
Code Vision
Click OK
And click on the
debugger…..
37
Code Vision
Click OK
And click on the
debugger
AVR Studio is
called (to run its
simulator)
38
Code Vision
Choose OPEN
39
Code Vision
Choose OPEN
And then choose
the *.cof file that
was created by
CodeVision
40
Code Vision
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
Code Vision
Choose AVR and
AVR Simulator
42
Code Vision
Choose AVR and
AVR Simulator
Click finish
The simulator
(debugger) starts
automatically
43
Code Vision
You’ll want to watch
the variables and
registers
(how else would
you know if your
program is
working)
44
Code Vision
From the VIEW Menu
choose Watch
45
Code Vision
From the VIEW Menu
choose Watch
The watch window
opens
46
Code Vision
Double click on the
slots in the NAME
column
Type in the names
of the variables you
want to watch
47
Code Vision
You may also want to
watch the registers or
memory
Click on VIEW
registers
48
Code Vision
The register window
opens
49
Code Vision
You may also want to
view the ports
Click the + next to
I/O ATMEGA128
And then the + next
to PORTA and
PORTB
50
Code Vision
Finally, set break
points (variables will
not be updated will
running the program)
51
Code Vision
Click RUN …….
52
Code Vision
Click RUN and the
program will run to the
1st break point
53
Code Vision
Click RUN and the
program will run to the
1st break point
The do/while loop will
continue as long as
PINA.0 =0
54
Code Vision
A few iterations later,
the values have
changed for the
variables
55
Code Vision
A few iterations later,
the values have
changed for the
variables
Set PINA.0 and the
program will continue
56
Code Vision
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
Discussed and used #define and #include
Ports
ex: first ^=seed>>1;
Read ports
Wrote to ports
Used some of the control statements learned in
the last topic
58
© Copyright 2025 Paperzz