#includeLPC21xx.h#includemath.h#includefuns.h/*****************************************************************************名称:motor.c*功能:XY轴电机正反转****************************************************************************/voidDelayMs(intz)//延时ms{intx,y;for(x=z;x0;x--)for(y=12000;y0;y--);}voidmotor_init(){PINSEL1&=~(0xFF22);IO0DIR|=(0xF12);IO0DIR|=(0xF27);}voidmotor_x_p()//x电机正转{if(8==k_p){k_p=0;}IO0CLR=0xF12;IO0SET=(motor_p[k_p]12);k_p++;}voidmotor_x_r()//x电机反转{if(8==k_r){k_r=0;}IO0CLR=0xF12;IO0SET=(motor_r[k_r]12);k_r++;}voidmotor_y_r()//y反转{if(8==j_r){j_r=0;}IO0CLR=0xF27;IO0SET=(motor_r[j_r]27);j_r++;}voidmotor_y_p()//y正转{if(8==j_p){j_p=0;}IO0CLR=0xF27;IO0SET=(motor_p[j_p]27);j_p++;}/*****************************************************************************名称:UART.c*功能:UART初始化和设置****************************************************************************/voidUART_init(void){uint16Fdiv;PINSEL0|=0x05;U0LCR=0x83;Fdiv=(Fpclk/16)/UART_BPS;U0DLM=Fdiv/256;U0DLL=Fdiv%256;U0LCR=0x03;}voidUART0_SendByte(uint8data){U0THR=data;while((U0LSR&0x40)==0);}voidUART0_SendStr(int8strs[]){uint32j=0;while(strs[j]!='\0'){UART0_SendByte(str[j]);j++;}}uint8UART0_RcvByte(void){uint8rcv_byte;while((U0LSR&0x01)==0);rcv_byte=U0RBR;//rcv_byte=rcv_byte-0x30;return(rcv_byte);}/*****************************************************************************名称:led.c*功能:led****************************************************************************/voidshow_init()//led管脚的初始化{IODIR0|=(0x7FF1);IOCLR0=0x7FF1;//IOSET0=0x7FF1;//while(1);}voidshow_led(uint8data,uint8bit){IOSET0=0x7FF1;IOCLR0=~(num[data]1|select[bit]8);}/*****在led灯中显示x和y值*******/voidshow_x_y(uint8x,uint8y){uint8x_data_bit,x_data_rem,y_data_bit,y_data_rem;x_data_bit=x/10;show_led(x_data_bit,1);DelayMs(1);x_data_rem=x%10;show_led(x_data_rem,2);DelayMs(1);y_data_bit=y/10;show_led(y_data_bit,3);DelayMs(1);y_data_rem=y%10;show_led(y_data_rem,4);DelayMs(1);}/*******led显示x和y的值,跟show一样*********/voidled_init(){IODIR0|=(0x3FF2);IOCLR0=(0x3FF2);}voidled_show_x(uint8x){IOCLR0=0x1F2;x&=0x1f;IOSET0=(x2);}voidled_show_y(uint8y){IOCLR0=0x1F7;y&=0x1f;IOSET0=(y7);}/****************************************************************************按键扫描程序*****************************************************************************/voidkey_init(){PINSEL1&=~(0x30);PINSEL1|=(0x010);//P0.16为外部中断0EXTMODE|=(10);VICVectCntl0=0x2E;VICVectAddr0=(unsigned)key_irq;VICIntEnable=0x6000;}/****扫描键盘的列,返回列的序号0~3************************/uint8row_scan(){IO0CLR=0x0F22;IO0SET=0X0E22;//ROW0key_null=(IO0PIN18)&0x0F;//p0.18~21,行的状态,本应全为1if(0x0f!=key_null){while(0x0f!=key_null)//等待按键放开{key_null=(IO0PIN18)&0x0F;}return0;//返回列序号}IO0CLR=0x0F22;IO0SET=0X0D22;//ROW1key_null=(IO0PIN18)&0x0F;if(0x0f!=key_null){while(0x0f!=key_null){key_null=(IO0PIN18)&0x0F;}return1;}IO0CLR=0x0F22;IO0SET=0X0B22;//ROW2key_null=(IO0PIN18)&0x0F;if(0x0f!=key_null){while(0x0f!=key_null){key_null=(IO0PIN18)&0x0F;}return2;}IO0CLR=0x0F22;IO0SET=0X0722;//ROW3key_null=(IO0PIN18)&0x0F;if(0x0f!=key_null){while(0x0f!=key_null){key_null=(IO0PIN18)&0x0F;}return3;}}/****扫描键盘的值,返回按键号real_row***************/uint8key_scan(){if(0x0e==key_null)//line1有按键{real_row=row_scan();//得到列号returnreal_row;}if(0x0d==key_null)//line2{real_row=row_scan();return(real_row+4);}if(0x0b==key_null)//line3{real_row=row_scan();return(real_row+8);}if(0x07==key_null)//line4{real_row=row_scan();return(real_row+12);}}/*******中断程序,有按键时运行******************************/voidkey_irq()__irq{//UART0_SendByte(8);key_flag=1;EXTINT=0x01;VICVectAddr=0x00000000;}//waitthenextbuttonvoidwait_next(){uint8next;do{next=(IOPIN116)&0x01;}while(next);//等next按下才继续运行}//waittheclearbuttonvoidwait_clear(){uint8next,clear;do{next=(IOPIN116)&0x01;clear=(IOPIN118)&0x01;if(!clear)//若clear键按下{set_line(0);clearscreen(1);clearscreen(2);//重新显示坐标轴,即清除图像}}while(next&clear);//next和clear其中一个按下继续运行}//waittheCONTINUSbuttonvoidwait_continus(){uint8con;do{con=(IOPIN117)&0x01;}while(con);//等continus键按下}/**********************************************************G01直线插补,x,y,f分别为,终点坐标(x,y)和转速f,起点为(0,0)***********************************************************/voidG01(uint16x,uint16y,uint16f){floatf_i,f_new,k_g;uint16x_i,y_i,number,i,show_time,k_x,k_y,show_x,show_y;number=y+x;show_x=0;show_y=0;k_x=0;k_y=0;x_i=0;//起点坐标y_i=0;f_i=0;//斜率kk_g=(float)y/x;for(i=0;inumber;i++)//插补x+y次{show_time=(1000*60)/(8*f);//电机一步的时间(ms)draw_dot(x_i,y_i);if(f_i=0)//进给+x{x_i+=1;f_new=f_i-k_g;motor_x_p();//x电机正转一步k_x++;if(8==k_x)//若够一圈{k_x=0;show_x++;//x的圈数加1}led_show_x(show_x);//led灯显示x的圈数}else//进给+y{y_i+=1;f_new=f_i+1;motor_y_p();k_y++;if(8==k_y){k_y=0;show_y++;}led_show_y(show_y);}DelayMs(show_time);//延时电机转一步的时间(ms)f_i=f_new;wait_continus();//是否继续,continus按下继续}}/***************G03逆圆插补********************************************************/voidG03(uint16x1,uint16y1,uint16x2,uint16y2,uint16f){uint16k,x_i,y_i,number,show_time,k_x,k_y,show_x,show_y;floatf_i,new_f_i;show