精品C程序控制风扇转动.docx
《精品C程序控制风扇转动.docx》由会员分享,可在线阅读,更多相关《精品C程序控制风扇转动.docx(18页珍藏版)》请在冰豆网上搜索。
![精品C程序控制风扇转动.docx](https://file1.bdocx.com/fileroot1/2023-1/3/7b5ad0bb-a98d-4658-939b-db730d60820b/7b5ad0bb-a98d-4658-939b-db730d60820b1.gif)
精品C程序控制风扇转动
C程序控制风扇转动
课程设计说明书
名称
院 系
班级
姓名
系 主 任
教研室主任
指导教师
课程设计任务书
课程名称:
起讫时间:
院系:
班 级:
指导教师:
系主任:
一、需求分析………………………………………………………………………..3
微控制器应用MotorSpeedControl程序设计…………………………………..3
二、设计过程………………………………………………………………………..4
三、运行结果………………………………………………………………………..7
1.实验板的布局图……………………………………………………………………...7
2.实训步骤……………………………………………………………………..............8
3.运行结果展示………………………………………………………………………...9
四、程序代码………………………………………………………………………..10
1.Demonstrator.c程序…………………………………………………………………10
2.PC_Comm.c程序…………………………………………………………………...13
五、心得体会……………………………………………………………………….18
六、参考文献……………………………………………………………………….19
一.需求分析
本程序由5人共同设计,用以实现以下程序:
微控制器应用MotorSpeedControl程序设计
打开ProgrammersNotepad
用C语言完成程序的编写,再打开AVRstudio,把程序载入到Butterfly实验板进行调试,最终实现由微控制器控制的风扇的转动。
过程中,要求有实际的程序,并保证程序的正确性与可操作性,运行可执行程序,得到运行结果,风扇转动,对结果进行分析,结果是否合理,不合理要对实验程序进行重新调试,通过PC机对程序进行故障排查。
二.设计过程
实验器件:
一个9V的电动机,一个9V的电池,一个9V的电池连接器,一个4N28光隔离器,一个TIP115功率晶体管,一个150Ohm电阻,还有一个2.2k的电阻。
我已经包括了简单原理的光导体,它帮助减少我们在修改这一电路的状态,以致我们将破坏这个微控制器。
我实际上设法将光导体和大功率的晶体管燃烧,当以开玩笑的言行做这个设计。
至少对我来说,这不是太夸张了。
因为这不是一个电子文本,我们不需要学习关于电流是怎么工作的。
对应插图,我们就不会有任何的问题。
画出原理图
打开ProgrammersNotepad软件
设计出实验程序
打开AVRstudio
把程序载入到Butterfly实验板进行调试。
具体过程:
1.马达可以用泡沫芯板切割下来组成和黏贴来作为底部,左边直立部分用于下一个工程来做光导体。
2.风扇部分类似于车轮的模式在课本的附录6,剪下来贴在一张比较坚实的薄硬纸板上,用电胶带贴在轴上,使得轮子中心部分沿着径向是可滑动的,工作的。
如图的抠图图象用于下一个工程。
我们用一个光隔离器来隔离电机功率电路从而帮助减少在蝴蝶效应中产生爆炸的可能性。
光绝缘体是一个类似于光隔离器的装置,它有一个空中通道在IR灯发光二极管和IR检测晶体管之间,如图。
这个不透明的对象通过二极管和检测器导致晶体管熄灭因此电流“中断”。
我们可以把晶体管联结到蝴蝶效应的引脚和检测器的中断上。
你有没有注意到在图中的轮子的开关?
如果拼凑好电机基础部分,使车轮通过在光导体槽旋转时,每次正常运行。
当槽通过时,晶体管开启和关闭。
写好软件程序,以便于附着在光导体引脚电压变化引起的蝴蝶中断的效应,如果指望正好一秒,有多少次每秒旋转的车轮,这是在HZ.COOL转速!
接一根长的电线到光隔离器上,然后添加电工胶带以防止短路。
接下来小心的用胶水贴到在一个电机位置上,通过它旋转风轮。
确保风轮平衡,稳定,要完全封锁和解除封
在风轮光隔离器插槽转动时。
.
光导体的引脚1对应3V
光导体的引脚2对应一个200ohm的电阻
200ohm的电阻对应电池的接地。
光导体的引脚3对应PORTB引脚4。
光导体引脚4对应电池的接地。
我意识我已经学会了机械工程技术运用于这个工程在初始阶段。
尽管初学者作做的比我更有吸引力,但是目的达到了,它工作了。
三.运行结果
实训步骤
(1)启动运行programmers软件做相应设计,如图:
(2)打开控制程序pc_comm.c和makefile文件,按相应步骤进行编译.
(3)编译无误后,打开AVRstudio4软件,如图:
(4)在上图中打开pc_comm.cof文件,装入给定程序模板,运行演示;
(5)运行成功
编译程序
效果图:
四.程序代码
第一个程序Demonstrator.c
//Demonstrator.cMotorSpeedControlversion
#include"PC_Comm.h"
#include"Demonstrator.h"
unsignedcharmilliseconds=0;
unsignedintsecond=0;//countto1000andtriggeronesecondevent
unsignedintspeed=0;//IRdetectorcountpersecond
unsignedintlastspeed=0;//IRdetectorcountpersecond
voidinitializer()
{
//Calibratetheoscillator:
OSCCAL_calibration();
//InitializetheUSART
USARTinit();
//SetforpinchangeonPINB0
DDRB=(1<PORTB=(1<PCMSK0=(1<EIFR=(1<<7);//flagforPCINT15-8
EIMSK=(1<<7);//maskforPCINT15-8
DDRB=0X00;//setPORTBforinput
PORTB=0xFF;//enablepulluponforinput
//setPORTDforoutput
DDRD=(1<PORTD=(1<milliSecInit(127);//50%dutycycle1kHzsignal
//sayhello
sendString("\rPC_Comm.creadytocommunicate.\r");
//identifyyourselfspecifically
sendString("YouaretalkingtotheMotorSpeedControldemo.\r");
sendString("setxxxtosetspeed\r");
}
voidparseInput(chars[])
{
//parsefirstcharacter
switch(s[0])
{
case's':
if((s[1]=='e')&&(s[2]=='t'))
parse_set(s);
break;
case'd':
if((s[1]=='e')&&(s[2]=='m')&&(s[3]=='o')&&(s[4]=='?
'))
sendString("YouaretalkingtotheMotorSpeedControldemo.\r");
break;
default:
sendString("\rYousent:
'");
sendChar(s[0]);
sendString("'-Idon'tunderstand.\r");
break;
}
s[0]='\0';
}
intparse_set(chars[])
{
charset[11];
unsignedchari=3,j=0;
while((s[i]!
='\0')&&(j<=11))
{
if((s[i]>='0')&&(s[i]<='9'))
{
set[j++]=s[i++];
}
else
{
sendString("Error-Parse_setreceivedanoninteger:
");
sendChar(s[i]);
sendChar('\r');
return0;
}
}
set[j]='\0';
if(j>4)//mustbe<256
{
sendString("Error-Parse_setnumbertoolarge");
return0;
}
else
{
set_speed(atoi(set));
}
return1;
}
voidset_speed(intcount)
{
charspeed[11];
sendString("SettingtheCompareTimerCountto:
");
itoa(count,speed,10);
sendString(speed);
sendChar('\r');
milliSecInit(count);
}
/*
TheUSARTinitsetthesystemoscillatorto2mHz.WesettheTimer0prescaler
toclk/8whichgivesa250kHzinputtothetimer/counter.Acompareof250throws
aninterrupteverymillisecond.
*/
voidmilliSecInit(unsignedcharcount)
{
//Enabletimer0compareinterrupt
TIMSK0=(1<//Setsthecomparevalue
setOCR0A(count);
//SetPWMPhaseCorrectmode,CLK/8prescaler
TCCR0A=(0<}
voidsetOCR0A(unsignedcharcount)
{
//Setsthecomparevalue
OCR0A=count;
}
//InterruptoccurstwiceperMillisec,timedforPWM
SIGNAL(SIG_OUTPUT_COMPARE0)
{
//TogglePORtDpin0
if(PORTD&=1)cbi(PORTD,0);
elsesbi(PORTD,0);
}
第二个程序:
PC_Comm.c
//PC_Comm.c
#include"Demonstrator.h"
#include"PC_Comm.h"
intmain(void)
{
charstring[64];
unsignedcharcount=0;
//runtheinitializationroutines
initializer();
//BeginforeverchattingwiththePC
for(;;)
{
//Checktoseeifacharacteriswaiting
if(isCharAvailable()==1)
{
//Ifanewcharacterisreceived,getit
string[count++]=receiveChar();
//receiveapacketupto64byteslong
if(string[count-1]=='\n')//Hyperterminalstringendswith\r\n
{
string[count-2]='\0';//converttoastring
parseInput(string);
string[0]='\0';
count=0;
}
elseif(count>64)
{
count=0;
string[0]='\0';
sendString("Error-received>64characters");
}
}
}
return0;
}
charisCharAvailable()
{
//DoestheRX0bitoftheUSARTStatusandControlRegister
//indicateacharhasbeenreceived?
if((UCSR0A&(0x80)))return1;
elsereturn0;
}
charreceiveChar()
{
//ReturnthecharintheUDR0register
returnUDR0;
}
voidsendChar(chardata)
{
inti=0;
//TosenddatawiththeUSARTputthedataintheUSARTdataregister
UDR0=data;
//Checktoseeiftheglobalinterruptsareenabled
if(SREG&0x80)
{
//Waituntilthebyteissentorwecountout
while(!
(UCSR0A&0x40)&&(i<10000))
{
i++;
}
}
else//Waituntilthebyteissent
while(!
(UCSR0A&0x40));
//CleartheTXCflag
UCSR0A=UCSR0A|0x40;
}
voidsendString(chars[])
{
inti=0;
while(i<64)//don'tgetstuckifitisabadstring
{
if(s[i]=='\0')break;//quitonstringterminator
sendChar(s[i++]);
}
}
voidUSARTinit()
{
//Increasetheoscillatorto2Mhzforthe19200baudrate:
CLKPR=(1<//setprescaler=4,InterRC8Mhz/4=2Mhz
CLKPR=(1<//SettheUSARTbaudrateregistersfor19200
UBRR0H=0;//(unsignedchar)(baudrate>>8);
UBRR0L=12;//(unsignedchar)baudrate;
//Enable2xspeedchange
UCSR0A=(1<//Enablereceiverandtransmitter
UCSR0B=(1<//SettheUSARTtoasynchronousat8bitsnoparityand1stopbit
UCSR0C=(0<//Enableinterrupts
sei();
//Setuptheinterruptmaskandflags
PCMSK1=(1<EIFR=(1<EIMSK=(1<}
//CalibratetheinternalOSCCALbyte,usingtheexternal
//32,768kHzcrystalasreference
voidOSCCAL_calibration(void)
{
unsignedcharcalibrate=0;//FALSE;
inttemp;
unsignedchartempL;
CLKPR=(1<//setprescaler=8,InterRC8Mhz/8=1Mhz
CLKPR=(1<TIMSK2=0;//disableOCIE2AandTOIE2
ASSR=(1<OCR2A=200;//settimer2comparevalue
TIMSK0=0;//deleteanyinterruptsources
TCCR1B=(1<TCCR2A=(1<while((ASSR&0x01)|(ASSR&0x04));//waitforTCN2UBandTCR2UBtobecleared
//waitforexternalcrystaltostabilise
for(inti=0;i<10;i++)
_delay_loop_2(30000);
while(!
calibrate)
{
cli();//mt__disable_interrupt();//disableglobalinterrupt
TIFR1=0xFF;//deleteTIFR1flags
TIFR2=0xFF;//deleteTIFR2flags
TCNT1H=0;//cleartimer1counter
TCNT1L=0;
TCNT2=0;//cleartimer2counter
while(!
(TIFR2&&(1<TCCR1B=0;//stoptimer1
sei();//__enable_interrupt();//enableglobalinterrupt
if((TIFR1&&(1<{
temp=0xFFFF;//iftimer1overflows,setthetempto0xFFFF
}
else
{//readoutthetimer1countervalue
tempL=TCNT1L;
temp=TCNT1H;
temp=(temp<<8);
temp+=tempL;
}
if(temp>6250)
{
OSCCAL--;//theinternRCoscillatorrunstofast,decreasetheOSCCAL
}
elseif(temp<6120)
{
OSCCAL++;//theinternRCoscillatorrunstoslow,increasetheOSCCAL
}
else
calibrate=1;//TRUE;//theinterRCiscorrect
TCCR1B=(1<}
}
五.心得体会
作为一名应用电子技术专业的大二学生,我觉得做微控制器的设计是十分有意义的,而且是十分必要的。
在已度过的大学时间里,我们大多数接触的是专业课。
我们在课堂上掌握的仅仅是专业课的理论知识,如何去锻炼我们的实践能力?
如何把我们所学的专业基础课理论知识运用到实践中去呢?
我想做类似的课程设计就为我们提供了良好的实践平台。
在做本次课程设计的过程中,我感触最深的当属查阅大量的设计资料了。
为了让自己的设计更加完善,查阅这方面的设计资料是十分必要的,同时也是必不可少的。
我们是在做课程设计,但我们不是艺术家,他们可以抛开实际尽情在幻想的世界里翱翔,而我们一切都要有据可依,有理可寻