- 8條回答
-
WYF13760287143
06-29 23:22
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?
/*串口接收*/
#include
sbit led1=P1^0;
sbit led2=P1^1;??
sbit led3=P1^2;??
sbit led4=P1^3;? ???
static unsigned char??dat,countT0;
void delay(int t)
{
? ? char i;
? ? while(t--)
? ? {
? ?? ???for(i=0;i<100;i++);
? ? }
}? ? ? ???
void timerinit()
{? ?
? ? PCON &= 0x7F;? ?? ???//波特率不倍速
? ? SCON = 0x50;? ?? ???//8位數(shù)據(jù),可變波特率? ?
? ? //TMOD &= 0x0F;? ?? ???//清除定時(shí)器1模式位
? ? TMOD = 0x22;? ?? ???//設(shè)定定時(shí)器1、定時(shí)器0為8位自動(dòng)重裝方式
? ? TL1 = 0xe8;? ?? ???//設(shè)定定時(shí)初值? ?? ? //1200
? ? TH1 = 0xe8;? ?? ???//設(shè)定定時(shí)器重裝值
? ? ET1 = 0;? ?? ???//禁止定時(shí)器1中斷
? ? TR1 = 1;? ?? ???//啟動(dòng)定時(shí)器1
? ? ? ?
? ? ? ? ET0 = 1;? ? ? ? ? ? ? ? ? ? ? ? //允許定時(shí)器0中斷
? ? ? ? TR0 = 1;? ? ? ? ? ? ? ? ? ? ? ? //timer0? ?start
? ? TL0 = 0xe8;? ?? ???//設(shè)定定時(shí)0初值? ?? ? //0.1ms
? ? TH0 = 0xe8;? ?? ???//設(shè)定定時(shí)器0重裝值
? ? ES=1;
? ? EA=1;
}
void qianjing()
{
? ? ? ? if(countT0<50)
? ? ? ? {
? ? ? ? ? ? ? ? led1=0;
? ? ? ? }
? ? ? ? if(countT0>50)
? ? ? ? {
? ? ? ? ? ? ? ? led1=1;
? ? ? ? }? ? ? ???? ? ? ? ? ? ? ?? ?
}
void houtui()
{
? ? ? ? if(countT0<50)
? ? ? ? {
? ? ? ? ? ? ? ? led2=0;
? ? ? ? }
? ? ? ? if(countT0>50)
? ? ? ? {
? ? ? ? ? ? ? ? led2=1;
? ? ? ? }? ? ? ? ? ? ? ?? ?
}
void left()
{
? ? ? ? if(countT0<50)
? ? ? ? {
? ? ? ? ? ? ? ? led3=0;
? ? ? ? }
? ? ? ? if(countT0>50)
? ? ? ? {
? ? ? ? ? ? ? ? led3=1;
? ? ? ? }? ? ? ? ? ? ? ?? ?
}
void right()
{
? ? ? ? if(countT0<50)
? ? ? ? {
? ? ? ? ? ? ? ? led4=0;
? ? ? ? }
? ? ? ? if(countT0>50)
? ? ? ? {
? ? ? ? ? ? ? ? led4=1;
? ? ? ? }? ? ? ? ? ? ? ?? ?
}
void jieshou()
{
? ? ? ? switch(dat)
? ? ? ? {
? ? ? ???? ? ? ? case 0xaa:qianjing();break;//如果串口接收的為0xaa ,則P1.0口輸出PWM
? ? ? ???? ? ? ? case 0x77:houtui();break;//如果串口接收的為0x77 ,則P1.1口輸出PWM
? ? ? ???? ? ? ? case 0xa7:left(); break;//如果串口接收的為0xa7 ,則P1.2口輸出PWM
? ? ? ???? ? ? ? case 0x7a:right();break;//如果串口接收的為0x7a ,則P1.3口輸出PWM
? ? ? ? // ? ? ? ? case 0xa0:P1=0xe0;break;
? ? ? ? //? ? ? ? default:P1=0xff;break;
? ? ? ? }
}
void main()
{
? ? ? ? dat=0x00;
? ? timerinit();
? ? ? ? delay(1);
? ? while(1)
? ? {? ? ? ?
? ? ? ? ? ? ? ? jieshou();? ? ? ? ? ? ? ?? ?
? ? }
}
/*串口接收*/
void uart() interrupt 4
{
? ? if(RI)? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //接收中斷
? ? {? ?
? ?? ???RI=0;? ?? ?? ?? ?? ?? ?? ?? ?? ?//讀取接受到的數(shù)據(jù)
? ?? ???dat=SBUF;
? ? }??? ? ? ?? ???? ? ? ? ? ? ? ?
}
void timer0() interrupt 1
{??
? ?// TL0 = 0x9c;? ?? ???//設(shè)定定時(shí)0初值? ?? ? //0.1ms
? ?// TH0 = 0x9c;? ?? ???//設(shè)定定時(shí)器0重裝值
? ? ? ? ? ? ? ? countT0++;
? ? ? ? ? ? ? ? if(countT0==100)countT0=0;
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ???? ? ? ?
}
-
zixiang_huang
06-27 22:02
但是實(shí)測(cè)PWM輸出有不定時(shí)的斷續(xù)現(xiàn)象
-
鎂酷科技
06-26 02:12
由于串口是用433M無(wú)線傳輸?shù)?,是信?hào)誤碼原因引起的,問(wèn)題已確定,但是這個(gè)誤碼怎么處理呢?
-
60user92
06-27 21:52
誤碼處理,在傳輸固定數(shù)據(jù)的時(shí)候,可用查表法處理。
但是在傳輸不確定的數(shù)據(jù)情況下怎么處理誤碼呢?
在線等。
-
劉祥1982
06-23 20:52
通常無(wú)線傳輸是靠協(xié)議和校驗(yàn)來(lái)保證數(shù)據(jù)正確性的,高要求下可以使用糾錯(cuò)編碼來(lái)恢復(fù)部分有錯(cuò)的數(shù)據(jù)。不過(guò),樓主的應(yīng)用是根據(jù)無(wú)線傳輸?shù)臄?shù)據(jù)來(lái)決定PWM輸出,那將是實(shí)時(shí)的,糾錯(cuò)算法需占用大量帶寬,樓主所用信道未必能很好的支持,所以還是用校驗(yàn)吧,發(fā)現(xiàn)有錯(cuò)則維持上一幀數(shù)據(jù)不變,這雖然不真實(shí),但總比誤碼好。
-
sdfdsg
06-27 23:16
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?
/*串口接收*/
#include
sbit led1=P1^0;
sbit led2=P1^1;??
sbit led3=P1^2;??
sbit led4=P1^3;? ???
static unsigned char dat,dat0,countT0;
void delay(int t)
{
? ? unsigned int i;
? ? while(t--)
? ? {
? ?? ???for(i=0;i<100;i++);
? ? }
}? ? ? ???
void timerinit()
{? ?
? ? PCON &= 0x7F;? ?? ???//波特率不倍速
? ? SCON = 0x50;? ?? ???//8位數(shù)據(jù),可變波特率? ?
? ? //TMOD &= 0x0F;? ?? ???//清除定時(shí)器1模式位
? ? TMOD = 0x22;? ?? ???//設(shè)定定時(shí)器1、定時(shí)器0為8位自動(dòng)重裝方式
? ? TL1 = 0xe8;? ?? ???//設(shè)定定時(shí)初值? ?? ? //1200
? ? TH1 = 0xe8;? ?? ???//設(shè)定定時(shí)器重裝值
? ? ET1 = 0;? ?? ???//禁止定時(shí)器1中斷
? ? TR1 = 1;? ?? ???//啟動(dòng)定時(shí)器1
? ? ? ?
? ? ? ? ET0 = 1;? ? ? ? ? ? ? ? ? ? ? ? //允許定時(shí)器0中斷
? ? ? ? TR0 = 1;? ? ? ? ? ? ? ? ? ? ? ? //timer0? ?start
? ? TL0 = 0xe8;? ?? ???//設(shè)定定時(shí)0初值? ?? ? //0.1ms
? ? TH0 = 0xe8;? ?? ???//設(shè)定定時(shí)器0重裝值
? ? ES=1;
? ? EA=1;
}
void qianjing()
{? ? ? ???
? ? ? ? if(countT0<50)
? ? ? ? {
? ? ? ? ? ? ? ? led1=0;
? ? ? ? }
? ? ? ? if(countT0>50)
? ? ? ? {
? ? ? ? ? ? ? ? led1=1;
? ? ? ? }? ? ? ???? ? ? ? ? ? ? ?? ?
}
void houtui()
{
? ? ? ? if(countT0<50)
? ? ? ? {
? ? ? ? ? ? ? ? led2=0;
? ? ? ? }
? ? ? ? if(countT0>50)
? ? ? ? {
? ? ? ? ? ? ? ? led2=1;
? ? ? ? }? ? ? ? ? ? ? ?? ?
}
void left()
{
? ? ? ? led3=1;? ? ? ? ? ? ? ???
}
void right()
{
? ? ? ? led4=1;? ? ? ? ? ? ? ???
}
void jieshou()
{
? ? ? ? switch(dat)
? ? ? ? {
? ? ? ???? ? ? ? case 0xaa:qianjing();break;
? ? ? ???? ? ? ? case 0x77:houtui();break;
? ? ? ???? ? ? ? case 0x7a:left();break;
? ? ? ???? ? ? ? case 0xa7:right();break;
? ? ? ? ? ? ? ? default:P1=0x00;break;
? ? ? ? }
}
void main()
{? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?
? ? ? ? dat=0x00;
? ? ? ? P1=0xff;
? ? timerinit();
? ? ? ? delay(1);
? ? while(1)
? ? {? ? ? ?
? ? ? ???? ? ? ? jieshou();
? ? }
}
/*串口接收*/
void uart() interrupt 4
{? ? ? ? ? ? ? ? ? ? ? ?? ???
? ? if(RI)? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //接收中斷
? ? {? ?
? ?? ???dat0=SBUF;
? ?? ???RI=0;? ?? ?? ?? ?? ?? ?? ?? ?? ?//讀取接受到的數(shù)據(jù)
? ? }??? ? ? ?
? ? ? ? switch(dat0)
? ? ? ? {
? ? ? ? ? ? ? ? case 0xaa:dat=dat0;break;
? ? ? ? ? ? ? ? case 0x77:dat=dat0;break;
? ? ? ? ? ? ? ? case 0x7a:dat=dat0;break;
? ? ? ? ? ? ? ? case 0xa7:dat=dat0;break;
? ? ? ? ? ? ? ? case 0xa0:dat=dat0;break;
? ? ? ? ? ? ? ? default:P1=0x00;break;
? ? ? ? }??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ???? ? ? ?? ?? ? ? ? ? ? ? ?
}
void timer0() interrupt 1
{??
? ? ? ? ? ? ? ? countT0++;
? ? ? ? ? ? ? ? if(countT0>99)countT0=0;? ? ? ?? ? ? ? ? ? ? ? ? ???? ? ? ?
}
-
guotong1984
07-01 14:56
是不是外圍元件有問(wèn)題,換片或上拉試試。
-
kghfh
06-28 02:49
端口少時(shí)正常,多了不正常,說(shuō)明程序有誤,特別要注意循環(huán)處理時(shí)的時(shí)間是否滿足。