标题: 请问我想把这单片机程序输出由锁定状态改成点动状态,请高手帮助,谢谢 [打印本页]

作者: dj3365191    时间: 2022-10-21 13:33
标题: 请问我想把这单片机程序输出由锁定状态改成点动状态,请高手帮助,谢谢
如下程序是个蓝牙开关程序,共有六路,我想把前四路输出由锁定状态改成点动状态,就是手机那边改成触摸一下执行一下,触摸不离开就一直执行,松手后输出停止,请问高手们需要怎么改,谢谢!
单片机源程序如下:
#include<reg52.h>
#include<string.h>
sfr AUXR = 0x8E;
sfr T2H  = 0xD6;
sfr T2L  = 0xD7;
#define uchar unsigned char
#define uint unsigned int

sbit power1=P1^2;//继电器端口依次类推
sbit power2=P1^3;
sbit power3=P1^4;
sbit power4=P1^5;
sbit power5=P1^6;
sbit power6=P1^7;

uchar temp;
bit flag;

void UART_Send_Byte(uchar mydata)//发送一个字节
{
        SBUF=mydata;
        while(!TI);
        TI=0;
}

void UART_Send_Str(uchar *s)//发送字符串
{
        while (*s)
        {
                UART_Send_Byte(*s++);
        }
}

void UartInit(void)                //9600bps@11.0592MHz
{
        SCON = 0x50;                //8位数据,可变波特率
        AUXR |= 0x01;                //串口1选择定时器2为波特率发生器
        AUXR |= 0x04;                //定时器2时钟为Fosc,即1T
        T2L = 0xE0;                //设定定时初值
        T2H = 0xFE;                //设定定时初值
        AUXR |= 0x10;                //启动定时器2
        EA = 1;
        ES = 1;
}
void main(void)
{
        UartInit();
        UART_Send_Str("AT+VERSION");//发送AT指令,测试模块是否正常
        while(1)
        {
                if(flag)
                {
                        flag=0;
                        if(temp=='2') power1=0; //如果接收到的数据是0x02小灯亮
                        else if(temp=='1') power1=1;//否则如果接收到是0x01,则熄灭

                        if(temp=='8') power2=0; //
                        else if(temp=='4') power2=1;//

                        if(temp=='7') power3=0; //
                        else if(temp=='9') power3=1;//

                        if(temp=='b') power4=0; //
                        else if(temp=='a') power4=1;//

                        if(temp=='d') power5=0; //
                        else if(temp=='c') power5=1;//
                                                                        
                                                                                                if(temp=='e') power6=0; //
                        else if(temp=='f') power6=1;//
                                                                        
                }
        }
}

void serial_IT(void) interrupt 4
{
        RI=0;        
        temp = SBUF;//串口接收到的数据给temp
        UART_Send_Byte(temp);//单片机接收到数据后发给电脑串口助手
        flag=1;
}



作者: wpppmlah    时间: 2022-10-21 15:57
手机端触摸时  发送一个开指令,停止触摸时 再发送一个关指令 ......
作者: zcl0815    时间: 2022-10-21 16:26
瞎写了一下
#include<reg52.h>
#include<string.h>
sfr AUXR = 0x8E;
sfr T2H  = 0xD6;
sfr T2L  = 0xD7;
#define        uchar                unsigned char
#define        uint                unsigned int
#define        uint32        unsigned long

sbit power1=P1^2;//继电器端口依次类推
sbit power2=P1^3;
sbit power3=P1^4;
sbit power4=P1^5;
sbit power5=P1^6;
sbit power6=P1^7;

uchar temp;
bit flag;

void UART_Send_Byte(uchar mydata)//发送一个字节
{
        SBUF=mydata;
        while(!TI);
        TI=0;
}

void UART_Send_Str(uchar *s)//发送字符串
{
        while (*s)
        {
                UART_Send_Byte(*s++);
        }
}

void UartInit(void)                //9600bps@11.0592MHz
{
        SCON = 0x50;                //8位数据,可变波特率
        AUXR |= 0x01;                //串口1选择定时器2为波特率发生器
        AUXR |= 0x04;                //定时器2时钟为Fosc,即1T
        T2L = 0xE0;                //设定定时初值
        T2H = 0xFE;                //设定定时初值
        AUXR |= 0x10;                //启动定时器2
        EA = 1;
        ES = 1;
}
void main(void)
{
                                uint32 Power1_Count = 0;

        UartInit();
        UART_Send_Str("AT+VERSION");//发送AT指令,测试模块是否正常

        while(1)
        {
                if(flag)
                {
                        flag=0;
                        
                        if(temp=='2')
                                                                                                {
                                                                                                                        temp=0x00;
                                                                                                                        Power1_Count = 1;
                                                                                                                        power1=0; //如果接收到的数据是0x02小灯亮
                                                                                                }
                                                                                                if(Power1_Count)
                                                                                                {
                                                                                                        Power1_Count++;
                                                                                                }
                                                                                                if(Power1_Count>100000)//延时一段时间关闭。为了提高程序响应其他控制组,没有使用软件DELAY延时,用了循环计数,这个时间毛估估,如果要准确就要开定时器了。
                                                                                                {

                                                                                                                        Power1_Count = 0;
                                                                                                                        power1=1;//否则如果接收到是0x01,则熄灭
                                                                                                }
                                                                                               
                                                                                                //加入其他控制组
                                                                        
                }
        }
}

void serial_IT(void) interrupt 4
{
        RI=0;        
        temp = SBUF;//串口接收到的数据给temp
        UART_Send_Byte(temp);//单片机接收到数据后发给电脑串口助手
        flag=1;
}




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1