Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.

Post on 26-Dec-2015

227 views 0 download

Tags:

Transcript of Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.

Embedded Systems Programming

1

ETEE 3285

Topic HW3: Coding, Compiling, Simulating

Assignment Write the “Chasing Lights Program” in C Use Codevision AVR to compile the program

and remove all syntax errors Simulate the program in AVR Studio, identify

all logic errors If logic errors are found, go back to Codevision

AVR and make the necessary changes

2

LED Sequence

3

The leftmost LED should light (all others are off)

4

Then turn off the leftmost LED and turn on the next one to its right

LED Sequence

5

Then shift the on LED to the right again

LED Sequence

6

And again

LED Sequence

LED Sequence

7

And again

LED Sequence

8

And again

LED Sequence

9

And again

LED Sequence

10

And again

LED Sequence

11

Then start over at the leftmost LED (and continue forever)

First Program

12

Many inexperienced C programmers will write a program as shown Assuming a 1 is on and a 0 is off

void main(void){ DDRB = 0xFF; // PortB is all outputs PORTB = 0x00; // turn off all the lights while(1) // do forever.. { PORTB = 0b10000000; PORTB = 0b01000000; PORTB = 0b00100000; PORTB = 0b00010000; PORTB = 0b00001000; PORTB = 0b00000100; PORTB = 0b00000010; PORTB = 0b00000001; }}

void main(void){ DDRB = 0xFF; // PortB is all outputs PORTB = 0x00; // turn off all the lights while(1) // do forever.. { PORTB = 0b10000000; PORTB = 0b01000000; PORTB = 0b00100000; PORTB = 0b00010000; PORTB = 0b00001000; PORTB = 0b00000100; PORTB = 0b00000010; PORTB = 0b00000001; }}

First Program

13

Many inexperienced C programmers will write a program as shown Assuming a 1 is on and a 0 is off

void main(void){ DDRB = 0xFF; // PortB is all outputs PORTB = 0x00; // turn off all the lights while(1) // do forever.. { PORTB = 0b10000000; PORTB = 0b01000000; PORTB = 0b00100000; PORTB = 0b00010000; PORTB = 0b00001000; PORTB = 0b00000100; PORTB = 0b00000010; PORTB = 0b00000001; }}

void main(void){ DDRB = 0xFF; // PortB is all outputs PORTB = 0x00; // turn off all the lights while(1) // do forever.. { PORTB = 0b10000000; PORTB = 0b01000000; PORTB = 0b00100000; PORTB = 0b00010000; PORTB = 0b00001000; PORTB = 0b00000100; PORTB = 0b00000010; PORTB = 0b00000001; }}

While this will work, there are better solutions

This is what I call a “brute force” program – it is inelegant.

First Program

14

Many inexperienced C programmers will write a program as shown Assuming a 1 is on and a 0 is off

void main(void){ DDRB = 0xFF; // PortB is all outputs PORTB = 0x00; // turn off all the lights while(1) // do forever.. { PORTB = 0b10000000; PORTB = 0b01000000; PORTB = 0b00100000; PORTB = 0b00010000; PORTB = 0b00001000; PORTB = 0b00000100; PORTB = 0b00000010; PORTB = 0b00000001; }}

void main(void){ DDRB = 0xFF; // PortB is all outputs PORTB = 0x00; // turn off all the lights while(1) // do forever.. { PORTB = 0b10000000; PORTB = 0b01000000; PORTB = 0b00100000; PORTB = 0b00010000; PORTB = 0b00001000; PORTB = 0b00000100; PORTB = 0b00000010; PORTB = 0b00000001; }}

While this will work, there are better solutions

This is what I call a “brute force” program – it is inelegant.

Let’s enter it in Codevision

Open Codevision

15

Open Codevision.

It will only look like this IF you were not working on a program the last time you used it

Open New Project

16

Click on File -> New

Open New Project

17

Click on Project and then OK

Use Code Wizard

18

It is easiest to use the Code Wizard, so click yes.

Code Wizard

19

Here you can change the chip and the clock speed

Ports

20

You can also initialize the ports

Serial Ports, Interrupts, Timers

21

The Code Wizard also helps you setup:

External Memory

Interrupts

Timers

Serial Ports

LCDs

Done?

22

When done, click on

File -> Generate, Save, and Exit

Save Files

23

When done, click on

File -> Generate, Save, and Exit

It will ask you to save 3 files it will prompt you

New File

24

Finally, the new file will open (It doesn’t take long)

Unnecessary Code

25

But, there is a lot of unnecessary code: it initializes all the ports, interrupts, timers,etc according to the default parameters – but that code is not needed for this program

Template

26

You’ll be left with a template

Place global variables here

Local variables here

And the program statements here

Enter the Program

27

Place the program here

Build All

28

Save everything

And the choose Build All from the Project Menu

No Errors

29

Hopefully we’ll have no errors or warnings

The Debugger

30

So, its time to simulate

Click on the ladybug

This will call up AVR Studio

Open Project

31

Choose Open

*.cof & *_cof.aps files

32

Choose Open and then the *.cof and the _cof.aps files then click next

Simulator and ATMega128

33

Choose AVR simulator and the ATmega128

The AVR Simulator

34

This setup opens because it is the way I left AVR simulator the last time I exited – yours may look different.

I click on I/O ATmega128 so that I can see PortB

I/O

35

PortB still has to be opened so that we can see the rest of Portb

PortB.7

36

With PortB open, PORTB, DDRB, and PINB are all visible

Note also that F11 has been pressed so that the instruction that sets b7 in PORTB has executed (PORTB.7 = 1)

PortB.6

37

Now PORTB.6 = 1

PortB.5

38

Now PORTB.5 = 1

PortB.4-1

39

Now PORTB.4 = 1Now PORTB.3 = 1Now PORTB.2 = 1 PORTB.1 = 1

PortB.0…..Glitch

40

Now PORTB.0 = 1, then something happens – it executes the while, and during that time PORTB.0 stays 1.

Arrays

41

So a new program was tried using an array and a “for” loop. But there was still a delay while the “while” and “for” statements were executing.

One more program was tried

void main(void){char LEDs[8] = { 0b10000000, 0b01000000, 0b00100000, 0b00010000, 0b00001000, 0b00000100, 0b00000010, 0b00000001 }; char i

PORTB=0x00;DDRB=0xFF;

while (1) {

for (i=0; i<8; ++i) PORTB = LEDs[i];

};}

void main(void){char LEDs[8] = { 0b10000000, 0b01000000, 0b00100000, 0b00010000, 0b00001000, 0b00000100, 0b00000010, 0b00000001 }; char i

PORTB=0x00;DDRB=0xFF;

while (1) {

for (i=0; i<8; ++i) PORTB = LEDs[i];

};}

Shift Operator

42

This program worked very well – there was no difference in timing between one PORTB bit and the next, and especially between PORTB.0 and PORTB.7

char leds = 0b10000000;

void main(void){ DDRB = 0xFF; // PortB is all outputs PORTB = 0x00; // turn off all the lights while(1) // do forever.. { PORTB = leds; leds >>= 1; if (leds == 0) leds = 128; }}

char leds = 0b10000000;

void main(void){ DDRB = 0xFF; // PortB is all outputs PORTB = 0x00; // turn off all the lights while(1) // do forever.. { PORTB = leds; leds >>= 1; if (leds == 0) leds = 128; }}

Summary The “Chasing Lights Program” was written in

C several times Brute Force Arrays Shift Operator

The shift operator proved to be the best Discovered while simulating

43