查看之前的申请帖,发现时间显示功能未完成,因此本帖在上一贴的基础上进行改进完善
首先介绍一下触控台灯的功能:
可实现对LED灯的开关、调光控制,显示对当前按键的操作;
实时显示温湿度值,显示当前时间,并可对时间进行调整。
具体触摸按键功能如下:
板载K1、K2控制灯的开、关;K3、K4实现调光及调整时间;
LED开的情况下,按K3调亮LED,按K4键调暗LED;
LED关的情况下,按K3调整“时”(按一次,时加1);按K4键调整“分”(按一次,分加1)
单片机资源使用情况:
P10温湿度传感器接口;P30LED驱动板(输出PWM信号调光)
P12、P32-P35外节LCD液晶模块;P14-P17板载触摸按键
调光控制利用单片机PWM输出功能,复用到P3口;
时间使用看门狗定时器产生秒时基,软件实现时间显示功能(时间会有一定的误差),
代码部分
- #include "..\Public\CH554.H"
- #include "..\Public\Debug.H"
- #include "TouchKey.H"
- #include "PWM.H"
- #include "stdio.h"
- #include "GPIO.H"
- #include <string.h>
- #include <intrins.h>
- #include "nokia_5110.h"
- #include "bmp_pixel.h"
- #pragma NOAREGS
- sbit P1_0= P1^0; //DHT11数据口
- UINT8 U8FLAG,k;
- UINT8 U8count,U8temp;
- UINT8 U8T_data_H=0,U8T_data_L,U8RH_data_H=0,U8RH_data_L,U8checkdata;
- UINT8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
- UINT8 U8comdata;
- UINT8 outdata[4];
- //sbit LED0 = P1^6;
- //sbit LED1 = P1^7;
- void init_PWM(void);
- //void init_GPIO(void);
- void init_TK(void);
- void get_TK(void);
- void check_PWM(void);
- UINT8 x,y,t=0,h=22,m=52;
- bit led;
- void init_WDT(void)
- {
- CH554WDTModeSelect(0);
- IE_WDOG=0;
- CH554WDTFeed(0x48);
- }
- void get_time()
- {
- if(t>59)
- {
- t=0;
- m++;
- }
- if(m>59)
- {
- m=0;
- h++;
- }
- if(h>23)
- {
- h=0;
- }
- }
- void COM(void) //?????????
- {
- UINT8 i;
- for(i=0;i<8;i++)
- {
- U8FLAG=2;
- while((!P1_0)&&U8FLAG++);//??254? ???????? 50us???????
- mDelayuS(40);
- U8temp=0; //??????0??1 ?????????0????????? 1
- if(P1_0)U8temp=1;
- U8FLAG=2;
- while((P1_0)&&U8FLAG++); //??0/1???????
- //?????for??
- // if(U8FLAG==1)break; //??255us ???????
- U8comdata<<=1;
- U8comdata|=U8temp;
- }
- }
- void RH(void)
- {
- //????18ms
- P1_0=0;
- mDelaymS(20);
- P1_0=1;
- //????????? ????20us
- mDelayuS(30);
-
- //?????? ????????
- P1_0=1;
- //?????????????? ?????????,???????
- if(!P1_0) //p20==0????
- {
- U8FLAG=2;
- //?????? 80us ????????????
- while((!P1_0)&&U8FLAG++); //??????????254us
- U8FLAG=2;
- //?????? 80us ????,????????????
- while((P1_0)&&U8FLAG++);
- //??????
- COM();
- U8RH_data_H_temp=U8comdata;
- COM();
- U8RH_data_L_temp=U8comdata;
- COM();
- U8T_data_H_temp=U8comdata;
- COM();
- U8T_data_L_temp=U8comdata;
- COM();
- U8checkdata_temp=U8comdata;
- P1_0=1;
- //????
- U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
- if(U8temp==U8checkdata_temp)
- {
- U8RH_data_H=U8RH_data_H_temp;
- U8RH_data_L=U8RH_data_L_temp; //????0
- U8T_data_H=U8T_data_H_temp;
- U8T_data_L=U8T_data_L_temp; //????0
- U8checkdata=U8checkdata_temp;
- }
- }
- }
- void convert(UINT8 a,UINT8 b)
- {
- UINT8 i,j;
- i=a%10;
- j=a/10;
- outdata[0]=i+0x30;
- outdata[1]=j+0x30;
- i=b%10;
- j=b/10;
- outdata[2]=i+0x30;
- outdata[3]=j+0x30;
- }
- void initio( )
- {
- // Port1Cfg(3,0); //p1.0
- Port1Cfg(3,2); //p1.2
- // Port3Cfg(3,1); //p1.1
- // Port3Cfg(3,1); //p3.2
- Port3Cfg(3,2); //p3.3
- Port3Cfg(3,3); //p3.4
- Port3Cfg(3,4); //p3.5
- Port3Cfg(3,5); //p3.6
- // Port3Cfg(3,6); //p3.6
- // Port3Cfg(3,7); //p3.6
- }
- void LCD_dis()
- {
- convert(U8RH_data_H,U8T_data_H); //转换成字符
- // LCD_write_english_string(0,0," Hello World! ");
- // mDelaymS(500);
- // LCD_write_english_string(0,0," Nokia5110 LCD");
- // mDelaymS(500);
- // LCD_write_english_string(0,3,"DHT11 Testing");
- // mDelaymS(500);
- LCD_write_chinese_string(12,0,12,4,10,5);
- LCD_write_chinese_string(12,2,12,4,4,5);
-
- LCD_write_english_string(0,4,"T:");
- LCD_write_char(outdata[3]); //温度高位
- LCD_write_char(outdata[2]); //温度低位
- LCD_write_char(0x7c);
- LCD_write_char('C'); LCD_write_char(' ');
- LCD_write_char('H');LCD_write_char(':');
- LCD_write_char(outdata[1]); //湿度高位
- LCD_write_char(outdata[0]); //湿度地位
- LCD_write_char('%');
- LCD_write_english_string(0,5,"Time ");
- LCD_write_char(h/10+48);
- LCD_write_char(h%10+48);
- LCD_write_char(':');
- LCD_write_char(m/10+48);
- LCD_write_char(m%10+48);
- LCD_write_char(':');
- LCD_write_char(t/10+48);
- LCD_write_char(t%10+48);
- // mDelaymS(500);
- // LCD_write_chinese_string(12,4,12,4,0,5);
-
- // LCD_clear();
- }
- main( )
- {
- /*
-
- CfgFsys( ); //CH554时钟选择配置
- mDelaymS(5); //配置时钟后,建议延时稳定时钟
- mInitSTDIO( );
- init_PWM();
- init_TK(); //串口0初始化
- printf("start ...\n");
- while(1)
- {
- get_TK();
- mDelaymS(100);
- }
- */
- UINT16 j = 0;
- CfgFsys( ); //CH554时钟选择配置
- mDelaymS(20);
- mInitSTDIO( );
- y=0x2f;
- init_PWM();
- init_TK();
- initio( );
- LCD_init(); //?????
- LCD_clear();
- mDelaymS(50);
- LCD_write_chinese_string(12,0,12,4,10,5);
- LCD_write_chinese_string(12,4,12,4,0,5);
- mDelaymS(1000);
- RH();
- LCD_clear();
- mDelaymS(50);
- // LCD_write_english_string(0,0," LED -- OFF ");
- init_WDT();
- while(1)
- {
- if(WDOG_COUNT>=0xb6)
- {
- t++;
- WDOG_COUNT=0x00;
- }
- get_time();
- get_TK();
- if(t%3==0)
- RH(); //测量温湿度
- LCD_dis();
-
-
-
- }
- }
-
- void init_PWM(void)
- {
- PWM1PINAlter( );
- // P3_MOD_OC &= ~(bPWM1_| bPWM2_); //设置PWM引脚为推挽输出
- // P3_DIR_PU |= bPWM1_ | bPWM2_;
- Port3Cfg(1,0); //p3.0
- // Port3Cfg(1,1); //p3.1
-
- SetPWMClk(4); //PWM时钟配置 ,4分频
- ForceClearPWMFIFO( ); //强制清除PWM FIFO和COUNT
- CancleClearPWMFIFO( ); //取消清除PWM FIFO和COUNT
- PWM1OutEnable( ); //允许PWM1输出
- // PWM2OutEnable( ); //允许PWM2输出
- PWM1OutPolarHighAct( ); //PWM1输出默认低,高有效
- // PWM2OutPolarLowAct( ); //PWM2输出默认高,低有效
- SetPWM1Dat(0x00); //占空比0x10/256
- // SetPWM2Dat(0xf0);
- }
- /*
- void init_GPIO( void )
- {
- Port1Cfg(1,6); //P16设置推挽模式
- Port1Cfg(1,7); //P17设置推挽模式
- LED0 = 0;
- LED1 = 0;
- // GPIOInterruptCfg(); //GPIO中断配置函数
- // EA = 1;
- }
- */
- void init_TK(void)
- {
- TK_Init( BIT4+BIT5+BIT6+BIT7, 1, 1 ); /* Choose TIN2, TIN3, TIN4, TIN5 for touchkey input. enable interrupt. */
- TK_SelectChannel(0); /* NOTICE: ch is not compatible with the IO actually. */
- EA = 1;
- }
- void get_TK(void)
- {
- if( Touch_IN != 0 )
- {
- if( Touch_IN & CH2 )
- {
- // printf("CH2 is pressed.\n");
- // printf("LED ON.\n");
- // LED0=1 ;
- // LED1=1 ;
- led=0;
- x=0;
- SetPWM1Dat(x);
- LCD_write_english_string(0,0," LED -- OFF ");
- LCD_write_english_string(0,1," ");
- mDelaymS(500);
- LCD_write_english_string(0,0," ");
- }
- if( Touch_IN & CH3 )
- {
- // printf("CH3 is pressed.\n");
- // printf("LED OFF.\n");
- // LED0=0 ;
- // LED1=0 ;
- led=1;
- x=y;
- SetPWM1Dat(x);
- LCD_write_english_string(0,0," LED -- ON ");
- LCD_write_english_string(0,1," ");
- mDelaymS(500);
- LCD_write_english_string(0,0," ");
- }
- if( Touch_IN & CH4 )
- {
- // printf("CH3 is pressed.\n");
- // printf("LED OFF.\n");
- // LED0=0 ;
- // LED1=0 ;
- if(led==1)
- {
-
- if(x<255&led==1)
- {
- x=x+3;
- SetPWM1Dat(x);
- LCD_write_english_string(0,0," LED -- ON ");
- LCD_write_english_string(0,1," Light: ++++ ");
- //LCD_write_char('*');
- mDelaymS(50);
- LCD_write_english_string(0,1," ");
- }
- else
- {
- LCD_write_english_string(0,0," LED -- ON ");
- LCD_write_english_string(0,1," Light: MAX ");
-
- }
- y=x;
- LCD_write_english_string(0,0," ");
- }
- else
- {
- h++;
- mDelaymS(200);
- }
- }
- if( Touch_IN & CH5 )
- {
- // printf("CH3 is pressed.\n");
- // printf("LED OFF.\n");
- // LED0=0 ;
- // LED1=0 ;
- if(led==1)
- {
- if(x>1&led==1)
- {
- x=x-3;
- SetPWM1Dat(x);
- LCD_write_english_string(0,0," LED -- ON ");
- LCD_write_english_string(0,1," Light: ---- ");
- mDelaymS(50);
- LCD_write_english_string(0,1," ");
- }
- else
- {
- LCD_write_english_string(0,0," LED -- ON ");
- LCD_write_english_string(0,1," Light: MIN ");
-
- }
- y=x;
- LCD_write_english_string(0,0," ");
- }
- else
- {
- m++;
- mDelaymS(200);
- }
- }
-
- Touch_IN = 0;
- }
- // LCD_dis();
- }
- void check_PWM(void)
- {
- UINT8 x=0x00;
- if(PWM_CTRL&bPWM_IF_END)
- {
- PWM_CTRL |= bPWM_IF_END; //清除PWM中断
-
- for(x=0;x<255;x++)
- {
- SetPWM1Dat(x);
- mDelaymS(10);
- }
- for(x=255;x>0;x--)
- {
- SetPWM1Dat(x);
- mDelaymS(10);
- }
-
- /*
- #ifdef DE_PRINTF
- printf("PWM_CYC_END %02X\n",(UINT16)PWM_CTRL);
- #endif */
-
- }
- }
DIY部分完成。
此内容由EEWORLD论坛网友zhang7309原创,如需转载或用于商业用途需征得作者同意并注明出处
视频效果地址:
http://v.youku.com/v_show/id_XMz ... j.8428770.3416059.1