|
开发环境:MAPLAB V8.36 && HI-TECH C PRO for the PIC10/12/16 MCU family V9.65
以下是预览:
/*
MCU:PIC12F508
时钟:IntRC
提前角度:20度
触发方式:先正后负(硬件实现后,两个触发信号都为负脉冲)
WDT:开
大约250rpm以下不发火
因为程序很简单,我就不做太多注释了
*/
#include <pic.h>
__CONFIG(MCLRDIS & WDTEN & UNPROTECT & INTRC);
#define uchar unsigned char
#define uint unsigned int
#define FIST GP3 //第一个触点信号
#define SEND GP2 //第二个触点信号
#define FIRE GP0 //发火输出信号
bit hspeed; //高速标志
bit nofire; //不发火标志
bit tlfull; //TMR0最高位为1标志
bit gofire; //允许发火标志
bit tconfull; //记时器已满标志
uchar cnt; //点火延时
uchar tcon; //记时器
uchar timer; //存贮TMR0的值
uchar timeh; //延时的高字节
uchar timel; //延时的低字节
uint times; //总时间
uint timed; //需要延时的时间
uint timex; //记数器
void main(void)
{
while(1)
{
GPIO = 0; //重置一些变量
TRIS = 0x3E;
OPTION = 0xCE;
tcon = 0;
tlfull = 0;
tconfull = 0;
asm("CLRWDT");
do{NOP();}while(FIST == 0);
while(FIST); //等待第一个信号
TMR0 = 0;
hspeed == 1 ? FIRE = 1 : FIRE = 0 ; //如果高速标志为1,则发火
if(nofire == 0)
{
do
{
if(TMR0 & 0x80)
{
tlfull = 1;
}
else if(tlfull)
{
tlfull = 0;
tcon++;
if(tcon == 0xFF)
{
tconfull = 1;
FIRE = 0;
}
if(tcon == timeh && tconfull == 0)
gofire = 1;
}
if(gofire == 1)
{
if((TMR0 > timel) || (tcon > timeh))
{
FIRE = 1;
gofire = 0;
}
}
}
while(SEND);
FIRE = 1;
timer = TMR0;
cnt = 100;
do{}while(--cnt);
FIRE = 0;
}
else
{
do
{
if(TMR0 & 0x80)
{
tlfull = 1;
}
else if(tlfull)
{
tlfull = 0;
tcon++;
if(tcon == 0xFF)
{
tconfull = 1;
FIRE = 0;
}
}
}
while(SEND);
timer = TMR0;
}
/************************************************************************************/
NOP();
nofire = 0;
hspeed = 0;
gofire = 0;
if(tconfull)
{
nofire = 1;
continue;
}
if((tlfull == 1) && (timer < 0x80))
{
tcon++;
timer = 0;
}
if(tcon > 80)
{
nofire = 1;
timeh = 0;
continue;
}
times = ((tcon * 256) + timer) / 4;
if(times < 200)
{
hspeed = 1;
timeh = 0;
continue;
}
if(times > 450)
{
timeh = 0;
continue;
}
timed = 0;
for(timex = 200 ; timex < 451 ; timex++)
{
timed += 7;
if(timex == times)
break;
}
timeh = timed / 256;
timel = timed % 256;
if(timeh == 0)
{
gofire = 1;
}
}
}
自己找的不知道怎么样! |
|