Học liệu

Điều khiển quét Led với MSP430

  • 11/05/2019
  • Học liệu

#include "io430.h"
//#include <msp430f2011.h>
#define DELTA_8MHZ 1953
#include "intrinsics.h"

void Delay(unsigned int nLoop);
void MoRongP1(unsigned int e);
void MoRongP2(unsigned int d);
void MoRongP3(unsigned int s);
void MoRongP4(unsigned int p);

void LayTungBit(unsigned int ta,unsigned char vt);

unsigned char k;
union reg {
struct bit {
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
}_BIT;
unsigned char _BYTE;
};
union reg*p1_dir=(union reg*)0x22;
union reg*p1_out=(union reg*)0x21;
union reg*p2_dir=(union reg*)0x2A;
union reg*p2_out=(union reg*)0x29;

void delay_timer();
void Delay1(unsigned int Value);
void delay_ms(unsigned int delay);


void main(void)
{
unsigned int i;
unsigned char j;
unsigned char k;
unsigned char m;
volatile unsigned char bien;
//unsigned char MaLed[]={0x4D,0x9B,0x6C,0xDB,0x4D,0x9B,0x6C,0xDB,0x79,0x9B,0x3C,0xDB,0x31,0x9B,0x38,0xDB,0x31,0x9B,0x38,0xDB,0x31,0x9B,0x3C,0xDF,0x37,0x9F,0x6C,0xCE};
//Dong lenh nay qua nhieu bit nen can chia ra// unsigned char MaLed[]={0xC66678F8C,0x1E666FCF8C,0x3F63CCCC0C,0x33618CCC0C,0x33618CCC1E,0x3363CCCC33,0x33666CCC33,0x33666CCC33};
unsigned char MaLed1[]={0x4D,0x4D,0x79,0x31,0x31,0x31,0x37,0x37}; //Ma cho MoRongP1
unsigned char MaLed2[]={0x9B,0x9B,0x9B,0x9B,0x9B,0x9B,0x9F,0x9F}; //Ma cho MoRongP1
unsigned char MaLed3[]={0x6C,0x6C,0x3C,0x38,0x38,0x3C,0x6C,0x6C}; //Ma cho MoRongP3
unsigned char MaLed4[]={0xDB,0xDB,0xDB,0xDB,0xDB,0xDF,0xCE,0xC4}; //Ma cho MoRongP4
/*unsigned char MaLed1[]={0x18,0x3C,0x7E,0x66,0x66,0x66,0x66,0x66}; //Ma cho MoRongP1
unsigned char MaLed2[]={0xCC,0xCC,0xC7,0xC3,0xC3,0xC7,0xCC,0xCC}; //Ma cho MoRongP1
unsigned char MaLed3[]={0xCF,0xDF,0x99,0x19,0x19,0x99,0xD9,0xD9}; //Ma cho MoRongP3
unsigned char MaLed4[]={0x1F,0x9F,0x98,0x98,0x98,0x98,0x98,0x98}; //Ma cho MoRongP4*/
//BCSCTL1 = CALBC1_1MHZ;
//DCOCTL = CALDCO_1MHZ;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
p1_dir->_BYTE = 0xFF; // Set P1 to output direction
p1_out->_BYTE = 0x00;

while(1){
//Phan nay_Dung phuong phap quet cot
p1_out->_BIT.b6 = 0; //OE = 0;
p1_out->_BIT.b6 = 1; //CLR = 1;
//Hieu ung Led cho tat (2 lan)
for(j=0;j<32;j++) //Cho Led ma tran sang het
{
//Cho 0 ra hang
p1_out->_BIT.b3 = 0; //Qua Transistor thanh muc 0
p1_out->_BIT.b4 = 1;
p1_out->_BIT.b4 = 0;
p1_out->_BIT.b5 = 1;
p1_out->_BIT.b5 = 0;
//Cho 1 ra cot
p1_out->_BIT.b0 = 0;
p1_out->_BIT.b1 = 1;
p1_out->_BIT.b1 = 0;
}
p1_out->_BIT.b2 = 1; //Xuat ra => tat ca led sang
p1_out->_BIT.b2 = 0;
//************************
Delay(50000);
Delay(50000);
//Delay(50000);
for(j=0;j<32;j++) //Cho Led ma tran tat het
{
//Cho 0 ra cot (Led tat)
p1_out->_BIT.b0 = 1;
p1_out->_BIT.b1 = 1;
p1_out->_BIT.b1 = 0;
}
p1_out->_BIT.b2 = 1; //Xuat ra => tat ca led tat
p1_out->_BIT.b2 = 0;
//********************
Delay(50000);
Delay(50000);
//Delay(50000);
for(j=0;j<32;j++) //Cho Led ma tran sang het
{
//Cho 1 ra cot (Led sang)
p1_out->_BIT.b0 = 0;
p1_out->_BIT.b1 = 1;
p1_out->_BIT.b1 = 0;
}
p1_out->_BIT.b2 = 1; //Xuat ra => tat ca led sang
p1_out->_BIT.b2 = 0;
//*********************
Delay(50000);
Delay(50000);
for(j=0;j<32;j++) //Cho Led ma tran tat het
{
//Cho 0 ra cot (Led tat)
p1_out->_BIT.b0 = 1;
p1_out->_BIT.b1 = 1;
p1_out->_BIT.b1 = 0;
}
p1_out->_BIT.b2 = 1; //Xuat ra => tat ca led tat
p1_out->_BIT.b2 = 0;
//********************
Delay(50000);
Delay(50000);
for(j=0;j<32;j++) //Cho Led ma tran sang het //Bat dau hieu ung mo mang
{
//Cho 1 ra cot (Led sang)
p1_out->_BIT.b0 = 0;
p1_out->_BIT.b1 = 1;
p1_out->_BIT.b1 = 0;
}
p1_out->_BIT.b2 = 1; //Xuat ra => tat ca led sang
p1_out->_BIT.b2 = 0;
//************************Hieu ung mo man*******************************************************
Delay(50000);
Delay(50000);
k = 15;
while(k > 0)//for(m=0;m<16;m++)
{
for(j=0;j<k;j++) //Cho cot 17 den 31 = 1 (Moi lan giam mot cot)
{
p1_out->_BIT.b0 = 0;
p1_out->_BIT.b1 = 1;
p1_out->_BIT.b1 = 0;
}
for(j=0;j<31-k*2+1;j++) //Cho cot 15, 16 = 0 (Moi lan giam 2 cot)
{
p1_out->_BIT.b0 = 1;
p1_out->_BIT.b1 = 1;
p1_out->_BIT.b1 = 0;
}
for(j=0;j<k;j++) //Cho cot 0 den 14 = 1 (Moi lan giam 1 cot)
{
p1_out->_BIT.b0 = 0;
p1_out->_BIT.b1 = 1;
p1_out->_BIT.b1 = 0;
}
p1_out->_BIT.b2 = 1; //Xuat ra (bat dau mo man)
p1_out->_BIT.b2 = 0;
Delay(50000);
k--; //Giam k de tiep tuc keo man (bo 2 cot giua tiep theo) (moi lan giam 2 cot sang)
}
//**************************************************************************************************************
j=0;
for(i=0;i<32;i++) //Xoa du lieu con luu lai o ngo ra (cot) cua 74595 do hieu ung truoc
{
p1_out->_BIT.b0 = 1;
p1_out->_BIT.b1 = 1; //Ck11
p1_out->_BIT.b1 = 0;
p1_out->_BIT.b2 = 1; //Ck12 => Xuat ra ngo ra 74595 quet cot
p1_out->_BIT.b2 = 0;
}
//p1_out->_BIT.b6 = 0; //CLR = 0 => CLR (Active low!)
p1_out->_BIT.b6 = 1; //CLR = 1
//Phan nay_Dung phuong phap quet hang//*********************************/////////////////////////////////////
for(m=0;m<20;m++) //Quet 100 lan (tuc la hien chu 100 lan moi co the nhin thay)
{
i=0;
//j=0;
p1_out->_BIT.b6 = 1; //clr = 1
p1_out->_BIT.b3 = 0; //Set du lieu vao DS de quet hang 0
while(i<8)//for(i=0;i<16;i++) //Quet 8 hang
{
p1_out->_BIT.b4 = 1; //Ck21
p1_out->_BIT.b4 = 0; //
p1_out->_BIT.b5 = 1; //Ck22 => Xuat ra ngo ra 74595 quet hang
p1_out->_BIT.b5 = 0; //
//MoRongP2(0x0000);
//MoRongP2(bien);
//Delay(5000);

//j=0;
/*for(j=0;j<32;j++)
{
MoRongP1(MaLed[j]);
if((j+1)%4==0){
p1_out->_BIT.b2=1; // Ck12 //Xuat du lieu ra 32 cot
p1_out->_BIT.b2=0;
}
}*/


MoRongP1(MaLed1[i]); //Lay ma hien chu ra Led ma tran tuong ung voi so hang i //MoRongP1 va MoRongP2 phai lien tiep
MoRongP2(MaLed2[i]);
MoRongP3(MaLed3[i]);
MoRongP4(MaLed4[i]);
//bien=bien<<1;
Delay(200);//__delay_cycles(7990);//Delay(2000);//__delay_cycles(7975);
//j++; //Tang j de xuat ma ke tiep khi quet cot tiep theo
p1_out->_BIT.b3 = 1;
i++;
}
p1_out->_BIT.b6 = 0; //clr = 0
//j = 0; //Bat dau xuat ma hang lai cho lan quet chu ke tiep
}
//***************DICH CHU TU TREN XUONG DUOI*****************************
/* unsigned int vitri;
unsigned char lap;
j=31; //Vi tri bat dau xuat chu
vitri=0;
//*****QUET HANG*********
p1_out->_BIT.b6 = 1; //clr = 1
p1_out->_BIT.b3 = 0; //Set du lieu vao DS de quet hang 0
vitri=1; //Hang 1
lap = 0;
while(vitri<=8)
{
while(lap<50)
{
i=0;
while(i<vitri)//for(i=0;i<16;i++) //Quet 8 hang
{
p1_out->_BIT.b4 = 1; //Ck21
p1_out->_BIT.b4 = 0; //
p1_out->_BIT.b5 = 1; //Ck22 => Xuat ra ngo ra 74595 quet hang
p1_out->_BIT.b5 = 0;
MoRongP1(MaLed1[7-i]); //Lay ma hien chu ra Led ma tran tuong ung voi so hang i //MoRongP1 va MoRongP2 phai lien tiep
MoRongP2(MaLed2[7-i]);
MoRongP3(MaLed3[7-i]);
MoRongP4(MaLed4[7-i]);
Delay(200);
i++;
if(lap==49)
{
vitri++;
lap = 0;
}
}
lap++;
}
}
*/
//**********************************************************************
}
}
//******************************************************************************
void Delay(unsigned int nLoop)
{
while(nLoop--);
}
//******************************************************************************
void MoRongP1(unsigned int e)
{
volatile unsigned int g,h; // Bien khai bao trong RAM
unsigned int f;
f=0x80; // 32 bit vi co 32 cot
g=8; // Vi co 32 cot
while(g>0) //
{
h = e&f; // h = e and f
if ( h == f) // Neu h=f
{
p1_out->_BIT.b0=0; // bit 3 port 1 len muc 1
}
else //
{
p1_out->_BIT.b0=1; // bit 3 port 1 muc 0
}
p1_out->_BIT.b1=1; //Ck21
p1_out->_BIT.b1=0; //
f=f>>1; // Xoay phai f
g--; //Lay bit ke tiep cua ma
}
//p1_out->_BIT.b2=1; // Ck12 //Khong xuat ra, chi xuat o MoRongP4 de xuat ra 32 bit luon
//p1_out->_BIT.b2=0; //
//__delay_cycles(40);
/*if(p1_out->_BIT.b3==1){ // Cho thoi gian Led sang lau 1 chut
Delay(10);
}*/
}
//**************Tiep phan mo rong***********Do qua nhieu bit nen phai tach ra
void MoRongP2(unsigned int d)
{
volatile unsigned int a,b; // Bien khai bao trong RAM
unsigned int c;
c=0x80; // 32 bit vi co 32 cot
a=8; // Vi co 32 cot
while(a>0) //
{
b = d&c; // h = e and f
if ( b == c) // Neu h=f
{
p1_out->_BIT.b0=0; // bit 3 port 1 len muc 1
}
else //
{
p1_out->_BIT.b0=1; // bit 3 port 1 muc 0
}
p1_out->_BIT.b1=1; //Ck21
p1_out->_BIT.b1=0; //
c=c>>1; // Xoay phai f
a--; //Lay bit ke tiep cua ma
}
//p1_out->_BIT.b2=1; // Ck22
//p1_out->_BIT.b2=0; //
}
void MoRongP3(unsigned int s)
{
volatile unsigned int t,u; // Bien khai bao trong RAM
unsigned int x;
x=0x80; // 32 bit vi co 32 cot
t=8; // Vi co 32 cot
while(t>0) //
{
u = s&x; // h = e and f
if ( u == x) // Neu h=f
{
p1_out->_BIT.b0=0; // bit 3 port 1 len muc 1
}
else //
{
p1_out->_BIT.b0=1; // bit 3 port 1 muc 0
}
p1_out->_BIT.b1=1; //Ck21
p1_out->_BIT.b1=0; //
x=x>>1; // Xoay phai f
t--; //Lay bit ke tiep cua ma
}
//p1_out->_BIT.b2=1; // Ck22
//p1_out->_BIT.b2=0; //
}
void MoRongP4(unsigned int p)
{
volatile unsigned int q,r; // Bien khai bao trong RAM
unsigned int y;
y=0x80; // 32 bit vi co 32 cot
q=8; // Vi co 32 cot
while(q>0) //
{
r = p&y; // h = e and f
if ( r == y) // Neu h=f
{
p1_out->_BIT.b0=0; // bit 3 port 1 len muc 1
}
else //
{
p1_out->_BIT.b0=1; // bit 3 port 1 muc 0
}
p1_out->_BIT.b1=1; //Ck21
p1_out->_BIT.b1=0; //
y=y>>1; // Xoay phai f
q--; //Lay bit ke tiep cua ma
}
p1_out->_BIT.b2=1; // Ck22
p1_out->_BIT.b2=0; //
}
//***************************************************************************
void LayTungBit(unsigned int ta,unsigned char vt) //vt la vi tri bit trong 8 bit cua MaLed b7b6b5b4b3b2b1b0//De dich chu sang trai => Xuat ra tu bit phai -> trai
{
volatile unsigned int tc; // Bien khai bao trong RAM
unsigned int td;
td=0x01; // 32 bit vi co 32 cot
td=td<<vt;
tc = ta&td; // h = e and f
if ( tc == td) // Neu h=f
{
p1_out->_BIT.b0=0; // bit 0 port 1 len muc 1
}
else //
{
p1_out->_BIT.b0=1; // bit 0 port 1 muc 0
}
p1_out->_BIT.b1=1; //Ck21
p1_out->_BIT.b1=0; //

p1_out->_BIT.b2=1; // Ck12 //Khong xuat ra, chi xuat o MoRongP4 de xuat ra 32 bit luon
p1_out->_BIT.b2=0; //
}

//***************************************************************************
void delay_timer()
{
TACCR0=5000;
TACTL=MC_1|ID_3|TASSEL_2|TACLR;
while (TACTL_bit.TAIFG == 0);
{
}
TACTL_bit.TAIFG=0;
}
void Delay1(unsigned int Value) // H?m DeLay
{
volatile unsigned int l = 0;
for(l=Value; l>0; l--);
}
//****************************************************
void delay_ms(unsigned int delay)
{
while (delay--)
{
__delay_cycles(8000);//__delay_cycles(PUT_CPU_CLOCK_SPEED_IN_HZ_DIVIDED_BY_1000_HERE);
}
}
//****************************************************

 

Các tin khác