单片机控制步进电机课程设计报告

uchar a[]={0x10,0x20,0x40,0x80}; uchar b[]={0x80,0x40,0x20,0x10}; uchar x=0,T,tempL,tempH; void delay(uint i) {

while(i--); }

void bell(void) { uchar i;

for(i=0;i<50;i++) {

bel=0;

delay(10000); bel=1;

delay(10000); } }

void zj(uchar zj) { uint i,j; D2=1;

for(j=0;j

P0=a[i]; delay(800); }

D2=0; }

void fj(uchar fj) { uint i,j; D3=1;

for(j=0;j

P0=b[i];

delay(800); }

D3=0; }

void aj(void) {

if(k1==0) {delay(M);

if(k1==0)x=3; }

if(k2==0) {

delay(M); if(k2==0)x=6; }

if(k3==0) {

delay(M); if(k3==0)x=9; }

if(k4==0)

{

delay(M);

if(k4==0)x=12; }

if(k5==0) {

delay(M);

if(k5==0)x=15; }

if(k6==0) {

delay(M);

if(k6==0)x=18; }

if(k7==0) {

delay(M);

if(k7==0)x=21; }

if(k8==0) {

delay(M);

if(k8==0)x=24; }

if(k9==0) {

delay(M);

if(k9==0)x=27;

} }

void motorstart(void) {

if(k10==0) {

delay(M);

if(k10==0)P0=0x0f; }

if(k11==1&&k12==0&&k10==1) {

delay(M);

if(k11==1&&k12==0&&k10==1)zj(x); }

if(k11==0&&k12==1&&k10==1) {

delay(M);

if(k10==1&&k11==0&&k12==1)fj(x); }

}

void ds18b20init(void) {

uchar ds=0; DQ=1; delay(8); DQ=0; delay(80);

DQ=1;

delay(5); ds=DQ; delay(20); }

void WriteOneChar(uchar dat) {

uchar i=0;

for(i=8;i>0;i--) {

DQ=0;

DQ=dat&0x01; delay(5); DQ=1; dat>>=1; }

delay(4); }

uchar ReadOneChar(void) {

unsigned char i=0; unsigned char dat=0; for (i=8;i>0;i--) {

DQ=1; delay(1); DQ=0; dat>>=1; DQ=1; if(DQ)

dat|=0x80; delay(4); }

return(dat); }

void ReadTemperature(void) {

ds18b20init();

WriteOneChar(0xcc); WriteOneChar(0x44); delay(125); ds18b20init();

WriteOneChar(0xcc); WriteOneChar(0xbe); tempL=ReadOneChar(); tempH=ReadOneChar(); }

void worktemp(void) {

T=((tempL&0xf0)>>4)+((tempH&0x07)<<4); }

void main() { T=0;

P1=0XFF; P0=0X0F; P2=0X00; while(1) {

ds18b20init(); WriteOneChar(0XCC); WriteOneChar(0X44); ReadTemperature(); worktemp(); aj();

if(T<50) {

D1=0; bel=1; motorstart(); }

if(T>=50) {

P0=0; D1=1; bell();

delay(1000);

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4