|
|
发表于 2017-1-28 21:17:37
|
显示全部楼层
本帖最后由 payne.pan 于 2017-1-28 21:30 编辑
前天刚看到这篇文章,支持楼主,代码我改写了一遍,更方便大家阅读。
========================================
/* ppm to pwm for stc15f104w 12Mhz */
/* from http://www.moz8.com/thread-63467-1-1.html */
/* original by "917840533", changed by "Payne.Pan" */
/* not allowed for commercial usage */
/* 2017.1 */
#include"reg52.h"
#include "intrins.h"
#define u8 unsigned char
#define u16 unsigned int
#define PWM_FrLen 22500
sfr P3M1=0xb1;
sfr P3M0=0xb2;
sfr AUXR=0x8e;
sfr T2H=0xd6;
sfr T2L=0xd7;
sfr IE2=0xaf;
sbit out=P3^4;
sbit k0=P3^0;
sbit k1=P3^1;
sbit k2=P3^2;
u16 ppm,ppms[8],pwm;
u8 sel_ch=0, read_pos=0;
bit pwm_setting=1, timeout = 0 ;
void ppm_int() interrupt 2 {
TR0=0;
/* read data in us */
ppm=TH0;
ppm=(ppm<<8)|TL0;
/* clear T0 for next read*/
TH0=0;
TL0=0;
TR0=1;
if(ppm>2500 || timeout) {
read_pos=0;
timeout=0;
return;
}
if ( read_pos>7) return;
ppms[read_pos++] = ppm;
}
void T2_int() interrupt 12 {
pwm=ppms[sel_ch];
if (pwm_setting) {
out=0; T2H=(65535-pwm)/256; T2L=(65535-pwm )%256;
} else {
out=1; T2H=(65535-PWM_FrLen+pwm)/256; T2L=(65535-PWM_FrLen+pwm)%256;
}
pwm_setting = !pwm_setting;
}
void T0_int() interrupt 1 {
timeout=1;
}
void main()
{
/* p3.4 as output, p3.3 as input */
P3M0=0x10; //10000
P3M1=0x08; //01000
/* interrupt 1 */
EX1=1; // enable INT1
IT1=1; // mode
PX1=1; // priority
/* T0 */
ET0=1; // enable T0 interrupt
TMOD=0x01; // T1 16bit mode
/* T2 */
T2H = T2L = 0;
IE2=0x04; // enable T2 interrupt
AUXR=0x10; // T2 start
EA=1;
sel_ch=k0;
sel_ch=(sel_ch<<1)|k1;
sel_ch=(sel_ch<<1)|k2;
while(1);
}
|
| |