Commit 82bfdbee authored by 陈敏's avatar 陈敏
Browse files

按键开关机功能版本V0.1

parent 4fd625b9
Showing with 461 additions and 397 deletions
+461 -397
This diff is collapsed.
......@@ -38,7 +38,7 @@ uint8_t sys_idle_cnt;
#define MCP_STATE_SLEEP (2)
#define MCP_SLEEP_CNT (50)
#define SYS_SLEEP_CNT (20)
#define SYS_SLEEP_CNT (10)
#define sys_exit_idle() clean_sys_idle_cnt()
#define clean_sys_idle_cnt() (sys_idle_cnt = 0)
......@@ -56,10 +56,10 @@ void APP_config(void)
//========================================================================
void sys_idle_handle(void)
{
PrintString1("idle");
if (sys_idle_cnt < SYS_SLEEP_CNT) {
sys_idle_cnt++;
} else {
// PrintString1("SYS_EVENT_SLEEP\r\n");
set_sys_event(SYS_EVENT_SLEEP);
clean_sys_idle_cnt();
}
......@@ -82,6 +82,7 @@ void sys_abnormal_handle(void)
delay_ms(1);
PrintString1("weak up!");
}
clean_all_sys_event();
}
}
......@@ -95,16 +96,16 @@ void MCP16502_handle(void)
switch (MCP_work_state)
{
case MCP_STATE_OFF:
if (get_user_event(USER_EVENT_KEY_CLICK, true))
if (get_user_event(USER_EVENT_PWR_ON, true))
{
MCP_TURNON();
MCP_TURNON();
MCP_work_state = MCP_STATE_ON;
}
break;
case MCP_STATE_ON:
if (get_user_event(USER_EVENT_KEY_CLICK, true))
if (get_user_event(USER_EVENT_SHORT_PRESS, true))
{
MCP_TURNOFF();
MCP_TURNOFF();
MCP_work_state = MCP_STATE_OFF;
}
......@@ -118,20 +119,21 @@ void MCP16502_handle(void)
}
break;
case MCP_STATE_SLEEP:
if (get_user_event(USER_EVENT_KEY_CLICK, true))
if (get_user_event(USER_EVENT_SHORT_PRESS, true))
{
MCP_TURNOFF();
MCP_TURNOFF();
MCP_work_state = MCP_STATE_OFF;
}
if (get_user_event(USER_EVENT_LONG_PRESS, true))
{
MCP_TURNOFF();
MCP_TURNOFF();
MCP_work_state = MCP_STATE_OFF;
}
break;
default:
break;
}
clean_all_user_event();
}
//========================================================================
......@@ -139,8 +141,9 @@ void MCP16502_handle(void)
//========================================================================
void key_press_handle(void)
{
static uint8_t pwr_state, key_no_release,pwr_cnt = 0;
static uint16_t key_press_cnt;
static uint16_t read_cnt;
static uint8_t read_cnt;
uint8_t state = 0;
if (++read_cnt > 6)
......@@ -151,26 +154,58 @@ void key_press_handle(void)
if (state & 0x20)
{
PrintString1(" off\r\n");
set_user_event(USER_EVENT_LONG_PRESS);
}
pwr_state = 0;
}
else
{
if (KEY_STATUS == KEY_PRESS)
{
if (key_press_cnt < 65530)
key_press_cnt++;
P13 = 1;
sys_exit_idle();
if (key_press_cnt == 2)
if (key_press_cnt == 100 && MCP_work_state == MCP_STATE_OFF)
{
key_no_release = 1;
set_user_event(USER_EVENT_PWR_ON);
}
if (key_press_cnt > 400 && key_no_release == 0 && MCP_work_state != MCP_STATE_OFF)
{
set_user_event(USER_EVENT_KEY_CLICK);
}
}
pwr_state++;
}
}
else
{
// if (key_press_cnt > 2 && key_press_cnt < 200)
// {
// set_user_event(USER_EVENT_KEY_CLICK);
// }
key_no_release = 0;
P13 = 0;
key_press_cnt = 0;
}
}
if (P12 && pwr_cnt < 200)
{
pwr_cnt++;
}
if (!P12)
{
pwr_cnt = 0;
}
if (P12 == 1 && pwr_cnt == 10)
{
pwr_state = 1;
}
else if (P12 == 0 && pwr_state == 1)
{
pwr_state = 0;
set_user_event(USER_EVENT_SHORT_PRESS);
}
else if (pwr_state > 1)
{
pwr_state = 0;
set_user_event(USER_EVENT_SHORT_PRESS);
}
}
\ No newline at end of file
......@@ -41,6 +41,8 @@ void GPIO_config(void)
// P6_MODE_IO_PU(GPIO_Pin_All); //P6 设置为准双向口
// P7_MODE_IO_PU(GPIO_Pin_All); //P7 设置为准双向口
P3_MODE_IN_HIZ(GPIO_Pin_4);
P1_MODE_IN_HIZ(GPIO_Pin_2);
P1_MODE_OUT_PP(GPIO_Pin_3);
P3_MODE_OUT_PP(GPIO_Pin_3);
P3_MODE_OUT_PP(GPIO_Pin_6);
P1_MODE_OUT_OD(GPIO_Pin_4 | GPIO_Pin_5);
......@@ -124,14 +126,14 @@ void UART_config(void)
COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
COMx_InitStructure.BaudRateDouble = DISABLE; //波特率加倍, ENABLE或DISABLE
UART_Configuration(UART1, &COMx_InitStructure); //初始化串口1 UART1,UART2,UART3,UART4
NVIC_UART1_Init(DISABLE,Priority_1); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
NVIC_UART1_Init(ENABLE,Priority_1); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
COMx_InitStructure.UART_BRT_Use = BRT_Timer2; //选择波特率发生器, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2, 所以不用选择)
COMx_InitStructure.UART_BaudRate = 115200ul; //波特率, 110 ~ 115200
COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
UART_Configuration(UART2, &COMx_InitStructure); //初始化串口2 USART1,USART2,USART3,USART4
NVIC_UART2_Init(DISABLE,Priority_2); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
// COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
// COMx_InitStructure.UART_BRT_Use = BRT_Timer2; //选择波特率发生器, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2, 所以不用选择)
// COMx_InitStructure.UART_BaudRate = 115200ul; //波特率, 110 ~ 115200
// COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
// UART_Configuration(UART2, &COMx_InitStructure); //初始化串口2 USART1,USART2,USART3,USART4
// NVIC_UART2_Init(ENABLE,Priority_2); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
}
//========================================================================
......@@ -236,6 +238,9 @@ void Switch_config(void)
//========================================================================
void SYS_Init(void)
{
WDT_InitTypeDef wtd = {ENABLE,
WDT_IDLE_RUN,
WDT_SCALE_16};
GPIO_config();
Timer_config();
// ADC_config();
......@@ -248,5 +253,6 @@ void SYS_Init(void)
EA = 1;
APP_config();
WDT_Inilize(&wtd);
}
......@@ -21,7 +21,7 @@ static TASK_COMPONENTS idata Task_Comps[]=
{
//状态 计数 周期 函数
// {0, 250, 250, Sample_Lamp}, /* task 1 Period: 250ms */
{0, 5, 5, key_press_handle}, /* task 2 Period: 5ms */
{0, 10, 10, key_press_handle}, /* task 2 Period: 5ms */
{0, 10, 10, MCP16502_handle}, /* task 3 Period: 10ms */
{0, 100, 100, sys_idle_handle}, /* task 4 Period: 100ms */
// {0, 1, 1, Sample_I2C_PS}, /* task 5 Period: 1ms */
......
......@@ -12,9 +12,9 @@
// macro define
//========================================================================
#define USER_EVENT_NONE (0 << 0)
#define USER_EVENT_KEY_CLICK (1 << 0)
#define USER_EVENT_SHORT_PRESS (1 << 0)
#define USER_EVENT_LONG_PRESS (1 << 1)
#define USER_EVENT_3 (1 << 2)
#define USER_EVENT_PWR_ON (1 << 2)
#define USER_EVENT_4 (1 << 3)
#define USER_EVENT_5 (1 << 4)
#define USER_EVENT_6 (1 << 5)
......
......@@ -8,6 +8,7 @@ void main(void)
{
Task_Pro_Handler_Callback();
sys_abnormal_handle();
WDT_Clear();
MCU_IDLE();
}
}
\ No newline at end of file
......@@ -36,7 +36,7 @@
//========================================================================
#define UART1 1
#define UART2 2
//#define UART2 2
//#define UART3 3
//#define UART4 4
......
......@@ -4,7 +4,8 @@
#else
#include "STC8G_H_Soft_I2C.h"
#endif
#include "STC8G_H_Delay.h"
#include "STC8G_H_UART.h"
//========================================================================
// @brief: write a byte to a designated register.
// @param: addr: designated register address
......@@ -125,8 +126,36 @@ void MCP16502_change_ouput(uint8_t channel_addr,uint8_t target_vol)
// @param: None.
// @return: system state mask.
//========================================================================
#define soft_reset() IAP_CONTR = IAP_CONTR | 0x20
uint8_t MCP16502_read_state(void)
{
uint8_t buck1,buck2,buck3,buck4;
// buck1 = MCP16502_single_read(STS_B1);
// buck2 = MCP16502_single_read(STS_B2);
// buck3 = MCP16502_single_read(STS_B3);
// buck4 = MCP16502_single_read(STS_B4);
// if (buck1 & 0xc0)
// {
// PrintString1("bcuk1\r\n");
// soft_reset();
// }
// if (buck2 & 0xc0)
// {
// PrintString1("buck2\r\n");
// soft_reset();
// }
// if (buck3 & 0xc0)
// {
// PrintString1("buck3\r\n");
// soft_reset();
// }
// if (buck4 & 0xc0)
// {
// PrintString1("buck4\r\n");
// soft_reset();
// }
return MCP16502_single_read(STS_SYS);
}
......@@ -138,9 +167,7 @@ uint8_t MCP16502_read_state(void)
void MCP16502_init(void)
{
uint8_t temp = 0;
MCP_TURNOFF();
MCP_TURNOFF();
temp = MCP16502_single_read(LDO1_SEQ);
temp = temp & 0xC7;
MCP16502_single_write(LDO1_SEQ,temp);
......@@ -178,23 +205,27 @@ void MCP16502_init(void)
MCP16502_single_write(OUT3_A,temp);
MCP16502_single_write(OUT3_LPM,temp);
temp = MCP16502_single_read(OUT2_CFG); //BUCK2 output enable
temp = temp | 0x03;
MCP16502_single_write(OUT2_CFG,temp);
temp = MCP16502_single_read(OUT2_SEQ); //BUCK2 output enable
temp = temp | 0x08;
MCP16502_single_write(OUT2_SEQ,temp);
temp = MCP16502_single_read(OUT3_CFG); //BUCK3 output enable
temp = temp | 0x03;
MCP16502_single_write(OUT3_CFG,temp);
temp = MCP16502_single_read(OUT3_SEQ); //BUCK3 output enable
temp = temp | 0x08;
MCP16502_single_write(OUT3_SEQ,temp);
temp = MCP16502_single_read(OUT4_CFG); //BUCK4 output enable
temp = temp | 0x03;
MCP16502_single_write(OUT4_CFG,temp);
temp = MCP16502_single_read(OUT4_SEQ); //BUCK4 output enable
temp = temp | 0x08;
MCP16502_single_write(OUT4_SEQ,temp);
temp = MCP16502_single_read(OUT1_CFG); //BUCK1 output enable
temp = temp | 0x03;
MCP16502_single_write(OUT1_CFG,temp);
temp = MCP16502_single_read(OUT1_SEQ); //BUCK1 output enable
temp = temp | 0x08;
MCP16502_single_write(OUT1_SEQ,temp);
temp = MCP16502_single_read(LDO1_CFG); //LDO1 output disable
temp = temp | 0x03;
temp = temp & 0xFC;
MCP16502_single_write(LDO1_CFG,temp);
temp = MCP16502_single_read(SYS_TMG);
temp = (temp & 0x3F) | 0x80;
MCP16502_single_write(SYS_TMG,temp);
}
\ No newline at end of file
......@@ -97,5 +97,4 @@ uint8_t MCP16502_single_read(uint8_t addr);
void MCP16502_change_ouput(uint8_t channel_addr,uint8_t target_vol);
uint8_t MCP16502_read_state(void);
#endif
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment