/** * @brief Example main entry point. * @par Parameters: * None * @retval * None */ void main(void) { /* Initialize I/Os in Output Mode for LEDs */ GPIO_Init(LEDS_PORT, LED1_PIN | LED2_PIN, GPIO_MODE_OUT_PP_LOW_FAST); /* Initialize I/O in Input Mode with Interrupt for button and joystick */ /* Button */ GPIO_Init(BUTTON_PORT, BUTTON_PIN, GPIO_MODE_IN_FL_IT); /* Joystick */ GPIO_Init(JOYSTICK_PORT, JOYSTICK_DOWN_PIN, GPIO_MODE_IN_FL_IT); /* Initialize the Interrupt sensitivity for button and joystick */ EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOB, EXTI_SENSITIVITY_FALL_LOW); EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_LOW); /* Change the software priority between button and joystick: - By hardware the PORTB (joystick) has a higher priority than PORTC (button) - We configure the PORTC (button) to have a higher sw priority than PORTB (joystick) */ ITC_SetSoftwarePriority(ITC_IRQ_PORTB, ITC_PRIORITYLEVEL_1); /* joystick */ ITC_SetSoftwarePriority(ITC_IRQ_PORTC, ITC_PRIORITYLEVEL_2); /* button = higher sw priority */ enableInterrupts(); /* LEDs are ON together */ GPIO_WriteHigh(LEDS_PORT, (LED1_PIN | LED2_PIN)); while (1); }
void timer_init() { TIM2_DeInit(); TIM2_TimeBaseInit(TIM2_PRESCALER_1, 1600); //TIM2_PrescalerConfig(TIM2_PRESCALER_32768, TIM2_PSCRELOADMODE_IMMEDIATE); TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE); ITC_SetSoftwarePriority(ITC_IRQ_TIM2_OVF, ITC_PRIORITYLEVEL_1); TIM2_Cmd(ENABLE); }
void setINTPri(void) { disableInterrupts(); ITC_SetSoftwarePriority(ITC_IRQ_TIM1_OVF, ITC_PRIORITYLEVEL_2); ITC_SetSoftwarePriority(ITC_IRQ_ADC1, ITC_PRIORITYLEVEL_1); ITC_SetSoftwarePriority(ITC_IRQ_TIM2_OVF, ITC_PRIORITYLEVEL_2); ITC_SetSoftwarePriority(ITC_IRQ_TIM4_OVF, ITC_PRIORITYLEVEL_2); ITC_SetSoftwarePriority(ITC_IRQ_UART2_TX, ITC_PRIORITYLEVEL_3); ITC_SetSoftwarePriority(ITC_IRQ_UART2_RX, ITC_PRIORITYLEVEL_3); enableInterrupts(); }
/** * @brief Main program. * @param None * @retval None */ void main() { /* TIM4 & I2C clock Enable*/ CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE); CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE); #ifdef FAST_I2C_MODE /* system_clock / 1 */ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); #else /* system_clock / 2 */ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_2); #endif /* Initialize LEDs mounted on STM8L1526-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Initialize push-buttons mounted on STM8L1526-EVAL board */ STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_EXTI); /* Initialize TIM4 peripheral to generate an interrupt each 1ms */ TIM4_TimeBaseInit(TIM4_Prescaler_128, TIM4_PERIOD); /* Enable Update interrupt */ TIM4_ITConfig(TIM4_IT_Update, ENABLE); /* Initialize I2C peripheral */ I2C_Init(I2C1, I2C_SPEED, 0xA0, I2C_Mode_I2C, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit); /* Set I2C IT software priority as highest */ #ifdef STM8L15X_MD ITC_SetSoftwarePriority(I2C1_IRQn, ITC_PriorityLevel_3); #elif defined (STM8L15X_HD) || defined (STM8L15X_MDP) ITC_SetSoftwarePriority(I2C1_SPI2_IRQn, ITC_PriorityLevel_3); #endif /* STM8L15X_MD */ enableInterrupts(); /* Enable TIM4 */ TIM4_Cmd(ENABLE); while (1) { switch (PressedButton) { case BUTTON_RIGHT: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x01; ButtonPressed = 1; break; case BUTTON_LEFT: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x02; ButtonPressed = 1; break; case BUTTON_UP: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x03; ButtonPressed = 1; break; case BUTTON_DOWN: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x04; ButtonPressed = 1; break; case BUTTON_SEL: PressedButton = NO_BUTTON; NumOfBytes = BUFFERSIZE; TxBuffer[0] = 0xAA; ButtonPressed = 1; for (i = 1; i < NumOfBytes; i++) { TxBuffer[i] = i; } break; default: break; } if (ButtonPressed == 1) { /* Enable Buffer and Event Interrupt*/ I2C_ITConfig(I2C1, (I2C_IT_TypeDef)(I2C_IT_EVT | I2C_IT_BUF) , ENABLE); /* Generate the Start condition */ I2C_GenerateSTART(I2C1, ENABLE); /* Data transfer is performed in the I2C interrupt routine */ /* Wait until end of data transfer */ while (NumOfBytes); while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); /* Make sure that the STOP bit is cleared by Hardware before CR2 write access */ while ((I2C1->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP); ButtonPressed = 0; } } }
/** * @brief Main program. * @param None * @retval None */ void main(void) { /* Initialize LEDs mounted on STM8L152X-EVAL board */ STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); /* Initialize key and Joystick down buttons mounted on STM8L152X-EVAL board */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_EXTI); #ifdef USE_STM8L1526_EVAL /* Set PC1 sensitivity to falling edge and low level */ EXTI_SetPinSensitivity(EXTI_Pin_1, EXTI_Trigger_Falling_Low); /* Set PD4 sensitivity to falling edge and low level */ EXTI_SetPinSensitivity(EXTI_Pin_4, EXTI_Trigger_Falling_Low); /* Change the software priority between key and joystick down buttons: - By default the Pin_1 (key) and Pin_4 have the same priority level: level3 - We configure the Pin_4 (joystick_down) to have a higher sw priority than Pin_1 (key) */ /* Set the EXTI1 IRQ (which is connected to key button) level to 1 */ ITC_SetSoftwarePriority(EXTI1_IRQn, ITC_PriorityLevel_1); /* Set the EXTI4 IRQ (which is connected to joystick down button) software level to 2 */ /* Thus joystick down button IRQ software priority is higher than key one */ /* Pressing joystick down button interrupts the execution of the EXTI1 Interrupt Service Routine */ ITC_SetSoftwarePriority(EXTI4_IRQn, ITC_PriorityLevel_2); #elif defined USE_STM8L1528_EVAL /* Set PH6 sensitivity to falling edge and low level */ EXTI_SetPinSensitivity(EXTI_Pin_6, EXTI_Trigger_Falling_Low); /* Set PG1 sensitivity to falling edge and low level */ EXTI_SetPinSensitivity(EXTI_Pin_1, EXTI_Trigger_Falling_Low); /* Change the software priority between key and joystick down buttons: - By default the Pin_6 (key) and Pin_1 have the same priority level: level3 - We configure the Pin_1 (joystick_down) to have a higher sw priority than Pin_6 (key) */ /* Set the EXTI6 IRQ (which is connected to key button) level to 1 */ ITC_SetSoftwarePriority(EXTI6_IRQn, ITC_PriorityLevel_1); /* Set the EXTI1 IRQ (which is connected to joystick down button) software level to 2 */ /* Thus joystick down button IRQ software priority is higher than key one */ /* Pressing joystick down button interrupts the execution of the EXTI6 Interrupt Service Routine */ ITC_SetSoftwarePriority(EXTI1_IRQn, ITC_PriorityLevel_2); #endif /* USE_STM8L152X_EVAL */ /* enable interrupts by setting level 0 */ enableInterrupts(); /* LD2 and LD3 are on */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); while (1); }