Tải bản đầy đủ (.docx) (18 trang)

code thực tập vi xử lý

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (52.71 KB, 18 trang )



Đk đc bước bằng số bước nhập từ phím ma trận

#include "msp430f249.h"
#include "intrinsics.h"
void Dkmotor (unsigned char key)
{
unsigned char CODE [] ={0x01,0x02,0x04,0x08};
unsigned char i, j=0;
for (i=key; i>0; i--) //quay so buoc bang gia tri cua key
{
if(j==4) //neu het chy ky thi
j=0; //nap lai giá tri 0x01
P1OUT=CODE[j];
__delay_cycles (500000) ;//delay 0.5s
j++;
}
P1OUT=0x00; //dung motor
}
void main ( void )
{
WDTCTL = WDTPW + WDTHOLD;
unsigned char key;
P1DIR=0xFF; //motor buoc
P5DIR=0x0F; //phim hex
While (1)
{
// kiem tra cac phim o cot 1
P5OUT =0xfe;
If (! (P5IN & BIT4)) //phim 1




{key =1;
Dkmotor(key);}
else if (!(P5IN & BIT5)) //phim 4
{key=4;
Dkmotor(key);}
else if (!(P5IN & BIT6)) //phim 7
{key=7;
Dkmotor(key);}
else If (!(P5IN & BIT7)) //phim 10
{key=10;
Dkmotor(key);}
// kiem tra cac phim o cot 2
P5OUT = 0xfd;
If (!(P5IN & BIT4)) //phim 2
{key =2;
Dkmotor(key);}
else if (!(P5IN & BIT5)) //phim 5
{key=5;
Dkmotor(key);}
else if(!(P5IN & BIT6)) //phim 8
{key=8;
Dkmotor(key);}
else if(!(P5IN & BIT7)) //phim 0
{key=0;
Dkmotor(key);}
// kiem tra cac phim o cot 3
P5OUT =0xfb;
if(!(P5IN & BIT4)) //phim 3

{key =3;


Dkmotor(key);}
else if(!(P5IN & BIT5)) //phim 6
{key=6;
Dkmotor(key);}
else if(!(P5IN & BIT6)) //phim 9
{key=9;
Dkmotor(key);}
else if(!(P5IN & BIT7)) //phim 11
{key=11;
Dkmotor(key);}}}



điều khiển quay thuận nghịch động cơ bước bằng phím đơn

#include "msp430f249.h"
#define SW1 BIT2
#define SW2 BIT3
unsigned char code[]={0x01,0x02,0x04,0x08};
unsigned char code1[]={0x08,0x04,0x02,0x01};
int j=0,i=0;
void main( void )
{
WDTCTL = WDTPW + WDTHOLD;
P2DIR =0x00; //P3.2_SW1,P3.3_SW2: input
P1DIR=0xff; //P4.4_relay: output
while(1)

{

while(!(P2IN&SW1))//SW1 dc an
{
P2OUT=0x0a;} // khi phím sw1 đx nhấn P2 sẽ bằng gia trị này


while(!(P2IN&SW2))//SW2 dc an
{
P2OUT=0x0c; // khi phím sw2 đx nhấn P2 sẽ bằng gia trị này
}
if(P2OUT==0x0a)
{
if(j==4)
j=0;
P1OUT=code[j];
__delay_cycles(500000);

}
else if(P2OUT==0x0c)
{
if(j==4)
j=0;
P1OUT=code1[j];
__delay_cycles(500000);

}}}


Điều khiển động cơ bước bằng phim đơn số bước bằng số lần ấn

phím, SW1 quay thuận, SW2 quay nghịch.
#include "msp430f249.h"
#include "intrinsics.h"
#define SW1 BIT2
#define SW2 BIT3
unsigned char CODE[]={0x01,0x02,0x04,0x08};
unsigned char CODE1[]={0x08,0x04,0x02,0x01};
int i=0,j=0;
void main( void )


{
WDTCTL = WDTPW + WDTHOLD;
unsigned char key;
P1DIR=0x0f; //motor buoc
P2DIR=0x00; //phim don
while(1)
{
while(!(P2IN&SW1))//SW1 dc an
{ key+=1;
for(i=key;i>0;i--) //quay so buoc bang gia tri cua key
{
if(j==4) //neu het chy ky thi
j=0; //nap lai giá tri 0x01
P1OUT=CODE[j];
__delay_cycles(500000);//delay 0.5s
}
}
P1OUT=0x00;
key=0;

while(!(P2IN&SW2))//SW1 dc an
{ key+=1;
for(i=key;i>0;i--) //quay so buoc bang gia tri cua key
{
if(j==4) //neu het chy ky thi
j=0; //nap lai giá tri 0x01
P1OUT=CODE1[j];
__delay_cycles(500000);//delay 0.5s
}
}
P1OUT=0x00;
key=0;
}}


Chương trình motor quay thuận 4 bước, sau đó dừng 2s và rồi quay nghịch
8 bước rồi lặp lại quá trình với số lần lặp được nhập từ bàn phím
#include "msp430f249.h"
#include "intrinsics.h"
int i=0,key=0;
unsigned char code[]= {0x01,0x02,0x04,0x08};
unsigned char code1[]= {0x08,0x04,0x02,0x01};
void dk( unsigned char key)
{
for(int k=0;k{
for (i=0;i<4;i++)
{
P1OUT=code[i];



__delay_cycles(500000);
}
P1OUT=0x00;
__delay_cycles(2000000);
for (int t=0;t<2;t++)
{for (i=0;i<4;i++)
{P1OUT=code1[i];
__delay_cycles(500000);
}}}
P1OUT=0x00;
}
void main( void )
{
WDTCTL = WDTPW + WDTHOLD;
P1DIR= 0x0f;
P2DIR=0x00;
P5DIR=0x0f;
while(1)
{//cot 1
P5OUT=0xfe;
if(!(P5IN&BIT4))
{key=1;
dk(key);}
if(!(P5IN&BIT5))
{key=4;
dk(key);}
if(!(P5IN&BIT6))
{key=7;
dk(key);}

if(!(P5IN&BIT7))
{key=0x0a;
dk(key);}
P5OUT=0xfd;
if(!(P5IN&BIT4))
{key=2;
dk(key);}
if(!(P5IN&BIT5))
{key=5;
dk(key);}
if(!(P5IN&BIT6))
{key=8;
dk(key);}
if(!(P5IN&BIT7))
{key=0;
dk(key);}
P5OUT=0xfb;
if(!(P5IN&BIT4))
{key=3;


dk(key);}
if(!(P5IN&BIT5))
{key=6;
dk(key);}
if(!(P5IN&BIT6))
{key=9;
dk(key);}
if(!(P5IN&BIT7))
{key=0x0b;

dk(key);}
}


}

Nhập 2 số :số chẵn đếm lên số lẻ đếm xuống

#include "msp430f249.h"
#include "intrinsics.h"
unsigned char led7[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66,
0x6d, 0x7d, 0x07, 0x7f, 0x6f,0x77,0x7c};
void main( void )
{ unsigned char i;
unsigned char key=0,t=0;
WDTCTL = WDTPW + WDTHOLD;
P4DIR = 0xff; //DATA
P5DIR = 0x0f; //phim hex
P6DIR = 0x0F; //P3.0-P3.4 scan
while(1)
{ // kiem tra cac phim o cot 1
P5OUT =0xfe;
if(!(P5IN & BIT4)) //phim 1
{key =0x01;
goto hienthi;}
else if(!(P5IN & BIT5)) //phim4
{key=0x04;


goto hienthi;}

else if(!(P5IN & BIT6)) //phim7
{key=7;
goto hienthi;}
else if(!(P5IN & BIT7)) //phim A
{key=0x0a;
goto hienthi;}
// kiem tra cac phim o cot 2
P5OUT =0xfd;
if(!(P5IN & BIT4)) //phim2
{key =0x02;
goto hienthi;}
else if(!(P5IN & BIT5)) //phim5
{key=0x05;
goto hienthi;}
else if(!(P5IN & BIT6)) //phim8
{key=0x08;
goto hienthi;}
else if(!(P5IN & BIT7)) //phim0
{key=0x00;
goto hienthi;}
// kiem tra cac phim o cot 3
P5OUT =0xfb;
if(!(P5IN & BIT4)) //phim3
{key =0x03;
goto hienthi;}
else if(!(P5IN & BIT5)) //phim6
{key=0x06;
goto hienthi;}



else if(!(P5IN & BIT6)) //phim9
{key=9;
goto hienthi;}
else if(!(P5IN & BIT7)) //phim B
{key=0x0b;
goto hienthi;}
//hien thi ma phim
hienthi:
if(((key%2)==0)&key>0)
{for (int t=key;t<99;t++)
{
for(i=0;i<10;i++)
{ P6OUT=0x0F;
P4OUT=led7[t&0x0f]; //hang don vi
P6OUT=0x01;
__delay_cycles(10000);
P6OUT=0x0F;
P4OUT=led7[(t&0xf0)>>4]; //hang chuc
P6OUT=0x00;
__delay_cycles(10000);
}
}}
else {
for (t=key;t>0;t--)
{
for(i=0;i<10;i++)
{ P6OUT=0x0F;
P4OUT=led7[t&0x0f]; //hang don vi
P6OUT=0x01;



__delay_cycles(10000);
P6OUT=0x0F;
P4OUT=led7[(t&0xf0)>>4]; //hang chuc
P6OUT=0x00;
__delay_cycles(10000);
}
}}
key=0;t=0;
}}


Nhập số có 4 chữ số sau đó đếm về 0

#include "msp430f249.h"
#include "intrinsics.h"
unsigned char led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
unsigned int num1=0,i=1,dem=0;
//unsigned char key=0x00;

P4DIR=0xff;
P6DIR=0x0f;
P5DIR=0x0f;
while(1)
{


P5OUT=0xfe;
if(!(P5IN&BIT4))
{ num1=num1*10+1; dem=dem+1;goto hienthi; } //biến điếm dùng để xác định số chữ số đã nhập


else if(!(P5IN&BIT5))
{ num1=num1*10+4; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT6))
{ num1=num1*10+7; dem=dem+1;goto hienthi; }

P5OUT=0xfd;
if(!(P5IN&BIT4))
{ num1=num1*10+2; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT5))
{ num1=num1*10+5; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT6))
{ num1=num1*10+8; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT7))
{ num1=num1*10+0; dem=dem+1;goto hienthi; }

P5OUT=0xfb;
if(!(P5IN&BIT4))
{ num1=num1*10+3; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT5))
{ num1=num1*10+6; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT6))
{ num1=num1*10+9; dem=dem+1;goto hienthi; }

if(dem==4)//nếu đã nhập 4 số thì bắt đầu đếm
{

for(int t=num1;t>0;t--)
{
num1-=1;
goto hienthi;}


dem=0;
}
hienthi:

for(i=0;i<20;i++)
{ P6OUT=0x0f;
P4OUT=led[(((num1%1000)%100)%10)];
P6OUT=0x03;
__delay_cycles(5000);

P6OUT=0x0f;
P4OUT=led[(((num1%1000)%100)/10)];
P6OUT=0x02;
__delay_cycles(5000);

P6OUT=0x0f;
P4OUT=led[((num1%1000)/100)];
P6OUT=0x01;
__delay_cycles(5000);

P6OUT=0x0f;
P4OUT=led[num1/1000];
P6OUT=0x00;
__delay_cycles(5000);

}}}


Nhập số 4 chữ số sau đó đếm lên từ số vừa nhập đến 9999 rồi về
0000
#include "msp430f249.h"


#include "intrinsics.h"
unsigned char led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
unsigned int num1=0,i=1,dem=0;
//unsigned char key=0x00;
P4DIR=0xff;
P6DIR=0x0f;
P5DIR=0x0f;
while(1)
{
P5OUT=0xfe;
if(!(P5IN&BIT4))
{ num1=num1*10+1; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT5))
{ num1=num1*10+4; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT6))
{ num1=num1*10+7; dem=dem+1;goto hienthi; }
P5OUT=0xfd;
if(!(P5IN&BIT4))

{ num1=num1*10+2; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT5))
{ num1=num1*10+5; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT6))
{ num1=num1*10+8; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT7))
{ num1=num1*10+0; dem=dem+1;goto hienthi; }
P5OUT=0xfb;
if(!(P5IN&BIT4))
{ num1=num1*10+3; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT5))


{ num1=num1*10+6; dem=dem+1;goto hienthi; }
else if(!(P5IN&BIT6))
{ num1=num1*10+9; dem=dem+1;goto hienthi; }
if(dem==4)
{
if(num1<=10000)
{num1+=1;
if(num1==10000)
{num1=0;
dem=0;}
goto hienthi;
}}
hienthi:
for(i=0;i<20;i++)
{ P6OUT=0x0f;
P4OUT=led[(((num1%1000)%100)%10)];
P6OUT=0x03;

__delay_cycles(5000);
P6OUT=0x0f;
P4OUT=led[(((num1%1000)%100)/10)];
P6OUT=0x02;
__delay_cycles(5000);
P6OUT=0x0f;
P4OUT=led[((num1%1000)/100)];
P6OUT=0x01;
__delay_cycles(5000);
P6OUT=0x0f;
P4OUT=led[num1/1000];
P6OUT=0x00;
__delay_cycles(5000);
}}}




trừ hai số nhập từ phím

#include "msp430f249.h"
#include "intrinsics.h"
unsigned char led7[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned int num1=0,num2=0,kq=0,i=1,j;
void main (void)
{ WDTCTL=WDTPW+WDTHOLD;
P4DIR=0xff;
P6DIR=0x0f;
P5DIR=0x0f;
while(1)

{
P5OUT=0xfe;
if(!(P5IN&BIT4))
{num1=num1*10+1;
goto hienthi;}
else if(!(P5IN&BIT5))
{num1=num1*10+4;
goto hienthi;}
else if(!(P5IN&BIT6))
{num1=num1*10+7;
goto hienthi;}
else if(!(P5IN&BIT7))
{num2=kq;num1=0;
goto hienthi;}

P5OUT=0xfd;
if(!(P5IN&BIT4))


{num1=num1*10+2;
goto hienthi;}
else if(!(P5IN&BIT5))
{num1=num1*10+5;
goto hienthi;}
else if(!(P5IN&BIT6))
{num1=num1*10+8;
goto hienthi;}
else if(!(P5IN&BIT7))
{num1=num1*10;
goto hienthi;}


P5OUT=0xfb;
if(!(P5IN&BIT4))
{num1=num1*10+3;
goto hienthi;}
else if(!(P5IN&BIT5))
{num1=num1*10+6;
goto hienthi;}
else if(!(P5IN&BIT6))
{num1=num1*10+9;
goto hienthi;}
else if(!(P5IN&BIT7))

{kq=num2 - num1;num1=0;num2=kq;//tương tự cho các chức năng +, *, /
goto hienthi;}

//---------hien thi---------

hienthi:


for(j=0;j<15;j++)
{
P6OUT=0x0f;
P4OUT=led7[((kq%1000)%100)%10];
P6OUT=0x03;
__delay_cycles(5000);

P6OUT=0x0f;
P4OUT=led7[((kq%1000)%100)/10];

P6OUT=0x02;
__delay_cycles(5000);

P6OUT=0x0f;
P4OUT=led7[(kq%1000)/100];
P6OUT=0x01;
__delay_cycles(5000);

P6OUT=0x0f;
P4OUT=led7[kq/1000];
P6OUT=0x00;
__delay_cycles(5000);
}
}
}


chương trình đếm lên các số chẵn từ 00 đến 99, sau đó đếm lùi các số lẻ từ
98 về 0.
#include "msp430f249.h"
#include "intrinsics.h"

unsigned char code [] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};


void main (void)
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P4DIR=0xff;

P6DIR=0x0f;
for(int t=0;t<99;t=t+2)
{
if(t>98)
t=98;
for(int i=0;i<10;i++)
{
P6OUT=0x0f;
P4OUT=code[(t/10)&0x0f];
P6OUT=0x00;
__delay_cycles(50000);
P6OUT=0x0f;
P4OUT=code[(t%10)&0x0f];
P6OUT=0x01;
__delay_cycles(50000);
}}
for(int t=97;t>0;t=t-2)
{
if(t<=0)
t=0;
for(int i=0;i<10;i++)
{
P6OUT=0x0f;
P4OUT=code[(t/10)&0x0f];
P6OUT=0x00;
__delay_cycles(50000);
P6OUT=0x0f;
P4OUT=code[(t%10)&0x0f];
P6OUT=0x01;
__delay_cycles(50000);

}}
}



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×