广工信工测控数控课设c文件源码.docx_第1页
广工信工测控数控课设c文件源码.docx_第2页
广工信工测控数控课设c文件源码.docx_第3页
广工信工测控数控课设c文件源码.docx_第4页
广工信工测控数控课设c文件源码.docx_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include #include #include funs.h/* * 名称: motor.c * 功能: XY轴电机正反转*/void DelayMs(int z) /延时msint x,y;for(x=z;x0;x-) for(y=12000;y0;y-);void motor_init()PINSEL1 &=(0xFF22);IO0DIR |=(0xF12);IO0DIR |=(0xF27);void motor_x_p() /x电机正转if(8=k_p)k_p=0;IO0CLR=0xF12;IO0SET=(motor_pk_p12);k_p+;void motor_x_r() /x电机反转if(8=k_r)k_r=0;IO0CLR=0xF12;IO0SET=(motor_rk_r12);k_r+;void motor_y_r() /y反转if(8=j_r)j_r=0;IO0CLR=0xF27;IO0SET=(motor_rj_r27);j_r+;void motor_y_p() /y正转if(8=j_p)j_p=0;IO0CLR=0xF27;IO0SET=(motor_pj_p27);j_p+;/* * 名称: UART.c * 功能: UART初始化和设置*/void UART_init(void)uint16 Fdiv;PINSEL0|=0x05;U0LCR=0x83;Fdiv=(Fpclk/16)/UART_BPS;U0DLM=Fdiv/256;U0DLL=Fdiv%256;U0LCR=0x03;void UART0_SendByte(uint8 data)U0THR=data;while(U0LSR&0x40)=0);void UART0_SendStr(int8 strs)uint32 j=0;while(strsj!=0)UART0_SendByte(strj);j+;uint8 UART0_RcvByte(void)uint8 rcv_byte; while(U0LSR&0x01)=0);rcv_byte= U0RBR;/rcv_byte=rcv_byte-0x30;return (rcv_byte);/* * 名称: led.c * 功能: led*/ void show_init()/led管脚的初始化IODIR0|=(0x7FF1);IOCLR0 = 0x7FF1; /IOSET0=0x7FF1;/while(1); void show_led(uint8 data,uint8 bit)IOSET0= 0x7FF1;IOCLR0= (numdata1|selectbit8);/*在led灯中显示x和y值*/void show_x_y(uint8 x,uint8 y)uint8 x_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一样*/void led_init()IODIR0|=(0x3FF2);IOCLR0 = (0x3FF2);void led_show_x(uint8 x)IOCLR0=0x1F2; x &= 0x1f;IOSET0=(x2);void led_show_y(uint8 y)IOCLR0=0x1F7;y &=0x1f;IOSET0=(y7);/* 按键扫描程序*/void key_init()PINSEL1&=(0x30);PINSEL1|=(0x010);/P0.16为外部中断0EXTMODE|=(10);VICVectCntl0=0x2E;VICVectAddr0 = (unsigned)key_irq;VICIntEnable = 0x6000;/*扫描键盘的列,返回列的序号03*/uint8 row_scan()IO0CLR=0x0F22;IO0SET=0X0E18)&0x0F;/p0.1821,行的状态,本应全为1if(0x0f!=key_null)while(0x0f!=key_null)/等待按键放开key_null= (IO0PIN18)&0x0F;return 0;/返回列序号IO0CLR=0x0F22;IO0SET=0X0D18)&0x0F;if(0x0f!=key_null)while(0x0f!=key_null)key_null= (IO0PIN18)&0x0F;return 1;IO0CLR=0x0F22;IO0SET=0X0B18)&0x0F;if(0x0f!=key_null)while(0x0f!=key_null)key_null= (IO0PIN18)&0x0F;return 2;IO0CLR=0x0F22;IO0SET=0X0718)&0x0F;if(0x0f!=key_null)while(0x0f!=key_null)key_null= (IO0PIN18)&0x0F;return 3;/*扫描键盘的值,返回按键号real_row*/uint8 key_scan()if(0x0e=key_null)/line1有按键real_row=row_scan();/得到列号return real_row;if(0x0d=key_null) /line2real_row=row_scan();return(real_row+4);if(0x0b=key_null) /line3real_row=row_scan();return(real_row+8);if(0x07=key_null)/line4real_row=row_scan();return(real_row+12);/*中断程序,有按键时运行*/void key_irq() _irq/UART0_SendByte(8);key_flag=1;EXTINT =0x01;VICVectAddr = 0x00000000;/wait the next buttonvoid wait_next()uint8 next;donext =(IOPIN116)&0x01;while(next); /等next按下才继续运行/wait the clear buttonvoid wait_clear()uint8 next,clear;donext =(IOPIN116)&0x01;clear =(IOPIN118)&0x01;if(!clear)/若clear键按下set_line(0);clearscreen(1); clearscreen(2); /重新显示坐标轴,即清除图像while(next&clear); /next和clear其中一个按下继续运行/wait the CONTINUS buttonvoid wait_continus()uint8 con;docon =(IOPIN117)&0x01;while(con); /等continus键按下/*G01直线插补,x,y,f分别为,终点坐标(x,y)和转速 f,起点为(0,0) */void G01(uint16 x,uint16 y,uint16 f) float f_i,f_new,k_g;uint16 x_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;i=0)/进给+xx_i +=1;f_new=f_i-k_g;motor_x_p();/x电机正转一步k_x+;if(8=k_x) /若够一圈 k_x=0;show_x+; /x的圈数加1led_show_x(show_x); /led灯显示x的圈数else /进给+yy_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逆圆插补*/void G03(uint16 x1,uint16 y1,uint16 x2,uint16 y2,uint16 f)uint16 k,x_i,y_i,number,show_time,k_x,k_y,show_x,show_y;float f_i,new_f_i;show_x=0;x_i=x1;y_i=y1;k_x=0;k_y=0;f_i=0;show_time=(1000*60)/(8*f);number=x1+y2-x2-y1; /总插补次数numberfor(k=0;k=0)new_f_i=f_i-2*x_i+1;x_i-;k_x+;motor_x_r();if(8=k_x)k_x=0;show_x+;led_show_x(show_x);elsenew_f_i=f_i+2*y_i+1;y_i+;k_y+;motor_y_p();if(8=k_y)k_y=0;show_y+;led_show_y(show_y);f_i=new_f_i;DelayMs(show_time);wait_continus();/*G02顺圆插补*/void G02(uint16 x1,uint16 y1,uint16 x2,uint16 y2,uint16 f)uint16 k,x_i,y_i,number,show_time,k_x,k_y,show_x,show_y;float f_i,new_f_i;k_x=0;k_y=0;show_x=0; show_y=0;x_i=x1;y_i=y1;f_i=0;show_time=(1000*60)/(8*f);number=x2+y1-x1-y2; /总次数for(k=0;k=0)new_f_i=f_i-2*y_i+1;y_i-;k_y+;motor_y_r();if(8=k_y)k_y=0;show_y+;led_show_y(show_y);elsenew_f_i=f_i+2*x_i+1;x_i+;k_x+;motor_x_p();if(8=k_x)k_x=0;show_x+;led_show_x(show_x);f_i=new_f_i;DelayMs(show_time);wait_continus();/*检测G代码,返回G值*/uint16 check_g(uint8 a40)uint16 check_k,g_value=0;for(check_k=0;check_k40;check_k+)if(G=button_numberacheck_k)/检查按键是否为Gcheck_k+=2;/检查是G01,或G02,或G03g_value=(button_numberacheck_k-48);return g_value;/返回1,或2,或3/*检测G01中的x值*/uint16 check_x(uint8 a40)uint16 check_k,x_value=0,temp;for(check_k=0;check_k127)/UART0_SendStr(str);x_value=127;return x_value; /*检测G01中的y值*/uint16 check_y(uint8 a40)uint16 check_k,y_value=0,temp;for(check_k=0;check_k63)/UART0_SendStr(str);y_value=63;return y_value;/*检测速度f的值*/uint16 check_f(uint8 a40)uint16 check_k,f_value=0,temp;for(check_k=0;check_k40;check_k+)if(F=button_numberacheck_k)check_k+;for(; !=button_numberacheck_k;check_k+)temp=(button_numberacheck_k-48);f_value= 10*f_value+temp;return f_value; /*检测G02 G03中的x值*/uint16 check_x1(uint8 a40)uint16 check_k,x1_value=0,temp=0;for(check_k=0;check_k127)/UART0_SendStr(str);x1_value=127;return x1_value; uint16 check_x2(uint8 a40)uint16 check_k,x2_value=0,temp=0;for(check_k=0;check_k127)/UART0_SendStr(str);x2_value=127;return x2_value; /*检测G02 G03中的y值*/uint16 check_y1(uint8 a40)uint16 check_k,y1_value=0,temp=0;for(check_k=0;check_k63)/UART0_SendStr(str);y1_value=63;return y1_value; uint16 check_y2(uint8 a40)uint16 check_k,y2_value=0,temp=0;for(check_k=0;check_k63)/UART0_SendStr(str);y2_value=63;return y2_value; /* 译码与执行插补*/void operate(uint8 a40)uint16 operate_x,operate_y,operate_f,operate_g;uint16 operate_x1,operate_x2,operate_y1,operate_y2;operate_g=check_g(a);switch(operate_g)case 1:operate_x=check_x(a); /得到终点坐标(x,y)和转速foperate_y=check_y(a);operate_f=check_f(a);G01(operate_x,operate_y,operate_f); /画直线break;case 2:operate_x1=check_x1(a);operate_x2=check_x2(a);operate_y1=check_y1(a);operate_y2=check_y2(a);operate_f=check_f(a);G02(operate_x1,operate_y1,operate_x2,operate_y2,operate_f);/G02(0,60,60,0,300); /画顺圆break;case 3:operate_x1=check_x1(a);operate_x2=check_x2(a);operate_y1=check_y1(a);operate_y2=check_y2(a);operate_f=check_f(a);G03(operate_x1,operate_y1,operate_x2,operate_y2,operate_f);break; /画逆圆default :break;/*清除数组*/void clear_number(uint8 a40)uint8 k;for(k=0;k40;k+)ak= ;DelayMs(5);/*main函数*/int main()i=0;motor_init(); /x;p0.1215,y:p0.2730,八个管脚都是输出led_init(); /p0.211为X.Y值得显示引脚,初始化为全灭UART_init();key_init(); /p0.16为外部中断0IO_set();/p1.2731为输出lcd_init();/开显示,起始行为0行clearscreen(0);set_line(0);show_name();/显示姓名信息selectscreen(0);for(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论