UART STM32L0, Parity bit implementation - c++

I am setting up communication via USART2 Asynchronous to receive my data. Configuration for receive the data is 9600/7bit/1-bit stop/Parity Even/Mode Rx/Tx. The implementation works fine when no parity is implemented. But the specification dictates that even parity should be supported.
If added the parity, I received any value.I do not understand where is the problem.
My configuration is with STMCubeMx for STM32L031K6 nucleo.
void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_7B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT|UART_ADVFEATURE_DMADISABLEONERROR_INIT;
huart2.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
huart2.AdvancedInit.DMADisableonRxError = UART_ADVFEATURE_DMA_DISABLEONRXERROR;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(uartHandle->Instance==USART2)
{
/* Peripheral clock enable */
__HAL_RCC_USART2_CLK_ENABLE();
/**USART2 GPIO Configuration
PA9 ------> USART2_TX
PA10 ------> USART2_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral interrupt init */
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==USART2)
{
/* Peripheral clock disable */
__HAL_RCC_USART2_CLK_DISABLE();
/**USART2 GPIO Configuration
PA9 ------> USART2_TX
PA10 ------> USART2_RX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
/* Peripheral interrupt Deinit*/
HAL_NVIC_DisableIRQ(USART2_IRQn);
}
}
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
/*Configure GPIO pin : PB3 */
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/*********************** interruption **********************/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART2) //current UART
{
Receive();
HAL_UART_Receive_IT(&huart2, Rx_data, 1);
}
}
/******************** Main ******************************/
int main(void)
{
HW_Init();
HAL_UART_Receive_IT(&huart2, Rx_data, 1);
while (1)
{
}
}

When using parity on STM32 series, you need to increase the UART_WORDLENGTH_7B to UART_WORDLENGTH_8B (or UART_WORDLENGTH_9B if you have 8 bits of data).
I just experienced the same problem myself recently. The HAL API should been better at this point...

Related

How do I get all the clipboard events provided by XCB?

I am currently working on a cross-host clipboard sharing tool that can share text, rich text, or files.
Based on what I've learned so far, I already know which X11 events need to be handled, but I can't listen to them.
My current code I can only catch events that select text.
how to get the clipboard change event caused by ctrl+c?
#include <QCoreApplication>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <xcb/xcb.h>
#include <xcb/xfixes.h>
/**
* Enumeration of standard X11 atom identifiers
*/
typedef enum std_x_atoms {
/** The TARGETS atom identifier **/
X_ATOM_TARGETS = 0,
/** The MULTIPLE atom identifier **/
X_ATOM_MULTIPLE,
/** The TIMESTAMP atom identifier **/
X_ATOM_TIMESTAMP,
/** The INCR atom identifier **/
X_ATOM_INCR,
/** The CLIPBOARD atom identifier **/
X_ATOM_CLIPBOARD,
/** The UTF8_STRING atom identifier **/
X_ATOM_UTF8_STRING,
X_ATOM_XCLIPD,
/** End marker sentinel **/
X_ATOM_END
} std_x_atoms;
/**
* Union to simplify getting interned atoms from XCB
*/
typedef union atom_c {
/** The atom **/
xcb_atom_t atom;
/** The cookie returned by xcb_intern_atom **/
xcb_intern_atom_cookie_t cookie;
} atom_c;
/**
* The standard atom names. These values should match the
* std_x_atoms enumeration.
*/
const char * const g_std_atom_names[X_ATOM_END] = {
"TARGETS", "MULTIPLE", "TIMESTAMP", "INCR",
"CLIPBOARD", "UTF8_STRING", "XCLIPD"
};
atom_c std_atoms[X_ATOM_END];
/**
* \brief Interns the list of atoms
*
* \param [in] xc The XCB connection.
* \param [out] atoms The location to store interned atoms.
* \param [in] atom_names The names of the atoms to intern.
* \param [in] number The number of atoms to intern.
* \return true iff all atoms were interned.
*/
static bool x11_intern_atoms(xcb_connection_t *xc, atom_c *atoms, const char * const *atom_names, int number) {
for (int i = 0; i < number; i++) {
atoms[i].cookie = xcb_intern_atom(xc, 0,
strlen(atom_names[i]), atom_names[i]);
}
for (int i = 0; i < number; i++) {
xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(xc,
atoms[i].cookie, NULL);
if (reply == NULL) {
return false;
}
atoms[i].atom = reply->atom;
free(reply); /* XCB: Do not use custom allocators */
}
return true;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
xcb_connection_t *c;
xcb_screen_t *screen;
xcb_window_t win;
xcb_generic_event_t *e;
uint32_t mask = 0;
uint32_t values[2];
/* Create the window */
c = xcb_connect (NULL, NULL);
screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
if (!x11_intern_atoms(c, std_atoms, g_std_atom_names, X_ATOM_END))
return 1;
// xcb_flush (c);
win = xcb_generate_id (c);
mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
values[0] = screen->white_pixel;
values[1] = XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;;
xcb_create_window (c, /* Connection */
XCB_COPY_FROM_PARENT, /* depth */
win, /* window Id */
screen->root, /* parent window */
0, 0, /* x, y */
150, 150, /* width, height */
10, /* border_width */
XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
screen->root_visual, /* visual */
mask, values); /* masks */
xcb_map_window (c, win);
xcb_flush (c);
// init xfixes
xcb_generic_error_t *error = 0;
const xcb_query_extension_reply_t *reply = xcb_get_extension_data(c, &xcb_xfixes_id);
if (!reply || !reply->present) {
return -1;
}
xcb_xfixes_query_version_cookie_t xfixes_query_cookie = xcb_xfixes_query_version(c,
XCB_XFIXES_MAJOR_VERSION,
XCB_XFIXES_MINOR_VERSION);
xcb_xfixes_query_version_reply_t *xfixes_query = xcb_xfixes_query_version_reply (c,
xfixes_query_cookie, &error);
if (!xfixes_query || error || xfixes_query->major_version < 2) {
free(error);
}
free(xfixes_query);
// delivers request
mask = XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE
| XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY
| XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER;
xcb_xfixes_select_selection_input_checked(c, win, XCB_ATOM_NONE, mask);
xcb_flush(c);
// recevie events
uint response_type;
while (e = xcb_wait_for_event(c)) {
xcb_xfixes_selection_notify_event_t *notify_event = reinterpret_cast<xcb_xfixes_selection_notify_event_t *>(e);
printf("response_type = %d\n", response_type);
response_type = notify_event->response_type & ~0x80;
printf("response_type = %d\n", response_type);
if (response_type == reply->first_event + XCB_XFIXES_SELECTION_NOTIFY) {
printf("notify\n");
} else {
printf("code:%d\n", response_type);
}
}
return a.exec();
}

C++, ncurses: unable to display menu before a key is pressed

With a friend I am working on a tetris like game within a terminal. We are using ncurses to manage it and we have different "views". For example a view manages the grid while the other manages the Menu. However we have a problem: when we start the game, we need to press a key in order to see it, otherwise the window is empty.
Here is the code ran when we create the menu (the constructor):
MenuScreen::MenuScreen()
{
if (has_colors())
{
use_default_colors();
start_color();
init_pair(WHITEONRED, COLOR_WHITE, COLOR_RED);
init_pair(WHITEONBLUE, COLOR_WHITE, COLOR_BLUE);
init_pair(REDONWHITE, COLOR_RED, COLOR_WHITE);
}
char *choices[] = /* The menu choices */
{
(char *)" Solo ",
(char *)" Bot ",
(char *)" Tetrix ",
(char *)" Quitter ",
NULL};
/* Calculate nchoices */
for (n_choices = 0; choices[n_choices]; n_choices++)
;
/* alloction of an iteam array for the menu */
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for (ssChoice = 0; ssChoice < n_choices; ++ssChoice)
my_items[ssChoice] = new_item(choices[ssChoice], NULL);
my_items[n_choices] = (ITEM *)NULL;
/* menu structure creation */
my_menu = new_menu((ITEM **)my_items);
/* symbole on the left of selected iteam*/
set_menu_mark(my_menu, "> ");
/* Windows Border cration */
wBorder = newwin(8, 40, LINES / 2 - 8 / 2, COLS / 2 - 40 / 2);
registerWindow(wBorder);
wattrset(wBorder, COLOR_PAIR(WHITEONRED));
windowsFilling(wBorder);
box(wBorder, 0, 0);
windowsBorderTitle(wBorder, " option ");
wUI = derwin(wBorder, 8 - 2, 40 - 2, 2, 2);
registerWindow(wUI);
set_menu_sub(my_menu, wUI);
set_menu_fore(my_menu, COLOR_PAIR(REDONWHITE));
set_menu_back(my_menu, COLOR_PAIR(WHITEONRED));
/* menu display */
post_menu(my_menu);
}
And when the next key is pressed:
void MenuScreen::next()
{
menu_driver(my_menu, REQ_DOWN_ITEM);
update();
}
And here is what update() contains:
void MenuScreen::update()
{
touchwin(wUI);
wrefresh(wUI);
touchwin(wBorder);
wrefresh(wBorder);
}
If we remove update() from the next() function, the menu is no longer displayed when we press the next key, so we tried to add a call to update() at the end of the constructor, but this didn't work. We have no idea what could cause this bug. Here is our entire source code:
https://github.com/Th0rgal/poyuterm/tree/22e2422930a29fbe83414e382fd566c3eac69366
EDIT: Here is a minimal reproducible example made by my friend:
To build it, copy paste it in a menu.cpp file and type: g++ menu.cpp -o menu -lncurses -l menu
#include <stdlib.h> /* calloc() */
#include <string.h> /* strlen() */
#include <ncurses.h>
#include <menu.h>
#include <curses.h>
#define WHITEONRED 10
#define WHITEONBLUE 20
#define WHITEONBLACK 30
#define BLACKONWHITE 40
#define REDONWHITE 50
void windowsBorderTitle(WINDOW *pwin, const char *title)
{
int x, maxy, maxx, stringsize;
getmaxyx(pwin, maxy, maxx);
stringsize = 4 + strlen(title);
x = (maxx - stringsize) / 2;
mvwaddch(pwin, 0, x, ACS_RTEE);
waddch(pwin, ' ');
waddstr(pwin, title);
waddch(pwin, ' ');
waddch(pwin, ACS_LTEE);
}
void windowsFilling(WINDOW *pwin)
{
int y, x, maxy, maxx;
getmaxyx(pwin, maxy, maxx);
for (y = 0; y < maxy; y++)
for (x = 0; x < maxx; x++)
mvwaddch(pwin, y, x, ' ');
}
int main(int argc, char const *argv[])
{
initscr(); /* start ncurses */
cbreak(); /* immediately acquire each keystroke */
noecho(); /* do not echo user keystrokes */
keypad(stdscr, TRUE); /* enable detection of function keys */
int c;
ITEM **my_items;
MENU *my_menu;
WINDOW *wUI;
WINDOW *wBorder;
int n_choices;
int ssChoice;
int my_choice = -1;
if (has_colors())
{
use_default_colors();
start_color();
init_pair(WHITEONRED, COLOR_WHITE, COLOR_RED);
init_pair(WHITEONBLUE, COLOR_WHITE, COLOR_BLUE);
init_pair(REDONWHITE, COLOR_RED, COLOR_WHITE);
}
char *choices[] = /* The menu choices */
{
(char *)" Solo ",
(char *)" Bot ",
(char *)" Tetrix ",
(char *)" Quitter ",
NULL};
for(n_choices=0; choices[n_choices]; n_choices++);
/* ALLOCATE ITEM ARRAY AND INDIVIDUAL ITEMS */
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(ssChoice = 0; ssChoice < n_choices; ++ssChoice)
my_items[ssChoice] = new_item(choices[ssChoice], NULL);
my_items[n_choices] = (ITEM *)NULL;
/* CREATE THE MENU STRUCTURE */
my_menu = new_menu((ITEM **)my_items);
/* PUT > TO THE LEFT OF HIGHLIGHTED ITEM */
set_menu_mark(my_menu, "> ");
/* SET UP WINDOW FOR MENU'S BORDER */
wBorder = newwin(16, 40, 2, 20);
wattrset(wBorder, COLOR_PAIR(WHITEONRED) | WA_BOLD);
windowsFilling(wBorder);
box(wBorder, 0, 0);
windowsBorderTitle(wBorder, "Choose one");
/* SET UP WINDOW FOR THE MENU'S USER INTERFACE */
wUI = derwin(wBorder, 16-2, 40-2, 2, 2);
/* ASSOCIATE THESE WINDOWS WITH THE MENU */
set_menu_win(my_menu, wBorder);
set_menu_sub(my_menu, wUI);
/* MATCH MENU'S COLORS TO THAT OF ITS WINDOWS */
set_menu_fore(my_menu, COLOR_PAIR(REDONWHITE));
set_menu_back(my_menu, COLOR_PAIR(WHITEONRED) | WA_BOLD);
/* SET UP AN ENVIRONMENT CONDUCIVE TO MENUING */
keypad(wUI, TRUE); /* enable detection of function keys */
noecho(); /* user keystrokes don't echo */
curs_set(0); /* make cursor invisible */
/* DISPLAY THE MENU */
post_menu(my_menu);
touchwin(wBorder);
wrefresh(wBorder);
while(my_choice == -1)
{
touchwin(wBorder);
wrefresh(wBorder);
touchwin(wUI); /* refresh prior to getch() */
wrefresh(wUI); /* refresh prior to getch() */
c = getch();
switch(c)
{
case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: /* Enter */
my_choice = item_index(current_item(my_menu));
pos_menu_cursor(my_menu);
break;
}
}
unpost_menu(my_menu);
for(ssChoice = 0; ssChoice < n_choices; ++ssChoice)
free_item(my_items[ssChoice]);
free_menu(my_menu);
delwin(wUI);
delwin(wBorder);
touchwin(stdscr);
wrefresh(stdscr);
endwin();
}

QThread wiringPi GPIO

I use a RaspberryPi and Qt for my Qt for my embedded Project
I have read about QThread. I tested QThread and it is working very fine.
I want to control some GPIO pins in my Thread, but this doesn't work
My GPIO pins are working, I have tested it.
Here my Code:
class referenz_thread:public QThread
{
Q_OBJECT
public:
bool x_isRef;
bool y_isRef;
void run()
{
x_isRef = false;
y_isRef = false;
digitalWrite(x_treiber,1);
digitalWrite(y_treiber,1);
digitalWrite(x_richtung,0);
digitalWrite(y_richtung,1);
while(1)
{
if(digitalRead(x_end) == 0)
{
digitalWrite(x_treiber,0);
x_isRef = true;
}
if(digitalRead(y_end) == 0)
{
digitalWrite(y_treiber,0);
y_isRef = true;
}
if((x_isRef == true) && (y_isRef == true))
{
break;
}
digitalWrite(x_v,1);
digitalWrite(y_v,1);
delay(1);
digitalWrite(x_v,0);
delay(1);
digitalWrite(x_v,1);
delay(1);
digitalWrite(x_v,0);
digitalWrite(y_v,0);
delay(1);
}
}
public slots:
};
This is in MainWindow.cpp
referenz_thread *ref_thread = new referenz_thread();
connect(ui->btn_ref,SIGNAL(clicked()),ref_thread,SLOT(start()));
I have wiringPiSetup already executed in my MainThread.

When i compile acode i have this error cannot open file X11/Xlib.h in c++

/* Sequential Mandelbrot program mandelbrot.c */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define X_RESN 800 /* x resolution */
#define Y_RESN 800 /* y resolution */
typedef struct complextype
{ float real, imag;
} Compl;
int main (int argc, char *argv[])
{ Window win; /* window initialization */
unsigned
int width, height, /* window size */
x, y, /* window position */
border_width, /* border width in pixels */
display_width,
display_height, /* size of screen */
screen; /* which screen */
char *window_name = "Mandelbrot Set", *display_name = NULL;
GC gc;
unsigned long valuemask = 0;
XGCValues values;
Display *display;
XSizeHints size_hints;
Pixmap bitmap;
XPoint points[800];
FILE *fp, *fopen ();
char str[100];
XSetWindowAttributes attr[1];
/* Mandlebrot variables */
int i, j, k;
Compl z, c;
float lengthsq, temp;
/* connect to Xserver */
if ( (display = XOpenDisplay (display_name)) == NULL )
{ fprintf (stderr, "drawon: cannot connect to X server %s\n",
XDisplayName (display_name) );
exit (-1);
}
/* get screen size */
screen = DefaultScreen (display);
display_width = DisplayWidth (display, screen);
display_height = DisplayHeight (display, screen);
/* set window size */
width = X_RESN;
height = Y_RESN;
/* set window position */
x = 0;
y = 0;
/* create opaque window */
border_width = 4;
win = XCreateSimpleWindow(display, RootWindow (display, screen),
x, y, width, height, border_width,
BlackPixel (display, screen), WhitePixel (display, screen));
size_hints.flags = USPosition|USSize;
size_hints.x = x;
size_hints.y = y;
size_hints.width = width;
size_hints.height = height;
size_hints.min_width = 300;
size_hints.min_height = 300;
XSetNormalHints (display, win, &size_hints);
XStoreName(display, win, window_name);
/* create graphics context */
gc = XCreateGC (display, win, valuemask, &values);
XSetBackground (display, gc, WhitePixel (display, screen));
XSetForeground (display, gc, BlackPixel (display, screen));
XSetLineAttributes (display,gc,1,LineSolid,CapRound,JoinRound);
attr[0].backing_store = Always;
attr[0].backing_planes = 1;
attr[0].backing_pixel = BlackPixel(display, screen);
XChangeWindowAttributes(display, win,
CWBackingStore | CWBackingPlanes | CWBackingPixel, attr);
XMapWindow (display, win);
XSync(display, 0);
/* Calculate and draw points */
for(i=0; i < X_RESN; i++)
{ for(j=0; j < Y_RESN; j++)
{ z.real = z.imag = 0.0; /* 800x800 scale factors */
c.real = ((float) j - 400.0)/200.0;
c.imag = ((float) i - 400.0)/200.0;
k = 0;
do
{ /* iterate for pixel color */
temp = z.real*z.real - z.imag*z.imag + c.real;
z.imag = 2.0*z.real*z.imag + c.imag;
z.real = temp;
lengthsq = z.real*z.real+z.imag*z.imag;
k++;
} while (lengthsq < 4.0 && k < 100);
if (k == 100) XDrawPoint (display, win, gc, j, i);
} }
XFlush (display);
sleep (30);
/* Program Finished */
}
Open up a command prompt and run the two commands:
find / -type f - name Xlib.h
find / -type d - name X11
That should help you locate that file and/or directory so you can see whether it's available.
If it is available (e.g., it's found at /usr/include/X_stuff/X11/Xlib.h), make sure your compiler command references it, like:
g++ -I/usr/include/X_stuff ...
If it's not there, install it.
Either you forgot to install the X development packages, or the directory containing the headers was not added to the includedir list.

Arduino web client class not working

I am trying to use the Arduino client class to fetch an HTML page from the Internet (example from the Arduino library itself), but it's not working (connection is not getting established).
It's failing at:
client.connect();
I have tried both Ethernet and Ethernet2 libraries.
My Arduino development platform version is 0017, OS is Windows XP.
Following is my code and configurations inline:
#include <Client.h>
#include <Ethernet2.h>
// #include <Ethernet.h>
#include <Print.h>
#include <Server.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,7 };
byte server[] = { 74,125,47,103 }; // Google home page
byte gateway[] = { 192,168,1,1 };
byte mask[] = { 255,255,255,0 };
Client client(server, 80);
void setup()
{
Ethernet.begin(mac, ip, gateway, mask);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;);
}
}
I don't know the reason but I had to modify the following setup() function to get the code working:
void setup() {
Ethernet.begin(mac, ip, gateway, mask);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
for(int i = 0;i <100 ; i++) {
if (client.connect()) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
break;
} else {
Serial.println("connection failed");
}
}
}
The code:
client.connect()
does fail twice or thrice but eventually it connects to google web server as well as my local web server in the 3rd or 4th iteration of the 'for' loop.
If anyone knows the reason for such behavior, please reply.