Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.
-
Upload
madison-fowler -
Category
Documents
-
view
221 -
download
0
Transcript of Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.
![Page 1: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/1.jpg)
Embedded Systems Programming
1
ETEE 3285
Topic HW3: Coding, Compiling, Simulating
![Page 2: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/6.jpg)
6
And again
LED Sequence
![Page 7: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/7.jpg)
LED Sequence
7
And again
![Page 8: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/8.jpg)
LED Sequence
8
And again
![Page 9: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/9.jpg)
LED Sequence
9
And again
![Page 10: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/10.jpg)
LED Sequence
10
And again
![Page 11: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/14.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/15.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/16.jpg)
Open New Project
16
Click on File -> New
![Page 17: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/17.jpg)
Open New Project
17
Click on Project and then OK
![Page 18: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/18.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/19.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/20.jpg)
Ports
20
You can also initialize the ports
![Page 21: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/21.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/22.jpg)
Done?
22
When done, click on
File -> Generate, Save, and Exit
![Page 23: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/23.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/24.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/25.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/26.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/27.jpg)
Enter the Program
27
Place the program here
![Page 28: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/28.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/29.jpg)
No Errors
29
Hopefully we’ll have no errors or warnings
![Page 30: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/30.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/31.jpg)
Open Project
31
Choose Open
![Page 32: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/32.jpg)
*.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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/33.jpg)
Simulator and ATMega128
33
Choose AVR simulator and the ATmega128
![Page 34: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/34.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/35.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/36.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/37.jpg)
PortB.6
37
Now PORTB.6 = 1
![Page 38: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/38.jpg)
PortB.5
38
Now PORTB.5 = 1
![Page 39: Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/39.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/40.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/41.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/42.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649e115503460f94afc9e4/html5/thumbnails/43.jpg)
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