/*
函数功能
脉冲计数:外部中断0和外部中断1的上升沿和下降沿脉冲数之和;
旋转方向:通过判断中断0 中断1在前后来判断正反转;
修改数值:上位机输入Z或z修改当前内存变量0-9999999(十进制数值);
清零操作:上位机输入C或c修改当前内存变量 0000000;
加电初始值为:5000000;
SP485允许接收和发送的状态;接收允许 P2^7=0;发送允许 P2^7=1;
MCU芯片:STC15F2k60S2 ;
外部中断0:P3^2 ;外部中断1:P3^3 ;
串口接收:P3^0 ;串口发送:P3^1 ;
*/
#include
#include "intrins.h"
sfr AUXR = 0x8E;
sfr P0M0 = 0x94; //0000,0000 端口0模式寄存器0
sfr P0M1 = 0x93; //0000,0000 端口0模式寄存器1
sfr P1M0 = 0x92; //0000,0000 端口1模式寄存器0
sfr P1M1 = 0x91; //0000,0000 端口1模式寄存器1
sfr P2M0 = 0x96; //0000,0000 端口2模式寄存器0
sfr P2M1 = 0x95; //0000,0000 端口2模式寄存器1
sfr P3M0 = 0xB2; //0000,0000 端口3模式寄存器0
sfr P3M1 = 0xB1; //0000,0000 端口3模式寄存器1
sfr P4M0 = 0xB4; //0000,0000 端口4模式寄存器0
sfr P4M1 = 0xB3; //0000,0000 端口4模式寄存器1
sfr P5M0 = 0xCA; //0000,0000 端口5模式寄存器0
sfr P5M1 = 0xC9; //0000,0000 端口5模式寄存器1
sfr P6M0 = 0xCC; //0000,0000 端口6模式寄存器0
sfr P6M1 = 0xCB; //0000,0000 端口6模式寄存器1
sfr P7M0 = 0xE2; //0000,0000 端口7模式寄存器0
sfr P7M1 = 0xE1; //0000,0000 端口7模式寄存器1
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L //System frequency
#define BAUD 9600 //UART baudrate
/*Define UART parity mode*/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT EVEN_PARITY //Testing even parity
***it waibu1=P3^2;
***it waibu2=P3^3;
//int t=0;
BYTE a;
bit busy;
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
::增量式运动控制方向标志位
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
bit a_1;
bit b_1;
bit dir=0;
BYTE shuzi[]="0123456789";
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
::设置编码器寄存器
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
BYTE cnt_9=0,cnt_8=0, cnt_7=0,cnt_6=0,cnt_5=0,cnt_4=0,cnt_3=0,cnt_2=0,cnt_1=0,cnt_0=0;
unsigned long cnt;
BYTE byte_cnt=0;
bit zheng_flag_0=0,fan_flag_0=0; //函数反馈值
bit bit_flag_0,bit_flag_1,bit_flag_2,bit_flag_3;
WORD time_rx;
bit time_flag;
BYTE recive;
BYTE pont_a;point_b;
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
::程序宏定义
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
void SendData(BYTE dat);
void SendString(char *s);
void Delay100ms() [url=]//@11.0592MHz[/url]
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 5;
j = 52;
k = 195;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void int_waibu(void)
{
IT0=0;
IT1=0;
EX0=1;
EX1=1;
}
void UartInit(void) [url=]//9600bps@8MHz[/url]
{
PCON &= 0x7F; //波特率不倍速
SCON = 0x50; //8位数据,可变波特率
AUXR &= 0xBF; //定时器1时钟为Fosc/12,即12T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //清除定时器1模式位
TMOD |= 0x20; //设定定时器1为8位自动重装方式
TL1 = 0xFD; //设定定时初值
TH1 = 0xFD; //设定定时器重装值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
ES=1;
EA = 1;
}
void chuankou(void)
{
SendData(shuzi[cnt/1000000]);
SendData(shuzi[cnt/100000%10]);
SendData(shuzi[cnt/10000%10]);
SendData(shuzi[cnt/1000%10]);
SendData(shuzi[cnt/100%10]);
SendData(shuzi[cnt/10%10]);
SendData(shuzi[cnt%10]);
SendString("rn");
Delay100ms();
}
void main()
{
P3M0=0;
P3M1=0;
waibu1=1;
waibu2=1;
int_waibu();
UartInit();
while (1)
{
chuankou();
Delay100ms();
}
}
void zhongduan0() interrupt 0
{
if(waibu1==0)
{
if(waibu2==1)
{
dir=1;
}
else dir=0;
}
if(waibu1==1)
{
if(waibu2==0)
{
dir=1;
}
else dir=0;
}
if(dir==1){cnt++;}
else {if(cnt>0)cnt--;
else cnt=0;}
}
void zhongduan1() interrupt 2
{
if(waibu2==0)
{
if(waibu1==0)
{
dir=1;
}
else dir=0;
}
if(waibu2==1)
{
if(waibu1==1)
{
dir=1;
}
else dir=0;
}
if(dir==1){cnt++;}
else{if(cnt>0)cnt--;
else cnt=0;}
}
/*中断函数Uart_Isr()上位机输入C或c清零;
输入0-99999999的十进制数值,单片机接收并通过函数SendData发送给上位机;
若收到Z或Z通过改变编码器的计数方向,正向计数变为反向计数,反向计数变为正向计数;
*/
void Uart_Isr() interrupt 4 using 1
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
recive = SBUF;
if(recive!=0x63&&recive!=0X43&&recive!=0x00/*&&recive!=0x5A&&recive!=0X7A*/) //C和c清除
{
time_flag=1;
switch (a)
{
case 6:for(byte_cnt=0;byte_cnt<10 ;byte_cnt++)
{
if(shuzi[byte_cnt]==recive)cnt_0=byte_cnt;
}
break;
case 5:for(byte_cnt=0;byte_cnt<10 ;byte_cnt++)
{
if(shuzi[byte_cnt]==recive)cnt_1=byte_cnt;
}
break;
case 4:for(byte_cnt=0;byte_cnt<10; byte_cnt++)
{
if(shuzi[byte_cnt]==recive)cnt_2=byte_cnt;
}
break ;
case 3:for(byte_cnt=0;byte_cnt<10 ;byte_cnt++)
{
if(shuzi[byte_cnt]==recive)cnt_3=byte_cnt;
}
break ;
case 2:for(byte_cnt=0;byte_cnt<10;byte_cnt++)
{
if(shuzi[byte_cnt]==recive)cnt_4=byte_cnt;
}
break ;
case 1:for(byte_cnt=0;byte_cnt<10;byte_cnt++)
{
if(shuzi[byte_cnt]==recive)cnt_5=byte_cnt;
}
break ;
case 0:for(byte_cnt=0;byte_cnt<10;byte_cnt++)
{
if(shuzi[byte_cnt]==recive)cnt_6=byte_cnt;
}
break ;
default :break;
}
a++;
cnt=cnt_0+cnt_1*10+cnt_2*100+cnt_3*1000+cnt_4*10000+cnt_5*100000+cnt_6*1000000;
}
if(recive==0x63||recive==0X43) //C和c清除
{
recive=0;
cnt=0;
}
/* if(recive==0x5A||recive==0X7A) //Z和z反置 正反转方向
{
t=waibu1;
waibu1=waibu2;
waibu2=t;
}
*/
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
ACC = dat; //Calculate the even parity bit P (PSW.0)
if (P) //Set the parity bit according to P
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 0; //Set parity bit to 0
#endif
}
busy = 1;
SBUF = ACC; //Send data to UART buffer
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
while (*s) //Check the end of the string
{
SendData(*s++); //Send current char and increment string ptr
}
}
|