#include <pic.h>
#include <math.h>
//1602液晶相关I/O设置
#define E RB0
#define RW RB1
#define RS RB2
//A/D转换用寄存器设置
unsigned int ADbuf;
double i_val=0, f_val=0;
unsigned char adcbuf[16];
//名称:1602液晶用延时函数
void Delay1602(unsigned int t)
{
unsigned int k;
for(k=0;k<t;k++);
}
//名称:1602液晶忙检测函数
void LCD1602_busy(void)
{
TRISB7=1;
RS=0;
RW=1;
E=1;
while(RB7==1);
E=0;
TRISB7=0;
}
//名称:1600写命令函数
void LCD1602_Write_com(unsigned char combuf)
{
RS=0;
RW=0;
PORTB=combuf&0xf0;
E=1;
asm("NOP");asm("NOP");
E=0;
PORTB=(combuf&0x0f)<<4;
E=1;
asm("NOP"); asm("NOP");
E=0;
}
//名称:1602写命令函数(带忙检测)
void LCD1602_Write_com_busy(unsigned char combuf)
{
LCD1602_busy();
LCD1602_Write_com(combuf);
}
//名称:1602写数据函数(带忙检测)
void LCD1602_Write_data_busy(unsigned char databuf)
{
LCD1602_busy();
RS=1;
RW=0;
PORTB=databuf&0xf;
E=1;
asm("NOP"); asm("NOP");
E=0;
PORTB=(databuf&0x0f)<<4;
E=1;
asm("NOP"); asm("NOP");
E=0;
}
//名称:1602液晶显示地址写函数
void LCD1602_Write_address(unsigned char x,unsigned char y)
{
x&=0x07;
y&=0x01;
if(y==0)
LCD1602_Write_com_busy(x|0x80);
else
LCD1602_Write_com_busy((x+0x40)|0x80);
}
//名称:1602液晶初始化函数
void LCD1602_init(void)
{
Delay1602(1500);
LCD1602_Write_com(0x28);
Delay1602(500);
LCD1602_Write_com(0x28);
Delay1602(500);
LCD1602_Write_com(0x28);
LCD1602_Write_com_busy(0x28);
LCD1602_Write_com_busy(0x08);
LCD1602_Write_com_busy(0x01);
LCD1602_Write_com_busy(0x06);
LCD1602_Write_com_busy(0x0C);
}
//名称:1602液晶指定地址显示函数
void LCD1602_Disp(unsigned char x,unsigned char y,unsigned char buf)
{
LCD1602_Write_address(x,y);
LCD1602_Write_data_busy(buf);
}
//名称: 主函数
void main(void)
{
unsigned char i;
TRISA=0B11111111;
TRISB=0B00000000;
PORTA=0B00000000;
PORTB=0B00000000;
LCD1602_init(); //调用1602液晶初始化函数
//****A/D模块初始化****
ADCON1=0B00000000; //RA0为A/D输入口;ADFM=0,转换后数据左移,ADRES为高8位数据
ADIE=0; //禁止A/D中断
//*********************
while(1)
{
for(i=0;i<16;i++) //连续采样16次,然后取平均数
{
//***启动一次A/D转换***
ADCON0=0B00000001; //选择AN0通道准备进行A/D转换,启动A/D模块
asm("NOP");
asm("NOP");
asm("NOP");
asm("NOP");
GODONE=1;
while(GODONE) con
tinue;
adcbuf[i]=ADRES; //将8位A/D转换结果暂存在数组中
//*********************
}
ADbuf=0;
for(i=0;i<16;i++)
{
ADbuf+=adcbuf[i];
}
ADbuf=ADbuf/16;
i_val=(double)ADbuf;
i_val=(i_val*500)/255;
f_val = modf(i_val, &i_val);
if(i_val>999) i_val=999;
ADbuf=(int)i_val;
//*******************************
LCD1602_Disp(0,0,(ADbuf/100)+0x30); //百位
LCD1602_Disp(1,0,'.');
LCD1602_Disp(2,0,(ADbuf%100)/10+0x30); //十位
LCD1602_Disp(3,0,(ADbuf%10)+0x30); //个位
LCD1602_Disp(4,0,'A');
LCD1602_Disp(4,1,'o');
}
}
求大神帮忙修改下程序,弄成4线控制1206的。。。。弄不好呀,没反映!
-
一周热门 更多>