Download presentation
Presentation is loading. Please wait.
Published byMadison Fowler Modified over 9 years ago
1
Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating
2
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
3
LED Sequence 3 The leftmost LED should light (all others are off)
4
4 Then turn off the leftmost LED and turn on the next one to its right LED Sequence
5
5 Then shift the on LED to the right again LED Sequence
6
6 And again LED Sequence
7
7 And again
8
LED Sequence 8 And again
9
LED Sequence 9 And again
10
LED Sequence 10 And again
11
LED Sequence 11 Then start over at the leftmost LED (and continue forever)
12
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; }
13
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.
14
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
15
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
16
Open New Project 16 Click on File -> New
17
Open New Project 17 Click on Project and then OK
18
Use Code Wizard 18 It is easiest to use the Code Wizard, so click yes.
19
Code Wizard 19 Here you can change the chip and the clock speed
20
Ports 20 You can also initialize the ports
21
Serial Ports, Interrupts, Timers 21 The Code Wizard also helps you setup: External Memory Interrupts Timers Serial Ports LCDs
22
Done? 22 When done, click on File -> Generate, Save, and Exit
23
Save Files 23 When done, click on File -> Generate, Save, and Exit It will ask you to save 3 files it will prompt you
24
New File 24 Finally, the new file will open (It doesn’t take long)
25
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
26
Template 26 You’ll be left with a template Place global variables here Local variables here And the program statements here
27
Enter the Program 27 Place the program here
28
Build All 28 Save everything And the choose Build All from the Project Menu
29
No Errors 29 Hopefully we’ll have no errors or warnings
30
The Debugger 30 So, its time to simulate Click on the ladybug This will call up AVR Studio
31
Open Project 31 Choose Open
32
*.cof & *_cof.aps files 32 Choose Open and then the *.cof and the _cof.aps files then click next
33
Simulator and ATMega128 33 Choose AVR simulator and the ATmega128
34
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
35
I/O 35 PortB still has to be opened so that we can see the rest of Portb
36
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)
37
PortB.6 37 Now PORTB.6 = 1
38
PortB.5 38 Now PORTB.5 = 1
39
PortB.4-1 39 Now PORTB.4 = 1 Now PORTB.3 = 1 Now PORTB.2 = 1 PORTB.1 = 1
40
PortB.0…..Glitch 40 Now PORTB.0 = 1, then something happens – it executes the while, and during that time PORTB.0 stays 1.
41
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]; }; }
42
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; }
43
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.