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

📄 ad.c

📁 利用c8051f020进行温度采集
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************
                        C8051F020 AD采集       
*******************************************************************/
/************************预定义************************************/
#include <C8051F020.h>
#include <intrins.h>   
#include <stdio.h>
#include <math.h>
/*******************************************************************
                        定义IO口和变量
*******************************************************************/
sbit CD=P3^0; 					 
sbit RE=P3^1;					
sbit WR=P3^2;					
sfr ADC0 = 0xbe;
unsigned int xdata ADC0_data[100];  //采集数据存放数组
unsigned int data ADC0_data_n=0;      //采集次数
#define IO P4
bit m;                              //AD采集完标志
/*******************************************************************
                        函数声明
*******************************************************************/
void Sjcl(void);                         //AD采集完100次数据处理函数
void ADC0_ISR(void);                     //ADC0中断函数声明
void Write_data(unsigned char WRitedata);//写数据到LCD
/********************************************************************
                        LCD定义
********************************************************************/
#define LCD_CUR_POS 0X21   	 //指针设置指令
#define LCD_CGR_POS 0X22    
#define LCD_ADR_POS 0x24  
#define LCD_TXT_STP 0X40  	 //显示区域设置指令
#define LCD_TXT_WID 0X41   
#define LCD_GRH_STP 0X42    
#define LCD_GRH_WID 0X43   
#define LCD_MOD_OR  0X80   	 //显示方式设置指令,当CG(D3)为0启用CGROM
#define LCD_MOD_XOR 0X81   
#define LCD_MOD_AND 0x83   
#define LCD_MOD_TCH 0x84   
#define LCD_DIS_SW  0x90  	 //显示开关设置指令
#define LCD_CUR_SHP 0xA0   	 //光标开头设置指令
#define LCD_AUT_WR  0xB0   	 //自动读写设置指令
#define LCD_AUT_RD  0xB1   
#define LCD_AUT_OVR 0xB2    
#define LCD_INC_WR  0xC0   	 //数据一次读写设置指令
#define LCD_INC_RD  0xC1   
#define LCD_DEC_WR  0xC2   
#define LCD_DEC_RD  0xC3   
#define LCD_ZER_WR  0xc4   
#define LCE_ZER_RD  0xc5   
#define LCD_SCN_RD  0xE0   	 //屏读设置指令
#define LCD_SCN_CP  0xE8   	 //屏拷贝
#define LCD_BIT_OP  0xF0   	 //位操作指令 
/*********************************************************************** 
函数名称:Busy()
功    能:判断是否为“忙”标志
入口参数:无
出口参数:dat 
说    明:将状态字送给Busy
***********************************************************************/ 
 unsigned char Busy(void) 
{
  unsigned char dat;
  CD=1;
  RE=1;
  WR=1;
  IO=0xff;
  RE=0;
  dat=IO;
  RE=1;
  return(dat);
}
/***********************************************************************
函数名称:LCD_WRite_Data()
功    能:写数据子程序,将数据输入T6963C
入口参数:data   想要写入的数据
出口参数:无
说    明:在写命令之前,状态位D0(STA0),D1(STA1)必须全为1
***********************************************************************/
void LCD_Write_Data(unsigned char data1)
{	unsigned char busym;
    busym=Busy();
    while(((busym)&0x03)!=0x03);
    CD=0;
    WR=0;
    IO=data1;
    WR=1;
    CD=1;
}
/***********************************************************************
函数名称:LCD_WRite_Command0()
功    能:写无参数命令字程序,将T6963C对应的指令写入,没有参数
入口参数:commond    想要写入的命令字
出口参数:无
说    明:在写命令之前,状态位D0(STA0),D1(STA1)必须全为1
***********************************************************************/
void LCD_Write_Command0(unsigned char command)
{	 unsigned char busym;
     busym=Busy();
     while(((busym)&0x03)!=0x03) ;
	 CD=1;
     WR=0;
     IO=command;
     WR=1;
}

/***********************************************************************
函数名称:LCD_WRite_Command1()
功    能:写单参数命令字程序,将T6963C对应的指令写入
入口参数:commond    想要写入的命令字
出口参数:无
说    明:先送数据,再送命令字
***********************************************************************/
void LCD_Write_Command1(unsigned char command,unsigned char data1)
{   
    LCD_Write_Data(data1);
    LCD_Write_Command0(command);
}

/***********************************************************************
函数名称:LCD_WRite_Command2()
功    能:写双参数命令子程序,将T6963C对应的指令写入
入口参数:commond    想要写入的命令字
出口参数:无
说    明:先送数据,再送命令字
***********************************************************************/
void LCD_Write_Command2(unsigned char command,unsigned char data1,unsigned char data2)
{
    LCD_Write_Data(data1);
    LCD_Write_Data(data2);
    LCD_Write_Command0(command);
}

/************************************************************************
函数名称:LCD_T6963_Ini()
功    能:液晶模块初始化工作
入口参数:无
出口参数:无
说    明:初始化工作的主要要完成对文本方式的RAM起始地址的设定,文本模式的显示
         宽度,图形方式的RAM的起始地址,以及其宽度;设置显示方式等工作.
**************************************************************************/  
void    LCD_T6963_Ini(void)
{
    LCD_Write_Command2(LCD_TXT_STP,0x00,0x00);  		
    LCD_Write_Command2(LCD_TXT_WID,  16,0x00);  		
    LCD_Write_Command2(LCD_GRH_STP,0x00,0x08);  	
    LCD_Write_Command2(LCD_GRH_WID,  16,0x00);   	
    LCD_Write_Command0(LCD_CUR_SHP|0x07);       	
 	LCD_Write_Command0(LCD_MOD_OR);        		 	  
  	LCD_Write_Command0(LCD_DIS_SW|0x94);        	
}

/***************************************************************************
函数名称:LCD_Fill_All()
功    能:LCD填充.对LCD的空间填入同一个数据
入口参数:dat
出口参数:无
说    明:填充,主要可用来对液晶进行清屏
****************************************************************************/
void LCD_Fill_All(unsigned char dat)
{	unsigned char busym;
    unsigned int i;
    LCD_Write_Command2(LCD_ADR_POS,0x00,0x00);  	
    LCD_Write_Command0(LCD_AUT_WR);         		
    for(i=0;i<8191;i++)
    {	busym=Busy();
        while((busym&0x08)!=0x08)
    	{
           CD=0;
           WR=0;
           IO=dat;
           WR=1;                 			
    	}
    }
    LCD_Write_Command0(LCD_AUT_OVR);            	
    LCD_Write_Command2(LCD_ADR_POS,0x00,0x00);  	
}

/*******************************************************************************建立CGRAM
函数名称:LCD_Mak_Cgr()
功    能:创建CGRAM,CGRAM中包含能用文本方式显示的自定义字符
入口参数:cgramnum
出口参数:无
说    明:在CGRAM中写入字符点阵
********************************************************************************/
void LCD_Mak_Cgr(unsigned char *cgram,unsigned char cgramnum)
{   unsigned char busym;
	unsigned int i;	
	LCD_Write_Command2(LCD_CGR_POS,0x03,0x00);		//设置CGRAM偏置地址
	LCD_Write_Command2(LCD_ADR_POS,0x00,0x1c);		//设置RAM地址指针 
	LCD_Write_Command0(LCD_AUT_WR);					//设置自动写方式
	for(i=0;i<cgramnum*32;i++)					    //写字符点阵
	{
	    busym=Busy();
		while((busym&0x08)!=0x08);
		CD=0;
        WR=0;
        IO=cgram[i];
        WR=1; 
	}
	LCD_Write_Command0(LCD_AUT_OVR);				//自动写结束
}

/*********************************************************************************显示中文数组
函数名称:LCD_DIS_CHI()
功    能:利用CGRAM自定字符,以文本方式形式输出
入口参数:x,y,*chinacode,codelongth
出口参数:无
说    明:在屏上输出汉字数组
**********************************************************************************/
void LCD_Dis_Chi(unsigned char x,unsigned char y,unsigned char *chinacode,unsigned char codelongth)
{
	unsigned char i,xlable,ylable;
	unsigned int dataadd;
    LCD_Write_Command0(0x9c); 								//设置设置显示方式
	LCD_Write_Command0(LCD_MOD_TCH);  						//设置为文本特征显示方式    				
	for(i=0;i<codelongth;i++)
	{	
	
		dataadd=y*16+(x+2*i);								//计算i字符RAM地址
		xlable=dataadd&0xff;
		ylable=dataadd/256;									//计算字符RAM的地址
		LCD_Write_Command2(LCD_ADR_POS,xlable,ylable);		//设置址
		LCD_Write_Command1(LCD_INC_WR,chinacode[i]);		//左上
		LCD_Write_Command1(LCD_INC_WR,chinacode[i]+2);		//右上
		LCD_Write_Command2(LCD_ADR_POS,(xlable+16),ylable);	//下部分地址
		LCD_Write_Command1(LCD_INC_WR,chinacode[i]+1);		//左下
		LCD_Write_Command1(LCD_INC_WR,chinacode[i]+3);		//左上
	}
}
/*显示数字,字符*/
void LCD_Dis_Char(unsigned char x,unsigned char y,unsigned char *chinacode,unsigned char codelongth)
{
	unsigned char i,xlable,ylable;
	unsigned int dataadd;
    LCD_Write_Command0(0x9c); 								//设置设置显示方式
	LCD_Write_Command0(LCD_MOD_TCH);  						//设置为文本特征显示方式    				
	for(i=0;i<codelongth;i++)
	{	
		dataadd=y*16+(x+i);								    //计算i字符RAM地址
		xlable=dataadd&0xff;
		ylable=dataadd/256;									//计算字符RAM的地址
		LCD_Write_Command2(LCD_ADR_POS,xlable,ylable);		//设置址
		LCD_Write_Command1(LCD_INC_WR,chinacode[i]);		//左上	
		LCD_Write_Command2(LCD_ADR_POS,(xlable+16),ylable);	//下部分地址
		LCD_Write_Command1(LCD_INC_WR,chinacode[i]+1);		//左下
	
	}
}
/*********************************************************************************
                       定义字模:汉字为温度显示
					   自定义数字为:0到9
					   从0x80开始
**********************************************************************************/


⌨️ 快捷键说明

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