⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 d015.c

📁 / 功能:背部上下行走,分颈肩背腰全程 // 座垫一振动
💻 C
📖 第 1 页 / 共 2 页
字号:
//project name :RT-dD015
// 功能:背部上下行走,分颈肩\背腰\全程
// 座垫一振动,分三档速度
// 小腿一揉捏,分三档速度
// 小腿四敲击,分三档力度
// coded by pengxifeng
#include <ht46r23.h>
#define TRUE 1                           //常量定义无前缀n及l
#define FALSE 0
#define BeatOn 1
#define BeatOff 0
#define High 1
#define Low 0
#define On 1
#define Off 0
#define LedOn 0
#define LedOff 1
#define VibOn 0
#define VibOff 1
#define MaxHighLevel 2
#define StopState 0              // 行走电机停止状态
#define UpState 1                // 行走电机上升状态
#define DownState 2              // 行走电机下降状态
#define StopWard 0
#define UpWard 1
#define DownWard 0
#define ACStopTime 4
#define RelayStopTime 1
#define ResetACStopTime 40
#define ResetRelayStopTime 10
#define ACOn 0
#define ACOff 1
#define MaxKeyUpTime 1                          // 键盘处理时间(松开时间)
#define MaxKeyDownTime 1                        // 键盘处理时间(按下时间)
#define bWalkUpDwTriac _pb5                     // 背部行走电机控制
#define bWalkUpDwRelay _pb6                     // 背部电机继电器换向控制
#define bWLimUp _pb3                            // 上行程开关检测
#define bWLimDw _pb4                            // 下行程开关检测
#define bSelA _pb0
#define bSelB _pb1                              // 138译码选择端
#define bSelC _pb2
#define bVib1 _pc1
#define bVib2 _pc2                              // 振动电机1、2、3控制
#define bVib3 _pc3
#define bLegKnead _pd0                          

struct Back
{
	unsigned char MoveDirection;
	unsigned char CheckTime;
	unsigned char MoveTime;
};

const BackFullMoveDirection[]		=	{2 ,1 };
const BackFullCheckTime[]			=	{1 ,1 };
const BackFullMoveTime[] 			=	{42,42};

const BackUpperMoveDirection[]	=	{1 ,2};
const BackUpperCheckTime[]		=	{1 ,0};
const BackUpperMoveTime[] 		=	{42,42};

const BackLowerMoveDirection[]	=	{2, 1};
const BackLowerCheckTime[]		=	{1,	0};
const BackLowerMoveTime[] 		=	{42,42};

const unsigned char Seg[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc6,0x86,0x8e};  //显示码
const unsigned char TotalAutoStep[] = {0,2,2,2};
#pragma rambank0                               
unsigned char nLedValue0 @0x40;            // 指示灯显示缓冲区
unsigned char nLedValue1 @0x41;            // 数码管显示缓冲区
unsigned char nLedValue2 @0x42;
unsigned char nLedValue3 @0x43;
unsigned char nLedValue4 @0x44;
unsigned char nLedValue5 @0x45;
#define bLedAuto 			_40_0          // 全自动灯
#define bLedBackSW			_40_1          // 背部开关灯
#define bLedVibSW			_40_2          // 坐垫振动开关灯
#define bLedLegKneadSW  	_40_4          // 小腿揉捏开关灯
#define bLedBackLower		_41_1          // 背腰灯
#define bLedVibStrong		_41_3          // 振动强灯
#define bLedLegKneadFast 	_41_7          // 小腿揉捏快灯
#define bLedBackUpper		_42_1          // 颈肩灯
#define bLedVibMid			_42_3          // 振动力度中灯
#define bLedLegKneadMid		_42_4		   // 小腿揉捏中速灯
#define bLedBackFull		_43_1          // 上半身灯
#define bLedVibWeak			_43_3          // 振动弱灯
#define bLedLegKneadSlow	_43_4          // 小腿揉捏慢灯

unsigned char nCS @0x46;
#define bCS0        _46_0
#define bCS1        _46_1
#define bCS2        _46_2

bit bVibFlag;
bit bRun,bBackSW,bVibSW,bPowerSW,bLegBeatSW,bLegKneadSW,bAutoProcess; // 电源键、各开关键、自动键状态位
bit bBackMassageChange;                    // 背部按摩部位改变状态
bit bLocationReset;
bit bBackSwitchReset;
//-------------------------------------------------
unsigned char nRunTimeIndex,nBackLocateIndex,nVibStrengthIndex;  // 运行时间指示、背部位置指示、振动力度指示
unsigned char nVibStrength,nCurVibStrength;
unsigned char nKneadSpeedIndex;                                 // 小腿揉捏速度指示
unsigned char nLocateResetStep,nLocateResetTimer;
//---------- 敲打参量定义 ---------------------------------------
unsigned char nBeatActions ;
unsigned char nCurPulsesPerBeat ;
unsigned char nCurInsertPulses ;
unsigned char nCurTailPulses ;
unsigned char nCurBeatActionStep ;
unsigned char nCurRepeatTimes ;
unsigned char nCurDirectorStep ;
//--------------------------
unsigned char nBeatStrengthIndex,nBeatSpeedIndex;      // 敲打力度指示、敲打速度指示
unsigned char nBackStepChange;
unsigned char nCurBackStep;

/*Global Variables Related to Interrupt*/
unsigned char nScanCount ;
/*Global Variables Related to RunTime*/
bit bTimer500ms ;
// relate to key
unsigned long    nOldKeyCode,nKeyCode,nNewKeyCode;    // 双字节,用于合并两次键盘扫描值
unsigned long    nNewKeyCode1 ;
unsigned char    nKeyTemp,nNewKeyCode0;
unsigned char nTimeCounter,nScanCount,nKeyProcessStep;
unsigned char nKeyUpTime,nKeyDownTime;
unsigned char nTimeIndex;
unsigned char nRunTime;                  // 定时时间值
unsigned char nTime500ms;
unsigned char nVibMassageTime,nTime50ms;
unsigned long nRunTime1;

//time interrupt process at this please
#pragma vector Timer0 @ 0x08
void Timer0(void)
{
	_clrwdt();
	_tmrl = 0x48;    /*3ms for 4M/f448 for 3ms */
	_tmrh = 0xf4 ;
	nCS = _pb;
	bCS0 = 1; bCS1 = 1; bCS2 = 1;
	_pb = nCS;                                // CS7 = 0
	_pa = 0xff;
	nTimeCounter++;
	nRunTime1++;
	nTime500ms++;
	nTime50ms++;
	nCurVibStrength++;
	switch(nScanCount)
	{
		case 0:
				nScanCount = 1;                      // 准备下轮扫描
				_pac = 0xff;                         // 数据总线设为输入状态
				nCS = _pb;
				bCS0 = 0; bCS1 = 1; bCS2 = 1;
				_pb = nCS;                           // CS6 = 0
				_nop();
				_nop();
				_nop();
				_nop();
				_nop();
				_nop();
				_nop();
				_nop();
				_nop();
				_nop();
				_nop();
				_nop();
				_nop();
				_nop();
				_nop();
				nNewKeyCode0 = _pa;                  // 读keyboard 1(读数时,先片选,再读数)
				_pac = 0x00;                         // 数据总线设为输出状态
				_pa  = nLedValue0;                   // 送显示数据到LED1 (先送数到输出锁存器,再片选)
				nCS = _pb;
				bCS0 = 0; bCS1 = 0; bCS2 = 0;
				_pb = nCS;                           // CS0 = 0
				break ;                              // 同一次扫描中,可完成读键盘和送显示两个步骤。先读键盘,后亮灯,以保证灯显示时间
		case 1:
				_pac = 0xff;                         // CS7 =0
				_nop();
				_nop();
				_nop();
				nNewKeyCode1 = _pa;
				nNewKeyCode1<<=8;                    // keyboard2数据左移8位,构成长字节的高8位。
				nNewKeyCode = nNewKeyCode0|nNewKeyCode1; // keyboard1和keyboard2相与,构成16位数,可排除双键同时按下之读数。
				_pac = 0x00;
				_pa  = nLedValue1;
				nCS = _pb;
				bCS0 = 1; bCS1 = 0; bCS2 = 0;
				_pb = nCS;                           // CS1 = 0				
				nScanCount = 2;
				break ;
		case 2:
				_pac = 0x00;
				_pa  = nLedValue2;
				nCS = _pb;
				bCS0 = 0; bCS1 = 1; bCS2 = 0;
				_pb = nCS;                           // CS2 = 0
				nScanCount = 3 ;
				break;
		case 3:
				_pac = 0x00;
				_pa  = nLedValue3;
				nCS = _pb;
				bCS0 = 1; bCS1 = 1; bCS2 = 0;
				_pb = nCS;                           // CS3 = 0
				nScanCount = 4;
				break;
		case 4:
				_pac = 0x00;
				_pa  = nLedValue4;
				nCS = _pb;
				bCS0 = 0; bCS1 = 0; bCS2 = 1;
				_pb = nCS;                           // CS4 = 0
				nScanCount = 5;
				break;
		case 5:
				_pac = 0x00;
				_pa  = nLedValue5;
				nCS = _pb;
				bCS0 = 1; bCS1 = 0; bCS2 = 1;
				_pb = nCS;                           // CS5 = 0
				nScanCount = 0;
				break;
	}
}
void KeyProcess(void)                     // “键盘处理”函数
{
	
	
	if(nNewKeyCode == 0xffff)            // 按键去抖(在主函数中进行,而不在中断里进行)
	{
		nKeyDownTime = 0 ;
		if(nKeyProcessStep == 1)
		{
			nKeyUpTime++ ;
			if(nKeyUpTime >= MaxKeyUpTime)
			{
				nKeyUpTime = MaxKeyUpTime ;
				nKeyProcessStep = 2 ;
			}
		}
	}
	else                                 // 释放去抖?
	{
		nKeyUpTime = 0 ;
		if(nOldKeyCode == nNewKeyCode)
		{
			if(nKeyProcessStep == 0) 
			{
				nKeyDownTime++ ;
				if(nKeyDownTime >= MaxKeyDownTime)
				{
					nKeyDownTime = MaxKeyDownTime ;
					nKeyProcessStep = 1 ;
					nKeyCode = nNewKeyCode ;
				}
			}
		}
	}
	if(nKeyProcessStep == 2)
	{
		nKeyProcessStep = 0 ;
		nKeyDownTime = 0 ;
 		nKeyUpTime = 0;
		switch(nKeyCode)
		{
			case 0xfffe://time           时间键
						if(bRun == On)
						{
							nRunTimeIndex++;
							if(nRunTimeIndex>3)
							{
								nRunTimeIndex =1;
							}
							switch(nRunTimeIndex)
							{
								case 1:
										nRunTime = 10;
										nRunTime1 = 0;
										
										break;
								case 2:
										nRunTime = 20;
										nRunTime1 = 0;
										break;
								case 3:
										nRunTime = 30;
										nRunTime1 = 0;
							}
							nLedValue4 = Seg[nRunTime/10];       //时间数码显示
							nLedValue5 = Seg[nRunTime%10];
						}
						break;
			case 0xfffd://locate           “背部定位”键
						if(bBackSW == On)
						{
							nBackLocateIndex++;                  // 三个位置状态依次循环
							bLedAuto = LedOff;
							if(nBackLocateIndex>3)
							{
								nBackLocateIndex = 1;            // 不是从0开始,而是从1开始
							}
							bBackMassageChange = TRUE;           // 背部按摩部位发生改变
						}
						break;
			
			case 0xfff7:                                         //  “坐垫振动力度”键
						//vib strength
						if(bVibSW == On)
						{
							bLedAuto = LedOff;
							nVibStrengthIndex++;                 //  三个振动力度依次改变
							if(nVibStrengthIndex >3)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -