I am trying to rotate the displaying letter by 90 degrees in both clockwise and anticlockwise direction. But when I'm executing the nested for loop, it gives an error like this.
ALPHA is a 2d array which stores the hardcoded patterns of the letters A-Z. This program displays a static letter so far, if i comment out the nested for loop.
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++)
^
is it not possible to assign values in avr like this?
#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);
}
}
First of all, your arrays the outer dimension of your arrays is [1]:
int ALPHA[1][8] = ...
int ALPHA_NEW[1][8] = ...
I.e. acceptable indices only [0]. But you're iterating from 0 to 7
for(int i = 0; i< 8; i++)
...
for(int k = 0; k<8; k++)
ALPHA_NEW[k][j] = ALPHA[i][k];
When accessing arrays out of their bounds the memory content can be damaged and it may cause unintended behavior.
Second. If you need 1-dimension array of 8-bit unsigned integers, then, why not to define it in such a way?
uint8_t ALPHA[8] = { .... }
Third. It is not clear from your question, but as I suppose from the array content, you have an image of 8 x 8 pixels size which is contained as a bit-map in 8 successive items of an array. You want to have another 8-items array where the same image will be rotated clockwise or counter-clockwise.
For that, you need to copy into the first item of new array values of all first (or last) bits of the source array. In the second item - all second (or 7th) bits, etc 8 times. Bit order depends on which direction you're turning the image, and how the image is stored. Let's assume arrays item store horizontal rows of pixels, from top to bottom, the most significant bit represents the leftmost pixel.
// 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;
}
}
after that dest_img will contain the rotated bit-mapped image of a 8x8 letter