downtoearth 发表于 2012-11-25 17:10:20

想做一个鼠标呼吸灯,请教大家鼠标待机时的检测方法?

本帖最后由 downtoearth 于 2012-11-25 19:00 编辑


想做一个鼠标呼吸灯,请教大家鼠标待机时的检测方法?
就是鼠标待机,呼吸灯开始工作,鼠标工作呼吸灯就关闭。
用的是STC15F104E,我把一个IO设成高阻连到三极管基极,
但好像检测不到电平变化。鼠标待机是红外LED是快闪的,没有
示波器,程序就当它按键这样处理,程序在测试是连到高低电平,
是受控的,现在请教大家为什么检测不到电平变化,或者有什么更
好的办法!程序如下:********************************************************************/
#include <stdlib.h>
#include "..\My_Header_Files\MacroAndConst.H"
#include "..\STC_Header_Files\STC15.H"
/*******************************************************************/
#define ON1
#define OFF 0

#define RED    0
#define GREEN1
#define BLUE   2
#define YELLOW 3
#define PURPLE 4
#define CYAN   5
#define WHITE6
/*******************************************************************/
sbit Mouse_Standby = P3^4 ;
sbit Red_Led   = P3^3 ;
sbit Green_Led = P3^1 ;
sbit Blue_Led= P3^0 ;

bit Timer0_200UsFlag ;
bit Breathe_Flag;
uchar MouseWork_Time    ;
/*******************************************************************/
void Timer0_Init (void)
{
AUXR &= 0x7f ;
TMOD &= 0xf0 ;
TMOD |= 0x01 ;
TH0   = (65535-200) / 256 ;
TL0   = (65535-200) % 256 ;
ET0   = 1 ;
TR0   = 1 ;
EA    = 1 ;
}
/*******************************************************************/
void Timer0_Isr (void) interrupt 1 using 0
{
TH0   = (65535-200) / 256 ;
TL0   = (65535-200) % 256 ;
Timer0_200UsFlag = 1 ;       
}
/*******************************************************************/
void Breathe_Control (void)
{
static uchar All_PWM ;
static uchar PWM_Count ;
static uchar Time_Count ;
static uchar Led_Colour ;
static bit   Direction = 1 ;
if ( ++Time_Count == 100 )
    {
   Time_Count = 0 ;
   if ( Direction )
                            {
                                                               if ( ++All_PWM == 99 ) Direction = 0 ;
                                                                }
                     else   {
               if ( --All_PWM == 0 )
                                                                  {
                                                                               Direction= 1 ;
                     if ( MouseWork_Time >= 60 )
                                                                                                {
                                                                                               MouseWork_Time = 0 ;
                                                                                               Breathe_Flag   = 0 ;
                                                                                                }
                     Led_Colour = rand() % 7 ;
                  }
                }   
               PWM_Count= All_PWM ;                        
    }       
if ( PWM_Count > 0 )
         {
                                                switch ( Led_Colour )
                                                     {
                                                        case RED    : Red_Led   = ON ;   Green_Led = Blue_Led = OFF ; break ;
                                                        case GREEN: Green_Led = ON ;   Red_Led   = Blue_Led = OFF ; break ;
                                                                                case BLUE   : Blue_Led= ON ;   Green_Led = Red_Led= OFF ; break ;
                                                                                case YELLOW : Red_Led = Green_Led = ON ; Blue_Led   = OFF ; break ;
                                                                                case PURPLE : Red_Led = Blue_Led= ON ; Green_Led    = OFF ; break ;
                                                                                case CYAN   : Green_Led = Blue_Led= ON ; Red_Led      = OFF ; break ;
                                                                                case WHITE: Red_Led = Green_Led = Blue_Led = ON ;         break ;
                                                                                default : break ;
                   }
                PWM_Count-- ;
         }
                       else Red_Led = Green_Led = Blue_Led = OFF ;
}
/*******************************************************************/
void main (void)
{
uchar Time50Ms_Count ;
Timer0_Init();
P3 = 0x10 ;
P3M1 = 0x10 ;
P3M0 = 0x0b ;
srand(0xffff) ;
while (1)
      {
       if ( Timer0_200UsFlag )
                          {
         Timer0_200UsFlag = 0 ;
                                       if ( ++Time50Ms_Count == 250 )
                                          {
                                             Time50Ms_Count = 0 ;
                                                       if ( Mouse_Standby == 0 )
                                                                        {
                                                                       Breathe_Flag   = 1 ;
                                                                       MouseWork_Time = 0 ;
                                                                        }
                                                               else {
                                                                             if ( Breathe_Flag ) MouseWork_Time++ ;
                                                                      }
            }
                                       if ( Breathe_Flag ) Breathe_Control() ;
          }
      }
}
/*******************************************************************/为什贴出的代码会这样乱,无语!

lcw_swust 发表于 2012-11-25 21:15:54

本帖最后由 lcw_swust 于 2012-11-26 10:07 编辑

三极管基极到发射级是一个二极管,电压最高0.7V左右,单片机IO口要在2V左右才认为是高电平
最好是检测集电极(注意加上拉电阻)(不好意思,先前说成发射极了)

error_dan 发表于 2012-11-25 21:34:09

一个馊主意,如果是光电鼠标,待机的时候下面的发光二级管亮度会降低(目测是扫描频率降低了。。。。算么。。。(但是这样有个bug很多鼠标如果离开桌面下面的二极管亮度也会降低。。。

devcang 发表于 2012-11-25 22:57:35

检测接收光线的元件的电压——应该要加运放

downtoearth 发表于 2012-11-26 02:27:51

本帖最后由 downtoearth 于 2012-11-26 13:37 编辑

想到个办法不知可不可以,加个相同的NPN三极管再连单片机IO,大家比点意见。

lixingwei0122 发表于 2012-11-30 22:13:24

鼠标上跟本没必要加这灯。只听说过笔记本键盘上有这个玩意

tonyone 发表于 2012-11-30 23:39:10

mark                  

jssd 发表于 2012-12-1 00:02:39

为什么不直接接鼠标LED检测?根本不用什么代码

downtoearth 发表于 2012-12-1 02:20:41

本帖最后由 downtoearth 于 2012-12-1 02:26 编辑

jssd 发表于 2012-12-1 00:02 static/image/common/back.gif
为什么不直接接鼠标LED检测?根本不用什么代码

大哥你看电路,如果接到红外LED那,就只会检测高电平1,三极管导通和截止都是高电平!我都不知我说得对不对,模电差!
页: [1]
查看完整版本: 想做一个鼠标呼吸灯,请教大家鼠标待机时的检测方法?