本帖最后由 sunmebaby 于 2021-8-4 21:55 编辑
要实现的功能是通过按键和蓝牙两种方式实现对风扇的调速,按键部分的程序调试过没有问题,但是蓝牙部分的程序不管怎么调试都没有反应,串口助手上也可以接收到数据,但是数码管和风扇都没有反应,实在找不出来是什么问题,希望大神指点
单片机源程序如下:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar time; //计时
char Data;
char count = 0; //占空比
uchar code smgduan[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f};// 0-9段码
sbit PWM = P1^0; //PWM通道
sbit key_speed_up = P3^1; //按键加档
sbit key_speed_down =P3^0;//按键减档
sbit smgwei1 = P2^2;
sbit smgwei2 = P2^3;
sbit smgwei3 = P2^4;
/*
void Motor_speed();//函数声明
void timer0_init();
void UsarInit
void delay_ms();
void Motor_up();
void Motor_down();
void display();
*/
void delay_ms(uint z)//延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=10;y>0;y--);
}
void display() //显示当前占空比
{
P0=smgduan[count/100];smgwei1=1; smgwei2=1;smgwei3=1;delay_ms(1);P0=0X00;
P0=smgduan[count/10%10];smgwei1=0; smgwei2=1;smgwei3=1;delay_ms(1);P0=0X00;
P0=smgduan[count%10];smgwei1=1; smgwei2=0;smgwei3=1;delay_ms(1);P0=0X00;
}
void Motor_up() //加档函数
{
if(key_speed_up==0)
{
delay_ms(2);
if(key_speed_up==0)
{
count += 25;
if(count > 100)
{
count = 0;
}
}
while(!key_speed_up);
}
}
void Motor_down() //减档函数
{
if(key_speed_down==0)
{
delay_ms(2);
if(key_speed_down==0)
{
count -= 25;
if(count <0)
{
count = 0;
}
}
while(!key_speed_down);
}
}
void timer0_init() //定时器0初始化
{
TMOD = 0x01;
TH0 = 0xff;
TL0 = 0xf7;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void UsarInit() //串口初始化
{
TMOD = 0x20; //设置计数器工作方式2
SM0 = 0; //设置串口工作方式1
SM1 = 1;
REN = 1; //允许串口接收
TH1 = 0xfd; //设置波特率9600
TL1 = 0xfd;
TR1 = 1; //启动定时器1
ES = 1; //开串口中断
EA = 1; //开总中断
}
void timer0_int() interrupt 1 //定时器0中断处理函数
{
TR0 = 0;
TH0 = 0xff;
TL0 = 0xf7;
TR0 = 1;
PWM = 0;
time++;
if(time<=count) PWM = 1;
else PWM = 0;
if(time>100) time = 0;
}
void Usart() interrupt 4 //串口通信中断处理函数
{
Data = SBUF;
RI = 0;
while(1)
{
switch(Data)
{case 'a':count = 0; break;
case 'b':count = 25;break;
case 'c':count = 50;break;
case 'd':count = 75;break;
case 'e':count =100;break;}
}
//while(!TI);
//TI = 0;
}
void main() //主函数
{
timer0_init();
UsarInit();
while(1)
{
display();
Motor_up();
Motor_down();
}
}
|