- #include <stc12c5a60s2.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- #define MAX_Delay 8
- #define MIN_Delay 1
- sbit S_1 = P1 ^ 1;
- sbit S_2 = P1 ^ 2;
- unsigned int pwm;
- unsigned char TimeDelay = 10;
- unsigned char Mode = 1;
- unsigned char code tabP2[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F}; //扫描
- uchar s=0;
- void DELAY_MS (uchar i){
- // unsigned int i;
- // while( --a != 0){
- // for(i = 0; i < 600; i++);
- // }
- uchar x,j;
- for(j=0;j<i;j++)
- for(x=0;x<=148;x++);
- }
- void Adc_Delay (uint a){
- uint i;
- while( --a != 0){
- for(i = 0; i < 600; i++);
- }
- }
- void Delay(uint z)
- {
- uint x,y;
- for(x=80;x>0;x--)
- for(y=z;y>0;y--);
- }
- void timer1_init()
- {
- TMOD |= 0x10;
- TH1 = 0;
- TL1 = 0;
- ET1 = 1;
- TR1 = 1;
- EA = 1;
- }
- /*******************************************************************************
- ********************** AD转换函数 **************/
- /******************************************************************************/
- unsigned char Read (unsigned char CHA)
- {
- unsigned char AD_FIN=0; //存储A/D转换标志
- CHA &= 0x07; //选择ADC的8个接口中的一个(0000 0111 清0高5位)
- ADC_CONTR = 0x00; //ADC转换的速度(0XX0 0000 其中XX控制速度,请根据数据手册设置)
- _nop_();
- ADC_CONTR |= CHA; //选择A/D当前通道
- _nop_();
- ADC_CONTR |= 0x80; //启动A/D电源
- Adc_Delay(1); //使输入电压达到稳定(1ms即可)
- ADC_CONTR |= 0x08; //启动A/D转换(0000 1000 令ADCS = 1)
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- while (AD_FIN ==0)
- { //等待A/D转换结束
- AD_FIN = (ADC_CONTR & 0x10); //0001 0000测试A/D转换结束否
- }
- ADC_CONTR &= 0xE7; //1111 0111 清ADC_FLAG位, 关闭A/D转换,
- return (ADC_RES); //返回A/D转换结果(8位)
- }
- /*******************************************************************************
- ************* AD电容式触摸按键检测程序 ******************/
- /*********************************************************************************************/
- //bit 1表示有感应物,0表示没有感应物 S1
- /**********************************************************************************************/
- bit S1(void)
- {
- unsigned char R,mm=0,i,j,k;
- bit aa;
- j=0;k=0;
- for(i=0;i<10;i++)
- { //一次采集数据的数量
- Adc_Delay(3);
- R=Read(1); //读对应的口,触发对应的触摸按键
-
- if(R<0x2f)
- { //此参数可调整感应灵敏度值在0x01到0x2F
- k++;
- }
- if(R == 0xFF)
- {
- j++;
- }
- }
- if(k>1 && j>1)
- {
- mm++;
- }
- else
- {
- mm=0;
- }
- if(mm>0)
- {
- aa =1;
- }
- else
- {
- aa=0;
- }
- return(aa);
- }
- /*********************************************************************************************/
- // S2
- /**********************************************************************************************/
- bit S2(void)
- {
- unsigned char R,mm=0,i,j,k;
- bit aa;
- j=0;k=0;
- for(i=0;i<10;i++)
- { //一次采集数据的数量
- Adc_Delay(3);
- R=Read(2);
- if(R<0x2F)
- { //此参数可调整感应灵敏度值在0x01到0x2F
- k++;
- }
- if(R == 0xFF)
- {
- j++;
- }
- }
- if(k>1 && j>1)
- {
- mm++;
- }
- else
- {
- mm=0;
- }
-
- if(mm>0)
- {
- aa =1;
- }
- else
- {
- aa=0;
- }
- return(aa);
-
- }
- //结束呼吸灯专用
- uchar code table[]={
- 0,0,1,2,3,4,5,6,7,8,9,10,
- 11,12,13,14,15,16,17,18,
- 19,20,21,22,23,24,25,26,27,
- 28,29,30,31,32,33,34,35,36,
- 37,38,39,40,41,42,43,44,45,
- 46,47,48,49,50,51,52,53,54,
- 55,56,57,58,59,60,61,62,63,
- 64,65,66,67,68,69,70,71,72,
- 73,74,75,76,77,78,79,80,81,82,
- 83,84,85,86,87,88,89,90,91,
- 92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,
- 110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,
- };
- void PwmUp(uint a, uchar c)
- {
- uchar i;
- if(c == 1)
- P3 = 0X00;
- else
- P0 = 0X00;
- P2 = 0X00;
- i = table[a];
- Delay(i);
- if(c == 1)
- P3 = 0XFF;
- else
- P0 = 0XFF;
- P2 = 0X00;
- Delay(150-i);
- }
- void PwmDown(uint a,uchar c)
- {
- uchar i;
- if(c == 1)
- P3 = 0XFF;
- else
- P0 = 0XFF;
- P2 = 0X00;
- i = table[a];
- Delay(i);
- if(c == 1)
- P3 = 0X00;
- else
- P0 = 0X00;
- P2 = 0X00;
- Delay(150-i);
- }
- void PwmAllon(uchar c)
- {
- int i;
- for(i = 0; i < 150 ;i++)
- {
- PwmDown(i,c);
- }
- if(c == 1)
- P3 = 0X00;
- else
- P0 = 0X00;
- P2 = 0X00;
- }
- void PwmOff(uchar c)
- {
- int i;
- // P3 = 0XFF;
- // P2 = 0X00;
- // Delay(100);
- for(i = 0; i < 150; i++)
- {
- PwmUp(i,c);
- }
-
- //P3 = 0X00;
- }
- void Allfalloff(uchar c,uchar time) //由全亮到下落关闭只剩一排
- {
- unsigned char code tabP0[5][8]={
- {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
- {0x00,0xFF,0xFF,0xFF,0x00,0xFF,0xFF,0xFF},
- {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
- {0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF},
- {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
- };
- int j,k,i;
- if(c==1)
- P3 = 0X00;
- else if(c==2)
- P0 = 0X00;
- else
- {
- P3 = 0XFF;
- P0 = 0XFF;
- }
- for(j=0;j<5;j++)
- {
- for(k=0;k<15;k++)
- {
- for(i=0;i<8;i++)
- {
- // P0=0;
- P2=tabP2[i];
- if(c == 1)
- P0=tabP0[j][i];
- else if(c == 2)
- P3 = tabP0[j][i];
- else
- P0=tabP0[j][i];
- DELAY_MS(time);
- }
- }
- }
- }
复制代码 |