embedded systems programming 1 etee 3285 topic hw3: coding, compiling, simulating

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

Upload: madison-fowler

Post on 26-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

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

Embedded Systems Programming

1

ETEE 3285

Topic HW3: Coding, Compiling, Simulating

Page 2: 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

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

LED Sequence

3

The leftmost LED should light (all others are off)

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

4

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

LED Sequence

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

5

Then shift the on LED to the right again

LED Sequence

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

6

And again

LED Sequence

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

LED Sequence

7

And again

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

LED Sequence

8

And again

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

LED Sequence

9

And again

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

LED Sequence

10

And again

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

LED Sequence

11

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

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

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

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

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.

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

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

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

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

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

Open New Project

16

Click on File -> New

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

Open New Project

17

Click on Project and then OK

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

Use Code Wizard

18

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

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

Code Wizard

19

Here you can change the chip and the clock speed

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

Ports

20

You can also initialize the ports

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

Serial Ports, Interrupts, Timers

21

The Code Wizard also helps you setup:

External Memory

Interrupts

Timers

Serial Ports

LCDs

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

Done?

22

When done, click on

File -> Generate, Save, and Exit

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

Save Files

23

When done, click on

File -> Generate, Save, and Exit

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

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

New File

24

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

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

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

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

Template

26

You’ll be left with a template

Place global variables here

Local variables here

And the program statements here

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

Enter the Program

27

Place the program here

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

Build All

28

Save everything

And the choose Build All from the Project Menu

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

No Errors

29

Hopefully we’ll have no errors or warnings

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

The Debugger

30

So, its time to simulate

Click on the ladybug

This will call up AVR Studio

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

Open Project

31

Choose Open

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

*.cof & *_cof.aps files

32

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

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

Simulator and ATMega128

33

Choose AVR simulator and the ATmega128

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

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

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

I/O

35

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

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

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)

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

PortB.6

37

Now PORTB.6 = 1

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

PortB.5

38

Now PORTB.5 = 1

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

PortB.4-1

39

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

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

PortB.0…..Glitch

40

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

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

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

};}

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

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

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

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