/*Control on-board LED through USART * PD5 - UART1-Tx * PD6 - UART1-Rx */ //Required Headers #include "STM8S.h" #include "stm8s103_serial.h" //https://github.com/CircuitDigest/STM8S103F3_SPL/blob/master/stm8s103%20Libraries/stm8s103_Serial.h #include "stdio.h" #include "stdbool.h" #define test_LED GPIOD, GPIO_PIN_4 #define light GPIOC, GPIO_PIN_5 bool relay_stt =false; bool status_light= false; int data; int data_old; //Main Code void Delayms_timer4(uint16_t time_delay); void CLK_Cofiguration(void); void readbutton(void); void checkbutton(void); //dang code do 8/3 void TurnOn_Light(){ GPIO_WriteHigh(light); } void TurnOff_Light(){ GPIO_WriteLow(light); } /////////////////// void main(void) { CLK_Cofiguration(); GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_IN_FL_NO_IT); GPIO_Init (test_LED, GPIO_MODE_OUT_PP_LOW_SLOW); GPIO_Init (light, GPIO_MODE_OUT_PP_LOW_SLOW); char ch; Serial_begin(9600); // Serial_print_string("Enter command"); Serial_newline(); while (1) //Loop function { readbutton(); if(Serial_available()) { // Serial_print_string("You have pressed: "); ch = Serial_read_char(); Serial_print_char(ch); Serial_newline(); if (ch == '1'){ //GPIO_WriteLow(test_LED); //LED ON if(relay_stt == false){ relay_stt = true; } Serial_print_string("open_1"); }else if (ch == '3'){ TurnOn_Light(); }else if (ch == '4'){ TurnOff_Light(); } } checkbutton(); } } void readbutton(){ data = GPIO_ReadInputPin(GPIOC, GPIO_PIN_4); if(data == 0 ){ relay_stt =true; } else{ relay_stt=false; } // data_old = data; } void checkbutton(){ if (relay_stt == true) { GPIO_WriteHigh(test_LED); Delayms_timer4(4000); // Open the door lock for 300 ms GPIO_WriteLow(test_LED); relay_stt = false; } else { relay_stt = false; } } void CLK_Cofiguration(void) { CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); } void Delayms_timer4(uint16_t time_delay) { /* Init TIMER 4 */ CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, ENABLE); // enable clock timer 4 TIM4_TimeBaseInit(TIM4_PRESCALER_64, 250); // set frequence timer 4 la 16*10^6/64/250 = 1000Hz TIM4_SetCounter(0); // set counter value = 0 //set counter ve gia tri ban dau =0 TIM4_ClearFlag(TIM4_FLAG_UPDATE); // clear flag update timer4(when over timer = 1) clear 0 TIM4_Cmd(ENABLE); // enable timer // enable timer hoat dong while(time_delay--) // tan so la 1000Hz -> khi lap lai 1000 lan la 1Hz = 1s { while(TIM4_GetFlagStatus(TIM4_FLAG_UPDATE)== 0) ; // watting over timer exit whiles TIM4_ClearFlag(TIM4_FLAG_UPDATE); // clear timer for next counter } /* Disable Counter */ TIM4_Cmd(DISABLE); // sau khi delay xong, disable timer. CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);// Disable clock for timer when finish delay } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t* file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */ while (1) { } } #endif