• 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 

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

    C equivalent 

    DDRB=(1<<5);

    2. 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 

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

    C equivalent 

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

    3.output values 10 to 15 as hex digits 

    1. #include <avr/io.h>
    2. uint8_t seven_seg[10] = { 63,6,91,79,102,109,125,7,127,111};
    3. int main(void) {
    4. uint8_t digit;
    5. /* Set port A pins to be outputs, port C pins to be inputs */
    6. DDRA = 0xFF;
    7. DDRC = 0; /* This is the default, could omit. */
    8. while(1) {
    9. /* Read in a digit from lower half of port C pins */
    10. /* We read the whole byte and mask out upper bits */
    11. digit = PINC & 0x0F;
    12. /* Write out seven segment display value to port A */
    13. if(digit < 10) {
    14. PORTA = seven_seg[digit];
    15. } else {
    16. PORTA = 0;
    17. }
    18. }
    19. }

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

    1. #include <avr/io.h>
    2. uint8_t seven_seg[16] = { 63,6,91,79,102,109,125,7,127,111,119,124,57,94,121,113};
    3. int main(void) {
    4. uint8_t digit;
    5. DDRA = 0xFF;
    6. DDRC = 0;
    7. while(1) {
    8. digit = PINC & 0x0F;
    9. if(digit < 16) {
    10. PORTA = seven_seg[digit];
    11. } else {
    12. PORTA = 0;
    13. }
    14. }
    15. }

    4. 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

    1. #include <avr/io.h>
    2. uint8_t seven_seg[16] = { 63,6,91,79,102,109,125,7,127,111,119,124,57,94,121,113};
    3. int main(void) {
    4. uint8_t digit;
    5. DDRA = 0xFF;
    6. DDRB = 0xFF;
    7. DDRC = 0;
    8. while(1) {
    9. digit = PINC & 0x0F;
    10. PORTB = digit;
    11. PORTA = seven_seg[digit];
    12. }
    13. }

    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

    1. #include <avr/io.h>
    2. uint8_t seven_seg[16] = { 63,6,91,79,102,109,125,7,127,111,119,124,57,94,121,113};
    3. int main(void) {
    4. uint8_t a, b, c;
    5. DDRA=0x00;
    6. DDRB=0x00;
    7. DDRC=0XFF;
    8. while(1) {
    9. a=PINA&0x0F;
    10. b=PINB&0x0F;
    11. c=a+b;
    12. PORTC=seven_seg[c&0x0F];
    13. }
    14. }
  • 相关阅读:
    Dubbo之参数配置(一)
    ES(elasticsearch) - kibana导出csv
    Unity中Shader的PBR的基础知识与理论
    Springboot毕设项目小区物业管理系统75n19java+VUE+Mybatis+Maven+Mysql+sprnig)
    软件项目可行性研究报告
    软件开发生命周期
    对外提供服务的方法
    关于sql中联接的问题
    C Primer Plus(6) 中文版 第4章 字符串和格式化输入/输出 4.1 前导程序
    21.2 Python 使用Scapy实现端口探测
  • 原文地址:https://blog.csdn.net/weixin_61023150/article/details/133468662