单片机——红外遥控器——代码

#include 
   
   
    
    
#include 
    
    
     
     
#include "./delay/delay.h"
#include "./lcd1602/lcd1602.h"

unsigned int irtime = 0;
bit startflag = 0;
unsigned char irdata[33];
unsigned char ircood[4];
bit irok = 0;
sbit IR = P3^2;

void timer0()
{
      EA = 1;
      TMOD |= 0x02;    
      TH0 = 0;
      TL0 = 0;
      ET0 = 1;
      TR0 = 1;
}

void int0()
{
      EA = 1;
      IT0 = 1;
      EX0 = 1;
}

void int0_isr() interrupt 0
{
      static unsigned char bitnum = 0;
      if(1 == startflag)
      {
          if((irtime > 40) && (irtime < 60))
         {
              bitnum = 0;
          }
        irdata[bitnum] = irtime;
        bitnum++;
        irtime = 0;
        if(33 == bitnum)
        {
            irok = 1;
            bitnum = 0;
            startflag = 0;
        }
     }
    else
    {
        startflag = 1;
        irtime = 0;
    }
}
void timer0_isr() interrupt 1
{
    irtime++;
}
unsigned char irprocess()
{
      unsigned char i,j,k;
      unsigned char temp;
      k = 1;

      for(j = 0; j < 4; j++)
      {
            for(i = 0; i < 8; i++)
            {
                temp >>= 1;
               if(irdata[k] > 6)
               {
                     temp += 0x80;
                }
               k++;
             }
            ircood[j] = temp;
    }   
    if((ircood[0] + ircood[1] == 0xff)  && (ircood[2] + ircood[3] == 0xff))
    {
        switch(ircood[2])
        {
                      case 0x16: return 0;
                      case 0x0c: return 1;
                      case 0x18: return 2;
                      case 0x5e: return 3;
                      case 0x08: return 4;
                      case 0x1c: return 5;
                      case 0x5a: return 6;
                      case 0x42: return 7;
                      case 0x52: return 8;
                      case 0x4a: return 9;
                      case 0x45: return 10;
                      case 0x46: return 11;
                      case 0x47: return 12;
                      case 0x44: return 13;
                      case 0x40: return 14;
                      case 0x43: return 15;
                      case 0x07: return 16;
                      case 0x15: return 17;
                      case 0x09: return 18;
                      case 0x19: return 19;
                      case 0x0d: return 20;
                        default: break;
         }
    }
    return 0xff;

}
void display()
{
      unsigned char high,low;
      unsigned char i;
      for(i = 0; i < 4; i++)
      {
              high = ircood[i]/16;
              low = ircood[i]%16;
              if(high > 9)
             {
                 high += 0x37;
              }
            else
            {
                 high += 0x30;
             }
            if(low > 9)
            {
                 low += 0x37;
             }
            else
            {
                  low += 0x30;
             } 
            lcd_print(1, 2*i+1, high);
            lcd_print(1, 2*i + 2, low);
     }
}
void irdisplay(unsigned char num)
{
        unsigned char high,low;
        high = num/10 +0x30;
        low = num%10 + 0x30;

        lcd_print(2, 1, high);
        lcd_print(2, 2, low);
}

void main()
{
      unsigned char keynum;
      lcd_init();
      timer0();
      int0();
      while(1)
      {
          if(1 == irok)
          {
               irok = 0;
              keynum = irprocess();
              display();
              if(keynum != 0xff)
              {
                     irdisplay(keynum);
               }
           }
       }
}
    
    
   
   
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STM32F407IGT6单片机 HC-SR501人体红外感应模块(串口屏显示)KEIL软件工程源码, int main(void) { uint8_t state; /* 复位所有外设,初始化Flash接口和系统滴答定时器 */ HAL_Init(); /* 配置系统时钟 */ SystemClock_Config(); /* 初始化串口并配置串口中断优先级 */ MX_DEBUG_USART_Init(); HMI_USARTx_Init(); /* 初始化LED */ LED_GPIO_Init(); HC_SR501_GPIO_Init(); /* 无限循环 */ while (1) { state=HC_SR501_StateRead(); switch(state) { case HC_SR501_LOW: HMI_string_setting("page1.t1.txt=\"无人\""); break; case HC_SR501_HIGH: HMI_string_setting("page1.t1.txt=\"有人\""); break; } } } /** * 函数功能: 向串口屏发送数据 * 输入参数: 无 * 返 回 值: 无 * 说 明: 无 */ void HMI_value_setting(const char *val_str,uint32_t value) { uint8_t tmp_str[30]={0}; uint8_t i; sprintf((char *)tmp_str,"%s=%d",val_str,value); for(i=0;i<strlen((char *)tmp_str);++i) { HMI_USARTx->DR=tmp_str[i]; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); } HMI_USARTx->DR=0xFF; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); HMI_USARTx->DR=0xFF; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); HMI_USARTx->DR=0xFF; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); } /** * 函数功能: 向串口屏发送浮点数据 * 输入参数: 无 * 返 回 值: 无 * 说 明: 无 */ void HMI_string_setting(const char *val_str) { uint8_t tmp_str[50]={0}; uint8_t i; sprintf((char *)tmp_str,val_str); for(i=0;i<strlen((char *)tmp_str);++i) { HMI_USARTx->DR=tmp_str[i]; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); } HMI_USARTx->DR=0xFF; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); HMI_USARTx->DR=0xFF; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); HMI_USARTx->DR=0xFF; while(__HAL_UART_GET_FLAG(&husartx_HMI, UART_FLAG_TXE) == RESET); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值