您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > msp430控制步进电机代码
#includemsp430x44x.h#includestring.h#includestdio.h//定义液晶显示笔画#definea(0x01)#defineb(0x02)#definec(0x10)#defined(0x08)#definee(0x40)#definef(0x20)#defineg(0x04)//#defineh(0x80)//显示字符的指针constunsignedcharLCD_Tab[]={a+b+c+d+e+f,//显示0,LCD_Tab[00]b+c,//显示1,LCD_Tab[01]a+b+d+e+g,//显示2,LCD_Tab[02]a+b+c+d+g,//显示3,LCD_Tab[03]b+c+f+g,//显示4,LCD_Tab[04]a+c+d+f+g,//显示5,LCD_Tab[05]a+c+d+e+f+g,//显示6,LCD_Tab[06]a+b+c,//显示7,LCD_Tab[07]a+b+c+d+e+f+g,//显示8,LCD_Tab[08]a+b+c+d+f+g,//显示9,LCD_Tab[09]a+b+c+e+f+g,//显示A,LCD_Tab[0A]c+d+e+f+g,//显示b,LCD_Tab[0B]a+d+e+f,//显示C,LCD_Tab[0C]b+c+d+e+g,//显示d,LCD_Tab[0D]a+d+e+f+g,//显示E,LCD_Tab[0E]a+e+f+g,//显示F,LCD_Tab[0F]0,//显示,LCD_Tab[10]};//步进电机步状态(4相8拍)constunsignedcharSTEP_OUT[]={0x10,//输出000100000x30,//输出001100000x20,//输出001000000x60,//输出011000000x40,//输出010000000xc0,//输出110000000x80,//输出100000000x90,//输出10010000};constunsignedcharSTEP_OUTT[]={0x90,//输出000100000x80,//输出001100000xc0,//输出001000000x40,//输出011000000x60,//输出010000000xc0,//输出110000000x30,//输出100000000x10,//输出10010000};//定义时钟struct{unsignedcharYear;unsignedcharMon;unsignedcharDay;unsignedcharHour;unsignedcharMin;unsignedcharSec;}Clock;unsignedintix;unsignedcharx,p3,Step,key;unsignedintresults;doubledx;intj=800,h,n=410;//intp=200;//v//oiddelay(unsignedinti)//{/intj;//for(i=0;i1000;i++)//{for(j=0;j1000;j++)}//};voidmInitSTDIO()//系统初始化{unsignedchari;//主时钟初始化WDTCTL=WDTPW+WDTHOLD;//stopwatchdogtimerFLL_CTL0|=XCAP14PF;//Configureloadcaps//定时器A初始化TACTL=TASSEL0+TACLR;//ACLK,clearTARTACCTL0=CCIE;//CCR0interruptenabledTACCR0=32768;//定时器常数(1秒)TACTL|=MC0;//StartTimer_ainupmode//定时器B初始化TBCTL=TASSEL0+TACLR;//ACLK,clearTARTBCCTL0=CCIE;//CCR0interruptenabledTBCCR0=328;//定时器常数(1/100秒)TBCTL|=MC0;//P1口初始化P1DIR=0;//设置P1.0~P1.7为输入方式P1IFG=0;//清除P1的中断标志P1IE=0xFF;//设置P1.0~P1.7中断P1IES=0xFF;//设置P1.0~P1.7下降沿中断//P2口初始化P2DIR|=0xCF;//设置P2.0~P2.3,P2.6,P2.7为输出方式//P3口初始化P3DIR=0xFF;//设置P3.0~P3.7为输出方式//P4口初始化P4DIR|=0xFC;//设置P4.2~P4.7为输出方式//P5口初始化P5SEL=0xFF;//P5用于LCD显示//P6口初始化P6SEL=1;//P6.0用于ADCP3DIR|=0xFE;//设置P6.1~P6.7为输出方式(临时)//ADC12初始化ADC12CTL0=ADC12ON+REFON+REF2_5V+SHT0_6;//打开ADC12,2.5V参考电压,设置SHT0=4*ADC12CLK*32ADC12CTL1=SHS_0+SHP+CONSEQ_0;//SHS_0:采样输入信号源选用ADC12SC//SHP:采样输入信号上升沿触发采样定时器,//CONSEQ_0:单通道单次模式,ADC12MCTL0=INCH_8+SREF_1+EOS;//ref+=AVcc,channel=A3,endseq.ADC12IE=0x01;//ADC12.0通道中断使能ADC12CTL0|=ENC;//ADC12转换允许//初始化LCDLCDCTL=LCDON+LCD4MUX+LCDP0;//4-MuxLCD,segmentsS0-S15BTCTL=BTFRFQ1;for(i=0;i8;i++)LCDMEM[i]=0;//清除显示//串行口0(RS-485)初始化UTCTL0=SSEL1;//UCLK=SMCLKUBR00=0x6D;//1048576Hz/9600bps=109.2266=006DhUBR10=0x00;//1048576Hz9600bpsUMCTL0=0x03;//03modulationUCTL0=CHAR;//8-bitcharacter*SWRST*ME1|=UTXE0+URXE0;//EnableUSART0TXD/RXDP2SEL|=0x30;//P2.4,P2.5用于串行口0(RS-232)P2DIR|=0x10;//P2.4outputdirectionIE1|=URXIE0;//EnableUSART0RXinterrupt//串行口1(RS-232)初始化UTCTL1=SSEL1;//UCLK=SMCLKUBR01=0x6D;//1048576Hz/9600bps=109.2266=006DhUBR11=0x00;//1048576Hz9600bpsUMCTL1=0x03;//03modulationUCTL1=CHAR;//8-bitcharacter*SWRST*ME2|=UTXE1+URXE1;//EnableUSART1TXD/RXDP4SEL|=0x03;//P4.0,P4.1用于串行口1(RS-485)P4DIR|=0x01;//P4.0,outputdirectionIE2|=URXIE1;//EnableUSART1RXinterrupt}intputchar(intchar_c)//通过串口输出监控信息{while((IFG2&UTXIFG1)==0);//USART0TXbufferready?TXBUF1=char_c;//chartoTXBUF0return(char_c);}voidDisplayInt(unsignedintData)//显示数据{unsignedcharx;unsignedintdiv_val;div_val=10000;x=4;do{LCDMEM[x]=LCD_Tab[Data/div_val];x--;Data%=div_val;div_val/=10;}while(div_val);return;}voiddelay(intw){w=(w*10000)/32768;}//主程序main(){mInitSTDIO();//系统初始化_EINT();//开启中断for(dx=0;dx1000;dx++);printf(Start\n);while(1){dx=dx*0.6857;results=(unsignedint)dx;DisplayInt(results);switch(key){case0x0:TBCCR0=328-82*3;key=0x16;break;case0x01:TBCCR0=328-82*2;key=0x16;break;case0x02:TBCCR0=328-82*1;key=0x16;break;case0x03:TBCCR0=328;key=0x16;break;case0x04:TBCCR0=328+82*1;key=0x16;break;case0x05:TBCCR0=328+82*2;key=0x16;break;case0x06:TBCCR0=328+82*3;key=0x16;break;case0x0a:h=2;n=410/h;break;case0x0b:h=1;n=410*h;break;case0x0c:h=2;n=410*h;break;case0x14:TBCCTL0=CCIE;break;case0x15:TBCCTL0=0;break;default:break;}/*if(key==0x10){TBCCR0=TBCCR0-82;key=0x16;}elseif(key==0x11){TBCCR0=TBCCR0+82;key=0x16;}/*elseif(key==0x02){h=2;TBCCR0=328+82*h;}elseif(key==0x03){h=3;TBCCR0=328+82*h;}elseif(key==0x04){h=4;TBCCR0=328+82*h;}*//*elseif(key==0x0a){h=1;n=200*h;}elseif(key==0x0b){h=2;n=200*h;}elseif(key==0x0c){h=4;n=200*h;}elseif(key==0x15){TBCCR0=0;key=0x16;}elseif(key==0x14){TBCCR0=328;key=0x16;}/*elseif(key==3){h=4;n=200*h;}elseif(key==4){h=5;n=200*h;}*/ADC12CTL0|=ADC12SC;//Startconversion//_BIS_SR(LPM3_bits);//EnterLPM3_BIS_SR(LPM0_bits);//EnterLPM0}}//TimerA0中断服务程序#pragmavector=TIMERA0_VECTOR__interruptvoidTimer_A0(void){p3=1;if(p3==0)p3=1;P3OUT=p3;//_BIC_SR_IRQ(LPM3_bits);//ClearLPM3bitsfrom0(SR)_BIC_SR_IRQ(LPM0_bits);//ClearLPM0,SETBREAKPOINTHERE}//TimerB0中断服务程序#pragmavector=TIMERB0_VECTOR__interruptvoidTimer_B0(void){x=1;//键盘扫描if((x&0x07)==0)x=1;P2OUT=(~x)&0x07;if(j0){Step++;//步进电机旋转if(Step7)Step=0;P4OUT=STEP_OUT[Step];j--;}if(j=0){Step++;//步进电机旋转if(Step7)Step=0;P4OUT=STEP_OUT[7-Step];j--;if(j-n){j=n;}}//i
本文标题:msp430控制步进电机代码
链接地址:https://www.777doc.com/doc-2889120 .html