首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >atmega32 8x8 led矩阵控件

atmega32 8x8 led矩阵控件
EN

Stack Overflow用户
提问于 2020-06-06 05:50:47
回答 1查看 312关注 0票数 0

我正在学习avr,我有一个任务:在LED8x8矩阵上创建运行点。从上到下和从下到上有两种方式。情况0: leds不工作(启动微控制器后的默认设置)情况1:从上到下情况2:从下到上:

代码语言:javascript
复制
for(int a=0;a<20;a++)
    {
        for (int i=0;i<8;i++)
        {
            PORTC = ~PORT[i];
            for (int i=0;i<8;i++)
            {
                PORTA = (1<<i);
                _delay_ms(200);
            }
            PORTC = PORT[i];

            PORTC = ~PORT[i+1];
            for (int i=7;i>=0;i--)
            {
                PORTA = (1<<i);
                _delay_ms(200);
            }
            i=i+1;
        }
    }

从下到上:

代码语言:javascript
复制
for(int a=0;a<20;a++)
    {
        for (int i=7;i>=0;i--)
        {
            PORTC = ~PORT[i];
            for (int c=7;c>=0;c--)
            {
                PORTA = (1<<c);
                _delay_ms(200);
            }
            PORTC = PORT[i];

            PORTC = ~PORT[i-1];
            for (int c=0;c<8;c++)
            {
                PORTA = (1<<c);
                _delay_ms(200);
            }
            i=i-1;
        }
    }

这部分对我来说工作得很好。但我需要通过点击按钮来切换它们。(中断方法)。如果我第一次点击按钮,模式会切换到第一种情况,但在第二次按下按钮后,它对我不再起作用。

完整代码

代码语言:javascript
复制
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 16000000UL
volatile int state = 0;
volatile int status = 0;
char PORT[8] = {1,2,4,8,16,32,64,128};
int i;

ISR(INT0_vect)
{
    //_delay_ms(200);
    if(status==0)
    {
        state=1;
    }
    else if(status==1) 
    {
        state=2;
    }
    else if(status==2)
    {
        state=1;
    }
}

void init()
{
    DDRA = 0xFF; //PORTA as output
    DDRC = 0xFF; //PORTC as output
    DDRD=0;         /* PORTD as input */
    PORTD=0xFF;
     GICR |= (1<<INT0);
     MCUCR |= (1<<ISC01)|(1<<ISC00);
}

void zigzaglab()
{
    for(int a=0;a<20;a++)
    {
        for (int i=0;i<8;i++)
        {
            PORTC = ~PORT[i];
            for (int i=0;i<8;i++)
            {
                PORTA = (1<<i);
                _delay_ms(200);
            }
            PORTC = PORT[i];

            PORTC = ~PORT[i+1];
            for (int i=7;i>=0;i--)
            {
                PORTA = (1<<i);
                _delay_ms(200);
            }
            i=i+1;
        }
    }
}

void zigzagkreis()
{
    for(int a=0;a<20;a++)
    {
        for (int i=7;i>=0;i--)
        {
            PORTC = ~PORT[i];
            for (int c=7;c>=0;c--)
            {
                PORTA = (1<<c);
                _delay_ms(200);
            }
            PORTC = PORT[i];

            PORTC = ~PORT[i-1];
            for (int c=0;c<8;c++)
            {
                PORTA = (1<<c);
                _delay_ms(200);
            }
            i=i-1;
        }
    }
}

int main(void)
{
    init();
    sei();

    while(1)
    {
        switch(state){
            case 0:
                status=0;
                PORTC = 0;
                PORTA = 0;
                break;
            case 1:
                status=1;
                zigzaglab();
                break;
            case 2:
                status=1;
                zigzagkreis();
                break;
        }
    }
}

PS。我正在使用proteus 8. Proteus project

EN

回答 1

Stack Overflow用户

发布于 2020-08-02 04:28:24

试着理解你想要做什么。将您的解决方案转换为:

代码语言:javascript
复制
#define F_CPU 16000000UL

#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>

volatile int status = 0;

ISR(INT0_vect)
{
    // !!!! NEVER PUT A DELAY IN AN INTERRUPT
    //_delay_ms(200);
    
    // If you are using a push-button the interrupt will be called more than one time cause of bouncing
    
    status++;
    
    if(status > 2)
        status = 0;
}

void init()
{
    DDRA = 0xFF;    //PORTA as output
    DDRC = 0xFF;    //PORTC as output
    DDRD = 0x00;    // Not necessary (all ports are setup to input on startup!)
    PORTD = 0xFF;   // Enable pullup resistors on PORTD
    
    // Enable interrupt 0
    GICR |= (1<<INT0);
    MCUCR |= (1<<ISC01)|(1<<ISC00);
    
    // Enable interrupts globally
    sei();
}

void matrix_forward()
{
    // Possible a better solution
    // 8*8 Matrix display can be ordered like a coordinate system (x/y)
    for (unsigned char i=0; i < 20; i++)
    {
        for(unsigned char x=0; x < 8; x += 2)
        {
            // Down
            PORTC = ~(1<<x);
            
            for (unsigned char y=0; y < 8; y++)
            {
                PORTA = (1<<y);
                _delay_ms(200);
            }
            
            PORTC = (1<<x);
            
            // Up
            PORTC = ~(1<<(x + 1));
            
            for (unsigned char y=0; y < 8; y++)
            {
                PORTA = (1<<y);
                _delay_ms(200);
            }
            
            PORTC = (1<<(x + 1));
        }
    }
}

void matrix_reverse()
{
    // Possible a better solution
    // 8*8 Matrix display can be ordered like a coordinate system (x/y)
    for (unsigned char i=0; i < 20; i++)
    {
        for(unsigned char x=0; x < 8; x += 2)
        {
            // Down
            PORTC = ~(1<<(7 - x));
            
            for (unsigned char y=0; y < 8; y++)
            {
                PORTA = (1<<(7 - y));
                _delay_ms(200);
            }
            
            PORTC = (1<<(7 - x));
            
            // Up
            PORTC = ~(1<<(6 - x));
            
            for (unsigned char y=0; y < 8; y++)
            {
                PORTA = (1<<(6 - y));
                _delay_ms(200);
            }
            
            PORTC = (1<<(6 - x));
        }
    }
}

int main(void)
{
    init();

    while(1)
    {
        switch(status){
            case 0:
                PORTC = 0x00;   // for PORTs, PINs, DDRs use HEX or BINARY notation
                PORTA = 0x00;   // cause of better readiness
                break;
            case 1:
                matrix_forward();
                break;
            case 2:
                matrix_reverse();
                break;
        }
    }
}

检查代码是不可能的,但也许你可以为你的解决方案提取一些代码!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62224601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档