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];
	}
}
相关推荐
xuanwenchao33 分钟前
ROS2学习笔记 - 2、类的继承及使用
服务器·笔记·学习
ILYT NCTR1 小时前
爬虫学习案例3
爬虫·python·学习
不灭锦鲤2 小时前
网络安全学习第59天
学习·安全·web安全
楼田莉子2 小时前
同步/异步日志系统:日志落地模块\日志器模块\异步日志模块
linux·服务器·c++·学习·设计模式
旖-旎3 小时前
递归(汉诺塔问题)(1)
c++·学习·算法·leetcode·深度优先·递归
SUNNY_SHUN3 小时前
清华团队提出TFA-Net,用模板特征聚合破解工业异常检测中的“捷径学习“难题
人工智能·学习·视觉检测·github
SuperHeroWu73 小时前
【鸿蒙基础入门】概念理解和学习方法论说明
前端·学习·华为·开源·harmonyos·鸿蒙·移动端
speop3 小时前
TASK05 | Reasoning Kindom拟合的陷阱 —— 统计相关性不是推理
学习
深念Y4 小时前
感知机 ≈ 可学习的逻辑门?聊聊激活函数与二元分类的本质
人工智能·学习·分类·感知机·激活函数·逻辑门·二元分类
程序员大雄学编程4 小时前
学习资源总汇
学习