📄 sync.c
字号:
///////////////////////////////////////////////////////
// 剔除程序
// CANCEL1.C
// Date: 2008/07/08
// VER1: P33输入同步脉冲,输出2倍频的剔除信号
///////////////////////////////////////////////////////
#include <stdlib.h>
#include<math.h>
#include<intrins.h>
#include "AT89x051.h"
////////////////宏定义/////////////
#define uchar unsigned char
#define uint unsigned int
#define ulint unsigned long int
////////////////函数申明/////////////
//void DelayMs(unsigned int n);
void Interrupt0(void);
void Interrupt1(void);
void Timer0(void);
void Timer1(void);
//void Initall(void);
bit Shift_Cancel(uchar data *cancel_ptr,uchar step,bit carry);
void Insert_Cancel(bit carry0,bit carry1,bit carry2,bit carry3);
void Cancel_Proc(void);
//void Timer0_Init(void);
//void Calcu_Cycle(void);
////////////////变量常数定义/////////////
/*单片机IO口定义*/
//sbit DOORTEST =P1^0; /*限位开关信号,打开时为0,盖子合上时为1*/
//sbit LED1 =P1^1; /*调试指示LED*/
//sbit BEEPCTRL =P1^2; /*蜂鸣器控制*/
//sbit CHIPSEL =P1^3; /*片选信号*/
sbit UPKEY =P1^4; /*向上调节按键=P1^2;//*/
sbit DNKEY =P1^5; /*向下调节按键=P1^3;// */
sbit SCL =P1^6; /*FLASH(24C01)芯片的时钟信号(SCL)*/
sbit SDA =P1^7; /*FLASH(24C01)芯片的数据信号(SDA)*/
//
//sbit RS485IN =P3^0; /*485控制*/
//sbit RS485OUT =P3^1; /*485控制*/
//sbit MOTORSPEED =P3^2; /*电机转速输入信号,每转输入一个脉冲。外部中断0(INT0)输入(ZS_F)*/
sbit PULSEIN =P3^2; /*脉冲输入。外部中断0(INT0)输入(W/R)*/
sbit LED =P1^7; /*调试指示LED*/
sbit LED1 =P1^5; /*调试指示LED*/
//sbit TEMPCTRL =P3^6; /*压缩机控制(TK_WD)*/
sbit PULSEOUT =P3^0; /*脉冲输出*/
ulint sync_period;
ulint cancel_period=0;
uchar temp_TH0;
uchar temp_TL0;
uchar temp_TH0H;
uchar TH0H=0;
uchar temp_TH1H=0;
uchar TH1H=0;
uchar cancel=0;
uchar step_CNT=0;
uint period=0;
//uint TEMPperiod=0;
//uint Modify=0;
bit cancel0_f=0;
bit cancel1_f=0;
bit cancel2_f=0;
bit cancel3_f=0;
bit detect0_f=0;
bit detect1_f=0;
bit detect2_f=0;
bit detect3_f=0;
bit SYNC_F=0;
bit SYNC_S=0;
bit PROC_F=0;
#define DEFAULT_P 2400
#define SYNC_PAR 16000
#define CANCEL0STEP 128
#define CANCEL1STEP 128
#define CANCEL01STEP 128
#define BEGIN_CANCEL PULSEOUT=1
#define END_CANCEL PULSEOUT=0
uchar data cancel0[CANCEL0STEP/8]={0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};
uchar data cancel1[CANCEL1STEP/8]={0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};
uchar data cancel01[CANCEL01STEP/8]={0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};
////////////////////////////////////////////////////////
void main(void) using 0
{
IE = 0;
TMOD = 0x11; // 0001 0001 16进制计数器
//IP = 0x05; // 0000 0001 int0,int1优先
IE = 0x85; // 1000 0101 int0,int1中断允许
TCON =0x05; // 0000 0101 int0,int1中断下降沿触发
TR0 = 1; // 定时器中断
//TR1 = 0;
ET0 = 1;
//ET1 = 1;
P1=0x1F;
P3 = 0x7f;
//PULSEOUT=PULSE_LO;
TH0=DEFAULT_P/256;
TL0=DEFAULT_P%256;
TH0H=0;
LED=0;
LED1=1;
while(1)
{
Cancel_Proc();
}
}
//------------------------------------------------------
//毫秒延时
//void DelayMs(uint n)
//{
// uchar data i;
// while(n--){for(i=0;i<125;i++);}
//}
/*************************************
4支烟同步波形输入
*************************************/
void Interrupt0() interrupt 0 using 1
{
LED=~LED;
SYNC_F=1;
///////////计算同步脉冲周期
// calcu_f=0;
if (SYNC_S==0)
{
TR0=0;
temp_TH0=TH0;
temp_TL0=TL0;
temp_TH0H=TH0H;
TH0=0;
TL0=0;
TH0H=0;
TR0=1;
period=temp_TH0<<8L;
period +=temp_TL0;
sync_period=(ulint)temp_TH0H<<16;
sync_period |= period;
cancel_period=sync_period>>2; ///4分频
period=(uint)cancel_period; //
period=(65535-period); //
cancel_period=cancel_period&0xffff0000;
cancel_period=cancel_period|period;
step_CNT=4;
TF1=1;
TR1=1;
TH1H=0;
//END_CANCEL;
//cancel_period=2000000; //for test
}
//////////检测烟支状态(上一次的检测结果)
detect0_f=P1_0;
detect1_f=P1_1;
detect2_f=P1_2;
detect3_f=P1_3;
}
/*************************************
单支烟同步波形输入
*************************************/
void Interrupt1() interrupt 2 using 1
{
//EX0=0;
SYNC_S=1;
TR1=0;
TF1=0;
TR0=0;
PROC_F=1;
}
/*************************************
在定时器0中断中计算脉冲宽度
*************************************/
void Timer0() interrupt 1 using 2
{
//TH0H++;
//TH0=0x0;
//TL0=0x0;
TR0=0;
TH0=(65535-SYNC_PAR/2)/256;
TL0=(65535-SYNC_PAR/2)%256;
TR0=1;
PULSEOUT=~PULSEOUT;
}
/*************************************
在定时器1中断中做4倍频输出
*************************************/
void Timer1() interrupt 3 using 3
{
if (TH1H==0)
{
PROC_F=1;
TR1=0;
TL1=cancel_period&0xff; //
TH1=cancel_period>>8; //
TH1H=cancel_period>>16;
TR1=1;
if (step_CNT==0)
{
TR1=0;
PROC_F=0;
END_CANCEL;
}
else step_CNT--;
}
else
{
TH1H--;
TH1=0;
TL1=0;
//TR1=1;
}
}
/*************************************
移位剔除烟支的状态
input: 哪排烟,剔除步数,当前烟的状态
output: 剔除烟的状态
*************************************/
bit Shift_Cancel(uchar data *cancel_ptr,uchar step,bit carry)
{
uchar i;
for (i=0;i<step;i++)
{
ACC = *cancel_ptr;
CY=carry;
_rlca_();
carry=CY;
*cancel_ptr=ACC;
cancel_ptr++;
}
return carry;
}
/*************************************
合并双排烟支的状态
input: 内排烟的状态,外排烟的状态
output:
*************************************/
void Insert_Cancel(bit carry0,bit carry1,bit carry2,bit carry3)
{
cancel01[0]&=0xfc;
cancel01[1]&=0xf3;
if (carry0==1) cancel01[0]|=0x01;
if (carry1==1) cancel01[0]|=0x02;
if (carry2==1) cancel01[1]|=0x04;
if (carry3==1) cancel01[1]|=0x08;
}
/*************************************
剔除烟支处理
input:
output:
*************************************/
void Cancel_Proc(void)
{
bit carry0,carry1,carry2,carry3;
if (PROC_F==1)
{
PROC_F=0;
LED1=~LED1;
if (SYNC_F==1)
{
SYNC_F=0;
carry0=Shift_Cancel(cancel0,CANCEL0STEP/8,detect0_f);
carry1=Shift_Cancel(cancel0,CANCEL1STEP/8,detect1_f);
carry2=Shift_Cancel(cancel1,CANCEL0STEP/8,detect2_f);
carry3=Shift_Cancel(cancel1,CANCEL1STEP/8,detect3_f);
Insert_Cancel(carry0,carry1,carry2,carry3);
}
carry1=Shift_Cancel(cancel01,CANCEL01STEP/8,0);
if (carry1==1) BEGIN_CANCEL;
else END_CANCEL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -