Microcontroller Section #4 (Autosaved)
Microcontroller Section #4 (Autosaved)
Section #4
Spring 23-24
Problem 1
Write an AVR C program to send values 00–FF to Port B.
1
Problem 1
Write an AVR C program to send values 00–FF to Port B.
Solution:
#include <avr/io.h> //standard AVR header
int main(void)
{
unsigned char z;
DDRB = 0xFF; //PORTB is output
for(z = 0; z <= 255; z++)
PORTB = z;
return 0;
}
//Notice that the program never exits the for loop because if you
//increment an unsigned char variable when it is 0xFF, it will
//become zero.
2
Problem 1
Write an AVR C program to send values 00–FF to Port B.
2
Problem 1
Write an AVR C program to send values 00–FF to Port B.
2
Problem 1
Write an AVR C program to send values 00–FF to Port B.
3
Problem 1
Write an AVR C program to send values 00–FF to Port B.
4
Problem 2
Write an AVR C program to send values of –4 to +4 to Port
B.
5
Problem 2
Write an AVR C program to send values of –4 to +4 to Port
B.
Solution:
#include <avr/io.h>
int main(void)
{
char mynum[] = {-4,-3,-2,-1,0,+1,+2,+3,+4};
unsigned char z;
DDRB = 0xFF; //PORTB is output
for(z=0; z<=8; z++)
PORTB = mynum[z];
while(1); //stay here forever
return 0;
}
Run the above program on your simulator to see how PORTB displays values of FCH,
FDH, FEH , FFH, 00H, 01H, 02H, 03H, and 04H (the hex values for –4, –3, –2, –1, 0, 1, etc.).
6
Problem 2
Write an AVR C program to send values of –4 to +4 to Port
B.
7
Problem 2
Write an AVR C program to send values of –4 to +4 to Port
B.
8
Problem 2
Write an AVR C program to send hex values for ASCII
characters of 0, 1, 2, 3, 4, 5, A,B, C, and D to Port B.
9
Problem 2
Write an AVR C program to send hex values for ASCII
characters of 0, 1, 2, 3, 4, 5, A,B, C, and D to Port B.
Solution:
#include <avr/io.h>
int main(void)
{
unsigned char myList[]= "012345ABCD";
unsigned char z;
DDRB = 0xFF; //PORTB is output
for(z=0; z<10; z++) //repeat 10 times and increment z
PORTB = myList[z]; //send the character to PORTB
while(1); //needed if running on a trainer
return 0;
}
10
Problem 3 Write an AVR C program to send hex values for ASCII
characters of 0, 1, 2, 3, 4, 5, A,B, C, and D to Port B.
11