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

📄 main.c

📁 arm7下的温度采集和显示程序
💻 C
字号:
#include "config.h"
#include "ks0108.h"
#define DQBIT  6
#define DQ  1<<DQBIT 

uint8 Dsp_buf[4];

uint8 LCDExist=1;
void Delay2(unsigned int i) 
{ 
	uint32 n;
	while(i--)
	for(n=0;n<50;n++); 
} 
//初始化函数 
uint8 Init_DS18B20(void) 
{ 
	//wtdg=!wtdg;
	uint8 n;
	IO0DIR |=DQ; 
	IO0SET=DQ; //DQ复位 
	Delay2(8); //稍做延时 
	IO0CLR=DQ; //单片机将DQ拉低 
	Delay2(80); //精确延时 大于 480us 
	IO0SET=DQ; //拉高总线 
	IO0DIR &=~(DQ);
	Delay2(14); 
	n=(IO0PIN>>DQBIT)&0x01; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败 
	Delay2(20); 
	IO0SET=DQ; 
	return(n);
} 

//读一个字节 
uint8 ReadOneChar(void) 
{ 
	uint8 i,dat = 0; 
	//wtdg=!wtdg; 
	for (i=8;i>0;i--) 
	{ 
		IO0DIR |=DQ;
		IO0CLR=DQ; 	// 给脉冲信号 
		dat>>=1; 
		IO0SET = DQ; // 给脉冲信号 
		IO0DIR &=~(DQ);
		Delay2(4);
		if((IO0PIN>>DQBIT)&0x01)
			dat|=0x80; 
		Delay2(4); 
	} 
	return(dat); 
} 

//写一个字节 
void WriteOneChar(uint8 dat) 
{ 
	uint8 i;
	//dg=!wtdg; 
	for (i=8; i>0; i--) 
	{ 
		IO0DIR |=DQ;
		IO0CLR=DQ; 
		if((dat&0x01)==1)
		   IO0SET=DQ;
		else
		   IO0CLR=DQ;
		Delay2(5);
		IO0SET=DQ;
		dat>>=1;
	} 
	Delay2(4); 
} 

void Config18b20() 
{ 
	Init_DS18B20(); 
	WriteOneChar(0xcc); //skip rom 
	WriteOneChar(0x4e); //write scratchpad 
	WriteOneChar(0x55); //上限 
	WriteOneChar(0x00); //下限 
	WriteOneChar(0x7f); //set 11 bit (0.125) 
	Init_DS18B20(); 
	WriteOneChar(0xcc); //skip rom 
	WriteOneChar(0x48); //保存设定值 
	Init_DS18B20(); 
	WriteOneChar(0xcc); //skip rom 
	WriteOneChar(0xb8); //回调设定值 
} 

void ReadTemp(void) 
{ 
	uint8 a=0; 
	uint8 b=0; 
	uint16 g; 
	Config18b20(); 
	Init_DS18B20(); 
	WriteOneChar(0xCC); // 跳过读序号列号的操作 
	WriteOneChar(0x44); // 启动温度转换 
	Delay2(500); 
	Init_DS18B20(); 
	WriteOneChar(0xCC); //跳过读序号列号的操作 
	WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度 
	a=ReadOneChar(); //读取温度值低位 
	b=ReadOneChar(); //读取温度值高位 
	IO0SET=DQ; 
	g=b<<8; //最终数据在a和b中,怎么处理你自己用吧 呵呵 
	g=g|a; 
	for(a=0;a<4;a++)
	   Dsp_buf[a]=0;
	if(g&0x8000){
		g=0xffff-g;
		Dsp_buf[3]=16;
	}
	g>>=4; //这是转换成具体温度值,1点代表0.625°C 
	
	  for(a=0;a<4;a++)
	  {Dsp_buf[a]=g%10;
	   g=g/10;
	  }
	Delay2(400);
}

///***************************************************************/
///* 函数功能: 延时
///* 入口参数:D_time:延迟的时间
///* 出口参数:无
///***************************************************************/

void Delay(uint8 D_time)
{
   uint8 i,j;
   for(j=0;j<D_time;j++)
	 for(i=0;i<127;i++);

}
void Delay3(unsigned int i) 
{ 
	uint32 n;
	while(i--)
	for(n=0;n<600;n++); 
} 

///***********************LCD12864驱动程序部分********************************/
///***************************************************************/
///* 函数功能: 延时判忙
///* 入口参数: 无
///* 出口参数: 无
///***************************************************************/
uint8  LCD12864_Delay(void)
{
	uint8 LCD_status=0x90,i=0,n;

	IOSET=LCD_RW; //读状态
	IOCLR=LCD_DI;
	
	if(LCDExist)
	{
		while(LCD_status&0x90)//判忙以及复位情况
		{
			if(i<1000)
			{
				IODIR &=~(LCDPORT);
				for (n=0;n<20 ;n++ );
				IOSET=LCD_EN ;
				Delay(1);
				LCD_status =(IO0PIN&LCDPORT)>>8;
				IODIR |=LCDPORT;
				IOCLR=LCD_EN;
				i++;
			}
			else
			{
				LCDExist = FALSE;
				return FALSE;
			}
			
		}
	}
	
	return TRUE;

}

////***************************************************************/
///* 函数功能: 向LCD12864中写入数据或命令
///* 入口参数: D_type:0->写入的是命令,1->写入的是数据
///* 			 D_code:要写入的数据
///* 出口参数: 无
///***************************************************************/
void LCD12864_Write_code(uint8 type,uint8 D_code)
{
	LCD12864_Delay();
	
	IOCLR=LCD_RW;
	if(type)
		IOSET=LCD_DI;
	else
		IOCLR=LCD_DI;
	
	IO0PIN=(IO0PIN&(~(LCDPORT)))|(D_code<<8);
	
	IOSET=LCD_EN;
	Delay(1);
	IOCLR=LCD_EN;
}

///***************************************************************/
///* 函数功能: 设置12864显示的初始地址
//** 入口参数: L_X:竖直方向(0<=L_X<=7)  L_Y:水平方向(0<=L_Y<=127)
///* 出口参数: 无
///***************************************************************/
void LCD_Set_XY(uint8 L_X,uint8 L_Y)
{

	L_Y &= 0x3f; // 0=<L_Y<=63   数据保护
	L_Y |= 0x40; //01xx xxxx
	LCD12864_Write_code(0,L_Y);//写横向地址
	
	L_X &= 0x07; //0<=L_X<=7  数据保护
    L_X |= 0xb8; //1011 1xxx
    LCD12864_Write_code(0,L_X); //写纵向地址
    
}


///****************************************************************
//** 函数功能: 在屏幕上显示汉字
//** 入口参数: bmp:所要画的图
//**           start_x,start_y:图的起始地址
//**			 postiton:左右半屏选择 1->右半屏 0->左半屏
//** 出口参数: 无
//****************************************************************/
void LCD12864_Darw_bmp_HZ(uint8 position,uint8 c,uint8 start_x,uint8 start_y)
{
	uint8 a;
	
	if(!position)
	{
		IOSET=CS1;
		IOCLR=CS2;
	}
	else
	{
		IOCLR=CS1;
		IOSET=CS2;
	}

	LCD_Set_XY(start_x++, start_y);//设置显示地址
	for(a=0 ;a<16 ;a++)
		LCD12864_Write_code(1, hanzi[c][a]);
	LCD_Set_XY(start_x, start_y);
	for(a=16 ;a<32 ;a++)
		LCD12864_Write_code(1, hanzi[c][a]);

}

///****************************************************************
//** 函数功能: 在屏幕上显示英文
//** 入口参数: bmp:所要画的图 position:左右半屏选择 0->左半屏 1->右半屏
//**           start_x,start_y:图的起始地址
//** 出口参数: 无
//****************************************************************/
void LCD12864_Darw_bmp_ENG(uint8 position,uint8 *bmp,uint8 Start_x,uint8 Start_y)
{
	uint8 a;
	
	if(!position)
	{
		IOSET=CS1;
		IOCLR=CS2;
	}
	else
	{
		IOCLR=CS1;
		IOSET=CS2;
	}

	LCD_Set_XY(Start_x++, Start_y);//设置显示地址
	for(a=0 ;a<8 ;a++)
		LCD12864_Write_code(1, bmp[a]);
	LCD_Set_XY(Start_x, Start_y);
		for(a=8 ;a<16 ;a++)
		LCD12864_Write_code(1, bmp[a]);

}


///****************************************************************
//** 函数功能: 在屏幕上显示数字
//** 入口参数: bmp:所要画的图 position:左右半屏选择 0->左半屏 1->右半屏
//**           start_x,start_y:图的起始地址
//** 出口参数: 无
//****************************************************************/
void LCD12864_Darw_bmp_NUM(uint8 position, uint8 c, uint8 Start_x, uint8 Start_y)
{
	uint8 a;
	
	if(!position)
	{
		IOSET=CS1;
		IOCLR=CS2;
	}
	else
	{
		IOCLR=CS1;
		IOSET=CS2;
	}
	LCD_Set_XY(Start_x++, Start_y);//设置显示地址
	for(a=0 ;a<8 ;a++)
		LCD12864_Write_code(1, BMP_NUM[c][a]);
	LCD_Set_XY(Start_x, Start_y);
	for(a=8 ;a<16 ;a++)
		LCD12864_Write_code(1, BMP_NUM[c][a]);

}

///****************************************************************
///** 函数功能: 清屏
//** 入口参数: 无
//** 出口参数: 无
//****************************************************************/
void LCD12864_Clr(void)
{
	uint8 i,j;
	IOSET=CS1;
	IOSET=CS2;
	
	Delay(1);
	for (i=0;i<8;i++ )
	{
		LCD_Set_XY(i, 0);
		for(j=0 ;j<64 ;j++)
			LCD12864_Write_code(1, 0x00);
			
	}
	
}
////***************************************************************/
///* 函数功能: LCD12864初始化
///* 入口参数: 无
///* 出口参数: 无
///***************************************************************/
void LCD12864_Init(void)
{
  IO0CLR=RET;
  IO0SET=RET;
  IOSET=CS1;
  IOSET=CS2;
  LCD12864_Write_code(0,LCD12864_OFF);
  LCD12864_Write_code(0,LCD12864_ON); //开显示
}

///****************************************END************************************/

int main()
{ 
  PINSEL0=0;
  PINSEL1=0;
  IODIR=~0;
  
  LCD12864_Init();
  LCD12864_Clr();
 /*for(i=0;i<4;i++)
  { 
    for(a=0;a<16;a++)
     {
      if(x>=64)
       p=1;
      else
       p=0;
      LCD12864_Darw_bmp_NUM(p,p,y,x);
      x=x+8;
      
      if(x>=128)
      x=0;
     }
      y=y+2;
  }*/
  //LCD12864_Darw_bmp_NUM(0,7,1,1); 
   LCD12864_Darw_bmp_HZ(0,0,0,0);
  LCD12864_Darw_bmp_HZ(0,1,0,16);
  LCD12864_Darw_bmp_HZ(0,2,0,32);
  LCD12864_Darw_bmp_HZ(0,3,0,48);
 while(1) {
 ReadTemp();
 LCD12864_Darw_bmp_NUM(1,Dsp_buf[3],0,0);
 LCD12864_Darw_bmp_NUM(1,Dsp_buf[2],0,8);
 LCD12864_Darw_bmp_NUM(1,Dsp_buf[1],0,16);
 LCD12864_Darw_bmp_NUM(1,Dsp_buf[0],0,24);
 Delay3(200);
 
 }
return 1;
}

⌨️ 快捷键说明

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