PTC_ON
PTC_OFF 值怎么确定啊 ! 告诉指点!!!!!!
现在的值不对! 麻烦指点 谢谢!!!
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0011101111000100);
const uchar dofly_DuanMa[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};// 0xBF,---- 0-9
const uchar dofly_WeiMa[]={0x38,0x34,0x2C,0x1C};
//const uchar TempData[4];
void display(uint num);
void delay(uint x);
uint Index=0; //通道
//uint Cap_Value; //当前扫描按键值
uchar AvgIndex ; //频率计数
void touchkey(); //按键判断函数
#define PCT_ON 8
#define PCT_OFF 7
volatile unsigned int Cap_Avg[]={0,0,0,0,0,0}; //平均
volatile unsigned int Cap_Value[]={0,0,0,0,0,0}; //当前值区
uchar key0,key1,key2,key3,key4,key5;
//=======按键寄存器初始化======
//=======TMR0定时 TMR1计数====
void Cap_Init(void)
{
CPSCON0=0B10001100; // 使能CPS模块 电容通道
CPSCON1=0B00000101; //选择CPS0-5通道
OPTION_REG=0B11010011; // TMR0 FOSC/4时钟 分频1:16
TMR0=0x65;
TMR0IF=0;
TMR0IE=1; //使能
T1CON=0B11000100; //TMR1 电容触摸时钟通道 TMR1ON打开
T1GCON=0B00000000; //不用比较器
TMR1IF=0;
TMR1IE=0; //关
}
//=======按键判断函数==========
void cap_ISR() //百分比判断法
{
uint percent;
percent=(Cap_Avg[Index]-Cap_Value[Index]);
if(percent<0)
{
percent=0;
}
else
{
percent=percent*100;
percent=percent/Cap_Avg[Index];
}
if(percent<PCT_OFF) //无键按下
{
switch(Index)
{
case 0:key0=0; break;
case 1:key1=0; break;
case 2:key2=0; break;
case 3:key3=0; break;
case 4:key4=0; break;
case 5:key5=0; break;
default:break;
}
if(AvgIndex<2) AvgIndex++;
else AvgIndex=0;
if(AvgIndex==2)
Cap_Avg[Index]=Cap_Avg[Index]+(Cap_Value[Index]-Cap_Avg[Index])/16;
}
else if(percent>PCT_ON)
{
switch(Index)
{
case 0:key0=1; break;
case 1:key1=1; break;
case 2:key2=1; break;
case 3:key3=1; break;
case 4:key4=1; break;
case 5:key5=1; break;
default:break;
}
}
}
//=======计数通道===============
void SetNext(void)
{
if(Index==5) Index=0;
else Index++;
CPSCON1=Index; //通道
}
//========TMR1启动函数==========
void RestartTimers(void)
{
TMR1ON=0;
TMR1H=0;
TMR1L=0;
TMR1ON=1; //TRM1使能
}
//========中断函数==============
void interrupt System()
{
if(TMR0IE&&TMR0IF) //4MHz 2.5ms
{
TMR0IF=0;
TMR0=0x65; //初值100
TMR1ON=0; //TMR1关闭
Cap_Value[Index]=(TMR1H<<8)+TMR1L; //当前值 读取振荡频率TMR1值
cap_ISR(); //按键判断函数
SetNext(); //通道设置
RestartTimers(); //重启TMR1
CLRWDT(); //清数据
}
}
//================延时函数===================
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=10;b>0;b--);
}
//========显示函数================
void display(uint num)
{
uchar DAT,key;
uint i,k;
TRISA=0XFF; //清除重影
PORTC=0;
TRISC=1;
DAT=dofly_DuanMa[num];
for(i=8;i>0;i--)
{
key=DAT&0x01;
RC7=0;
if(!key)
RC6=1;
else RC6=0;
RC7=1;
DAT=DAT>>1;
}
PORTA=dofly_WeiMa[num];// 00000010 RA5到RA2
TRISA=1;
delay(100);
}
//=============主函数===============
void main()
{
//uint j=0;
//System_Init(); //系统初始化
//CLRWDT(); //WDT 门狗
Cap_Init(); //中断 初始化
GIE = 1; //总中断允许
PEIE = 1; //外设中断允许
//display(0xBF);
while(1)
{
if(key0==1)
display(0);
if(key1==1)
display(1);
// if(key2==1)
// display(2);
// if(key3==1)
// display(3);
// if(key5==1)
// display(2);
}
} |