I am wokring with a STM32F746. This is my first ARM project. I have IAR ARM compiler as well as the ST Cube IDE.
I am trying to flash just 8 LEDS in sequence. but having a problem with the HAL_GPIO_TogglePin(). Two of the LEDs are on PORTG and six on
PORTH.
I can not seem to make a variable work to assign the different ports GPIOG or GPIOH. I have done a lot of C programming for 8 bit microcontrollers and don't normally have this problem. It is something simple but having a mind block at the moment.
Here is my code. It works if I write everything out but when I try to shorten it using for loops and if statements I run into problems.
This should be a no brainer!
Any help or ideas would be greatly appreciate. The final code should count display the binary of 0-255 but I need to get this working before I try that!
int main(void){ /* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */ SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC3_Init(); MX_QUADSPI_Init(); MX_UART5_Init(); MX_USB_OTG_HS_HCD_Init(); MX_FMC_Init(); MX_I2C1_Init(); MX_I2C2_Init(); MX_UART4_Init(); /* USER CODE BEGIN 2 */ uint16_t pin[] = {GPIO_PIN_2, GPIO_PIN_3, GPIO_PIN_10, GPIO_PIN_11, GPIO_PIN_12, GPIO_PIN_13, GPIO_PIN_14, GPIO_PIN_15};
uint8_t i;
int port;
port = GPIOG;
for (i=1;i<=9;i++) { if (i > 2) { port = GPIOH; }
HAL_GPIO_WritePin (port, pin[i],GPIO_PIN_SET);
}
// HAL_GPIO_WritePin (GPIOG, GPIO_PIN_2, GPIO_PIN_SET); // HAL_GPIO_WritePin (GPIOG, GPIO_PIN_3, GPIO_PIN_SET); // HAL_GPIO_WritePin (GPIOH, GPIO_PIN_10, GPIO_PIN_SET); // HAL_GPIO_WritePin (GPIOH, GPIO_PIN_11, GPIO_PIN_SET); // HAL_GPIO_WritePin (GPIOH, GPIO_PIN_12, GPIO_PIN_SET); // HAL_GPIO_WritePin (GPIOH, GPIO_PIN_13, GPIO_PIN_SET); // HAL_GPIO_WritePin (GPIOH, GPIO_PIN_14, GPIO_PIN_SET); // HAL_GPIO_WritePin (GPIOH, GPIO_PIN_15, GPIO_PIN_SET);
/* USER CODE END 2 */
/* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) {
for (i=1;i<=9;i++) { if (i <= 2) { port = GPIOG; } else { port = GPIOH; }
HAL_GPIO_TogglePin(port, pin[i]); HAL_Delay(500); HAL_GPIO_TogglePin(port, pin[i]); }
//HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_2); //HAL_Delay(500); //HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_2); //HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_3); //HAL_Delay(500); //HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_3); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_10); //HAL_Delay(500); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_10); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_11); //HAL_Delay(500); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_11); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_12); //HAL_Delay(500); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_12); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_13); //HAL_Delay(500); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_13); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_14); //HAL_Delay(500); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_14); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_15); //HAL_Delay(500); //HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_15);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */ } /* USER CODE END 3 */}
At first: Please use "Insert Code" when posting code.
Then: If it is a chip specific question (like STM32), you'd better ask this in an ST forum.Now: Use rather "int" not "uint8_t" unless you really mean to need 8 bits only.Even on 8biters, C arrays start at 0 not 1 ;-)
David-WD8CXB said:uint16_t pin[] = {GPIO_PIN_2, GPIO_PIN_3, GPIO_PIN_10, GPIO_PIN_11, GPIO_PIN_12, GPIO_PIN_13, GPIO_PIN_14, GPIO_PIN_15}; uint8_t i; int port; port = GPIOG; for (i=1;i<=9;i++)
uint16_t pin[] = {GPIO_PIN_2, GPIO_PIN_3, GPIO_PIN_10, GPIO_PIN_11, GPIO_PIN_12, GPIO_PIN_13, GPIO_PIN_14, GPIO_PIN_15};
for (i=1;i<=9;i++)
for( i = 0; i < sizeof(pin)/sizeof(uint16_t); ++i)
Is better, 1 and 9 are not correct.
Thanks for the reply. I did not see the code insert but will do so when posting again. Would make reading the code much easier.