261 lines
8.1 KiB
C
261 lines
8.1 KiB
C
//===============> Version lockBF version 1.1 30/5/2022 stm8s Suported by Thaonv <=====================//
|
|
#include "stm8s.h"
|
|
#include "stdbool.h"
|
|
#include "stm8s_uart2.h"
|
|
#include "stdio.h"
|
|
#include "uart.h"
|
|
#include <string.h>
|
|
#define relay GPIOD,GPIO_PIN_2 //Set Relay 1 Pin
|
|
#define relay_2 GPIOD,GPIO_PIN_3 //Set Relay 1 Pin
|
|
#define Light GPIOD,GPIO_PIN_4 //Set light 1 Pin
|
|
#define button GPIOC,GPIO_PIN_5 //set button pin
|
|
#define button_2 GPIOA,GPIO_PIN_3 //set button pin
|
|
uint16_t time_delay =6000;
|
|
int t1;
|
|
uint16_t countFlagRelay_2 = 0;
|
|
uint16_t countFlagRelay_1 = 0;
|
|
char input;
|
|
char rx_indx;
|
|
char rx_buffer[10];
|
|
char str3[]="tdoor";
|
|
char t[2];
|
|
int bt_stt,bt_sttold;
|
|
|
|
/* Private defines -----------------------------------------------------------*/
|
|
bool relay_stt = false;
|
|
bool relay_stt2 = false;
|
|
bool test2 = false;
|
|
bool kickFlag_2 = false;
|
|
bool kickFlag_1 = false;
|
|
// void Delayms_timer4(uint16_t time_delay);
|
|
void CLK_Cofiguration(void);
|
|
void GPIO_Configuration(void);
|
|
void Exint_Cofiguration(void);
|
|
void control(void);
|
|
void onled(void);
|
|
void offled(void);
|
|
char Serial_read_char(void);
|
|
char Serial_print (char string[]);
|
|
void Serial_print_string (char[]);
|
|
void Serial_print_int (int number) ;
|
|
void UART_Configuration(void);
|
|
void Delayms(void);
|
|
////////////////
|
|
/* Private functions --
|
|
-------------------------------------------------------*/
|
|
INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5)
|
|
{
|
|
relay_stt=true;
|
|
}
|
|
INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3)
|
|
{
|
|
relay_stt2=true;
|
|
}
|
|
INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
|
|
{
|
|
input=Serial_read_char();
|
|
if(input=='\n') {
|
|
rx_buffer[rx_indx] = '\0';
|
|
rx_indx = 0;
|
|
if(strcmp(rx_buffer,"1")==0){
|
|
Serial_print_string("open_1\n");
|
|
relay_stt=true;
|
|
}else if(strcmp(rx_buffer,"2")==0){
|
|
Serial_print_string("open_2\n");
|
|
relay_stt2=true;
|
|
}else if(strcmp(rx_buffer,"3")==0){
|
|
Serial_print_string("open_3\n"); // on led
|
|
onled();
|
|
}else if(strcmp(rx_buffer,"4")==0){
|
|
Serial_print_string("open_4\n");
|
|
offled(); // off led
|
|
}
|
|
else if(strncmp(rx_buffer,str3,5)==0){
|
|
strncpy( t, rx_buffer+6, 2 );
|
|
t[2]='\0';
|
|
t1 = atoi(t);
|
|
time_delay=t1*1000;
|
|
}
|
|
|
|
} else {
|
|
rx_buffer[rx_indx]=input;
|
|
rx_indx++;
|
|
}
|
|
}
|
|
INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
|
|
{
|
|
|
|
if(kickFlag_2 ==true){
|
|
countFlagRelay_2 ++;
|
|
if(countFlagRelay_2>=time_delay){
|
|
countFlagRelay_2=0;
|
|
kickFlag_2=false;
|
|
GPIO_WriteLow(relay_2);
|
|
// Serial_print_string("lockdoor2\n");
|
|
}
|
|
}
|
|
if(kickFlag_1 ==true){
|
|
countFlagRelay_1 ++;
|
|
if(countFlagRelay_1>time_delay){
|
|
countFlagRelay_1=0;
|
|
kickFlag_1=false;
|
|
GPIO_WriteLow(relay);
|
|
// Serial_print_string("lockdoor1\n");
|
|
}
|
|
}
|
|
TIM4_ClearITPendingBit(TIM4_IT_UPDATE); // clear flag update interrup(TIM1_SR1)
|
|
}
|
|
void Serial_print_int (int number) //Funtion to print int value to serial monitor
|
|
{
|
|
char count = 0;
|
|
char digit[5] = "";
|
|
|
|
while (number != 0) //split the int to char array
|
|
{
|
|
digit[count] = number%10;
|
|
count++;
|
|
number = number/10;
|
|
}
|
|
|
|
while (count !=0) //print char array in correct direction
|
|
{
|
|
UART1_SendData8(digit[count-1] + 0x30);
|
|
while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET); //wait for sending
|
|
count--;
|
|
}
|
|
}
|
|
char Serial_read_char(void)
|
|
{
|
|
|
|
while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET)
|
|
UART1_ClearFlag(UART1_FLAG_RXNE);
|
|
return (UART1_ReceiveData8());
|
|
}
|
|
|
|
void Serial_print_string (char string[])
|
|
{
|
|
char i=0;
|
|
while (string[i] != 0x00)
|
|
{
|
|
UART1_SendData8(string[i]);
|
|
while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
|
|
i++;
|
|
}
|
|
}
|
|
void onled(){
|
|
GPIO_WriteHigh(Light);
|
|
}
|
|
void offled(){
|
|
GPIO_WriteLow(Light);
|
|
}
|
|
void main(void)
|
|
{
|
|
CLK_Cofiguration();
|
|
GPIO_Configuration();
|
|
Exint_Cofiguration();
|
|
UART_Configuration();
|
|
Delayms();
|
|
while (1)
|
|
{
|
|
control();
|
|
}
|
|
}
|
|
void control(){
|
|
if(relay_stt==true){
|
|
GPIO_WriteHigh(relay); // on relay2
|
|
kickFlag_1 = true;
|
|
relay_stt = false;
|
|
}
|
|
if(relay_stt2==true){
|
|
GPIO_WriteHigh(relay_2); // on relay
|
|
kickFlag_2 = true;
|
|
relay_stt2 = false;
|
|
}
|
|
}
|
|
void CLK_Cofiguration(void)
|
|
{
|
|
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
|
|
}
|
|
void GPIO_Configuration(void)
|
|
{
|
|
GPIO_DeInit(GPIOD); // reset all register port D
|
|
GPIO_Init(relay, GPIO_MODE_OUT_PP_LOW_FAST); //config pin PD2 of portD with mode push pull speed 2Mhz
|
|
GPIO_Init(relay_2, GPIO_MODE_OUT_PP_LOW_FAST);//config pin PD3 of portD with mode push pull speed 2Mhz
|
|
GPIO_Init(Light, GPIO_MODE_OUT_PP_LOW_FAST);
|
|
}
|
|
void Exint_Cofiguration(void)
|
|
{
|
|
GPIO_DeInit(GPIOC);
|
|
GPIO_DeInit(GPIOA); // reset all register port c
|
|
GPIO_Init(button, GPIO_MODE_IN_PU_IT); // config pin PC5 is input, pull up , interupt
|
|
GPIO_DeInit(GPIOA); // reset all register port A
|
|
GPIO_Init(button_2, GPIO_MODE_IN_PU_IT); // config pin PA3 is input, pull up , interupt
|
|
EXTI_DeInit(); // reset all register interupt
|
|
EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC,EXTI_SENSITIVITY_RISE_ONLY); // set interupt is edge up or edge down
|
|
EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOA,EXTI_SENSITIVITY_RISE_ONLY); // set interupt is edge up or edge down
|
|
enableInterrupts(); // Enable interrupt.
|
|
}
|
|
void UART_Configuration(void)
|
|
{
|
|
UART1_DeInit(); // reset lai toan bo thanh ghi trong uart
|
|
UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
|
|
UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE); // cai dat toc do baund la , 1bit stop, k su dung nit chan le, khong su dung chan clock, su dung ca 2 chan Tx,Rx
|
|
UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE); // cau hinh la ngat nhan UART
|
|
UART1_Cmd(ENABLE); // cho phep chay UART.
|
|
enableInterrupts(); // cho phep ngat toan cuc
|
|
|
|
}
|
|
///////////////////////////
|
|
|
|
void Delayms(void)
|
|
{
|
|
/* Init TIMER 4 */
|
|
TIM4_DeInit();
|
|
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_ARRPreloadConfig(ENABLE);
|
|
TIM4_ITConfig(TIM4_IT_UPDATE,ENABLE);
|
|
TIM4_ClearFlag(TIM4_FLAG_UPDATE); // clear flag update timer4(when over timer = 1) clear 0
|
|
TIM4_Cmd(ENABLE); // enable timer // enable timer hoat dong
|
|
enableInterrupts();
|
|
}
|
|
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(u8* file, u32 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
|
|
|