?? multistep.c
字號:
//************************************************************
//12路PWM波程序
//王超炎2005-05-05
//********************************************************
#include <iom128v.h>
#include <macros.h>
#include "data.h"
unsigned int N1=4;
unsigned int N2=20;
unsigned int N3=20;
unsigned int N4=10;
unsigned int cycle_start1=192;
unsigned int cycle_start2=3;
unsigned int cycle_start3=2;
unsigned int cycle_start4=2;
unsigned int totalstep1=392;
unsigned int totalstep2=11;
unsigned int totalstep3=6;
unsigned int totalstep4=7;
void port_init(void)
{
DDRA = 0xFF;//A口設置為輸出
PORTA = 0x00;//A口輸出全0
DDRC = 0xFF;//...
PORTC = 0x00;//...
}
void timer1_init(void)
{
TCCR1A =0x00;//
TCNT1=0x00;
}
void ClrAll(void)
{
PORTA = 0x00;
PORTC = 0x00;//端口清零
}
//*******************************************************************************
//函數名: Motion_init(void)
//參 數:無
//功 能:初始化動作
//*******************************************************************************
void Motion_init(void)
{
int i=0, j=0, t=0, temp=0, temp1, temp2;
for(t=0;t<50;t++) //?50步循環 輸出50個周期的調零信號
{
for(j=1;j<9;j++) //?內嵌8步循環 對應20ms的一個輸出周期 分別操作12個端口 j代表端口
{
if(j<5) //當j小于5時
{
PORTA|=(1<<(j-1)); //把PA口的前4個引腳置1
temp2=2250+(j-1)*3750;//管理PA的四個端口的PWM波
}
PORTC|=(1<<(j-1)); //把PC口的8個引腳置1
temp1=2250+(j-1)*3750;//管理PC口的八個端口的PWM波
do
{
temp=TCNT1; //獲取定時器的值
if(temp>temp1)PORTC&=~(1<<(j-1)); //把PC口的8個引腳置0
if(temp>temp2)PORTA&=~(1<<(j-1)); //把PA口的8個引腳置0
}
while(temp<j*3750);//在此循環中輸出一個對應角度為90度的PWM波
}
TCNT1=0;
}
}
//*******************************************************************************
//函數名: Motion_excute()
//參 數:
//功 能:
//*******************************************************************************
void Motion_excute(unsigned int p)
{
unsigned int i=0,t=0,j=0;
unsigned int temp1=0,temp2=0,temp=0;
if(p==1)
{
for(i=0;i<cycle_start1;i++)
for(t=angle1[i][0];t>0;t--)
{
TCNT1=0x00;
for(j=1;j<9;j++)
{
if(j<5)
{
PORTA|=(1<<(j-1));//實際為PORTA=(PORTA|(1<<(j-1)));先位或在賦值
temp2=(50*angle1[i][j+8]/3+750)+(j-1)*3750;
}
PORTC|=(1<<(j-1));
temp1=(50*angle1[i][j]/3+750)+(j-1)*3750;
do
{
temp=TCNT1;//定時器1
if(temp>temp1)PORTC&=~(1<<(j-1));
if(temp>temp2)PORTA&=~(1<<(j-1));
}
while(temp<j*3750);
}
TCNT1=0x00;
}
do
{
for(i=cycle_start1;i<totalstep1;i++)
for(t=angle1[i][0];t>0;t--)
{
TCNT1=0x00;
for(j=1;j<9;j++)
{
if(j<5)
{
PORTA|=(1<<(j-1));
temp2=(50*angle1[i][j+8]/3+750)+(j-1)*3750;
}
PORTC|=(1<<(j-1));
temp1=(50*angle1[i][j]/3+750)+(j-1)*3750;
do
{
temp=TCNT1;
if(temp>temp1)PORTC&=~(1<<(j-1));
if(temp>temp2)PORTA&=~(1<<(j-1));
}
while(temp<j*3750);
}
TCNT1=0x00;
}
N1--;
}
while(N1);
for(i=0;i<4;i++)
for(t=angle1_2[i][0];t>0;t--)
{
TCNT1=0x00;
for(j=1;j<9;j++)
{
if(j<5)
{
PORTA|=(1<<(j-1));
temp2=(50*angle1_2[i][j+8]/3+750)+(j-1)*3750;
}
PORTC|=(1<<(j-1));
temp1=(50*angle1_2[i][j]/3+750)+(j-1)*3750;
do
{
temp=TCNT1;
if(temp>temp1)PORTC&=~(1<<(j-1));
if(temp>temp2)PORTA&=~(1<<(j-1));
}
while(temp<j*3750);
}
TCNT1=0x00;
}
}
if(p==2)
{
for(i=0;i<cycle_start2;i++)
for(t=angle2[i][0];t>0;t--) //讀取角度數組的第一行到t
{
TCNT1=0x00;//定時器置零
for(j=1;j<9;j++)//8個循環對應12個端口
{
if(j<5)//對PA口進行操作
{
PORTA|=(1<<(j-1));
temp2=(50*angle2[i][j+8]/3+750)+(j-1)*3750;//計算角度對應脈寬
}
PORTC|=(1<<(j-1));
temp1=(50*angle2[i][j]/3+750)+(j-1)*3750;
do
{
temp=TCNT1;
if(temp>temp1)PORTC&=~(1<<(j-1));
if(temp>temp2)PORTA&=~(1<<(j-1));
}
while(temp<j*3750);//在一個循環內對j-1口進行操作 輸出對應脈沖
}
TCNT1=0x00;
}
do
{
for(i=cycle_start2;i<totalstep2;i++)
for(t=angle2[i][0];t>0;t--)
{
TCNT1=0x00;
for(j=1;j<9;j++)
{
if(j<5)
{
PORTA|=(1<<(j-1));
temp2=(50*angle2[i][j+8]/3+750)+(j-1)*3750;
}
PORTC|=(1<<(j-1));
temp1=(50*angle2[i][j]/3+750)+(j-1)*3750;
do
{
temp=TCNT1;
if(temp>temp1)PORTC&=~(1<<(j-1));
if(temp>temp2)PORTA&=~(1<<(j-1));
}
while(temp<j*3750);
}
TCNT1=0x00;
}
N2--;
}
while(N2);
for(i=0;i<4;i++)
for(t=angle2_3[i][0];t>0;t--)
{
TCNT1=0x00;
for(j=1;j<9;j++)
{
if(j<5)
{
PORTA|=(1<<(j-1));
temp2=(50*angle2_3[i][j+8]/3+750)+(j-1)*3750;
}
PORTC|=(1<<(j-1));
temp1=(50*angle2_3[i][j]/3+750)+(j-1)*3750;
do
{
temp=TCNT1;
if(temp>temp1)PORTC&=~(1<<(j-1));
if(temp>temp2)PORTA&=~(1<<(j-1));}while(temp<j*3750);
}
TCNT1=0x00;
}
}
if(p==3)
{
for(i=0;i<cycle_start3;i++)
for(t=angle3[i][0];t>0;t--)
{
TCNT1=0x00;
for(j=1;j<9;j++)
{
if(j<5)
{
PORTA|=(1<<(j-1));
temp2=(50*angle3[i][j+8]/3+750)+(j-1)*3750;
}
PORTC|=(1<<(j-1));
temp1=(50*angle3[i][j]/3+750)+(j-1)*3750;
do
{
temp=TCNT1;
if(temp>temp1)PORTC&=~(1<<(j-1));
if(temp>temp2)PORTA&=~(1<<(j-1));}while(temp<j*3750);
}
TCNT1=0x00;
}
do
{
for(i=cycle_start3;i<totalstep3;i++)
for(t=angle3[i][0];t>0;t--)
{
TCNT1=0x00;
for(j=1;j<9;j++)
{
if(j<5)
{
PORTA|=(1<<(j-1));
temp2=(50*angle3[i][j+8]/3+750)+(j-1)*3750;
}
PORTC|=(1<<(j-1));
temp1=(50*angle3[i][j]/3+750)+(j-1)*3750;
do
{
temp=TCNT1;
if(temp>temp1)PORTC&=~(1<<(j-1));
if(temp>temp2)PORTA&=~(1<<(j-1));
}
while(temp<j*3750);
}
TCNT1=0x00;
}
N3--;
}
while(N3);
for(i=0;i<4;i++)
for(t=angle3_4[i][0];t>0;t--)
{
TCNT1=0x00;
for(j=1;j<9;j++)
{
if(j<5)
{
PORTA|=(1<<(j-1));
temp2=(50*angle3_4[i][j+8]/3+750)+(j-1)*3750;
}
PORTC|=(1<<(j-1));
temp1=(50*angle3_4[i][j]/3+750)+(j-1)*3750;
do
{
temp=TCNT1;
if(temp>temp1)PORTC&=~(1<<(j-1));
if(temp>temp2)PORTA&=~(1<<(j-1));}while(temp<j*3750);
}
TCNT1=0x00;
}
}
if(p==4)
{
for(i=0;i<cycle_start4;i++)
for(t=angle4[i][0];t>0;t--)
{
TCNT1=0x00;
for(j=1;j<9;j++)
{
if(j<5)
{
PORTA|=(1<<(j-1));
temp2=(50*angle4[i][j+8]/3+750)+(j-1)*3750;
}
PORTC|=(1<<(j-1));
temp1=(50*angle4[i][j]/3+750)+(j-1)*3750;
do
{
temp=TCNT1;
if(temp>temp1)PORTC&=~(1<<(j-1));
if(temp>temp2)PORTA&=~(1<<(j-1));}while(temp<j*3750);
}
TCNT1=0x00;
}
do
{
for(i=cycle_start4;i<totalstep4;i++)
for(t=angle4[i][0];t>0;t--)
{
TCNT1=0x00;
for(j=1;j<9;j++)
{
if(j<5)
{
PORTA|=(1<<(j-1));
temp2=(50*angle4[i][j+8]/3+750)+(j-1)*3750;
}
PORTC|=(1<<(j-1));
temp1=(50*angle4[i][j]/3+750)+(j-1)*3750;
do
{
temp=TCNT1;
if(temp>temp1)PORTC&=~(1<<(j-1));
if(temp>temp2)PORTA&=~(1<<(j-1));
}
while(temp<j*3750);
}
TCNT1=0x00;
}
N4--;
}
while(N4);
}
}
//*******************************************************************************
//函數名: main()
//參 數:
//功 能:
//*******************************************************************************
void main(void)
{
void port_init(void);//I/O端口的初始化
void timer1_init(void); //定時器的初始化
void Motion_init(void);
void ClrAll(void);
void Motion_excute(unsigned int p);
CLI(); //disable all interrupts
MCUCR=0x00;
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
timer1_init();
ClrAll();
TCCR1B = 0x02; //start Timer
Motion_init();
TCNT1=0;
TCNT0=0x00;
Motion_init();
ClrAll();
Motion_excute(1);
Motion_init();
Motion_excute(2);
Motion_init();
Motion_excute(3);
Motion_init();
Motion_excute(4);
Motion_init();
ClrAll();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -