您的位置:首页 > 产品设计 > UI/UE

switch Value 拨码开关

2015-11-09 23:28 316 查看
#include "stdafx.h"

#include <string.h>

#define TRUE (1)

#define FALSE (0)

#define DEFAULT_REG_VAL (0xA8)

#define REG_VAL_OFFSET (0x20)

#define MAX_REG_VAL (0xA8 + REG_VAL_OFFSET)

#define MIN_REG_VAL (0xA8 - REG_VAL_OFFSET)

#define REG_VAL_STEP ((REG_VAL_OFFSET * 2)/10)

#define IncreaseRegVal(RegVal, NStep) (RegVal + REG_VAL_STEP * NStep > MAX_REG_VAL ? RegVal = MAX_REG_VAL : RegVal += REG_VAL_STEP * NStep)

#define DecreaseRegVal(RegVal, NStep) (RegVal - REG_VAL_STEP * NStep < MIN_REG_VAL ? RegVal = MIN_REG_VAL : RegVal -= REG_VAL_STEP * NStep)

/*

注:上电后,DEFAULT_REG_VAL的值即为该函数返回的初始值,显示亮度的最佳值0xa8。

REG_VAL_OFFSET为寄存器值的调试范围的0x20,即寄存器最大值(0xa8+0x20),最小值为(0xa8-0x20)。

输 入:CurSwitchVal为轮询读到的拨码开关的值,范围为0~9

返回值:为要设置给亮度寄存器的值,范围为(0xa8+0x20)~(0xa8-0x20)

*/

unsigned char RegulateRegValue(int CurSwitchVal)

{

int NStep;

static int InitFlag = TRUE;

static int PreSwitchVal;

static unsigned char RegVal;

if(InitFlag == TRUE){
//first time read the switch value

PreSwitchVal = CurSwitchVal;

RegVal = DEFAULT_REG_VAL;

InitFlag = FALSE;

return RegVal;

}

if(CurSwitchVal > PreSwitchVal){

if(CurSwitchVal - PreSwitchVal >= 7){//backward,maybe move back multiple steps

NStep = PreSwitchVal + 10 - CurSwitchVal;

DecreaseRegVal(RegVal, NStep);

}

else{//forward,maybe move multiple steps

NStep = CurSwitchVal - PreSwitchVal;

IncreaseRegVal(RegVal, NStep);

}

PreSwitchVal = CurSwitchVal;

}else if(CurSwitchVal < PreSwitchVal){

if(PreSwitchVal - CurSwitchVal >= 7){//forward,maybe move forward multiple steps

NStep = CurSwitchVal + 10 - PreSwitchVal;

IncreaseRegVal(RegVal, NStep);

}

else{//backward,maybe move multiple steps

NStep = PreSwitchVal - CurSwitchVal;

DecreaseRegVal(RegVal, NStep);

}

PreSwitchVal = CurSwitchVal;

}

return RegVal;

}

int _tmain(int argc, _TCHAR* argv[])

{

int SwitchVal;

unsigned char RegVal;

while(1){

scanf("%d", &SwitchVal);

if(SwitchVal > 9){

printf("ERR: Input Value is Invalid !!!\n");

continue;

}

printf("Input SwitchValue:%d\n", SwitchVal);

RegVal = RegulateRegValue(SwitchVal);

printf("After the Fun, RegVal:0x%x\n\n", RegVal);

}

// getchar();

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: