#include <reg52.h>
#define data1 P0
#define data2 P2
sbit cww = P1^0;// 正反转设置
sbit clock = P1^1;// L297频率
sbit enable = P3^7; //步进电机转动使能端
sbit wr1 = P3^6; //ad转换使能端
char nstep; //标记细分数
char start; //启动标记
char count0; //定时器0计数标志
char count1; //定时器1计数标志
char half; //定时器0高低电平标志
char
timeover; //定时计数器1定时完成标志
char k; //暂停标志
unsigned char low1,high1;//定时器1要装的定时值
/*
*下面数组是二维细分表
* 5表示有5个细分档 细分档分别为1,2,4,8,16
* 16表示最多16细分
*/
unsigned char step_n[5][16] = { {0xFF},
{0x7F,0xFF},
{0x3F,0x7F,0xBF,0xFF},
{0x1F,0x3F,0x5F,0x7f,0x9F,0xBf,0xDF,0xFF},
{0x0F,0x1F,0x2F,0x3F,0x4F,0x5F,0x6F,0x7F,0x8f,0x9F,0xAF,0xBF,0xCF,0xDF,0xEF,0xFF}
};
void delay5(void) //定时5毫秒 误差 0us
{
unsigned char a;
for(a=1;a>0;a--);
}
void delay20(void) //定时20毫秒 误差 0us
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=7;a>0;a--);
}
void delay(void) //延时0.1秒 误差 0us
{
unsigned char a,b,c;
for(c=19;c>0;c--)
for(b=20;b>0;b--)
for(a=130;a>0;a--);
}
/*************************外部中断0**********************/
void int0(void) interrupt 0
{
delay20(); //消除抖动
if(P3^2 == 0)
{
if(k == 0)
{//判断程序是否启动
start = 0;//设置已开始启动
k = 1; //已经启动,暂停标志开始
}
else{
delay();//延时0.1s
while(P3^2);//判断是否启动
}
}
}
/*******************外部中断1*************************/
void int1(void) interrupt 2
{
delay20(); //20毫秒消除抖动
if(P3^3 == 0)
cww = ~cww;//将CW/CWW端口的状态改变 即正反装改变
}
/********************定时器0****************************/
void t0(void) interrupt 1
{
TR0 = 0;//关闭定时器
half++; //确定定时高低电平时间的标记
if((half = half%2)){ //clock周期低电平
TH0 = 0xF8;
TL0 = 0x30;
clock = 0;
}else{ //clock周期高电平
TH0 = 0xE0;
TL0 = 0xC0;
clock = 1;
count0++; //clock周期数加1
}
TR0 = 1;//开启定时器
}
/*******************************定时器1**********************************/
void t1(void) interrupt 3
{
TR1 = 0;//关闭定时器 1
TH1 = high1; //定时器1赋初值
TL1 = low1;
TR1 = 1; //开启定时器1
count1++; //定时器1 计数器加1
count1 = count1%nstep; //确定细分值下标
timeover = 0; //定时等待停止
}
/*******************************步进电机细分函数
******************************/
/* 定义一个i变量,用来表示细分值二维数组的一个下标
* 判断细分数,根据细分数设置定时器1定时时间和确定二维数组下标
* 开启297使能端,开启定时计数器
* 当定时器0定时周期次数小于8,即L297的8个时钟周期没有完成,程序则一直循环
* 在循环里根据定时器0的周期数和定时器1定时次数确定DA转换的初值
* 等待定时1定时一个周期完成
* 当定时器周期数大于等于8结束循环,关闭定时计数器,关闭297使能端
****************************************************************************/
void stepmotor()
{
char i;//定义一个变量来确定二维数组的下标
/*
* 判断细分数根据细分数确定定时计数器1的初值和二维数组下标
*
*/
switch(nstep){
case 1: low1 = 0xF0;
high1 = 0xD8;
i = 0;
break;
case 2: low1 = 0x78;
high1 = 0xEC;
i = 1;
break;
case 4: low1 = 0x3C;
high1 = 0xF6;
i = 2;
break;
case 8: low1 = 0x1E;
high1 = 0xFB;
i = 3;
break;
case 16:low1 = 0x8F;
high1 = 0xFD;
i = 4 ;
break;
default: break;
}
TH1 = high1;//定时器1赋初值
TL1 = low1;
TH0 = 0xE0; //定时器0赋初值
TL0 = 0xC0;
enable = 1; //开启297的使能端
clock = 1;
TR0 = TR1 = 1; //开启定时器0和定时器1
/*
*根据clock周期数和定时计数器1的count确定DA转化的初值
* DA转换的初值由297各个状态的确定的
*/
while(count0 < 8)
{ switch(count0)
{ case 0:
case 4: data1 = step_n[i][count1];// 给DAC0832端口赋转换值
data2 = step_n[i][nstep-1-count1];
wr1 = 0;//开启转化
delay5();//延时5us
wr1 = 1;//寄存器锁存
case 1:
case 5: data1 = 255;
data2 = 0;
wr1 = 0;
delay5();
wr1 = 1;
case 2: data1 = step_n[i][nstep-1-count1];
data2 = step_n[i][count1];
wr1 = 0;
delay5();
wr1 = 1;
case 3:
case 7: data1 = 0;
data2 = 255;
wr1 = 0;
delay5();
wr1 = 1;
default : break;
}
while(timeover);//定时器1定时等待
timeover = 1;
}
TR0 = TR1 = 0;//关闭定时计数器
enable = 0; //关闭297
}
/***********************************主程序
**************************************/
/* 先进行初始化,对外部中断,定时计数器进行初始化设置 开启外部中断
* 然后判断是否按下启动键 启动后将各种标志,计数清零
* 然后判断细分数,将细分标志置数
* 调用细分函数,使电机转动
******************************************************************************
/
int main(void)
{
enable = 0;//L297使能端关闭
wr1 = 1;//DA不转换
PX1 = 1;//外部中断1为高优先级
PT0 = 1;//设置定时中断0为高优先级
EX0 = 1;//开启外部中断0
EX1 = 1;//开启外部中断1
IT0 = 0;//设置为低电平触
TMOD = 0x11;//定时/计数器为定时状态 方式一定时
ET0 = ET1 = 1;//允许中断
IT0 = IT1 = 0;//低电平触发
start = 1; //启动标志位
EA = 1;//开启外部中段
while(start);//开始启动
//初始化完毕
/**将各种计数器清零,标志清零*/
while(1)
{
count0 = 0;//定时器0计数
count1 = 0;//定时器1计数
nstep = 1;//细分数默认为不细分
half = 0;//CLOCK高低电平标记
k = 0;//暂停标记 默认不暂停
/*
* 以下是判断细分数
* 判断细分数后设置细分标志
*/
if(P1^2 == 0)
nstep = 2;
else if(P1^3 == 0)
nstep = 4;
else if(P1^4 == 0)
nstep = 8;
else if( P1^5 == 0)
nstep = 16;
stepmotor();//调用细分函数
}
Build target 'Target 1'
compiling bishe.c...
BISHE.C(220): error C141: syntax error near 'while'
BISHE.C(220): error C141: syntax error near '1'
BISHE.C(222): error C231: 'count0': redefinition
BISHE.C(223): error C231: 'count1': redefinition
BISHE.C(224): error C231: 'nstep': redefinition
BISHE.C(225): error C231: 'half': redefinition
BISHE.C(226): error C231: 'k': redefinition
BISHE.C(231): error C141: syntax error near 'if'
BISHE.C(231): error C141: syntax error near '^', expected ')'
BISHE.C(232): error C279: 'nstep': multiple initialization
BISHE.C(233): error C141: syntax error near 'else'
BISHE.C(233): error C141: syntax error near '^', expected ')'
BISHE.C(234): error C279: 'nstep': multiple initialization
BISHE.C(235): error C141: syntax error near 'else'
BISHE.C(235): error C141: syntax error near '^', expected ')'
BISHE.C(236): error C279: 'nstep': multiple initialization
BISHE.C(237): error C141: syntax error near 'else'
BISHE.C(237): error C141: syntax error near '^', expected ')'
BISHE.C(238): error C279: 'nstep': multiple initialization
BISHE.C(239): error C231: 'stepmotor': redefinition
BISHE.C(241): error C141: syntax error near '}'
Target not created
这是为嘛啊,那个while怎么就错了啊,还有那几个为什么会是重定义了啊!!!求帮忙!!!谢谢!!!
客气啦! 最佳答案
一周热门 更多>