中级会员
积分 373
威望 259
贡献 92
兑换币 52
注册时间 2009-1-3
在线时间 11 小时
1 贡献
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#define LINE_MAX 107 // 每行采集的点数,即列数
#define ROW_MAX 2 // 采集到的行数
unsigned char Image[ROW_MAX][LINE_MAX];//图像数据。
//static unsigned int rowjd;//记录行中断
//static unsigned char space;
unsigned char g_SampleFlag=0,f;
void PWMout(int Direction, int Velocity);
void Init_PWMout(void);
int SignalProcess( unsigned int signal );
void SignalTest(void);
void DV_set(void);
int Direction;
int Velocity;
/***************************************************************/
void Init_PWMout(void)
{
PWME = 0x22; /*01:50Hz 45:1kHz */
PWMPOL = 0x22;
PWMCTL = 0x50;
PWMCLK = 0x02;
PWMSCLA = 10;
}
/***************************************************************/
void PWMout(int Direction, int Velocity)
{
Direction = Direction/3 + 1800;
Direction = Direction+20;
//if(Direction<1200) Direction=1200;
//if(Direction>1800) Direction=1800;
PWMPER01 = 60000; /* Center 1500ms*3 */
PWMDTY01 = Direction; /* 设置舵机角度 */
if(Velocity>24000) Velocity=24000;
PWMPER45 = 24000; /* 1kHz ( <10kHz ) */
PWMDTY45 = Velocity; /* 设置电机速度 */
}
/************************************************************************************/
/***************************行场中断初始化************************************/
void vinterruptsinit(void)
{
TIOS=0x00; //外部输入捕捉0,1通道
TCTL4=0x09; //通道0上升沿触发,通道1下降沿触发
TSCR1=0x80;
TIE_C1I = 1;
TIE_C0I = 1;
}
/******************************倍频*****************************************/
void PLLInit(void)
{ //BUS-CLOCK=PLL-CLOCK/2=24M
REFDV = 1;
SYNR =2;
while (!CRGFLG_LOCK); // wait here till the PLL is locked.
CLKSEL|=0x80; // switch the bus clock to the PLL.
}
/***************************IO口初始化************************************/
void IOPortInit(void)
{
DDRB=0X00;
}
/*-------串口初始化----------------------*/
void SciInit(){
SCI0BDL = (unsigned char)((24000000UL /* OSC freq */ ) / 9600 /* baud rate */ / 16 /*factor*/);
SCI0CR1=0; /*normal,no parity*/
SCI0CR2=0X2C; /*RIE=1,TE=1,RE=1*/
}
/*-------发射端程序----------------------*/
void SciTx(unsigned char text){
while (!(SCI0SR1&0x80)); /* wait for output buffer empty */
SCI0DRH=0;
SCI0DRL=text;
}
void Delay(int delay)
{ int i,j;
for(i=0;i<delay;i++)
{for(j=0;j<10;j++)
asm nop;
}
}
/************************************主函数************************************/
void main(void)
{
DisableInterrupts;
PLLInit();
vinterruptsinit();
IOPortInit();
Init_PWMout() ;
SciInit();
EnableInterrupts;
/* Delay(4000);
for(i=0;i<ROW_MAX;i++)
{ space=0xee;
for(j=0;j<LINE_MAX;j++)
SciTx(Image[i][j]);
for(k=0;k<10;k++)
SciTx(space);
} */
for(;;)
{
SignalTest();
DV_set();
PWMout(Direction,Velocity);
}
}
/************************************************************************/
void DV_set(void)
{
switch(f)
{ case 0x01 irection=1200 ; Velocity=4000 ;break;
case 0x02:Direction=800 ; Velocity=6000 ;break;
case 0x04:Direction=400 ; Velocity=8000 ;break;
case 0x08:Direction=100 ; Velocity=10000 ;break;
case 0x10:Direction=-100; Velocity=10000 ;break;
case 0x20:Direction=-400 ;Velocity=8000 ;break;
case 0x40:Direction=-800 ;Velocity=4000 ;break;
case 0x80:Direction=-1200; Velocity=6000 ;break;
default: break;
}
}
/************************************************************************/
void SignalTest(void)
{
if(Image[1][65]<0x18)
{
f=0x01;
}
if(Image[1][70]<0x18)
{
f=0x02;
}
if(Image[1][75]<0x18)
{
f=0x04;
}
if(Image[1][80]<0x18)
{
f=0x08;
}
if(Image[1][90]<0x18)
{
f=0x10;
}
if(Image[1][95]<0x18)
{
f=0x20;
}
if(Image[1][100]<0x18)
{
f=0x40;
}
if(Image[1][104]<0x18)
{
f=0x80;
}
}
/*****************************图像获得 *************************************/
/******************************行中断*****************************************/
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 8 Port0_interrupt(void)
{
TFLG1=0x01; //清除行中断标志
if ( g_SampleFlag == 0 )
{
Delay(1);
return;
}
TIE_C1I = 0;
Image[1][0] = PORTB;
Image[1][1] = PORTB;
Image[1][2] = PORTB;
Image[1][3] = PORTB;
Image[1][4] = PORTB;
Image[1][5] = PORTB;
Image[1][6] = PORTB;
Image[1][7] = PORTB;
Image[1][8] = PORTB;
Image[1][9] = PORTB;
Image[1][10] = PORTB;
Image[1][11] = PORTB;
Image[1][12] = PORTB;
Image[1][13] = PORTB;
Image[1][14] = PORTB;
Image[1][15] = PORTB;
Image[1][16] = PORTB;
Image[1][17] = PORTB;
Image[1][18] = PORTB;
Image[1][19] = PORTB;
Image[1][20] = PORTB;
Image[1][21] = PORTB;
Image[1][22] = PORTB;
Image[1][23] = PORTB;
Image[1][24] = PORTB;
Image[1][25] = PORTB;
Image[1][26] = PORTB;
Image[1][27] = PORTB;
Image[1][28] = PORTB;
Image[1][29] = PORTB;
Image[1][30] = PORTB;
Image[1][31] = PORTB;
Image[1][32] = PORTB;
Image[1][33] = PORTB;
Image[1][34] = PORTB;
Image[1][35] = PORTB;
Image[1][36] = PORTB;
Image[1][37] = PORTB;
Image[1][38] = PORTB;
Image[1][39] = PORTB;
Image[1][40] = PORTB;
Image[1][41] = PORTB;
Image[1][42] = PORTB;
Image[1][43] = PORTB;
Image[1][44] = PORTB;
Image[1][45] = PORTB;
Image[1][46] = PORTB;
Image[1][47] = PORTB;
Image[1][48] = PORTB;
Image[1][49] = PORTB;
Image[1][50] = PORTB;
Image[1][51] = PORTB;
Image[1][52] = PORTB;
Image[1][53] = PORTB;
Image[1][54] = PORTB;
Image[1][55] = PORTB;
Image[1][56] = PORTB;
Image[1][57] = PORTB;
Image[1][58] = PORTB;
Image[1][59] = PORTB;
Image[1][60] = PORTB;
Image[1][61] = PORTB;
Image[1][62] = PORTB;
Image[1][63] = PORTB;
Image[1][64] = PORTB;
Image[1][65] = PORTB;
Image[1][66] = PORTB;
Image[1][67] = PORTB;
Image[1][68] = PORTB;
Image[1][69] = PORTB;
Image[1][70] = PORTB;
Image[1][71] = PORTB;
Image[1][72] = PORTB;
Image[1][73] = PORTB;
Image[1][74] = PORTB;
Image[1][75] = PORTB;
Image[1][76] = PORTB;
Image[1][77] = PORTB;
Image[1][78] =PORTB;
Image[1][79] = PORTB;
Image[1][80] = PORTB;
Image[1][81] = PORTB;
Image[1][82] = PORTB;
Image[1][83] = PORTB;
Image[1][84] = PORTB;
Image[1][85] = PORTB;
Image[1][86] = PORTB;
Image[1][87] = PORTB;
Image[1][88] = PORTB;
Image[1][89] = PORTB;
Image[1][90] = PORTB;
Image[1][91] = PORTB;
Image[1][92] = PORTB;
Image[1][93] = PORTB;
Image[1][94] = PORTB;
Image[1][95] = PORTB;
Image[1][96] = PORTB;
Image[1][97] = PORTB;
Image[1][98] = PORTB;
Image[1][99] = PORTB;
Image[1][100] = PORTB;
Image[1][101] = PORTB;
Image[1][102] = PORTB;
Image[1][103] = PORTB;
Image[1][104] = PORTB;
Image[1][105] = PORTB;
Image[1][106] = PORTB;
SignalTest();
DV_set();
PWMout(Direction,Velocity);
g_SampleFlag = 0;
TIE_C2I = 1;
}
/**********场中断处理******************************************/
//#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 9 Port1_interrupt(void)
{
TFLG1=0x02;
g_SampleFlag = 1;
}
我来回答
附件:
您需要 登录 才可以下载或查看,没有帐号?注册