3. Initialize an array with binary numbers 0 to 9. Use a \"for loop\" to display
ID: 3560623 • Letter: 3
Question
3. Initialize an array with binary numbers 0 to 9. Use a "for loop" to display each array element on the Tower LEDs and have a one-second delay between each display. Define all of the variables as unsigned character. The delay should use inline assembly language.
4. Modify the for loop in Problem 3 so that it displays numbers starting from 9 down to 0. In other words, the loop should become: for( i=9; i>= 0; i--). Run the program and observe the LED values. Does the loop end?
5. Recompile the code in Problem 4, but this time, do not declare the loop counter i as unsigned char. Declare i as char. Does the loop end?
6. Modify Problem 4 so that it displays the values from 0 to 9 continuously.
Explanation / Answer
3.)PORTA is Port A data register and DDRA is Port A data direction register.
#include<avr/io.h>
#include<util/delay.h>
mov bp, 43690
mov si, 43690
delay:
dec bp
nop
jnz delay
dec si
cmp si,0
jnz delay”);
PTT= 0x30;
delay(1000);
PTT = dam[i];
for(;;);
}
}
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define D_1MS (24000/12) // Inner loop takes 12 cycles
// Need 24,000 cycles for 1 ms
void delay(unsigned short num);
main()
{
const char dam[] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned int i;
DDRB = 0xff; /* Make PORTB output */
PORTB = 0; /* Start with all off */
i = 0;
while(1)
{
PORTB = dam[i];
//inline assembly code for delay
mov bp, 43690
mov si, 43690
delay:
dec bp
nop
jnz delay
dec si
cmp si,0
jnz delay”);
i = i + 1;
if (i >= sizeof(dam)) i = 0; /* Start over when */ /* end is reached */
}
}
4.)
void main(void) {
char dam[10]= {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; // sets the array in decimal 0-9
unsigned char i;
DDRB = 0xFF;
for(i=9;i>=0;i--){
PORTB = dam[i] ;
DDRT = 0xff;
PTT= 0x30;
delay(1000);
PTT = dam[i];
}
}
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define D_1MS (24000/12) // Inner loop takes 12 cycles
// Need 24,000 cycles for 1 ms
void delay(unsigned short num);
main()
{
const char dam[] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned int i;
DDRB = 0xff; /* Make PORTB output */
PORTB = 0; /* Start with all off */
i = 9;
while(1)
{
PORTB = dam[i];
delay(1000);
i = i - 1;
if (i <= sizeof(dam)) i = 10; /* Start over again*/
}
}
5.)
void main(void) {
char dam[10]= {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; // sets the array in decimal 0-9
char i;
DDRB = 0xFF;
for(i=9;i>=0;i--)
{
PORTB = dam[i] ;
DDRT = 0xff;
delay(1000);
PTT= 0x30;
delay(1000);
PTT = dam[i];
}
for(;;);
}
YES
6.)
void main(void) {
char dam[10]= {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; // sets the array in decimal 0-9
unsigned char i;
DDRB = 0xFF;
for(i=9;i>=0;i--){
PORTB = dam[i] ;
DDRT = 0xff;
PTT= 0x30;
PTT = dam[i];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.