如何在avr中将矩阵旋转90度?

How to rotate a matrix by 90 degrees in avr?

我正在尝试将显示字母顺时针和逆时针旋转 90 度。但是当我执行嵌套的for循环时,它给出了这样的错误。

ALPHA 是一个二维数组,用于存储字母 A-Z 的硬编码模式。如果我注释掉嵌套的 for 循环,这个程序到目前为止显示一个静态字母。

led.c: In function ‘main’:
led.c:70:21: warning: iteration 1u invokes undefined behavior [- 
Waggressive-loop-optimizations]
 ALPHA_NEW[k][j] = ALPHA[i][k];
                 ^
led.c:68:4: note: containing loop
for(int k = 0; k<8; k++) 
^

在avr中不能这样赋值吗?

#include <avr/io.h>

//header to enable data flow control over pins

            #define F_CPU 1000000      

//telling controller crystal frequency attached

            #include <util/delay.h>

//header to enable delay function in program

int main(void)
{

      DDRD = 0xFF;//PORTD is set as output
      DDRA = 0xFF;
     //starts from msb..lsb

//int ALPHA[1][8] = {0b00010000,0b00010000,0b00010000,0b00010000,0b00010000,0b00010000,0b00010000,0b00010000};
int ALPHA[1][8] = {0b00111100,0b01000010,0b11000011,0b11111111,0b11000011,0b11000011, 0b11000011, 0b11000011};
    char NAME[] = {0};
    uint8_t l =0;
    char PORT[8] = {1,2,4,8,16,32,64,128};//pin values of PORTD

     int fl = 1; 

int ALPHA_NEW[1][8] = {0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000};                      
while(1)
{
    //fl = 1 means rotate by 90 degree clockwise
    if(fl == 1) {
    for(int i = 0; i< 8; i++)
    {
        for(int j = 8; j>0; j--)
        {
            for(int k = 0; k<8; k++) 
            {
                ALPHA_NEW[k][j] = ALPHA[i][k];
            }
        }
        //ALPHA[0][i] = 0b00000000; 

    }
//ALPHA[0][4] = 0b11111111;

        }
    for (int m=0;m<sizeof NAME;m++)
    {
        l = NAME[m];

    for (int n=0;n<800;n++)//execute 200 times for the eye to catch

     {
            for (int j=0;j<4;j++)

                            {
                                 PORTD = PORT[j];// ROW
                                 PORTA = ~ALPHA[l][j];
                    _delay_ms(1);
                }

                  // PORTD=0x00;//clear screen after show

                   for (int k=0;k<4;k++)

                         {
                PORTD = PORT[k+4];// ROW
                PORTA = ~ALPHA[l][k+4];
                 _delay_ms(1);

                         }



        }
        PORTD=0x00;//clear screen after show.
           // _delay_ms(500);


    }
        //_delay_ms(500);

      }

}

首先,你的数组你数组的外维度是[1]:

int ALPHA[1][8] = ...
int ALPHA_NEW[1][8] = ...

即只有可接受的指标 [0]。但是你从 0 迭代到 7

for(int i = 0; i< 8; i++)
...
        for(int k = 0; k<8; k++) 
            ALPHA_NEW[k][j] = ALPHA[i][k];

越界访问数组时,内存内容可能会损坏,并可能导致意外行为。

第二。如果你需要一个8位无符号整数的一维数组,那么,为什么不这样定义呢?

uint8_t ALPHA[8] = { .... }

第三。从你的问题中不清楚,但正如我从数组内容中推测的那样,你有一个 8 x 8 像素大小的图像,它作为位图包含在数组的 8 个连续项中。你想要另一个 8 项数组,其中相同的图像将顺时针或逆时针旋转。

为此,您需要将源数组的所有第一位(或最后一位)的新数组值复制到第一项。在第二项中 - 所有第二(或第 7)位等 8 次。位顺序取决于您转动图像的方向,以及图像的存储方式。 让我们假设数组项目存储像素的水平行,从上到下,最高有效位代表最左边的像素。

// Array containing the source 8x8 bit-mapped monochrome image
uint8_t source_img[8] = {0b00111100,0b01000010,0b11000011,0b11111111,0b11000011,0b11000011, 0b11000011, 0b11000011}; 
// Array to store rotated imaged
uint8_t dest_img[8];

if (counterclockwise) {
  for (uint8_t y = 0 ; y < 8 ; y++) { 
    uint8_t mask = (1 << y); // mask of a bit to pick from source array;
    uint8_t buf = 0; // buffer of one row bits in the destination array
    for (uint8_t x = 0 ; x < 8 ; x++) {
      buf <<= 1; // shift bits one position left 
      if (source_img[x] & mask) { // if masked bit in source array is set, 
        buf |= 1;  // set the rightmost bit in the buffer
      }
    } 
    dest_img[y] = buf;
  }
} else if (clockwise) {
  for (uint8_t y = 0 ; y < 8 ; y++) { 
    uint8_t mask = (0x80 >> y); // mask of a bit to pick from source array;
    uint8_t buf = 0; // buffer of one row bits in the destination array
    for (uint8_t x = 0 ; x < 8 ; x++) {
      buf >>= 1; // shift bits one position right
      if (source_img[x] & mask) { // if masked bit in source array is set, 
        buf |= 0x80;  // set the leftmost bit in the buffer
      }
    } 
    dest_img[y] = buf;
  }
}

之后 dest_img 将包含 8x8 字母的旋转位图图像