⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1602c.c

📁 是时钟与温度传感器的例子
💻 C
字号:
//采用单独口线方式驱动液晶
#include <reg52.H>
#include <ABSACC.H>
#include <INTRINS.H>
#include "DS1302.H"


#define lcd_data_port P0
sbit lcd_RS = P2^0 	;
sbit lcd_RW = P2^1 	;
sbit lcd_E =  P2^2	;	
sbit busy =P0^7;

//extern void Get_temperarue(void);
//extern uchar DataH,DataL;
//xtern unsigned long Data;//ds18b20数据
unsigned char TempBuffer[6];



/*----------------------------------函数声明------------------------------------------*/
void lcd_init(void);
void lcd_write_command(unsigned char command,unsigned char wait_en);
void lcd_write_data(unsigned char char_data);
void wait_enable(void);
void display_a_char(unsigned char position,unsigned char char_data);
void display_a_string(unsigned char col,unsigned char *ptr);
void delay_1ms(void);
void delay_nms(unsigned int n);
void initial(void);
void IntToStr(unsigned int t, unsigned char *str);
ReadTemperature(void);
//显示的字符串
unsigned char str1[]="D tebnucee wendu";
unsigned char str2[]="T mehongfe     ";

//unsigned char str1[]="-----@good@-----";
//unsigned char str2[]="--  wangshu  ---";
//unsigned char count=0;

/*------------------------------------主函数-------------------------------------------*/
void main(void)
{  
	  unsigned int i,a;  
	 SYSTEMTIME CurrentTime;
     //P2=0x00;
	 //delay_nms(100);
     lcd_init();       //液晶初始化
	 Initial_DS1302();//启动时钟芯片
	 
	 
// display_a_char(5,'d');	//该函数显示单个字符,5表示显示的位置(0-31)


  //do
   for(;;)
   {    

       
        i=ReadTemperature(); //读温度
        a=i;

        IntToStr(i,TempBuffer);

        //Get_temperarue();
        DS1302_GetTime(&CurrentTime);
		DateToStr(&CurrentTime);
		TimeToStr(&CurrentTime);
		str1[2]=CurrentTime.DateString[0];
        str1[3]=CurrentTime.DateString[1];
        str1[4]=CurrentTime.DateString[2];
	    str1[5]=CurrentTime.DateString[3];
        str1[6]=CurrentTime.DateString[4];
        str1[7]=CurrentTime.DateString[5];
		str1[8]=CurrentTime.DateString[6];
        str1[9]=CurrentTime.DateString[7];

		


	    str2[2]=CurrentTime.TimeString[0];
        str2[3]=CurrentTime.TimeString[1];
        str2[4]=CurrentTime.TimeString[2];
	    str2[5]=CurrentTime.TimeString[3];
        str2[6]=CurrentTime.TimeString[4];
        str2[7]=CurrentTime.TimeString[5];
		str2[8]=CurrentTime.TimeString[6];
        str2[9]=CurrentTime.TimeString[7];
		//str2[13]=Data/100+48;
        //str2[14]=(Data%100)/10+48;
        //str2[15]=(Data%100)%10+48;

		str2[11]=TempBuffer[0];
        str2[12]=TempBuffer[1];
        str2[13]=TempBuffer[2];
		str2[14]=TempBuffer[3]; 
		str2[15]=TempBuffer[4]; 
	    display_a_string(0,str1);	//0表示显示在第一行
        display_a_string(1,str2);//1表示显示在第二行
	    delay_nms(1000);
		//while(1);
  }
       //while(1)	;
       //P0=0xff;

}
 
/*------------------------------------液晶驱动函数-------------------------------------*/
//液晶初始化
void lcd_init(void)
{lcd_write_command(0x38,0);//显示模式设置三次(此时不管lcd空闲与否)
 delay_nms(5);
 lcd_write_command(0x38,0);
 delay_nms(5);
 lcd_write_command(0x38,0);
 delay_nms(5);
 
 lcd_write_command(0x38,1);//显示模式设置(从此之后均需lcd空闲)
 lcd_write_command(0x08,1);//显示关闭
 lcd_write_command(0x01,1);//显示清屏
 lcd_write_command(0x06,1);//显示光标移动设置
 lcd_write_command(0x0c,1);//显示开及光标设置
}

//写指令函数: E=高脉冲 RS=0 RW=0
void lcd_write_command(unsigned char command,unsigned char wait_en)//command为指令,wait_en指定是否要检测LCD忙信号
{if(wait_en) wait_enable();//若wait_en为1,则要检测LCD忙信号,等待其空闲
 lcd_RS=0; //RS=0
 lcd_RW=0; //RW=0
 lcd_data_port=command;
 lcd_E=0;//E=0,下面给LCD一个高脉冲
 _nop_();
 lcd_E=1;//E=1
 _nop_();
 lcd_E=0;//重设E=0
}

//写数据函数: E =高脉冲 RS=1 RW=0
void lcd_write_data(unsigned char char_data)
{wait_enable();//等待LCD空闲
 lcd_data_port=char_data;
 lcd_RS=1;//RS=1
 lcd_RW=0;//RW=0
 lcd_E=0;//E=0,下面给LCD一个高脉冲
 _nop_();
 lcd_E=1;//E=1
 _nop_();
 lcd_E=0;//重设E=0
}

//正常读写操作之前必须检测LCD控制器状态:E=1 RS=0 RW=1;DB7: 0 LCD控制器空闲,1 LCD控制器忙。
//检测忙信号,等待LCD空闲函数
void wait_enable(void)
{
  lcd_RS=0;//RS=0
  lcd_RW=1;//RW=1
  lcd_E=1;//E=1 
  _nop_();
//  while(busy);  //等待LCD_DB7为0
  delay_nms(1);
  lcd_E=0;//E=0
}

//指定位置显示一个字符:第一行位置0~15,第二行16~31
//显示一个字符函数
void display_a_char(unsigned char position,unsigned char char_data)//参数position指定位置0~31,char_data为要显示的字符
{
 unsigned char position_tem;
 if(position>=0x10)
 position_tem=position+0xb0;
 else
 position_tem=position+0x80;
 lcd_write_command(position_tem,1);
 lcd_write_data(char_data);
}

//指定一行显示连续字符串:0显示在第一行,1显示在第二行,注字符串不能长于16个字符
//显示一行连续字符串函数
void display_a_string(unsigned char col,unsigned char *ptr)//参数col指定行,*ptr指字符串数组的首指针
{
 unsigned char col_tem,i;
 col_tem=col<<4;//若col为1(即在LCD第二行显示字符串),先把col左移4位,使显示字符的首位置改到第二行首位,即位置16
 for(i=0;i<16;i++)
 display_a_char(col_tem++,*(ptr+i));
 
}

/*---------------------------------------延时函数-------------------------------------*/
//1ms延时函数
void delay_1ms(void)
{
 unsigned int i;
 for(i=0;i<120;i++);
}

//n ms延时函数
void delay_nms(unsigned int n)
{
 unsigned int i;
 for(i=0;i<n;i++)delay_1ms();
}



//*******************************ds18b20
sbit DQ =P2^7;   //定义通信端口
//延时函数
void delay(unsigned int i)
{
 	while(i--);
}

//初始化函数
Init_DS18B20(void)
{
	 unsigned char x=0;
	 DQ = 1;    //DQ复位
	 delay(8);  //稍做延时
	 DQ = 0;    //单片机将DQ拉低
	 delay(80); //精确延时 大于 480us
	 DQ = 1;    //拉高总线
	 delay(14);
	 x=DQ;      //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
	 delay(20);
}

//读一个字节
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;
		  delay(4);
	 }
 	return(dat);
}

//写一个字节
WriteOneChar(unsigned char dat)
{
 unsigned char i=0;
 for (i=8; i>0; i--)
 {
  DQ = 0;
  DQ = dat&0x01;
  delay(5);
  DQ = 1;
  dat>>=1;
 }
//delay(4);
}

//读取温度
ReadTemperature(void)
{
	unsigned char a=0;
	unsigned char b=0;
	unsigned int t=0;
	
	Init_DS18B20();
	WriteOneChar(0xCC); // 跳过读序号列号的操作
	WriteOneChar(0x44); // 启动温度转换
	Init_DS18B20();
	WriteOneChar(0xCC); //跳过读序号列号的操作
	WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
	a=ReadOneChar();
	b=ReadOneChar();
	t=b;
	t<<=8;
	t=t|a;
	t=t*0.625;//有效位到小数点后1位
	return(t);
}
//ds18b20结束



void IntToStr(unsigned int t, unsigned char *str) 
{

	str[0]=t/100+'0';   //百位,+'0'为数字转换成ASCII码
	str[1]=(t/10)%10+'0';//十位
	str[3]=t%10+'0';    //个位
	str[2]='.'; 
	str[4]='C';

} 





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -