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];
	}
}
相关推荐
Include everything3 小时前
Rust学习笔记(三)|所有权机制 Ownership
笔记·学习·rust
杜子不疼.4 小时前
《Python学习之文件操作:从入门到精通》
数据库·python·学习
★YUI★4 小时前
学习游戏制作记录(玩家掉落系统,删除物品功能和独特物品)8.17
java·学习·游戏·unity·c#
livemetee5 小时前
Flink2.0学习笔记:Flink服务器搭建与flink作业提交
大数据·笔记·学习·flink
INS_KF5 小时前
【C++知识杂记2】free和delete区别
c++·笔记·学习
Easocen6 小时前
Mybatis学习笔记(五)
笔记·学习·mybatis
丑小鸭是白天鹅8 小时前
嵌入式C语言学习笔记之枚举、联合体
c语言·笔记·学习
楼田莉子9 小时前
C++算法题目分享:二叉搜索树相关的习题
数据结构·c++·学习·算法·leetcode·面试
奶黄小甜包10 小时前
C语言零基础第18讲:自定义类型—结构体
c语言·数据结构·笔记·学习
rannn_11112 小时前
【MySQL学习|黑马笔记|Day7】触发器和锁(全局锁、表级锁、行级锁、)
笔记·后端·学习·mysql