#include<avr/io.h>
#include<util/delay.h>
#include"display.h"
//#include<avr/pgmspace.h>
//#include <stdio.h>
//#include <string.h>
#include <avr/interrupt.h>
#include "IIC.h"
#include "ADS1115.h"
#define BIT(x) (1 << (x))
#define uint unsigned int
#define uchar unsigned char
unsigned char yb=0x80;
uchar SZ[]={"0123456789"};//0123456789
uchar SZ1[]={"9876543210"};
//unsigned char name00[]={"浓度:CO2=X%ssssssssssssssssssssssssssssss"};
unsigned char name00[]={"时间 t=X X X"};
unsigned char name01[]={" 实测设置设置"};
unsigned char name02[]={"O2 X X X X X X"};
unsigned char name03[]={"CO2 X X X X X X"};
void Send(uchar type,uchar transdata);
#define cyCS 4 //P1.0,片选信号
#define cySID 5 //P1.1,串行数据
#define cyCLK 7 //P1.2,同步时钟
#define cyPORT PORTF
#define cyDDR DDRF=0xff
void Ini_Lcd(void)
{
//cyDDR |= BIT(cyCLK) + BIT(cySID) + BIT(cyCS); //相应的位端口设置为输出
DDRF|=0xB0;
PORTF|=0x70;
PORTF|=0x50;
PORTF|=0x40;
//delay_nms(600); //延时等待液晶完成复位
_delay_ms(600);
Send(0,0x30); /*功能设置:一次送8位数据,基本指令集*/
//delay_nms(10);
_delay_ms(10);
Send(0,0x02); /*DDRAM地址归位*/
//delay_nms(5);
_delay_ms(5);
Send(0,0x0c); /*显示设定:开显示,不显示光标,不做当前显示位反白闪动*/
//delay_nms(5);
_delay_ms(5);
Send(0,0x01); /*清屏,将DDRAM的位址计数器调整为“00H”*/
//delay_nms(500);
_delay_ms(500);
Send(0,0x06); /*功能设置,点设定:显示字符/光标从左到右移位,DDRAM地址加1*/
//delay_nms(10);
_delay_ms(10);
}
void shanshuo()
{
DDRF|=0xB0;;
PORTF|=0x70;
PORTF|=0x50;
PORTF|=0x40;
//delay_nms(600); //延时等待液晶完成复位
Send(0,0x30); /*功能设置:一次送8位数据,基本指令集*/
//delay_nms(10);
_delay_ms(10);
_delay_ms(5);
Send(0,0x0d); /*显示设定:开显示,不显示光标,不做当前显示位反白闪动*/
//delay_nms(5);
_delay_ms(5);
Send(0,0x06); /*功能设置,点设定:显示字符/光标从左到右移位,DDRAM地址加1*/
//delay_nms(10);
_delay_ms(10);
}
void guanshanshuo()
{
DDRF|=0xB0;;
PORTF|=0x70;
PORTF|=0x50;
PORTF|=0x40;
//delay_nms(600); //延时等待液晶完成复位
Send(0,0x30); /*功能设置:一次送8位数据,基本指令集*/
//delay_nms(10);
_delay_ms(10);
_delay_ms(5);
Send(0,0x0c); /*显示设定:开显示,不显示光标,不做当前显示位反白闪动*/
//delay_nms(5);
_delay_ms(5);
Send(0,0x06); /*功能设置,点设定:显示字符/光标从左到右移位,DDRAM地址加1*/
//delay_nms(10);
_delay_ms(10);
}
void Send(uchar type,uchar transdata)//0-写控制命令,1-写数据
{
uchar firstbyte = 0xf8;
uchar temp;
uchar i,j = 3;
if(type) firstbyte |= 0x02;
cyPORT |= BIT(cyCS);
cyPORT &= ~BIT(cyCLK);
while(j > 0)
{
if(j == 3) temp = firstbyte;
else if(j == 2) temp = transdata&0xf0;
else temp = (transdata << 4) & 0xf0;
for(i = 8;i > 0;i--)
{
if(temp & 0x80) cyPORT |= BIT(cySID);
else cyPORT &= ~BIT(cySID);
cyPORT |= BIT(cyCLK);
temp <<= 1;
cyPORT &= ~BIT(cyCLK);
}
//三个字节之间一定要有足够的延时,否则易出现时序问题
if(j == 3) _delay_ms(1); //delay_nms(1);
else _delay_ms(1); //delay_nms(1);
j--;
}
cyPORT &= ~BIT(cySID);
cyPORT &= ~BIT(cyCS);
}
void lcd_pos(uchar x,uchar y)//定位
{
uchar pos;
switch(x)
{
case 1:pos=0x80;break;
case 2:pos=0x90;break;
case 3:pos=0x88;break;
case 4:pos=0x98;break;
default:pos=0x80;
}
pos += y;
Send(0,pos);
}
void outChinese(unsigned char place,unsigned char point[])
{
unsigned char i;
Send(0,place);
//for(i=0;i<8;i++)//一个汉字为两个字符
//{
// progdata=pgm_read_byte(point+i);
for (i=0;i<16;i++)
Send(1,point[i]);
}
void ydgb(void)
{
Send(0,yb);//显示光标位置
yb=yb+0x01;
if(yb==0x86){yb=0x90;}
if(yb==0x95){yb=0x88;}
if(yb==0x8d){yb=0x98;}
if(yb==0x9e){yb=0x80;}
}
void display1(void)
{
ydgb();
outChinese(0x80,name00);//第一行:80-87H
outChinese(0x90,name01);//第二行:90-97H
outChinese(0x88,name02);//第三行:88-8FH
outChinese(0x98,name03);//第四行:98-9FH
//outchar(0x9f,2,name4);
}
void jianpan()
{
DDRF=0xff;
PORTF|=0x0F; //设置按键输出“1”,之后语句检测按键,若端口变为“0”,则表示被按
}
int main()
{
Ini_Lcd();
jianpan();
display1();
InitADS1115();
//outline1();
//outline2();
uint k;
k=0;
uint i;
i=0;
uint t1=0,oc=0,os1=0,os2=0,cc=0,cs1=0,cs2=0;
//uint og=0,os=0;
//uint cg=0,cs=0;
//uint o2nd,co2nd;\uint ocs=0,ocg=0;
uint sjb=0;
uint sjs=0,sjg=0;
uint ocs=0,ocg=0;
uint ccs=0,ccg=0;
uint oss1=0,osg1=0;
uint css1=0,csg1=0;
uint oss2=0,osg2=0;
uint css2=0,csg2=0;
DDRC=0xff;
PORTC=0xff;
unsigned char place1[]={0x85,0x86,0x87,0X8C,0X8D,0X8E,0X8F,0X9C,0X9D,0X9E,0X9F};
//DDRC|=0x80;
//PORTC|=0x80;
while(1)
{
if((PINF&0x0f)==0x0e) //1111 1110
{
_delay_us(20);
if((PINF&0x0f)==0x0e)
{
while((PINF&0x0f)==0x0e);
//_delay_us(20);
shanshuo();
Send(0,place1[k]);
k++;
if(k==11)
k=0;
}
}
if((PINF&0x0f)==0X0D) //1111 1101
{ _delay_us(20);
if((PINF&0x0f)==0X0D)
{
while((PINF&0x0f)==0X0D);
switch(k)
{
case 0:Send(0,0X9F);
Send(1,0x30+i);
csg2=i;
break;
case 1:Send(0,0X85);
Send(1,0x30+i);
sjb=i;
break;
case 2:Send(0,0X86);
Send(1,0x30+i);
sjs=i;
break;
case 3:Send(0,0X87);
Send(1,0x30+i);
sjg=i;
break;
case 4:Send(0,0X8c);
Send(1,0x30+i);
oss1=i;
break;
case 5:Send(0,0X8d);
Send(1,0x30+i);
osg1=i;
break;
case 6:Send(0,0X8e);
oss2=i;
Send(1,0x30+i);
break;
case 7:Send(0,0X8f);
osg2=i;
Send(1,0x30+i);
break;
case 8:Send(0,0X9c);
css1=i;
Send(1,0x30+i);
break;
case 9:Send(0,0X9d);
csg1=i;
Send(1,0x30+i);
break;
case 10:Send(0,0X9e);
css2=i;
Send(1,0x30+i);
break;
}
i++;
if (i==10)
i=0;
}
}
if((PINF&0x0f)==0X07) //1111 0111
{ _delay_us(10);
if((PINF&0x0f)==0X07)
{
while((PINF&0x0f)==0X07);
{
guanshanshuo();
t1=100*sjb+10*sjs+sjg;
oc=10*ocs+ocg;
cc=10*ccs+ccg;
os1=10*oss1+osg1;
os2=10*oss2+osg2;
cs1=10*css1+csg1;
cs2=10*css2+csg2;
Send(0,0x8c);
Send(1,0x30+oss1);
Send(0,0x8d);
Send(1,0x30+osg1);
Send(0,0x8e);
Send(1,0x30+oss2);
Send(0,0x8f);
Send(1,0x30+osg2);
//Send(0,0x93);
//Send(1,cs1);
//Send(0,0x94);
//Send(1,cs2);
if (os1<os2)
PORTC&=~0x80;
else
PORTC|=0x80;
}
}
}
uchar VOL[2];
float VX;
WriteWord();
ReadWord(VOL);
VX = 4.096*(VOL[0]*256+VOL[1])/32768;
}}