本实验主要是将DS18b20与1602LCD显示屏、步进电机的联合调用。 实验原理: 本实验是一个便利项目小实验,用于完成单片机的课程设计,原理也比较简单主要是将步进电机用温度来进行控制,通过ds18b20对温度的感知,来完成对步进电机速度的控制,并且在1602上显示温度和步进电机的速度,为了方便我将步进电机直接插入main.c函数中并未单独列出。
实验原理图 实验仿真图
main.c
#include<reg52.h>
#include<stdio.h>
#include "18b20.h"
#include "1602.h"
#include "delay.h"
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P0
sbit A1=P1^0;
sbit B1=P1^1;
sbit C1=P1^2;
sbit D1=P1^3;
#define Coil_A1 {A1=1;B1=0;C1=0;D1=0;}
#define Coil_B1 {A1=0;B1=1;C1=0;D1=0;}
#define Coil_C1 {A1=0;B1=0;C1=1;D1=0;}
#define Coil_D1 {A1=0;B1=0;C1=0;D1=1;}
#define Coil_OFF {A1=0;B1=0;C1=0;D1=0;}
bit ReadTempFlag;
void Init_Timer0(void);
void UART_Init(void)
{
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xFD;
TR1 = 1;
TI=1;
}
float Speed=0;
char dianji[16];
int temp;
float temperature;
char displaytemp[16];
void main (void)
{
LCD_Init();
DelayMs(20);
LCD_Clear();
Init_Timer0();
UART_Init();
Lcd_User_Chr();
LCD_Write_Char(13,1,0x01);
LCD_Write_Char(14,1,'C');
LCD_Init();
while (1)
{
if(ReadTempFlag==1)
{
ReadTempFlag=0;
temp=ReadTemperature();
temperature=(float)temp*0.0625;
if(temperature<28)
{
Speed=7;}
else if((temperature>=28)&&(temperature<30))
{Speed=5;}
else
{Speed=3;}
sprintf(dianji,"Speed % 7.3f",Speed);
LCD_Write_String(0,0,dianji);
sprintf(displaytemp,"Temp % 7.3f",temperature);
LCD_Write_String(0,1,displaytemp);
}
Coil_A1
DelayMs(Speed);
Coil_B1
DelayMs(Speed);
Coil_C1
DelayMs(Speed);
Coil_D1
DelayMs(Speed);
}
}
void Init_Timer0(void)
{
TMOD |= 0x01;
EA=1;
ET0=1;
TR0=1;
}
void Timer0_isr(void) interrupt 1
{
static unsigned int num;
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
num++;
if(num==300)
{
num=0;
ReadTempFlag=1;
}
}
18b20.c
#include"delay.h"
#include"18b20.h"
bit Init_DS18B20(void)
{
bit dat=0;
DQ = 1;
DelayUs2x(5);
DQ = 0;
DelayUs2x(200);
DelayUs2x(200);
DQ = 1;
DelayUs2x(50);
dat=DQ;
DelayUs2x(25);
return dat;
}
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat|=0x80;
DelayUs2x(25);
}
return(dat);
}
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
DelayUs2x(25);
DQ = 1;
dat>>=1;
}
DelayUs2x(25);
}
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
DelayMs(10);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
a=ReadOneChar();
b=ReadOneChar();
b<<=8;
t=a+b;
return(t);
}
1602.c
名称:LCD1602
引脚定义如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK
------------------------------------------------*/
#include "1602.h"
#include "delay.h"
sbit RS = P2^4;
sbit RW = P2^5;
sbit EN = P2^6;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P0
bit LCD_Check_Busy(void)
{
DataPort= 0xFF;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80);
}
void LCD_Write_Com(unsigned char com)
{
DelayMs(5);
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
void LCD_Write_Data(unsigned char Data)
{
DelayMs(5);
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
while (*s)
{
LCD_Write_Data( *s);
s ++;
}
}
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
void LCD_Init(void)
{
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08);
LCD_Write_Com(0x01);
LCD_Write_Com(0x06);
DelayMs(5);
LCD_Write_Com(0x0C);
}
void Lcd_User_Chr(void)
{
LCD_Write_Com(0x40);
LCD_Write_Data(0x00);
LCD_Write_Com(0x41);
LCD_Write_Data(0x04);
LCD_Write_Com(0x42);
LCD_Write_Data(0x0e);
LCD_Write_Com(0x43);
LCD_Write_Data(0x0e);
LCD_Write_Com(0x44);
LCD_Write_Data(0x0e);
LCD_Write_Com(0x45);
LCD_Write_Data(0x1f);
LCD_Write_Com(0x46);
LCD_Write_Data(0x04);
LCD_Write_Com(0x47);
LCD_Write_Data(0x00);
LCD_Write_Com(0x48);
LCD_Write_Data(0x03);
LCD_Write_Com(0x49);
LCD_Write_Data(0x03);
LCD_Write_Com(0x4a);
LCD_Write_Data(0x00);
LCD_Write_Com(0x4b);
LCD_Write_Data(0x00);
LCD_Write_Com(0x4c);
LCD_Write_Data(0x00);
LCD_Write_Com(0x4d);
LCD_Write_Data(0x00);
LCD_Write_Com(0x4e);
LCD_Write_Data(0x00);
LCD_Write_Com(0x4f);
LCD_Write_Data(0x00);
}
delay.c
#include "delay.h"
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
|