void delayMs(unsigned char t) { while (t--) { DelayUs2x(245); DelayUs2x(245); } }
/*------------------------------------------------ mS延时函数,含有输入参数 unsigned char t,无返回值 unsigned char 是定义无符号字符变量,其值的范围是 0~255 这里使用晶振12M,精确延时请使用汇编 ------------------------------------------------*/ void DelayMs(unsigned char t) { while(t--) { //大致延时1mS DelayUs2x(245); DelayUs2x(245); } }
/****************************************************************************** * Delays: * 函数原型: - * 函数功能: * 输入参数: * 输出参数: * 编写日期: 2014/04/13, ChenxianLong create this function ******************************************************************************/ void Delays(unsigned int t) { while(t--) { //大致延时1mS DelayUs2x(245); DelayUs2x(245); } }
/****************************************************************************** * Write_Cmd: * 函数原型: - * 函数功能: * 输入参数: * 输出参数: * 编写日期: 2014/04/13, ChenxianLong create this function ******************************************************************************/ void Write_Cmd(unsigned char Cmd) { Check_Busy(); RS=0; RW=0; E=1; DataPort=Cmd; DelayUs2x(5); E=0; DelayUs2x(5); }
/****************************************************************************** * Write_Data: * 函数原型: - * 函数功能: * 输入参数: * 输出参数: * 编写日期: 2014/04/13, ChenxianLong create this function ******************************************************************************/ void Write_Data(unsigned char Data) { Check_Busy(); RS=1; RW=0; E=1; DataPort=Data; DelayUs2x(5); E=0; DelayUs2x(5); }
/*------------------------------------------------ 写入一个字节 ------------------------------------------------*/ void WriteOneChar(unsigned char dat) { unsigned char i=0; for (i=8; i>0; i--) { DQ = 0; DQ = dat&0x01; DelayUs2x(50); DQ = 1; dat>>=1; } DelayUs2x(1000); }
/*------------------------------------------------ 18b20初始化 ------------------------------------------------*/ bit Init_DS18B20(void) { bit dat=0; DQ = 1; //DQ复位 DelayUs2x(80); //稍做延时 DQ = 0; //单片机将DQ拉低 DelayUs2x(800); //精确延时 大于 480us 小于960us DQ = 1; //拉高总线 DelayUs2x(140); //15~60us 后 接收60-240us的存在脉冲 dat=DQ; //如果x=0则初始化成功, x=1则初始化失败 DelayUs2x(200); //稍作延时返回 return dat; }
/****************************************************************************** * Init_ST7920: * 函数原型: - * 函数功能: * 输入参数: * 输出参数: * 编写日期: 2014/04/13, ChenxianLong create this function ******************************************************************************/ void Init_ST7920() { DelaynMs(10); Write_Cmd(0x30); //选择基本指令集 DelayUs2x(50); //延时大于100us Write_Cmd(0x30); //选择8bit数据流 DelayUs2x(20); //延时大于37us Write_Cmd(0x0c); //开显示(无游标、不反白) DelayUs2x(50); //延时大于100us Write_Cmd(0x01); //清除显示,并且设定地址指针为00H DelaynMs(15); //延时大于10ms Write_Cmd(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位,光标从右向左加1位移动 DelayUs2x(50); //延时大于100us }
// ***************************************************************************** // 函数功能: 延时1ms // 入口参数: U16 wTimeCnt: 延时值 // 出口参数: void // 说 明: 大致延时1mS // ***************************************************************************** void DelayMs(U16 wTimeCnt) { while(wTimeCnt--) { DelayUs2x(115); } }
/*------------------------------------------------ 读取温度 ------------------------------------------------*/ unsigned int ReadTemperature(void) { unsigned char a=0; unsigned int b=0; unsigned int t=0; Init_DS18B20(); WriteOneChar(0xCC); // 跳过读序号列号的操作 WriteOneChar(0x44); // 启动温度转换 DelayUs2x(1000); Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度 a=ReadOneChar(); //低位 b=ReadOneChar(); //高位 b<<=8; t=a+b; t=(float)t*0.625; //t为实际温度的十倍,这样方便取小数点后一位 return t; }
void Debug_Output(U8 bCnt) { DEBUG_HIGH(); while(bCnt--) { DEBUG_LOW(); DelayUs2x(1); DEBUG_HIGH(); } }
/*------------------------------------------------ 读取一个字节 ------------------------------------------------*/ 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(40); } return(dat); }
/****************************************************************************** * LCD_PutString: * 函数原型: - * 函数功能: * 输入参数: * 输出参数: * 编写日期: 2014/04/13, ChenxianLong create this function ******************************************************************************/ void LCD_PutString(unsigned char x,unsigned char y,unsigned char code *s) { switch(y) { case 1: Write_Cmd(0x80+x);break; case 2: Write_Cmd(0x90+x);break; case 3: Write_Cmd(0x88+x);break; case 4: Write_Cmd(0x98+x);break; default:break; } while(*s>0) { Write_Data(*s); s++; DelayUs2x(250); } }
/****************************************************************************** * 函数原型: -SetGraphicMode * 函数功能: * 输入参数: * 输出参数: * 编写日期: 2014/04/13, ChenxianLong create this function ******************************************************************************/ void SetGraphicMode() { Write_Cmd(0x36); //选择8bit数据流 图形模式 DelayUs2x(20); }