利用12写了个串口通信接收PWM控制信号的程序,可是通过串口调试,单片机能收到串口发送的数据但是不能将数据用作控制信号的变量,但是偶尔又可以控制,不知为什么下面是代码:
#include <reg52.h>
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
/************************************************/
/*************定义PCA特殊功能寄存器**************/
/************************************************/
sfr CCON = 0xd8;
sfr CMOD = 0xd9;
sfr CL = 0xe9;
sfr CH = 0xF9;
sfr CCAPM0 = 0xda;
sfr CCAP0L = 0xea;
sfr CCAP0H = 0xfa;
sfr CCAPM1 = 0xdb;
sfr CCAP1L = 0xeb;
sfr CCAP1H = 0xfb;
sfr CCAPM2 = 0xdc;
sfr CCAP2L = 0xec;
sfr CCAP2H = 0xfc;
sfr CCAPM3 = 0xdd;
sfr CCAP3L = 0xed;
sfr CCAP3H = 0xfd;
sfr PCAPWM0 = 0xf2;
sfr PCAPWM1 = 0xf3;
sfr PCAPWM2 = 0xf4;
sfr PCAPWM3 = 0xf5;
sfr P3M1=0xb1;
sfr P3M0=0xb2;
sfr P1M1=0x91;
sfr P1M0=0x92;
sbit CCF0 = CCON^0;
sbit CCF1 = CCON^1;
sbit CR = CCON^6;
sbit CF = CCON^7;
sbit LED1 = P1^7;
sbit LED2 = P1^6;
sbit LED3 = P1^5;
sbit rs485dir = P1^0;
uchar a, b, c, d;
//uchar addr = 0x01;
uchar ld;
uchar pl;
uchar status1;
uchar status2;
uchar omode = 0x01;
bit flag = 1;
uint fre[]={12000,6000,4000,3000,2400,2000,1714,1500,1200,1000};
uchar bri[]={0x19,0x32,0x4b,0x64,0x7d,0x96,0xaf,0xc8,0xe1,0xfa};
void delay(uint i);
void delayus(uchar a);
void Serial_communication();
void SerialInit();
void main()
{
P3M0 = 0; //******************//
P3M1 = 1; //*设定IO口工作模式*//
P1M0 = 0;
P1M1 = 0;
ld = 0;
pl = 9;
status1 = 0x00;
status2 = 0x03;
SerialInit();
while(1)
{
delay(1000);
LED1 = 1;
LED2 = 1;
LED3 = 1;
CCON = 0; //关闭PCA寄存器CF中断//
CL = 0; //初始化PCA16位寄存器值//
CH = 0; //***PWM功能使能****//
CMOD = 0x00; //*确定时钟频率模式*//
CCAPM1 = 0x42; //****PWM输出使能****//
if(a==0x01)
{
switch(b)
{
case 0x02:
switch(c)
{
case 0: ld = 0; break;
case 1: ld = 1; break;
case 2: ld = 2; break;
case 3: ld = 3; break;
case 4: ld = 4; break;
case 5: ld = 5; break;
case 6: ld = 6; break;
case 7: ld = 7; break;
case 8: ld = 8; break;
case 9: ld = 9; break;
default: break;
}
case 0x03:
switch(c)
{
case 0: ld = 0; break;
case 1: ld = 1; break;
case 2: ld = 2; break;
case 3: ld = 3; break;
case 4: ld = 4; break;
case 5: ld = 5; break;
case 6: ld = 6; break;
case 7: ld = 7; break;
case 8: ld = 8; break;
case 9: ld = 9; break;
default: break;
}
case 0x04:
switch(c)
{
case 0: status1 = 0; break;
case 1: status1 = 0x03; break;
default: break;
}
default: break;
}
}
CCAP1L = bri[ld]; //****调节占空比*****//
CCAP1H = bri[ld]; //****无缝传输下一个占空比值给CCAPL*****//
CR = 1; //***启动PCA计数器***//
flag = 1;
while(flag)
{
// if(flag==1)
// delay(fre[pl]);
PCAPWM1 = status1; //EPC0H与EPC0L为低PWM输出高//
delay(fre[pl]);
PCAPWM1 = status2; //EPC0H与EPC0L为高PWM输出低//
delay(fre[pl]);
}
}
}
void delay(uint i)
{
uint a,c;
for(a=399;a>0;a--)
for(c=0;c<i;c++);
}
void delayus(uchar a)
{
uchar i,j;
for(i=0;i<120;i++)
for(j=0;j<a;j++);
}
void SerialInit()
{
SCON = 0xf0;
PCON = 0x00;
TMOD = 0x20;
TH1 = 0xf9;
TH1 = 0xf9;
TR1 = 1;
ES = 1;
EA = 1;
rs485dir = 0;
}
void Serial_communication() interrupt 4
{
while(!RI);
RI = 0;
a = SBUF;
flag = 0;
if(RB8==1)
{
switch(a)
{
case 0x01:
SM2 = 0;
rs485dir = 1;
delayus(50);
TI = 0;
TB8 = 1;
SBUF = a;
while(!TI);
TI = 0;
LED1 = 0;
LED2 = 1;
LED3 = 1;
rs485dir = 0;
RI = 0;
while(!RI);
RI = 0;
delayus(30);
b = SBUF;
rs485dir = 1;
delayus(50);
SBUF = b;
TI = 0;
TB8 = 1;
while(!TI);
TI = 0;
LED1 = 1;
LED2 = 0;
LED3 = 1;
rs485dir = 0;
while(!RI);
RI = 0;
delayus(30);
c = SBUF;
rs485dir = 1;
delayus(50);
SBUF = c;
TI = 0;
TB8 = 1;
while(!TI);
TI = 0;
rs485dir = 0;
LED1 = 1;
LED2 = 1;
LED3 = 0;
while(!RI);
RI = 0;
delayus(30);
d = SBUF;
rs485dir = 1;
delayus(50);
SBUF = d;
TI = 0;
TB8 = 1;
while(!TI);
TI = 0;
rs485dir = 0;
SM2 = 1;
break;
case 0xfe:
SM2 = 0;
rs485dir = 1;
LED1 = 0;
LED2 = 1;
LED3 = 1;
delayus(50);
TI = 0;
TB8 = 1;
SBUF = a;
while(!TI);
TI = 0;
LED1 = 1;
LED2 = 0;
LED3 = 1;
rs485dir = 0;
RI = 0;
while(!RI);
RI = 0;
b = SBUF;
LED1 = 1;
LED2 = 1;
LED3 = 0;
while(!RI);
RI = 0;
c = SBUF;
SM2 = 1;
break;
default: SM2 = 1; break;
}
}
if(RB8==0)
{
SM2 = 1;
}
}
|