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

时间:2018-06-24 18:00:46

标签: avr avr-gcc

我正在尝试将显示字母沿顺时针和逆时针方向旋转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 个答案:

答案 0 :(得分:2)

首先,您的数组的外部尺寸为[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字母的位图图像