|
【新版CH554评测-DIY】4.带LCD触摸LED灯
[复制链接]
前段时间有点忙评测耽搁了,本次内容实现带LCD液晶显示的触摸调光灯
在上一贴的基础上,加上液晶显示(使用Nokia5110 LCD点阵液晶模块-无字库)
使用字符显示;
开关效果
调光效果
部分代码
- #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;
- bit led;
- 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,2,"DHT11 Testing");
- // mDelaymS(500);
-
- LCD_write_english_string(0,3," Shi du: ");
- LCD_write_char(outdata[1]); //湿度高位
- LCD_write_char(outdata[0]); //湿度地位
- LCD_write_char('%');
- LCD_write_english_string(0,4," Wen du: ");
- LCD_write_char(outdata[3]); //温度高位
- LCD_write_char(outdata[2]); //温度低位
- LCD_write_char(0x27);
- LCD_write_char('C');
- // 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( );
-
- init_PWM();
- init_TK();
- initio( );
- LCD_init(); //?????
- LCD_clear();
- mDelaymS(50);
- LCD_write_english_string(0,0," LED -- OFF ");
- while(1)
- {
- get_TK();
- // RH(); //测量温湿度
- // LCD_dis();
- // mDelaymS(500);
- mDelaymS(50);
-
-
- }
- }
-
- 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 ");
- }
- if( Touch_IN & CH3 )
- {
- // printf("CH3 is pressed.\n");
- // printf("LED OFF.\n");
- // LED0=0 ;
- // LED1=0 ;
- led=1;
- x=0x2f;
- SetPWM1Dat(x);
- LCD_write_english_string(0,0," LED -- ON ");
- }
- if( Touch_IN & CH4 )
- {
- // printf("CH3 is pressed.\n");
- // printf("LED OFF.\n");
- // LED0=0 ;
- // LED1=0 ;
- if(x<255&led==1)
- {
- x=x+3;
- SetPWM1Dat(x);
- LCD_write_english_string(0,2," Light: ++++ ");
- //LCD_write_char('*');
- mDelaymS(50);
- LCD_write_english_string(0,2," ");
- }
- else LCD_write_english_string(0,2," Light: MAX ");
-
- }
- if( Touch_IN & CH5 )
- {
- // printf("CH3 is pressed.\n");
- // printf("LED OFF.\n");
- // LED0=0 ;
- // LED1=0 ;
- if(x>1&led==1)
- {
- x=x-3;
- SetPWM1Dat(x);
- LCD_write_english_string(0,2," Light: ---- ");
- mDelaymS(50);
- LCD_write_english_string(0,2," ");
- }
- else
- LCD_write_english_string(0,2," Light: MIN ");
- }
-
- 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 */
-
- }
- }
复制代码
后续将实现LCD汉字显示,外加传感器,实现温湿度显示等功能。
视频效果地址: http://v.youku.com/v_show/id_XMz ... j.8428770.3416059.1
|
|