关于单片机通过蓝牙串口调pwm的问题,想弄个蓝牙串口调pwm的程序,pwm输出接的是开发板上LED灯,发现单独的pwm程序没问题,单独的蓝牙也能正常控制,可是两个程序加起来后,LED灯变得一闪一闪的,根本不是正常pwm输出的现象,也没法用手机控制,不知道是程序的问题,还是定时器0中断和串口中断优先级的问题?用的是stc89c52的单片机,是不是pwm得用定时器2,才能用蓝牙正常控制呢。。
单片机源程序如下:
- #include <reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit pwm_out = P1^0;
- uint num = 200;
- uint pwm = 0;
- void UART_Init()
- {
- SM0 = 0;
- SM1 = 1;
- REN = 1;
- EA = 1;
- ES = 1;
- TMOD = 0x20;
- TH1 = 0xfd;
- TL1 = 0xfd;
- TR1 = 1;;
- }
- void time0int()
- {
- TMOD = 0x02;
- TH0 = 220;
- TL0 = 220;
- TR0 = 1;
- EA = 1;
- ET0 = 1;
- }
- void time0() interrupt 1
- {
- TR0 =0;
- TH0 = 220;
- TL0 = 220;
- pwm++;
- if(pwm == 500)
- pwm_out = pwm = 0;
- if(num == pwm)
- pwm_out = 1;
- TR0 = 1;
- }
- void UART() interrupt 4
- {
- if(RI)
- {
- RI = 0;
- switch(SBUF)
- {
- case 0x02: if(num<=450) num+=50; break;
- case 0x01: if(num >50); num-=50;; break;
- }
- }
- }
- void main()
- {
- time0int();
- UART_Init();
- while(1);
- }
复制代码