Học liệu

Điều khiển quạt gió

  • 10/09/2019
  • Học liệu


#include "string.h"
#include "io430.h"
#include "lcd.h"
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*_P2_DIRECT = (union reg*)0x2A ;

union reg* _P2_SEL = (union reg*)0x2E ;

union reg* _P2_OUT = (union reg*)0x29 ;

union reg*_P2_IN = (union reg*)0x28 ;

union reg*_P1_DIRECT = (union reg*)0x22 ;

union reg*_P1_OUT = (union reg*)0x21 ;

union reg*_P1_IN = (union reg*)0x20 ;
#define EN (_P2_OUT->_BIT.b2)
#define RW (_P2_OUT->_BIT.b1)
#define RS (_P2_OUT->_BIT.b0)
#define LCDdata (P1OUT)
#define DQ _P2_DIRECT->_BIT.b3
#define DQIN _P2_IN->_BIT.b3
#define DQOUT _P2_OUT->_BIT.b3

unsigned char x;

void wait(void)
{
int n;
for(n=0;n<1000;n++)
{
}
EN=0;// Dua xung cao xuong thap de chot
}
void LCDcontrol(unsigned char x)
{

}

void LCDinit(void)
{

}

void LCDwrite(unsigned char c)
{
EN=1;// Cho fep muc cao
RS=1;// Ghi du lieu
RW=0;// Ghi len LCD
LCDdata=c;// Gia tri C
EN=0;// Xung cao xuong thap
wait();// Cho
}

void LCDputs( char s[],unsigned char row)
{
char len;

}

}

void LCDwritei(int d)
{
unsigned char i,j,k,l;
LCDwrite(48+j);
LCDwrite(48+i);
}

// *****************************************
// CHUONG TRINH DELAY
void delay_18B20(unsigned int i)
{
while(i--);
}
//=KHOI TAO CAM BIEN(RESET)
void Init_DS18B20()
{

DQ = 1;
DQOUT=1;
delay_18B20(8);
DQOUT = 0;
delay_18B20(80);
DQ = 1;
delay_18B20(14);
DQ=0;
x=DQIN;
delay_18B20(20);
}
//=DOC MOT BYTE
unsigned char Read_byte(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 1; //CHO DUONG DQ XUONG MUC THAP
DQOUT=0;
dat>>=1;
DQOUT = 0; //CHO DUONG DQ LEN MUC CAO
DQ=0;
if(DQIN)
dat|=0x80;
delay_18B20(4);
}
return(dat);
}
//========================
//=CHUONG TRINH GHI 1BYTE
void Write_byte(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ=1;
DQOUT = 0;
DQOUT = dat&0x01;
delay_18B20(5);
DQOUT = 1;
dat>>=1;
}
}
//========================
//=CHUONG TRINH DOC NHIET DO TU CAM BIEN
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;

Init_DS18B20();
Write_byte(0xCC);
Write_byte(0x44);
delay_18B20(100);
Init_DS18B20();
Write_byte(0xCC);
Write_byte(0xBE);
a=Read_byte();
b=Read_byte();
t = (b*256+a)*25;
return( t >> 2 );
}

// ham dieu che xung

void Delay(long int nLoop);
//**********************************************************************
void Delay(long int nLoop)
{
while(nLoop--);
}
//***********************************************************************
//=============================ham tao chu chay==========================
void delay_ms(unsigned int i)
{
unsigned int j;
while(i-->0)
{
for(j=0;j<5;j++)
{
;
}
}
}
void display_lcd(unsigned char *s)
{
while(*s)
{

delay_ms(4000);
LCDwrite(*s++);
}
}
void htphong(void)

{ LCDinit();
LCDcontrol(0x01);
LCDcontrol(0x82);
display_lcd("HOW ARE YOU? ");
delay_ms(10);
LCDcontrol(0x01);
LCDcontrol(0x40+0x8F);
LCDcontrol(0x07);
display_lcd(" CHAO THAY VA CAC BAN! ");
LCDcontrol(0x01);
LCDinit();
LCDcontrol(0x80);
display_lcd("DE TAI VI XU LY:");
delay_ms(10);
LCDcontrol(0x41+0x80);
display_lcd("QUAT THONG MINH ");
delay_ms(10);
LCDcontrol(0x01);
LCDcontrol(0x80);
display_lcd("GVHD: Th.s");
LCDcontrol(0x42+0x80);
display_lcd("Tran Huu Danh ");
delay_ms(10);
LCDcontrol(0x01);
LCDcontrol(0x8F);
LCDcontrol(0x07);
display_lcd("SINH VIEN THUC HIEN: ");
LCDcontrol(0x01);
LCDcontrol(0x8F);
LCDcontrol(0x07);
display_lcd("Huynh Thanh Phong 1091056 ");
LCDcontrol(0x01);
LCDcontrol(0X40+0x8F);
LCDcontrol(0x07);
display_lcd("Luong Van Hong Y 1090999 ");
LCDcontrol(0x01);
}
//=========================================================================
//...............chuong trinh chinh..................
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;

P1DIR=0xff;
unsigned int tmp;
int a,b,c,d;
_P2_DIRECT->_BIT.b0=1;
_P2_DIRECT->_BIT.b1=1;
_P2_DIRECT->_BIT.b2=1;
_P2_SEL->_BIT.b7=0;

LCDinit();

htphong();// goi ham chay chu
LCDinit();

while(1)
{

tmp = ReadTemperature();

if (tmp>3500) CCR1 =tmp/35;
else CCR1 =0;
//==============================================================//
//...........................dieu che do rong xung.....................
if(1&_P2_IN->_BIT.b4)
{
if(tmp<=3400 & tmp>=3100)
{
_P2_OUT ->_BIT.b5=1;
__delay_cycles(1000000);
_P2_OUT ->_BIT.b5=0;
__delay_cycles(100);
}

else
{
if(tmp<3100 & tmp>=3000)
{
_P2_OUT ->_BIT.b5=1;
__delay_cycles(100000);
_P2_OUT ->_BIT.b5=0;
__delay_cycles(1000);
}
else
{
if(tmp<3000 & tmp>=2900)
{
_P2_OUT ->_BIT.b5=1;
__delay_cycles(40000);
_P2_OUT ->_BIT.b5=0;
__delay_cycles(100);
}
else
{
if(tmp<2900 & tmp>=2800)
{
_P2_OUT ->_BIT.b5=1;
__delay_cycles(30000);
_P2_OUT ->_BIT.b5=0;
__delay_cycles(100);
}
else
{
if(tmp<2800 & tmp>=2700)
{
_P2_OUT ->_BIT.b5=1;
__delay_cycles(20000);
_P2_OUT ->_BIT.b5=0;
__delay_cycles(100);
}
else
{

_P2_OUT ->_BIT.b5=0;


}
}
}
}
}
}

else
{
if(1&_P2_IN->_BIT.b6)
{
_P2_OUT ->_BIT.b5=1;
__delay_cycles(600000);
_P2_OUT ->_BIT.b5=0;
__delay_cycles(1000);
}
else
{
if(1&_P2_IN->_BIT.b7)

{
_P2_OUT ->_BIT.b5=1;
__delay_cycles(1000);
_P2_OUT ->_BIT.b5=0;
__delay_cycles(1000);

}

else
{

_P2_OUT ->_BIT.b5=0;
}

}
}

//........================= hien thi LCD=================...........
LCDputs("good luck to you!",1);

if( tmp>=3200)
{
LCDputs ("NONG QUA:",2);
}
else
{
if( 2900<tmp & tmp<3200)
{
LCDputs ("MO QUAT :",2);
}
else
{
if( tmp<2900 )
{
LCDputs ("LANH QUA:",2);
}
}
}

a=tmp/1000;
b=(tmp/100)%10 ;
c=(tmp/10)%10;
d=tmp%10;
LCDwrite(a+48);
LCDwrite(b+48);
LCDwrite(46);
LCDwrite(c+48);
LCDwrite(d+48);
LCDwrite(32);
LCDwrite(67);
}
}

Các tin khác