92 lines
2.9 KiB
C
92 lines
2.9 KiB
C
|
|
/*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
|
|
#define test_LED GPIOD, GPIO_PIN_4
|
|
|
|
//Main Code
|
|
void Delayms_timer4(uint16_t time_delay);
|
|
void CLK_Cofiguration(void);
|
|
void readbutton(void);
|
|
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);
|
|
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
|
|
GPIO_WriteHigh(test_LED); //LED OFF
|
|
Serial_print_string("open_1");
|
|
Delayms_timer4(4000);
|
|
GPIO_WriteLow(test_LED); //LED ON
|
|
}
|
|
}
|
|
}
|
|
void readbutton(){
|
|
int data = 0;
|
|
data = GPIO_ReadInputPin(GPIOC, GPIO_PIN_4);
|
|
|
|
if(data == 0){
|
|
GPIO_WriteHigh(test_LED);
|
|
Delayms_timer4(4000);
|
|
GPIO_WriteLow(test_LED);
|
|
}
|
|
}
|
|
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 |