Code:
//=============led matrix 8x32=============//
//thay doi toc do dich o bien td( cang nho chay cang nhanh)//
//blink la bien cho hien thi cac mau xen ke nhau
//Array la mang hien thi gom 72 phan tu tuc la 32 ki tu se duoc luu o day de cho hien thi
//de hien thi chuoi ki tu dung lenh send_str
#include<AT89X52.h>
#include<ds1820.c>
const green=0,red=1,orange=2,blink=3;
unsigned int save=0;
unsigned char td=5,g_r=0,d=0,temp=0;
unsigned char Temperature[4]={0,0,87,'C'};
unsigned char Array[72]={62,65,65,65,62,0,17,33,127,1,1,0};
unsigned char code ma[]={0,
0,62,65,65,0,0, //( 0
0,65,65,62,0,0, //) 1
16,56,60,56,16,0, //* 2
8,8,127,8,8,0, //+ 3
0,0,9,18,28,0, //, 4
8,8,8,8,8,0, //- 5
0,7,5,7,0,0, //. 6
1,2,28,32,64,0, /// 7
62,65,65,65,62,0, //0 8
17,33,127,1,1,0, //1 9
35,69,73,73,49,0, //2 10
34,65,73,73,54,0, //3 11
12,20,36,79,4,0, //4 12
114,73,73,73,70,0, //5 13
62,73,73,73,38,0, //6 14
65,66,68,72,112,0, //7 15
54,73,73,73,54,0, //8 16
50,73,73,73,62,0, //9 17
0,0,99,99,0,0, //: 18
0,0,101,102,0,0, //; 19
0,8,20,34,65,0, //< 20
9,9,9,9,9,0, //= 21
65,34,20,8,0,0, //> 22
0,48,64,77,48,0, //? 23
62,65,89,56,0,0, //@ 24
63,68,68,68,63,0, //A 25
127,73,73,73,54,0, //B 26
62,65,65,65,34,0, //C 27
127,65,65,65,62,0, //D 28
127,73,73,73,65,0, //E 29
127,72,72,72,64,0, //F 30
62,65,73,73,46,0, //G 31
127,8,8,8,127,0, //H 32
65,65,127,65,65,0, //I 33
70,65,65,126,64,0, //J 34
127,8,20,34,65,0, //K 35
127,1,1,1,1,0, //L 36
127,32,24,32,127,0, //M 37
127,32,16,8,127,0, //N 38
62,65,65,65,62,0, //O 39
127,68,68,68,56,0, //P 40
62,65,69,66,61,0, //Q 41
127,68,68,74,49,0, //R 42
50,73,73,73,38,0, //S 43
64,64,127,64,64,0, //T 44
126,1,1,1,126,0, //U 45
124,2,1,2,124,0, //V 46
127,2,12,2,127,0, //W 47
65,34,28,34,65,0, //X 48
96,16,15,16,96,0, //Y 49
67,69,73,81,97,0, //Z 50
0,62,65,65,0,0, //[ 51
64,32,28,2,1,0, //\ 52
0,65,65,62,0,0, //] 53
16,32,64,32,16,0, //^ 54
1,1,1,1,1,0, //_ 55
0,0,64,32,16,0, //` 56
4,42,42,42,28,0, //a 57
126,18,18,18,12,0, //b 58
28,34,34,34,20,0, //c 59
12,18,18,20,126,0, //d 60
28,42,42,16,0,0, //e 61
4,62,72,64,32,0, //f 62
24,37,37,37,30,0, //g 63
126,16,16,16,14,0, //h 64
0,92,2,2,4,0, //i 65
4,2,2,92,0,0, //j 66
126,8,20,34,0,0, //k 67
124,2,2,2,0,0, //l 68
30,32,30,32,30,0, //m 69
62,8,16,62,0,0, //n 70
28,34,34,34,28,0, //o 71
31,36,36,36,24,0, //p 72
24,36,36,36,31,0, //q 73
0,30,32,32,24,0, //r 74
18,42,42,42,36,0, //s 75
0,32,124,34,4,0, //t 76
60,2,2,2,60,0, //u 77
56,4,2,4,56,0, //v 78
56,2,12,2,56,0, //w 79
34,20,8,20,34,0, //x 80
48,8,14,8,48,0, //y 81
34,38,42,50,34,0, //z 82
8,54,65,65,65,0, //{ 83
0,0,127,0,0,0, //| 84
65,65,65,54,8,0, //} 85
12,16,12,2,12,0, //~ 86
0,0,112,80,112,0, //dau o 87
0,0,0,0,0,0, //khoang trang 88
255};
//chuong trinh tao thoi gian tre de hien thi
void delay(unsigned int loop)
{
unsigned int x,y;
for (x=0;x<loop;x++)
{
for(y=0;y<13;y++);
}
}
//=============================================================================//
//chuong trinh chuyen ki tu can hien thi ra ma font
void Convert(unsigned int source)
{
unsigned int position,i;
position=(source>39)? (source-40)*6:0;
position=(source==32)? 88*6:position;
position=(source==87)? 87*6:position;
for(i=position;i<(position+6);i++)
{
Array[d]=ma[i];
if(source==0) Array[d]=0;
d++;
}
}
//=============================================================================//
//chuong trinh gui ma ra led
void send(unsigned int position,color)
{
unsigned int y=0;
unsigned char Color=0,x;
save=position; //de chuong trinh wait biet vi tri dang hien thi
for(x=16;x<48;x++) //16 la bat dau quet, do chan e cua 74154 (tich cuc muc 0) noi voi bit 5
{
if(color==0) Color=64; //green
if(color==1) Color=128; //red
if(color==2) Color=0; //orange
if(color==3)
{
if(Color==64) Color=128;
else Color=(Color==128) ? 0:64;
}
y=position;
if(position>72) y=0;
P0=Color|x; //dua vao 2 bit cao cua P0 dong thoi cho ma quet cot vao day
P2=Array[y]&255; //xuat ma hien thi ra P2
delay(3);
P0=255;
P2=0;
delay(2);
position--;
}
}
//=============================================================================//
//chuong trinh delay chay chu
void wait(unsigned char loop)
{
unsigned char x;
for(x=0;x<=loop;x++)
{
send(save,g_r);
}
}
//=============================================================================//
//chuong trinh xuat 1 chuoi ki tu ra led matrix
//color la bien chon mau 0:green, 1: red, 2:orange
//position la vi tri bat dau can hien thi ra led matrix (32->64)
//shi la bit chon dich ki tu 0:dung 1:dich(phai qua trai)
void send_str(unsigned char *str,unsigned char color,unsigned char position,bit shi)
{
unsigned char x,y=0;
g_r=color;
//nap font 12 ki tu vao mang Array de hien thi
d=0;
y=0;
while(y<12)
{
if(*str!='\0')
{
Convert(*str);
str++;
}
if(*str=='\0') Convert(0);
y++;
}
//----------------------------------------------------
//cho 12 ki tu cua chuoi chay tu dau vao
//font chu co kich thuoc la 7X6(6 la do co khoang trang giua 2 chu)
//phai bo qua 32 lan dich de bat dau ngay tu vi tri so ko
//vay de chay het 12 ki tu can 12*6=72 lan dich
for(x=position;x<(unsigned char)((shi)? 72:position+1);x++) //vong lap cho cac chu chay tu phai sang trai
{ //save de lap lai viec xuat du lieu nham tao toc do chu chay
send(x,g_r);
wait(td);
if(color==4) //sang thay doi mau
{
if((x%5)==0) g_r++;
g_r=(g_r>3) ? 0:g_r;
}
}
//---------------------------------------------------
// //tiep tuc chay tu ki tu thu 13 cua chuoi tren led matrix
if(shi==1)
{
while(*str!='\0')
{
for(x=0;x<72;x++) //dao ma font cua 6 ki tu sau len dau mang Array de dong chu chay ko bi ngat quan
{
Array[x]=Array[x+36];
}
d=36; //nap lai font cua 6 ki tu tiep theo
y=0;
while(y<6)
{
if(*str!='\0')
{
Convert(*str);
str++;
}
if(*str=='\0') Convert(0);
y++;
}
//cho hien thi 12 ki tu cua chuoi co trong mang Array bat dau o vi tri
//de dong chu chay duoc lien tuc can quet lai 6 ki tu phia sau
//6(6 ki tu dau da quet)*6(kich thuoc cua fone)=36
for(x=36;x<72;x++) //vong lap cho cac chu chay tu phai sang trai
{ //save de lap lai viec xuat du lieu nham tao toc do chu chay
send(x,g_r);
wait(td);
if(color==4) //sang thay doi mau
{
if((x%5)==0) g_r++;
g_r=(g_r>3) ? 0:g_r;
}
}
//---------------------------------------------
}
for(x=72;x<104;x++) //vong lap cho cac chu chay tu phai sang trai
{ //save de lap lai viec xuat du lieu nham tao toc do chu chay
send(x,g_r);
wait(td);
if(color==4) //sang thay doi mau
{
if((x%5)==0) g_r++;
g_r=(g_r>3) ? 0:g_r;
}
}
}
}
//=============================================================================//
//chuong trinh doc nhiet do ds1820
void Tem()
{
unsigned char i;
ReadTemp(&Temperature[0]);
temp=(Temperature[0]>>3)|(Temperature[1]<<5);
temp=temp>>1;
g_r=(temp>29) ? 1:0;
Temperature[1]=(temp%10)+48;
Temperature[0]=(temp/10)+48;
for(d=0;d<72;d++)
{
Array[d]=0;
}
d=0;
for(i=0;i<4;i++)
{
Convert(Temperature[i]);
}
for(i=0;i<20;i++)
{
send(28,g_r);
wait(td);
}
}
//=============================================================================//
//chuong trinh chinh
main()
{
unsigned char x=0;
P0=255;
P1=255;
P2=0;
P3=255;
Config(0,0,31); //cai che do 9 bit cho ds18b20
while(1)
{
send_str("Nhiet do phong hien tai la:",orange,0,1);
Tem();
if(temp>29) send_str("Nhiet do hien tai kha cao, tot nhat ban nen di tam va di sieu thi.",red,0,1);
if(temp<=29) send_str("Nhiet do trong phong kha mat me, ban hay o nha va ngu that sau.",green,0,1);
// send_str("0123456",0,0,1);
}
}
//=============================================================================//
File DS18B20.c:
//---------------------------------------
// DS1820 driver
//
//---------------------------------------
sbit DQ = P1^0; // connect with DS1820 Data pin
//----------------------------------------
// DELAY at 11.0592MHz crystal.
// each count takes another 17us.
//----------------------------------------
void DelayUs(int us)
{
int i;
for (i=0; i<us; i++);
}
//----------------------------------------
// Reset DS1820
//----------------------------------------
bit ResetDS1820(void)
{
bit presence=1;
while(presence)
{
DQ = 0; //pull DQ line low
DelayUs(29); // leave it low for about 490us
DQ = 1; // allow line to return high
DelayUs(3); // wait for presence 55 uS
presence = DQ; // get presence signal
DelayUs(25); // wait for end of timeslot 316 uS
}
return(presence); // presence signal returned
}
//-----------------------------------------
// Read one bit from DS1820
//-----------------------------------------
bit ReadBit(void)
{
unsigned char i=0;
DQ = 0; // pull DQ low to start timeslot
DQ=1;
for (i=0; i<3; i++); // delay 17 us from start of timeslot
return(DQ); // return value of DQ line
}
//-----------------------------------------
// Write one bit to DS1820
//-----------------------------------------
void WriteBit(bit Dbit)
{
unsigned char i=0;
DQ=0;
DQ = Dbit;
DelayUs(5); // delay about 39 uS
DQ = 1;
}
//-----------------------------------------
// Read 1 byte from DS1820
//-----------------------------------------
unsigned char ReadByte(void)
{
unsigned char i;
unsigned char Din = 0;
for (i=0;i<8;i++)
{
Din|=ReadBit()? 0x01<<i:Din; //FIFO
DelayUs(6);
}
return(Din);
}
//-----------------------------------------
// Write 1 byte
//-----------------------------------------
void WriteByte(unsigned char Dout)
{
unsigned char i;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
WriteBit((bit)(Dout & 0x1)); // write bit in temp into
Dout = Dout >> 1;
}
DelayUs(5);
}
//-----------------------------------------
// config for ds18b20
//-----------------------------------------
void Config(unsigned char A_th,A_tl,mode)
{
ResetDS1820();
WriteByte(0x4e);
WriteByte(A_th);
WriteByte(A_tl);
WriteByte(mode);
}
//-----------------------------------------
// Read temperature
//-----------------------------------------
void ReadTemp(unsigned char * buff)
{
unsigned char n;
EA=0; // disable all interrupt
ResetDS1820();
WriteByte(0xcc); // skip ROM
WriteByte(0x44); // perform temperature conversion
while (ReadByte()==0xff); // wait for conversion complete
ResetDS1820();
WriteByte(0xcc); // skip ROM
WriteByte(0xbe); // read the result
for (n=0; n<2; n++) // read 9 bytes but, use only two byte
{
buff[n]=ReadByte(); // read DS1820
}
EA=1;
}
( File Mô Phỏng + Code)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút hiện ra ở góc phải phía trên, nhấn vào để đến trang download
Nguồn : EchipKool - Chia sẻ kiến thức - Kết nối đam mê điện tử
Đăng nhận xét