Labs‘Codes review(AVR)(3)

  1. Example 1:Set bit 5 of port B to be an output and other bits to be inputs

Assembly language

ldi r16,(1<<5);
out DDRB,r16;

C equivalent

DDRB=(1<<5);
  1. Example 2:Set bits 3 and 4 of port B to be outputs whilst keeping the direction of the other bits the same

Assembly language

ldi r16,(1<<3)|(1<<4);
out DDRB,r16;

C equivalent

DDRB=(1<<3)|(1<<4)

3.output values 10 to 15 as hex digits

#include <avr/io.h>

uint8_t seven_seg[10] = { 63,6,91,79,102,109,125,7,127,111};

int main(void) {
    uint8_t digit;
    
    /* Set port A pins to be outputs, port C pins to be inputs */
    DDRA = 0xFF;
    DDRC = 0;    /* This is the default, could omit. */
    while(1) {
        /* Read in a digit from lower half of port C pins */
        /* We read the whole byte and mask out upper bits */
        digit = PINC & 0x0F;
        /* Write out seven segment display value to port A */
        if(digit < 10) {
            PORTA = seven_seg[digit];
        } else {
            PORTA = 0;
        }
    }
}

4.Modify the task 1 code so that your program can also output values 10 to 15 as hex digits

#include <avr/io.h>

uint8_t seven_seg[16] = { 63,6,91,79,102,109,125,7,127,111,119,124,57,94,121,113};

int main(void) {
    uint8_t digit;
    DDRA = 0xFF;
    DDRC = 0;    
    while(1) {
        digit = PINC & 0x0F;
        if(digit < 16) {
            PORTA = seven_seg[digit];
        } else {
            PORTA = 0;
        }
    }
}
  1. Modify the code in Task 2 so that your program also outputs the digit (in binary) on AVR port B. Connect the lower 4 bits of port B to 4 LEDs

    #include <avr/io.h>

    uint8_t seven_seg[16] = { 63,6,91,79,102,109,125,7,127,111,119,124,57,94,121,113};

    int main(void) {
    uint8_t digit;
    DDRA = 0xFF;
    DDRB = 0xFF;
    DDRC = 0;
    while(1) {
    digit = PINC & 0x0F;
    PORTB = digit;
    PORTA = seven_seg[digit];
    }
    }

5.Write a program that repeatedly reads the lower 4 bits of port A and the lower 4 bits of port B and adds these two values together and displays (on the SSD, using a port of your choice) the hexadecimal value of the lower 4 bits of the result

#include <avr/io.h>

uint8_t seven_seg[16] = { 63,6,91,79,102,109,125,7,127,111,119,124,57,94,121,113};

int main(void) {
	uint8_t a, b, c;
	DDRA=0x00;
	DDRB=0x00;
	DDRC=0XFF;
	
	while(1) {
		a=PINA&0x0F;
		b=PINB&0x0F;
		c=a+b;
		PORTC=seven_seg[c&0x0F];
	}
}
相关推荐
百流1 小时前
scala文件编译相关理解
开发语言·学习·scala
雁于飞3 小时前
c语言贪吃蛇(极简版,基本能玩)
c语言·开发语言·笔记·学习·其他·课程设计·大作业
大丈夫立于天地间12 小时前
ISIS基础知识
网络·网络协议·学习·智能路由器·信息与通信
Chambor_mak13 小时前
stm32单片机个人学习笔记14(USART串口数据包)
stm32·单片机·学习
PaLu-LI13 小时前
ORB-SLAM2源码学习:Initializer.cc⑧: Initializer::CheckRT检验三角化结果
c++·人工智能·opencv·学习·ubuntu·计算机视觉
yuanbenshidiaos13 小时前
【大数据】机器学习----------计算机学习理论
大数据·学习·机器学习
汤姆和佩琦14 小时前
2025-1-20-sklearn学习(42) 使用scikit-learn计算 钿车罗帕,相逢处,自有暗尘随马。
人工智能·python·学习·机器学习·scikit-learn·sklearn
Tech智汇站14 小时前
Quick Startup,快捷处理自启程序的工具,加快电脑开机速度!
经验分享·科技·学习·学习方法·改行学it
qq_3127384514 小时前
jvm学习总结
jvm·学习