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];
	}
}
相关推荐
机智的叉烧27 分钟前
前沿重器[57] | sigir24:大模型推荐系统的文本ID对齐学习
人工智能·学习·机器学习
量子-Alex1 小时前
【多模态聚类】用于无标记视频自监督学习的多模态聚类网络
学习·音视频·聚类
吉大一菜鸡1 小时前
FPGA学习(基于小梅哥Xilinx FPGA)学习笔记
笔记·学习·fpga开发
爱吃西瓜的小菜鸡4 小时前
【C语言】判断回文
c语言·学习·算法
小A1594 小时前
STM32完全学习——SPI接口的FLASH(DMA模式)
stm32·嵌入式硬件·学习
岁岁岁平安5 小时前
spring学习(spring-DI(字符串或对象引用注入、集合注入)(XML配置))
java·学习·spring·依赖注入·集合注入·基本数据类型注入·引用数据类型注入
武昌库里写JAVA5 小时前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计
qq_589568105 小时前
数据可视化echarts学习笔记
学习·信息可视化·echarts
兔C6 小时前
微信小程序的轮播图学习报告
学习·微信小程序·小程序
海海不掉头发6 小时前
苍穹外卖-day05redis 缓存的学习
学习·缓存