0% found this document useful (0 votes)
11 views3 pages

5 BT Tuan6

ko

Uploaded by

leduyphuong03312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

5 BT Tuan6

ko

Uploaded by

leduyphuong03312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Khúc Trọng Kiên 20216165

Bài làm

1. Difference between PORTC=0x00 and DDRC=0x00?


• PORTC = 0x00: This sets all pins of Port C to low (logic 0).
• DDRC = 0x00: This sets all pins of Port C as inputs. The Data Direction
Register (DDR) determines whether the port pins are inputs (0) or outputs
(1).
2. C program to toggle all bits of Port B every 200 ms:
#include <avr/io.h>
#include <util/delay.h>

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

DDRB = 0xFF; // Set Port B as output


DDRC = 0xFF; // Set Port C as output

PORTB = digit1; // Display first digit on Port B


PORTC = digit2; // Display second digit on Port C

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';

uint8_t bcd = ((ascii7 - '0') << 4) | (ascii2 - '0'); // Convert ASCII to


packed BCD

DDRB = 0xFF; // Set Port B as output


PORTB = bcd; // Display BCD on Port B

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);
}

You might also like