原创 用三星单片机S3F9454做的一个遥控开关的源代码

2008-12-4 22:06 3605 15 15 分类: MCU/ 嵌入式

这是来自21IC的一片文章,我没有用过,先留下来,将来研究一下。


引用的网址为:http://bbs.21ic.com/club/bbs/list.asp?boardid=11&page=3&t=3167499&tp=%u7528%u4E09%u661F%u5355%u7247%u673AS3F9454%u505A%u7684%u4E00%u4E2A%u9065%u63A7%u5F00%u5173%u7684%u6E90%u4EE3%u7801


#include <ioS3C9454.h>

#define uchar unsigned char
#define uint  unsigned int

#define NOP asm("NOP")
#define DI  asm("DI")
#define EI  asm("EI")

#define SET_LED (P0 |= 0x01)
#define CLR_LED (P0 &= ~0x01)
#define COM_LED (P0 ^= 0x01)
#define SET_SCR (P0 |= 0x04)
#define CLR_SCR (P0 &= ~0x04)
#define COM_SCR (P0 ^= 0x04)
#define GET_SCR (P0 & 0x04)
#define GET_IR  (P0 & 0x02)
#define SET_BUZZER (P2 |= 0x01)
#define COM_BUZZER (P2 ^= 0x01)

#define IR_USER_CODE 0x00
#define IR_POWER     0x7C
#define IR_Vol_Add   0x16
#define IR_Vol_Dec   0x02
#define IR_RIGHT     0x0F
#define IR_LEFT      0x0E
#define IR_UP        0x5B
#define IR_DOWN      0x53

__root const uchar smartOption[4] @ 0x003C ={0x00,0x00,0xE4,0x00};

uchar remBuffer[4];
uchar irFlag;
uint timeCount;

void InitSfr(void)
{
BTCON = 0x02;
CLKCON = 0x18;
P0CONH = 0xAA;
P0CONL = 0xA6;
P0PND = 0x00;
P1CON = 0x0A;
P2CONH = 0x4A;
P2CONL = 0xAA;
P0 = 0x00;
P2 = 0x00;
SET_SCR;
SET_LED;
SET_BUZZER;
irFlag = 0;
timeCount = 0;
}

void DelayMs(uchar ms)
{
    uchar i,j;
    for(i = 0; i < ms; i++)
    {
        for(j = 0; j < 250; j++)
        NOP;
    }
}

void SendDat(uchar byte)
{
  uchar i;
  for(i = 0; i < 8; i++)
  {
    CLR_LED;
    if(byte & 0x80)
    DelayMs(1);
    else
    DelayMs(3);
    SET_LED;
    byte <<= 1;
    DelayMs(1);
  }
}

void BuzzerOut(uint time)
{
  uint j;
  uchar i;
  for(j = 0; j < time; j++)
  {
    BTCON = 0x02;
    for(i = 0; i < 16; i++)
    {
      NOP;
    }
    COM_BUZZER;
  }
}

void main()
{
  DI;
  InitSfr();
  DelayMs(100);
  T0DATA = 250;//定时器初始值
  T0CON = 0xC8;//打开定时器 1/8之一分频
  P0PND = 0x08;
  BuzzerOut(3000);
  CLR_LED;
  EI;
  while(1)
  {
    BTCON = 0x02;
    P0CONL = 0xA6;
    SET_BUZZER;
    if(irFlag)
    {
      if(remBuffer[0] == IR_USER_CODE)
      {
        if(remBuffer[2] == IR_POWER)
        {
          COM_SCR;
          if(GET_SCR == 0)
          {
            BuzzerOut(1000);
            SET_LED;
          }
          else
          {
            BuzzerOut(2000);
            CLR_LED;
          }
        }
      }
      //SendDat(remBuffer[2]);
      irFlag = 0;
      P0PND = 0x08;
    }
    if(GET_SCR)
    {
      timeCount++;
      if(timeCount >= 55000)
      {
        timeCount = 0;
        SET_LED;
        DelayMs(50);
        CLR_LED;
      }
    }
  }
}
#pragma vector = 0x00
__interrupt void Interrupts(void)
{
  uchar count;
  uchar i,j;
  if(P0PND & 0x04)
  {
    P0PND = 0x00;
    for(count = 0;count < 160; count++)
    {
      T0CON = 0xC8;
      do
      NOP;
      while(T0CNT < 150);
      if(GET_IR)
      {
        P0PND = 0x08;
        return;
      }
    }
    
    while(GET_IR == 0);
    
    for(count = 0;count < 80;count++)
    {
      T0CON = 0xC8;
      do
      NOP;
      while(T0CNT < 150);
      if(GET_IR == 0)
      {
        P0PND = 0x08;
        return;
      }
    }
    
    while(GET_IR);
    
    for(j=0;j<4;j++)
    {
      for(i=0;i<8;i++)
      {
        while(GET_IR == 0);
        for(count = 0; count < 18; count++)
        {
          T0CON = 0xC8;
          do
          NOP;
          while(T0CNT < 150);
        }
        remBuffer[j] >>= 1;
        if(GET_IR)
        {
          remBuffer[j] |= 0x80;
          for(count = 0; count < 32; count++)
          {
            T0CON = 0xC8;
            do
            NOP;
            while(T0CNT < 150);
          }
          
        }
      }
    }
    irFlag = 1;
  }
}

文章评论0条评论)

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