#include "stm8s.h"
#include "system.h"
#define AFR_ADDRESS 0x4803
UINT8 g_decode_ok = 0,g_onoff_flag = 0;
UINT8 g_RcvData[3]={0x00,0x00,0x00};
UINT8 g_pwmVal[4]={30,50,100,180},g_downFlag[4],g_switch_output[4];
const KeyVal[10]={
0xc0,//1号按键
0x30,//2号按键
0xf0,//3
0x0c,//4
0xcc,//5
0x3c,//6
0xfc,//7
0x03,//8
0x33,//2+8组合键
0x0a};//
/*************************************************
Function: Delay
Description: set delay function
Input: delay arg
Output: none
Return: none
Others: none
*************************************************/
void Delay(UINT16 dly)
{
UINT16 x, y;
for(x = dly; x > 0; x--)
{
for(y = 220; y > 0; y--);
IWDG_ReloadCounter();
}
}
/*************************************************
Function: GPIO_AFuncRemap
Description: set GPIO alternate function remapping
Input: gpio afr arg
Output: none
Return: none
Others: none
*************************************************/
void GPIO_AFuncRemap(UINT8 Func)
{
UINT8 Temp;
FLASH_Unlock(FLASH_MEMTYPE_DATA);
while(!(FLASH->IAPSR & FLASH_IAPSR_DUL));/*等待解锁完毕*/
Temp=(UINT8)(FLASH_ReadOptionByte(AFR_ADDRESS)>>8);
while(!(FLASH->IAPSR & FLASH_IAPSR_DUL));/*等待解锁完毕*/
if((Temp&Func) != Func)
{
FLASH_ProgramOptionByte(AFR_ADDRESS,Func|Temp);
Temp=(UINT8)(FLASH_ReadOptionByte(AFR_ADDRESS)>>8);
}
FLASH_Lock(FLASH_MEMTYPE_DATA);/*操作完要加锁*/
}
/*************************************************
Function: Init_WatchDog
Description: initialize WatchDog
Input: none
Output: none
Return: none
Others: none
*************************************************/
void Init_WatchDog(void)
{
IWDG_Enable();
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
IWDG_SetPrescaler(IWDG_Prescaler_256);
IWDG_SetReload(0xff);
IWDG_ReloadCounter();
}
/*************************************************
Function: Init_GPIO
Description: initialize GPIOB for input
Input: none
Output: none
Return: none
Others: none
*************************************************/
void Init_GPIO(void)
{
//output
GPIO_Init(GPIOA, GPIO_PIN_3,GPIO_MODE_OUT_PP_HIGH_FAST);//led
GPIO_Init(GPIOD, GPIO_PIN_2,GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOC, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6,GPIO_MODE_OUT_PP_LOW_FAST);
//input
GPIO_Init(GPIOD, GPIO_PIN_3,GPIO_MODE_IN_PU_NO_IT);
}
/*************************************************
Function: Init_pwm_Output
Description: initialize the output compare of TIME1
Input: none
Output: none
Return: none
Others: none
*************************************************/
void Init_pwm_Output(void)
{
TIM1_DeInit();
TIM1_TimeBaseInit(0x04, TIM1_COUNTERMODE_UP, 2560, 0x00);//4分频,2560为周期值,频率为250Hz左右
TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE,
TIM1_OUTPUTNSTATE_DISABLE, 2560,
TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_HIGH,
TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_RESET);
TIM1_CtrlPWMOutputs(ENABLE); // enable TIM1 PWM output
TIM1_Cmd(ENABLE); // TIME1 Enable
}
/*************************************************
Function: Init_TIM2
Description: initialize TIME2 for pulse time
Input: none
Output: none
Return: none
Others: none
*************************************************/
void Init_TIM2(void)
{
TIM2_TimeBaseInit(TIM2_PRESCALER_1, 0xffff);
TIM2_SetCounter(0x0000);
TIM2_ITConfig(TIM2_IT_UPDATE, DISABLE);
TIM2_Cmd(ENABLE);
}
/*************************************************
Function: GPIO_input_signal
Description: parse GPIO input signal
Input: none
Output: none
Return: none
Others: none
*************************************************/
void GPIO_input_signal(void)
{
static UINT8 flag = 0, bitCode = 0, tmp_data = 0,pressCnt = 0;
static BitStatus inputSta = RESET,preInputSta = RESET;
static UINT16 t_cur = 0, t_pre = 0, t_width = 0, buadrate = 0;
static UINT16 H_level_cntVal = 0,L_level_cntVal = 0, Enter_sta = 0,;
inputSta = GPIO_ReadInputPin(GPIOD,GPIO_PIN_3);
if(preInputSta != inputSta)
{
t_cur = TIM2_GetCounter();
t_width = t_cur - t_pre;
if(inputSta) //form low to high
{
L_level_cntVal = t_width;
}else{
H_level_cntVal = t_width;
}
switch(Enter_sta)//解码状态机
{
case 0:
if(L_level_cntVal > 0x5000) //寻找同步码
{
GPIO_WriteLow(GPIOA,GPIO_PIN_3); //亮指示灯
Enter_sta = 1;
bitCode = 0;
flag = 0;
}
break;
case 1:
if(flag)
{
if(L_level_cntVal < H_level_cntVal)
{
tmp_data = (tmp_data<<1) | 0x01;
}else{
tmp_data = (tmp_data<<1);
}
bitCode ++;
if((bitCode%8) == 0)
{
g_RcvData[(bitCode/8)-1] = tmp_data;
if((bitCode/8) == 3)
{
g_decode_ok = 1;
Enter_sta = 2;
}
}
flag = 0;
}else{
flag = 1;
}
break;
case 2:
if(L_level_cntVal > 0x5000) //寻找同步码
{
if(pressCnt++ >= 5)
{
Enter_sta = 0;
pressCnt = 0;
GPIO_WriteHigh(GPIOA,GPIO_PIN_3);
}
}
if(t_width < 0x100)
{
Enter_sta = 0;
GPIO_WriteHigh(GPIOA,GPIO_PIN_3);
}
break;
default:
break;
}
t_pre = t_cur;
preInputSta = inputSta;
}
}
/*************************************************
Function: read_flag
Description: read flag data from eeprom
Input: addr of eeprom
Output: none
Return: data store in eeprom
Others: none
*************************************************/
UINT8 read_flag(UINT16 addr)
{
UINT8 tmp;
FLASH_Unlock(FLASH_MEMTYPE_DATA);
while(!(FLASH->IAPSR & FLASH_IAPSR_DUL))/*等待解锁完毕*/
{
IWDG_ReloadCounter();
}
tmp = FLASH_ReadByte(addr);
FLASH_Lock(FLASH_MEMTYPE_DATA);/*操作完要加锁*/
return tmp;
}
/*************************************************
Function: write_flag
Description: write flag data to eeprom
Input: addr of eeprom,data store in eeprom
Output: none
Return: none
Others: none
*************************************************/
void write_flag(UINT16 addr,UINT8 data)
{
FLASH_Unlock(FLASH_MEMTYPE_DATA);
while(!(FLASH->IAPSR & FLASH_IAPSR_DUL))/*等待解锁完毕*/
{
IWDG_ReloadCounter();
}
FLASH_ProgramByte(addr,data);
FLASH_Lock(FLASH_MEMTYPE_DATA);/*操作完要加锁*/
}
/*************************************************
Function: GPIO_input_signal
Description: parse input signal function
Input: none
Output: none
Return: none
Others: none
*************************************************/
void parse_func(void)
{
static UINT8 ch_num = 0,run_once = 0,pre_downFlag[4],pre_switch_output[4],pre_pwmVal[4];
UINT8 i;
for(i = 0; i < 10; i++)
{
if(g_RcvData[2] == KeyVal)
{
switch(i)
{
case 0:
if(g_onoff_flag == 1)
{
if(g_downFlag[ch_num] == 0)
{
++g_pwmVal[ch_num];
if(g_pwmVal[ch_num] > 255)
{
g_downFlag[ch_num] = 1;
if(g_switch_output[ch_num] == 0)
{
g_switch_output[ch_num] = 1;
}else{
g_switch_output[ch_num] = 0;
}
}
}else{
--g_pwmVal[ch_num];
if(g_pwmVal[ch_num] < 1)
{
g_downFlag[ch_num] = 0;
if(g_switch_output[ch_num] == 0)
{
g_switch_output[ch_num] = 1;
}else{
g_switch_output[ch_num] = 0;
}
}
}
TIM1_SetCompare1(g_pwmVal[ch_num]);
if(pre_downFlag[ch_num] != g_downFlag[ch_num])
{
write_flag(0x400A+ch_num,g_downFlag[ch_num]);
pre_downFlag[ch_num] = g_downFlag[ch_num];
}
if(pre_pwmVal[ch_num] != g_pwmVal[ch_num])
{
write_flag(0x4002+ch_num,g_pwmVal[ch_num]);
pre_pwmVal[ch_num] = g_pwmVal[ch_num];
}
}
break;
case 1:
if(g_onoff_flag == 1)
{
if(g_downFlag[ch_num] == 0)
{
--g_pwmVal[ch_num];
if(g_pwmVal[ch_num] < 1)
{
g_downFlag[ch_num] = 1;
if(g_switch_output[ch_num] == 0)
{
g_switch_output[ch_num] = 1;
}else{
g_switch_output[ch_num] = 0;
}
}
}else{
++g_pwmVal[ch_num];
if(g_pwmVal[ch_num] > 255)
{
g_downFlag[ch_num] = 0;
if(g_switch_output[ch_num] == 0)
{
g_switch_output[ch_num] = 1;
}else{
g_switch_output[ch_num] = 0;
}
}
}
TIM1_SetCompare1(g_pwmVal[ch_num]);
if(pre_downFlag[ch_num] != g_downFlag[ch_num])
{
write_flag(0x400A+ch_num,g_downFlag[ch_num]);
pre_downFlag[ch_num] = g_downFlag[ch_num];
}
if(pre_pwmVal[ch_num] != g_pwmVal[ch_num])
{
write_flag(0x4002+ch_num,g_pwmVal[ch_num]);
pre_pwmVal[ch_num] = g_pwmVal[ch_num];
}
}
break;
case 2:
if(run_once == 0)
{
GPIO_WriteLow(GPIOD,GPIO_PIN_2);
TIM1_CtrlPWMOutputs(ENABLE);
run_once = 1;
}
ch_num = 0;
g_onoff_flag = 1;
TIM1_SetCompare1(g_pwmVal[ch_num]);
break;
case 3:
if(run_once == 0)
{
GPIO_WriteLow(GPIOD,GPIO_PIN_2);
TIM1_CtrlPWMOutputs(ENABLE);
run_once = 1;
}
ch_num = 1;
g_onoff_flag = 1;
TIM1_SetCompare1(g_pwmVal[ch_num]);
break;
case 4:
if(run_once == 0)
{
GPIO_WriteLow(GPIOD,GPIO_PIN_2);
TIM1_CtrlPWMOutputs(ENABLE);
run_once = 1;
}
ch_num = 2;
g_onoff_flag = 1;
TIM1_SetCompare1(g_pwmVal[ch_num]);
break;
case 5:
if(run_once == 0)
{
GPIO_WriteLow(GPIOD,GPIO_PIN_2);
TIM1_CtrlPWMOutputs(ENABLE);
run_once = 1;
}
ch_num = 3;
g_onoff_flag = 1;
TIM1_SetCompare1(g_pwmVal[ch_num]);
break;
case 6:
if(g_onoff_flag == 1)
{
if(g_switch_output[ch_num] == 0)
{
g_switch_output[ch_num] = 1;
}else{
g_switch_output[ch_num] = 0;
}
}
break;
case 7://8按键
GPIO_WriteHigh(GPIOD,GPIO_PIN_2);
TIM1_CtrlPWMOutputs(DISABLE);
GPIO_Init(GPIOC,GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5,GPIO_MODE_OUT_PP_LOW_FAST);
g_onoff_flag = 0;
run_once = 0;
break;
case 8:
break;
default:
break;
}
if(g_onoff_flag == 1)
{
if(g_switch_output[ch_num] == 0)
{
GPIO_Init(GPIOC,GPIO_PIN_4,GPIO_MODE_IN_FL_NO_IT);
GPIO_Init(GPIOC,GPIO_PIN_3|GPIO_PIN_5,GPIO_MODE_OUT_PP_LOW_FAST);
}else{
GPIO_Init(GPIOC,GPIO_PIN_5,GPIO_MODE_IN_FL_NO_IT);
GPIO_Init(GPIOC,GPIO_PIN_3|GPIO_PIN_4,GPIO_MODE_OUT_PP_LOW_FAST);
}
if(pre_switch_output[ch_num] != g_switch_output[ch_num])
{
write_flag(0x4006+ch_num,g_switch_output[ch_num]);
pre_switch_output[ch_num] = g_switch_output[ch_num];
}
}
}
}
}
/*************************************************
Function: main
Description: The main function,receive RF signal and control PWM duty value output
Input: none
Output: none
Return: none
Others: none
*************************************************/
void main(void)
{
UINT8 flag;
int i;
GPIO_AFuncRemap(0x01);
Init_TIM2();
Init_GPIO();
Init_pwm_Output();
Init_WatchDog();
GPIO_WriteHigh(GPIOD,GPIO_PIN_2);
TIM1_CtrlPWMOutputs(DISABLE);
GPIO_Init(GPIOC,GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5,GPIO_MODE_OUT_PP_LOW_FAST);
flag = read_flag(0x4000);
if(flag != 0x55) //第一次使用
{
write_flag(0x4000,0x55);
for(i = 0;i < 4;i++)
write_flag(0x4002+i,g_pwmVal);
}
if(flag == 0x55)
{
for(i = 0;i < 4;i++)
{
g_pwmVal = read_flag(0x4002+i);
g_switch_output = read_flag(0x4006+i);
g_downFlag = read_flag(0x400A+i);
}
}
while(1)
{
IWDG_ReloadCounter();
if(g_decode_ok)
{
g_decode_ok = 0;
if((g_RcvData[0] == 0x47)&&(g_RcvData[1] == 0x71))
{
parse_func();
}
}
GPIO_input_signal();
}
}
在原基础上 按8键5秒 再按1键、7键加锁,使程序暂停不工作。 解锁按8 键5秒 在按2键、7键使程序正常工作 求大神帮忙 QQ9650886 |