5 BT Tuan6
5 BT Tuan6
Bài làm
int main(void) {
DDRB = 0xFF; // Set Port B as output
while (1) {
PORTB ^= 0xFF; // Toggle all bits of Port B
_delay_ms(200); // Wait for 200 ms
}
}
3. C program to toggle bits 1 and 3 of Port B every 200 ms:
#include <avr/io.h>
#include <util/delay.h>
int main(void) {
DDRB |= (1 << PB1) | (1 << PB3); // Set bit 1 and bit 3 of Port B as
output
while (1) {
PORTB ^= (1 << PB1) | (1 << PB3); // Toggle bits 1 and 3 of Port B
_delay_ms(200); // Wait for 200 ms
}
}
4. Time delay function for 100 ms:
#include <util/delay.h>
void delay_100ms() {
_delay_ms(100); // Delay for 100 ms
}
5. C program to toggle only bit 3 of Port C every 200 ms:
#include <avr/io.h>
#include <util/delay.h>
int main(void) {
DDRC |= (1 << PC3); // Set bit 3 of Port C as output
while (1) {
PORTC ^= (1 << PC3); // Toggle bit 3 of Port C
_delay_ms(200); // Wait for 200 ms
}
}
6. C program to count up Port B from 0–99 continuously:
#include <avr/io.h>
#include <util/delay.h>
int main(void) {
DDRB = 0xFF; // Set Port B as output
uint8_t count = 0;
while (1) {
PORTB = count; // Output count to Port B
_delay_ms(500); // Delay to slow down counting for visibility
count++;
if (count > 99) {
count = 0; // Reset count after 99
}
}
}
7. C program to convert packed BCD 0x34 to ASCII and display on PORTB
and PORTC:
#include <avr/io.h>
int main(void) {
uint8_t bcd = 0x34;
uint8_t digit1 = (bcd >> 4) + '0'; // Convert upper nibble to ASCII
uint8_t digit2 = (bcd & 0x0F) + '0'; // Convert lower nibble to ASCII
while (1);
}
8. Program to convert ASCII digits of ‘7’ and ‘2’ to packed BCD and display
them on Port B:
#include <avr/io.h>
int main(void) {
char ascii7 = '7';
char ascii2 = '2';
while (1);
}
9. AVR C program to store ‘G’ into location 0x005F of EEPROM:
#include <avr/io.h>
#include <avr/eeprom.h>
int main(void) {
eeprom_write_byte((uint8_t*)0x005F, 'G'); // Write 'G' to EEPROM
address 0x005F
while (1);
}
10. AVR C program to read the content of location 0x005F of EEPROM into
Port B:
#include <avr/io.h>
#include <avr/eeprom.h>
int main(void) {
DDRB = 0xFF; // Set Port B as output
uint8_t data = eeprom_read_byte((uint8_t*)0x005F); // Read EEPROM
address 0x005F
PORTB = data; // Output the value to Port B
while (1);
}