《实验406DSP直流电机控制实验教学文案.doc》由会员分享,可在线阅读,更多相关《实验406DSP直流电机控制实验教学文案.doc(30页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Good is good, but better carries it.精益求精,善益求善。实验406DSP直流电机控制实验-姓名吴镌樊想孙璐陈玉枫学号0816070110081607020208160702010816070203专业自动化时间2011/12/7成绩实验名称直流电机控制实验实验内容通过C语言编程控制控制I/O管脚产生不同占空比的PWM信号,从而控制直流电机的转速实验目的和要求1学习用C语言编制中断程序,控制F2812DSP通用I/O管脚产生不同占空比的PWM信号。2学习F2812DSP的通用I/O管脚的控制方法。3学习直流电机的控制原理和控制方法。实验设备计算机,ICETEK
2、-F2812-EDU实验箱(或ICETEK仿真器+ICETEKF2812-A系统板+相关连线及电源)。实验原理1TMS320F2812DSP的McBSP引脚通过设置PWM11和PWM5的工作方式和状态,可以实现将它们当成通用I/O引脚使用。2直流电机控制直流电动机是最早出现的电动机,也是最早能实现调速的电动机。近年来,直流电动机的结构和控制方式都发生了很大的变化。随着计算机进入控制领域,以及新型的电力电子功率元器件的不断出现,使采用全控型的开关功率元件进行脉宽调制(PulsWidthModulation,简称PWM)控制方式已成为绝对主流。PWM调压调速原理直流电动机转速n的表达式为:=KnU
3、IR其中,U为电枢端电压;I为电枢电流;R为电枢电路总电阻;为每极磁通量;K为电动机结构参数。所以直流电动机的转速控制方法可分为两类:对励磁磁通进行控制的励磁控制法和对电枢电压进行控制的电枢控制法。其中励磁控制法在低速时受磁极饱和的限制,在高速时受换向火花和换向器结构强度的限制,并且励磁线圈电感较大,动态响应较差,所以这种控制方法用得很少。现在,大多数应用场合都使用电枢控制法。绝大多数直流电机采用开关驱动方式。开关驱动方式是使半导体功率器件工作在开关状态,通过脉宽调制PWM来控制电动机电枢电压,实现调速。上图是利用开关管对直流电动机进行PWM调速控制的原理图和输入输出电压波形。图中,当开关管M
4、OSFET的栅极输入高电平时,开关管导通,直流电动机电枢绕组两端有电压Us。t1秒后,栅极输入变为低电平,开关管截止,电动机电枢两端电压为0。t2秒后,栅极输入重新变为高电平,开关管的动作重复前面的过程。这样,对应着输入的电平高低,直流电动机电枢绕组两端的电压波形如图中所示。电动机的电枢绕组两端的电压平均值Uo为SSSOUUTttttUU=+=11210式中为占空比,=t1/T占空比表示了在一个周期T里,开关管导通的时间与周期的比值。的变化范围为01。由此式可知,当电源电压Us不变的情况下,电枢的端电压的平均值Uo取决于占空比的大小,改变值就可以改变端电压的平均值,从而达到调速的目的,这就是P
5、WM调速原理。PWM调速方法:在PWM调速时,占空比是一个重要参数。以下3种方法都可以改变占空比的值:(1)定宽调频法:这种方法是保持t1不变,只改变t2,这样使周期T(或频率)也随之改变。(2)调宽调频法:这种方法是保持t2不变,只改变t1,这样使周期T(或频率)也随之改变。(3)定频调宽法:这种方法是使周期T(或频率)保持不变,而改变t1和t2。前两种方法由于在调速时改变了控制脉冲的周期(或频率),当控制脉冲的频率与系统的固有频率接近时,将会引起震荡,因此这两种方法用得很少。目前,在直流电动机的控制中,主要使用定频调宽法图中PWM输入对应ICETEKF2812-A评估板上P4外扩插座第26
6、引脚的PWM11信号,DSP将在此引脚上给出PWM信号用来控制直流电机的转速;图中的DIR输入对应ICETEKF2812-A评估板上P1外扩插座第6引脚的P4信号,DSP将在此引脚上给出高电平或低电平来控制直流电机的方向。从DSP输出的PWM信号和转向信号先经过2个与门和1个非门再与各个开关管的栅极相连。控制原理当电动机要求正转时,PWM11给出高电平信号,该信号分成3路:第1路接与门Y1的输入端,使与门Y1的输出由PWM决定,所以开关管V1栅极受PWM控制;第2路直接与开关管V4的栅极相连,使V4导通;第3路经非门F1连接到与门Y2的输入端,使与门Y2输出为0,这样使开关管V3截止;从非门F
7、1输出的另一路与开关管V2的栅极相连,其低电平信号也使V2截止。同样,当电动机要求反转时,PWM5给出低电平信号,经过2个与门和1个非门组成的逻辑电路后,使开关管V3受PWM信号控制,V2导通,V1、V4全部截止。4程序编制程序中采用定时器中断产生固定频率的PWM波,在每个中断中根据当前占空比判断应输出波形的高低电平。主程序用轮询方式读入键盘输入,得到转速和方向控制命令。在改变电机方向时为减少电压和电流的波动采用先减速再反转的控制顺序。实验设计及调试:(1)对实验内容和实验原理进行分析,理出完成实验的设计思路。通过C语言编程改变pwm波的占空比,将此pwm波从I/O口输出到直流电动机,从而改变
8、其转速,通过引脚上给出高电平或低电平和逻辑电路来控制直流电机的方向。(2)列出设计所需的特殊环节1.pwm初始化2.电机使能3.键盘实时监测4.中断调用(3)画出设计流程图。(4)调试程序#includeDSP281x_Device.h/DSP281xHeaderfileIncludeFile#includeDSP281x_Examples.h/DSP281xExamplesIncludeFile/Prototypestatementsforfunctionsfoundwithinthisfile.interruptvoidcpu_timer0_isr(void);voidDelay(unsi
9、gnedintnTime);voidGpio_select(void);voiderror(int);voidprogram_stop();voidGpio_PortA(void);voidGpio_PortB(void);voidGpio_PortF(void);voidGpio_PortDEG(void);charConvertScanToChar(unsignedcharcScanCode);voidRefreshLEDArray();/刷新显示voidSetLEDArray(intnNumber);/修改显示内容#defineT46uS0x0d40#defineSCANCODE_00x
10、70#defineSCANCODE_10x69#defineSCANCODE_20x72#defineSCANCODE_30x7A#defineSCANCODE_40x6B#defineSCANCODE_50x73#defineSCANCODE_60x74#defineSCANCODE_70x6C#defineSCANCODE_80x75#defineSCANCODE_90x7D#defineSCANCODE_Del0x49#defineSCANCODE_Enter0x5A#defineSCANCODE_Plus0x79#defineSCANCODE_Minus0x7B#defineSCANC
11、ODE_Mult0x7C#defineSCANCODE_Divid0x4A#defineSCANCODE_Num0x77#defineCTRGR*(int*)0x108000#defineCTRLCDCMDR*(int*)0x108001#defineCTRKEY*(int*)0x108001#defineCTRLCDCR*(int*)0x108002#defineCTRCLKEY*(int*)0x108002#defineCTRLCDLCR*(int*)0x108003#defineCTRLCDRCR*(int*)0x108004#defineCTRLA*(int*)0x108005#def
12、ineCTRLR*(int*)0x108007Uint16var1=0;Uint16var2=0;Uint16var3=0;Uint16test_count=0;Uint16Test_flag=0;Uint16Test_var=0;Uint16Test_status32;Uint16PASS_flag=0;unsignedintuWork;intjishu=0;unsignedintuWork,nCount=0,uN,uN1,nCount1,nDir;unsignedintuPort8000;unsignedintnScreenBuffer1024;unsignedcharledbuf8,le
13、dx8;unsignedcharledkey108=0x00,0x00,0x7C,0x82,0x82,0x82,0x7C,0x00,0x00,0x00,0x00,0x84,0xFE,0x80,0x00,0x00,/10x00,0x00,0x84,0xC2,0xA2,0x92,0x8C,0x00,/20x00,0x00,0x44,0x92,0x92,0x92,0x6C,0x00,0x00,0x00,0x30,0x28,0x24,0xFE,0x20,0x00,0x00,0x00,0x4E,0x92,0x92,0x92,0x62,0x00,0x00,0x00,0x7C,0x92,0x92,0x92,
14、0x64,0x00,0x00,0x00,0x02,0xC2,0x32,0x0A,0x06,0x00,0x00,0x00,0x6C,0x92,0x92,0x92,0x6C,0x00,0x00,0x00,0x4C,0x92,0x92,0x92,0x7C,0x00;voidmain(void)/intnCount=0;charcKey,cOldKey;unsignedintnScanCode,nKeyCode;unsignedintnSpeed;/Step1.InitializeSystemControl:/PLL,WatchDog,enablePeripheralClocks/Thisexampl
15、efunctionisfoundintheDSP281x_SysCtrl.cfile.InitSysCtrl();/Step2.InitalizeGPIO:/ThisexamplefunctionisfoundintheDSP281x_Gpio.cfileand/illustrateshowtosettheGPIOtoitsdefaultstate./InitGpio();/Skippedforthisexample/Step3.ClearallinterruptsandinitializePIEvectortable:/DisableCPUinterruptsDINT;/Initialize
16、thePIEcontrolregisterstotheirdefaultstate./ThedefaultstateisallPIEinterruptsdisabledandflags/arecleared./ThisfunctionisfoundintheDSP281x_PieCtrl.cfile.InitPieCtrl();StopCpuTimer0();/DisableCPUinterruptsandclearallCPUinterruptflags:IER=0x0000;IFR=0x0000;/InitializethePIEvectortablewithpointerstothesh
17、ellInterrupt/ServiceRoutines(ISR)./Thiswillpopulatetheentiretable,eveniftheinterrupt/isnotusedinthisexample.Thisisusefulfordebugpurposes./TheshellISRroutinesarefoundinDSP281x_DefaultIsr.c./ThisfunctionisfoundinDSP281x_PieVect.c.InitPieVectTable();/Interruptsthatareusedinthisexamplearere-mappedto/ISR
18、functionsfoundwithinthisfile.EALLOW;/ThisisneededtowritetoEALLOWprotectedregistersPieVectTable.TINT0=&cpu_timer0_isr;EDIS;/ThisisneededtodisablewritetoEALLOWprotectedregisters/Step4.InitializealltheDevicePeripherals:/ThisfunctionisfoundinDSP281x_InitPeripherals.c/InitPeripherals();/Notrequiredforthi
19、sexample/InitCpuTimers();/Forthisexample,onlyinitializetheCpuTimersCpuTimer0.RegsAddr=&CpuTimer0Regs;/Initializetimerperiodtomaximum:/CpuTimer0Regs.PRD.all=musicnCount0*450;CpuTimer0Regs.PRD.all=0x3000;/Initializepre-scalecountertodivideby1(SYSCLKOUT):CpuTimer0Regs.TPR.all=0;CpuTimer0Regs.TIM.all=0;
20、CpuTimer0Regs.TPRH.all=0;/Makesuretimerisstopped:CpuTimer0Regs.TCR.bit.TSS=1;CpuTimer0Regs.TCR.bit.SOFT=1;CpuTimer0Regs.TCR.bit.FREE=1;/Reloadallcounterregisterwithperiodvalue:CpuTimer0Regs.TCR.bit.TRB=1;CpuTimer0Regs.TCR.bit.TIE=1;/Resetinterruptcounters:CpuTimer0.InterruptCount=0;/Step5.Userspecif
21、iccode,enableinterrupts:/EnableCPUINT1whichisconnectedtoCPU-Timer0:IER|=M_INT1;/EnableTINT0inthePIE:Group1interrupt7PieCtrlRegs.PIEIER1.bit.INTx7=1;/EnableglobalInterruptsandhigherpriorityreal-timedebugevents:EINT;/EnableGlobalinterruptINTMERTM;/EnableGlobalrealtimeinterruptDBGMCTRGR=0x80;/初始化ICETEK
22、-CTRCTRGR=0x0;CTRGR=0x80;CTRLR=0;/关闭东西方向的交通灯CTRLR=0x40;/关闭南北方向的交通灯CTRLR=0xC0;CTRGR=0x81;uPort8000=CTRCLKEY;Gpio_PortA();Gpio_PortB();nSpeed=T46uS;uN=60;nCount=nCount1=0;nDir=0;cKey=cOldKey=0;StartCpuTimer0();/启动定时器while(1)nScanCode=*(int*)0x108001;/读扫描码nScanCode&=0x0ff;/低8位uPort8000=*(int*)0x108002;
23、/Delay(5);if(nScanCode!=0)if(nScanCode=9)break;elsecKey=nScanCode;if(cKey!=0&cOldKey!=cKey)cOldKey=cKey;switch(cKey)case1:uN=10;break;case2:uN=50;break;case3:uN=60;break;case4:uN=70;break;case5:uN=80;break;case6:uN=100;break;case7:uN1=uN;uN=60;/降速Delay(128);GpioDataRegs.GPADAT.bit.GPIOA4=1;/CpuTimer
24、0Regs.PRD.all=nSpeed;/CpuTimer0Regs.PRD.all=182*50;nDir=0;Delay(128);uN=uN1;break;case8:uN1=uN;uN=60;/降速Delay(128);GpioDataRegs.GPADAT.bit.GPIOA4=0;Delay(128);/CpuTimer0Regs.PRD.all=nSpeed;nDir=1;Delay(128);uN=uN1;break;/Delay(4);StopCpuTimer0();CTRGR=0;interruptvoidcpu_timer0_isr(void)CpuTimer0.Int
25、erruptCount+;/Acknowledgethisinterrupttoreceivemoreinterruptsfromgroup1PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;CpuTimer0Regs.TCR.bit.TIF=1;CpuTimer0Regs.TCR.bit.TRB=1;GpioDataRegs.GPBSET.bit.GPIOB4=1;GpioDataRegs.GPBDAT.bit.GPIOB4=(nCount1uN)?1:0;nCount1+;nCount1%=100;voidDelay(unsignedintnDelay)intii,
26、jj,kk=0;for(ii=0;iinDelay;ii+)for(jj=0;jj64;jj+)/RefreshLEDArray();kk+;voidRefreshLEDArray()inti;for(i=0;i8;i+)CTRGR=ledxi;CTRLA=ledbufi;voidGpio_PortA(void)/GPIOTest#2:/ConfigureUpper8bitsofPortasinputsandlower8bitsasoutputs/Loopbackbits7:0tobits15:8/Dontsetanyinputqualifiervar1=0x0000;/setsGPIOMux
27、sasI/Osvar2=0x00FF;/setsGPIO15-8DIRasinputs,7-0DIRasoutputsvar3=0x0000;/DontsetanyinputqualifierGpio_select();test_count=0;Test_statusTest_var=0x0002;Test_var+;Test_statusTest_var=0xD0BE;/Setthedefaultvalueofstatus/toPASSEDGpioDataRegs.GPACLEAR.all=0x00FF;/TestClearasm(RPT#5|NOP);GpioDataRegs.GPASET
28、.bit.GPIOA4=1;voidGpio_PortB(void)/GPIOTest#2:/ConfigureUpper8bitsofPortasinputsandlower8bitsasoutputs/Loopbackbits7:0tobits15:8/Dontsetanyinputqualifiervar1=0x0000;/setsGPIOMuxsasI/Osvar2=0x00FF;/setsGPIO15-8DIRasinputs,7-0DIRasoutputsvar3=0x0000;/DontsetanyinputqualifierGpio_select();test_count=0;
29、Test_statusTest_var=0x0002;Test_var+;Test_statusTest_var=0xD0BE;/Setthedefaultvalueofstatus/toPASSEDGpioDataRegs.GPBCLEAR.all=0x00FF;/TestClearasm(RPT#5|NOP);GpioDataRegs.GPBSET.bit.GPIOB4=1;voidGpio_select(void)EALLOW;GpioMuxRegs.GPAMUX.all=var1;/ConfigureMUXsasdigitalI/OsorGpioMuxRegs.GPBMUX.all=v
30、ar1;/peripheralI/OsGpioMuxRegs.GPDMUX.all=var1;GpioMuxRegs.GPFMUX.all=var1;GpioMuxRegs.GPEMUX.all=var1;GpioMuxRegs.GPGMUX.all=var1;GpioMuxRegs.GPADIR.all=var2;/GPIOPORTsasoutputGpioMuxRegs.GPBDIR.all=var2;/GPIODIRselectGPIOsasoutputGpioMuxRegs.GPDDIR.all=var2;GpioMuxRegs.GPEDIR.all=var2;GpioMuxRegs.
31、GPFDIR.all=var2;GpioMuxRegs.GPGDIR.all=var2;GpioMuxRegs.GPAQUAL.all=var3;/SetGPIOinputqualifiervaluesGpioMuxRegs.GPBQUAL.all=var3;GpioMuxRegs.GPDQUAL.all=var3;GpioMuxRegs.GPEQUAL.all=var3;EDIS;charConvertScanToChar(unsignedcharcScanCode)charcReturn;cReturn=0;switch(cScanCode)caseSCANCODE_0:cReturn=0
32、;break;caseSCANCODE_1:cReturn=1;break;caseSCANCODE_2:cReturn=2;break;caseSCANCODE_3:cReturn=3;break;caseSCANCODE_4:cReturn=4;break;caseSCANCODE_5:cReturn=5;break;caseSCANCODE_6:cReturn=6;break;caseSCANCODE_7:cReturn=7;break;caseSCANCODE_8:cReturn=8;break;caseSCANCODE_9:cReturn=9;break;/caseSCANCODE_F1:caseSCANCODE_Plus:cReturn=+;break;/caseSCANCODE_F2:caseSCANCODE_Minus:cReturn=-;break;returncReturn;调试过程中所遇到的问题及解决思路和方法。1. 调试时电机不运转,复位cpu重新写入程序运行,检查程序,观察是否有存在错误。2.点按键后电机运转没变化,长按按键观察或检查程序是否有问题。实验后的经验教训总结。实验前需熟悉实验仪器,了解实验要求和内容,熟悉程序的逻辑关系,再进行试验。在程序创作上,必须保证结构严谨、关键字正确和逻辑准确等,开始的认真是为了之后程序调试能更快更准确。教师评语-