原创 合泰单片机做得礼物

2009-4-12 09:02 1889 5 5 分类: MCU/ 嵌入式
合泰单片机做情人节礼物

 


原理图很简单,就是一个合泰单片机(因为便宜嘛),加上8个LED发光二极管排成一排、一个滚珠开关(测速用)。做了一个手摇棒,你摇动LED棒时候,会显示“我爱你”等。不摇动时候,就是一排亮的LED灯,一摇动显示的字就出来了。主要在于测速问题。手摇动的速度是不均的。计算好速度。


也可以改为手摇时钟。


/*


MCU:HT48f50e
frequency:1MHZ
cycle:4us
one instruction cycle:4us
time0 count cycle:1/(1MHZ/分频比)=128us
time1 count cycle:4us
dispart frequence scale:128
*/


#include <ht48r50a-1.h>
#define freq 128
#define _t0on _0e_4
#define _t1on _11_4
#pragma vector exti @ 0x4
#pragma vector time0 @ 0x8
#pragma vector time1 @ 0xc
const unsigned char dispcode[20]={0xff,0xff,0x7e,0x0,0x7e,0xff,0xff,0xe1,0xde,0xbd,
                                  0xde,0xe1,0xff,0xff,0xc0,0xbf,0x7f,0xbf,0xc0,0xff};
#pragma rambank0
bit flag;
unsigned char intcount,n,m;      //interrupt times count
unsigned int tmr1h_temp,tmr1l_temp,overcnt,tmr1_temp1,tmr1_temp2,overrec;


void safe_init()
 {
  _intc=0;
  _tmr0=0;
  _tmr0c=0;
  _tmr1h=0;
  _tmr1l=0;
  _tmr1c=0;
  _pac=0xff;
  _pbc=0xff;
  _pcc=0xff;
  _pgc=0xff;
 }
void initial()   //tmr1h_temp,tmr1l_temp,overcnt,tmr1_temp1,tmr1_temp2,overrec;
 {
  overcnt="0";
  n="0";
  m="20";
  intcount="0";     //interrupt counter clear zero
  overcnt="0";
  _intc=0x0f;     //disable timer1 interrupt
  _tmr0c=0x8e;    //dispart frequence scale:1:128,timer mode,close timer
  _tmr1c=0x88;    //timer mode,close time1
  _tmr1l=0x18;    //timer1 is a 16bit timer,timer1 initialization:65536-65000=536
  _tmr1h=0x02;    //overflow time:65000*4=260ms
  _pac=0;           //pa pin is a output port
  _pa=0;
  _pgc=0x01;        //pg0 pin is a iuput port
 }


void main()
 {
 
  safe_init();
  initial();
  while(1)
   {
    if(flag==1)
    {
    
     tmr1h_temp=(unsigned long)_tmr1h;
     tmr1l_temp=(unsigned long)_tmr1l;
     tmr1_temp1=(tmr1h_temp<<8)|tmr1l_temp;
     tmr1_temp1=(tmr1_temp1-536)/250;          //tmr1_temp1*4/1000=ms
     _tmr1l=0x18;  
     _tmr1h=0x02;
     _t1on=1;
     tmr1_temp2=(tmr1_temp1+overrec)/40;
     _tmr0=(256-(tmr1_temp2*1000/freq));
     _t0on=1;
    }
   }
 }
 


/*extern interrupt sub program*/
void exti()
 {
  intcount++;
  _t1on=1;
  if(intcount==2)
   {
    _t0on=0;
    overrec="overcnt";
    overcnt="0";
    intcount="1";
    _t1on=0;
    flag="1";  
   }
 }
/*time0 interrupt sub program*/
void time0()
 {
   n++;
   if(n<20)
    { _pa=dispcode[n];
     
     }
   else
     {
       m--;
       _pa=dispcode[m];
       if(m<1)
        {
          _t0on=0;
          n="0";
          m="20";
        }
     } 
  
 }
/*tim1 interrupt sub program*/
void time1()
 {
  overcnt+=260;
 }

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
5
关闭 站长推荐上一条 /3 下一条