作为单面机的小菜鸟,只会些简单的编程,需要大神的帮助。
该智能环境灯主要实现的功能:
1.能够检测环境光照强度,并显示;
2.能够实现根据当前环境来自动调节所需要的光照强度;
3.能够实现缓慢将灯变亮和熄灭;
该程序最后的结果只有LCD显示但不随光照强度的改变而改变,我已经完全懵了,求帮助!
- #include <reg52.h>
- /*---------------宏定义-----------------*/
- #define uint unsigned int
- #define uchar unsigned char
- #define PWM_CYC 20 //PWM周期
- uchar bright_lev = 0; //0最大亮度 20最小亮度
- #define LIGHT_SPEED 1000
- #define delay(x) {uint i,j;for(i = x;i>0;i--)for(j=50;j>0;j--);}
- #define THC0 ((65536 - 10000)/256) //10ms定时器定时值
- #define TLC0 ((65536 - 10000)%256)
- /*--------------引脚定义-----------------*/
- /* AD0809引脚 */
- sbit ST=P3^7;
- sbit EOC=P3^6;
- sbit OE=P2^4;
- sbit CLK=P2^3;
- sbit ALE = P3^7;
- #define ADCV P1
- /* LED 引脚 */
- sbit RS=P2^0;
- sbit RW=P2^1;
- sbit E=P2^2;
- #define LEDData P0
- /* 电机PWM脚 */
- sbit PWM = P2^6;
- /*---------------全局变量----------------*/
- uchar num[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; // ROM字符库
- uchar tab[]=" LUX "; //要显示什么就在这儿改
- uchar environment_bright;
- void PWM_Drive(void);
- uchar Get_Vol(void);
- void Display(void);
- void light_control(void);
- void writecmd(uchar com);
- void writedata(uchar dat);
- void init(void);
- void Display_Result(uchar d);
- /*---------------------------------
- * 函数:SysTemInit 初使化
- *----------------------------------*/
- void SysTemInit(void)
- {
- TMOD = 0x02; //定时器0 采用16位定时器
- TH0 = THC0; //设定定时值
- TL0 = TLC0;
- ET0 = 1; //定时器T0 溢出中断允许
- TR0 = 1; //启动定时器0
- EA = 1; //中断总开关开
- }
- /*---------------------------------
- * 函数:main 程序主入口
- *----------------------------------*/
- void main(void)
- {
- PWM = 0;
- SysTemInit();
- for(;;)
- {
- PWM_Drive(); //PWM驱动
- environment_bright = Get_Vol(); //获取环境亮度
- light_control();
- Display_Result(environment_bright); //显示
- }
- }
- /*---------------------------------
- * 函数:light_control 灯亮度控制
- *----------------------------------*/
- void light_control(void)
- {
- static uint light_speed = 0;
- if(environment_bright<127 && bright_lev > 0)
- {
- light_speed++;
- if(light_speed > LIGHT_SPEED)
- {
- bright_lev--;
- light_speed = 0;
- }
- }
- else if(environment_bright>127 && bright_lev < PWM_CYC)
- {
- light_speed++;
- if(light_speed > LIGHT_SPEED)
- {
- bright_lev++;
- light_speed = 0;
- }
- }
- }
- /*---------------------------------
- * 函数:time0 10ms定时器0中断入口 用于ADC时钟
- *----------------------------------*/
- void time0() interrupt 1
- {
- CLK = ~CLK;
- }
- /*---------------------------------
- * 函数:PWM_Drive 电机驱动
- * 参数:cmd 指令
- *----------------------------------*/
- void PWM_Drive(void)
- {
- static uchar cnt = 0;
- cnt++;
- if(cnt < bright_lev) PWM = 1;
- else PWM = 0;
- if(cnt > PWM_CYC) cnt = 0;
- }
- /*---------------------------------
- * 函数:Get_Vol 获取环境亮度
- * 返回:滑动变阻器电压值 0-255 对应0-5V
- *----------------------------------*/
- uchar Get_Vol(void)
- {
- uchar vol = 0;
- ST = OE = 0;
- ALE = 1;
- ST = 1;
- ALE = 0;
- ST = 0;
- while(EOC == 0);
- OE = 1;
- vol = ADCV;
- return vol;
- }
- /*---------------------------------
- * 函数:Display LED显示
- *----------------------------------*/
- void writecmd(uchar com) //写指令
- {
- E=0;
- RS=0;
- RW=0;
- P0=com;
- delay(2);
- E=1;
- delay(2);
- E=0;
- }
- void writedata(uchar dat)//写数据
- {
- E=0;
- RS=1;
- RW=0;
- P0=dat;
- delay(2);
- E=1;
- delay(2);
- E=0;
- }
- void init(void) //初始化
- {
- delay(15);
- writecmd(0x38);
- delay(5);
- writecmd(0x38);
- delay(5);
- writecmd(0x38);
- writecmd(0x38);
- writecmd(0x08);
- writecmd(0x01);
- writecmd(0x06);
- writecmd(0x0c);
- }
- void Display_Result(uchar n)
- {
- uchar j,d;
- init();
- d=(((n*4096)/1000.0)/0.000317969924);
- tab[5]=num[d%10];
- tab[4]=num[d/10%10];
- tab[3]=num[d/100%10];
- tab[2]=num[d/1000%10];
- tab[1]=num[d/10000%10];
- tab[0]=num[d/100000%10];
- writecmd(0x80); //液晶在80H+00地方显示 (设置地址指针)
- for(j=0;j<16;j++) //有16个字符,要循环16次
- {
- writedata(tab[j]);
- delay(2);
- }
- while(1); //让液晶显示就一直停在这里。
- }
复制代码
|