2020-03-23 07:44 AM
Hi,
I want to try use an array to set particulars outputs, base on values in that arrays
I have initiated two dimensional array like in example below:
uint8_t obrPr[4][4] = {
{0,0,1,1},
{1,1,0,0},
{0,1,1,0},
{1,0,0,1}
};
This is sequences, when the output should be ON and when OFF.
Using simple for loop, tried to toggle the outputs, but it does not working for me, like below:
if (Joystick[1] >= 2100) {
for(z=0,x=0; z < 64000; z++,x++){} // this is just for check whether the for loop is working
for(i=0; i<4; i++){
for(j=0; j<4; j++){
HAL_GPIO_WritePin(LED_GREEN_PD12_GPIO_Port, LED_GREEN_PD12_Pin, obrPr[0][j]); //IN1
HAL_GPIO_WritePin(LED_ORANGE_PD13_GPIO_Port, LED_ORANGE_PD13_Pin, obrPr[1][j]); //IN2
HAL_GPIO_WritePin(LED_RED_PD14_GPIO_Port, LED_RED_PD14_Pin, obrPr[2][j]); //IN3
HAL_GPIO_WritePin(LED_BLUE_PD15_GPIO_Port, LED_BLUE_PD15_Pin, obrPr[3][j]); //IN4
}
}
}
I'm using interrupts of TIM3 to fire the outputs with different frequencies and this above code is inside the PeriodelapsedCallback{} of TIM3.
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
if(htim->Instance == TIM3) {
...
}
Those frequencies are changing in main loop by joystick and I use below example code for it:
if (((Joystick[1] >= 2160) && (Joystick[1] <= 2650)) && tPr[0] == 1 ) {
TIM3->CNT = 0; // Set Tim3 cnt as 0
HAL_TIM_Base_Start(&htim3); // Enable Tim3
HAL_TIM_Base_Start_IT(&htim3); // enable interrupts for Tim3
TIM3->ARR = 65000;
tPr[0] = 0, tPr[1] = 1, tPr[2] = 1, tPr[3] = 1, tPr[4] = 1;
} else if (btnState == 1 && licznik_TIM3 == 1) {
} else if (((Joystick[1] >= 2703) && (Joystick[1] <= 3107)) && tPr[1] == 1 ) {
TIM3->CNT = 0;
HAL_TIM_Base_Start(&htim3);
HAL_TIM_Base_Start_IT(&htim3);
TIM3->ARR = 1600;
tPr[0] = 1, tPr[1] = 0, tPr[2] = 1, tPr[3] = 1, tPr[4] = 1;
} else if (((Joystick[1] >= 2086) && (Joystick[1] <= 2099)) && tPr[4] == 1) {
HAL_TIM_Base_Stop(&htim3);
HAL_TIM_Base_Stop_IT(&htim3);
TIM3->CNT = 0;
IN1_OFF;
IN2_OFF;
IN3_OFF;
IN4_OFF;
TIM3->ARR = 400;
tPr[0] = 1, tPr[1] = 1, tPr[2] = 1, tPr[3] = 1, tPr[4] = 0;
}
This above part is working correctly, but not in case, when the array is use. Using array, it seems the for loop stop on the last 4 step and only GRN & BLUE leds are lighting all the time, when the joy is moved to his right side. Looking at the values in obrPr array, the last step is 1,0,01, what fire the PD12 and PD15 output (32F411EDISCOVERY with STM32F407VGTx)
obrPr[4][4] = {
{0,0,1,1},
{1,1,0,0},
{0,1,1,0},
{1,0,0,1}
};
The below code, which I would like to exchange with the array, to shrink the amount of lines of the code is working OK. Means the led are switching as expected:
//Definitions at the beginning
#define IN1_ON HAL_GPIO_WritePin(LED_GREEN_PD12_GPIO_Port, LED_GREEN_PD12_Pin, GPIO_PIN_SET)
#define IN1_OFF HAL_GPIO_WritePin(LED_GREEN_PD12_GPIO_Port, LED_GREEN_PD12_Pin, GPIO_PIN_RESET)
#define IN2_ON HAL_GPIO_WritePin(LED_ORANGE_PD13_GPIO_Port, LED_ORANGE_PD13_Pin, GPIO_PIN_SET)
#define IN2_OFF HAL_GPIO_WritePin(LED_ORANGE_PD13_GPIO_Port, LED_ORANGE_PD13_Pin, GPIO_PIN_RESET)
#define IN3_ON HAL_GPIO_WritePin(LED_RED_PD14_GPIO_Port, LED_RED_PD14_Pin, GPIO_PIN_SET)
#define IN3_OFF HAL_GPIO_WritePin(LED_RED_PD14_GPIO_Port, LED_RED_PD14_Pin, GPIO_PIN_RESET)
#define IN4_ON HAL_GPIO_WritePin(LED_BLUE_PD15_GPIO_Port, LED_BLUE_PD15_Pin, GPIO_PIN_SET)
#define IN4_OFF HAL_GPIO_WritePin(LED_BLUE_PD15_GPIO_Port, LED_BLUE_PD15_Pin, GPIO_PIN_RESET)
...
if (Joystick[1] <= 1918) {
if (sekw == 1) //PD 12
{
IN2_OFF; //LED_ORANGE_PD13
IN4_OFF; //LED_BLUE_PD15
IN1_ON; //LED_GREEN_PD12
IN4_ON; //LED_BLUE_PD15
}
else if (sekw == 2) //PD 14
{
IN4_OFF; //LED_BLUE_PD15
IN3_ON; //LED_RED_PD14
}
else if (sekw == 3) //PD 13
{
IN1_OFF; //LED_GREEN_PD12
IN2_ON; //LED_ORANGE_PD13
IN3_ON; //LED_RED_PD14
}
else if (sekw == 4) //PD 15
{
IN3_OFF; //LED_RED_PD14
IN4_ON; //LED_BLUE_PD15
}
++sekw;
if (sekw == 5)
{
sekw = 1;
}
}
I would like to ask for help and explain, why is not working.
Regards
Piotr,
2020-03-23 10:37 AM
for(i=0; i<4; i++){
for(j=0; j<4; j++){
HAL_GPIO_WritePin(LED_GREEN_PD12_GPIO_Port, LED_GREEN_PD12_Pin, obrPr[0][j]); //IN1
HAL_GPIO_WritePin(LED_ORANGE_PD13_GPIO_Port, LED_ORANGE_PD13_Pin, obrPr[1][j]); //IN2
HAL_GPIO_WritePin(LED_RED_PD14_GPIO_Port, LED_RED_PD14_Pin, obrPr[2][j]); //IN3
HAL_GPIO_WritePin(LED_BLUE_PD15_GPIO_Port, LED_BLUE_PD15_Pin, obrPr[3][j]); //IN4
}
}
What is i doing here? Why is j looping at all? Why is everything inconsistently indented?
Code seems to be doing exactly what you're asking it to do, which is basically the following:
HAL_GPIO_WritePin(LED_GREEN_PD12_GPIO_Port, LED_GREEN_PD12_Pin, obrPr[0][3]); //IN1
HAL_GPIO_WritePin(LED_ORANGE_PD13_GPIO_Port, LED_ORANGE_PD13_Pin, obrPr[1][3]); //IN2
HAL_GPIO_WritePin(LED_RED_PD14_GPIO_Port, LED_RED_PD14_Pin, obrPr[2][3]); //IN3
HAL_GPIO_WritePin(LED_BLUE_PD15_GPIO_Port, LED_BLUE_PD15_Pin, obrPr[3][3]); //IN4
2020-03-23 04:27 PM
TDK,
Thanks for update
for(i=0; i<4; i++){
for(j=0; j<4; j++){
HAL_GPIO_WritePin(LED_GREEN_PD12_GPIO_Port, LED_GREEN_PD12_Pin, obrPr[0][j]); //IN1
HAL_GPIO_WritePin(LED_ORANGE_PD13_GPIO_Port, LED_ORANGE_PD13_Pin, obrPr[1][j]); //IN2
HAL_GPIO_WritePin(LED_RED_PD14_GPIO_Port, LED_RED_PD14_Pin, obrPr[2][j]); //IN3
HAL_GPIO_WritePin(LED_BLUE_PD15_GPIO_Port, LED_BLUE_PD15_Pin, obrPr[3][j]); //IN4
}
}
Hope now is better.
So, why j is looping? Because I'm using it to red the values in columns and this way set 1 or 0 in the parameter for HAL_GPIO_WritePin. Each column include info whether the particular of the 4 pins should be on(1) or off(0) in one time.
Suppose the loop is going to i = 4 & j = 4 and then leaves this loop. As the first parameter if (Joystick[1] >= 2100) is still met, again jumping to to the for loop and repeat it n times, till the condition of the if (Joystick[1] >= 2100) is true.
Is not that?
In debug mode, jumping line by line, the pins are toggled correctly.
2020-03-25 04:49 AM
Ok, I found the reason
Thanks