串口返回红外键值

#include <reg52.h>
#include <intrins.h>
#include "./delay/delay.h"

unsigned int irtime;
bit start_flag = 0;
bit irok = 0;
unsigned char irdata[33];
unsigned char bitnum = 0;

void time0_init()
{
    EA = 1;
    TMOD |= 0x02;
    TH0 = 0;
    ET0 = 1;  //可以进入中断
    TR0 = 1;  //开始计数    
}

void int0_init()
{
    IT0 = 1;  //设置外部中断的触发方式,下降沿触发
    EA = 1;  //打开总中断
    EX0 = 1;  //打开外部中断0
}

void timer0_isr() interrupt 1
{
    irtime++;
}

void int0_isr() interrupt 0  //打开中断服务程序
{
    if(start_flag)
    {
        if((irtime >= 40) && (irtime <= 60))
        {
            bitnum = 0;
        }
        irdata[bitnum] = irtime + '0';
        bitnum++;
        irtime = 0;
        if(bitnum == 33)
        {
            irok = 1;
            irdata[33] = '\0';
            bitnum = 0;
            start_flag = 0;
        }
    }
    else
    {
        irtime = 0;
        start_flag = 1;
    }
    //irtime++;  //0.256ms  引导码 : (9 + 4.5) / 0.256 = 53  0码 : 4  1码 : 9
}

void uart_init()
{
    SCON = 0x50;  //设置出口工作方式,打开接收允许
    //SM0 = 0; SM1 = 1; SM2 = 0;REN = 1;
    TMOD |= 0x20;  //设置定时器1为工作方式2

    TH1 = 0xfd;  //波特率为9600bits/s
    //ET1 = 1;  //打开定时器1中断允许
    TR1 = 1;  //开始计数
}

void uart_send_byte(unsigned char byte)
{
    SBUF = byte;
    //TI位自动置1,手动清0;
    while(!TI);  //while(TI != 1);
    TI = 0;     
}

void uart_send_string(char *buf)
{
    while(*buf != '\0')
    {
        uart_send_byte(*buf);
        buf++;
    }   
}
unsigned char buf[33];
void main()
{
    time0_init();
    int0_init();
    uart_init();

    while(1)
    {
        if(irok == 1)
        {
        for(bitnum = 0;bitnum < 33;bitnum++)
        {
            if(irdata[bitnum+1] <= 6+ '0')
            {
                buf[bitnum] = 0 + '0';
            }
            else
            {
                buf[bitnum] = 1 + '0';
            }
        }
        buf[32] = '\n';
        //if(irok == 1)
        //{
            for(bitnum = 0;bitnum < 33;bitnum++)
            {
                uart_send_byte(buf[bitnum]);
            }
        //  uart_send_string(irdata);   
        //  uart_send_byte('\n');
            irok = 0;
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是STM32串口显示按键值的示例代码: ``` #include "stm32f10x.h" #include <stdio.h> // 定义按键和LED的引脚 #define KEY_PIN GPIO_Pin_0 #define KEY_GPIO_PORT GPIOA #define KEY_GPIO_CLK RCC_APB2Periph_GPIOA #define LED_PIN GPIO_Pin_1 #define LED_GPIO_PORT GPIOA #define LED_GPIO_CLK RCC_APB2Periph_GPIOA // 定义串口参数 #define USARTx USART1 #define USARTx_CLK RCC_APB2Periph_USART1 #define USARTx_GPIO_CLK RCC_APB2Periph_GPIOA #define USARTx_TX_PIN GPIO_Pin_9 #define USARTx_TX_GPIO_PORT GPIOA #define USARTx_RX_PIN GPIO_Pin_10 #define USARTx_RX_GPIO_PORT GPIOA #define USARTx_IRQn USART1_IRQn // 定义变量 uint8_t key_value; // 函数声明 void GPIO_Config(void); void USART_Config(void); int main(void) { GPIO_Config(); USART_Config(); printf("STM32 USART Key Value Test\r\n"); while (1) { // 检测按键状态 if (GPIO_ReadInputDataBit(KEY_GPIO_PORT, KEY_PIN) == 0) { // 按键按下,点亮LED GPIO_SetBits(LED_GPIO_PORT, LED_PIN); key_value = 1; printf("Key Value: %d\r\n", key_value); } else { // 按键释放,熄灭LED GPIO_ResetBits(LED_GPIO_PORT, LED_PIN); key_value = 0; } // 稍微延时一下,避免按键抖动 for (int i = 0; i < 100000; i++); } } // 配置GPIO void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; // 打开GPIO时钟 RCC_APB2PeriphClockCmd(KEY_GPIO_CLK | LED_GPIO_CLK, ENABLE); // 配置按键引脚为输入模式 GPIO_InitStructure.GPIO_Pin = KEY_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStructure); // 配置LED引脚为输出模式 GPIO_InitStructure.GPIO_Pin = LED_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure); } // 配置USART void USART_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; // 打开USART时钟和GPIO时钟 RCC_APB2PeriphClockCmd(USARTx_CLK | USARTx_GPIO_CLK, ENABLE); // 配置USART引脚 GPIO_InitStructure.GPIO_Pin = USARTx_TX_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = USARTx_RX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStructure); // 配置USART参数 USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USARTx, &USART_InitStructure); USART_Cmd(USARTx, ENABLE); } ``` 该代码实现了通过串口显示按键值。按键按下时,点亮LED并将按键值设为1,同时通过串口输出按键值;按键释放时,熄灭LED并将按键值设为0。在主循环中添加了一个简单的延时以避免按键抖动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值