#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCD_CS = P1^0; //端口还没按原理图改过来,应为p0^0~p0^3;
sbit LCD_RD = P1^1;
sbit LCD_WR = P1^2;
sbit LCD_DATA = P1^3;
/
//------------------------------------------------------------------------------
//Subroutine Declare
//------------------------------------------------------------------------------
void Delay2us(uchar);
void Delay100us(uchar);
void Delay1ms(uchar);
void Delay10ms(uchar);
void Delay100ms(uchar);//==============================================================================
//Subroutine: Delay2us
//==============================================================================
void Delay2us(uchar Counter)
{while (--Counter);
}//==============================================================================
//Subroutine: Delay100us
//==============================================================================
void Delay100us(uchar Counter)
{while (Counter--){Delay2us(150);}
}//==============================================================================
//Subroutine: Delay1ms
//==============================================================================
void Delay1ms(uchar Counter)
{while (Counter--){Delay100us(11);}
}//==============================================================================
//Subroutine: Delay10ms
//==============================================================================
void Delay10ms(uchar Counter)
{while (Counter--){Delay1ms(11);}
}//==============================================================================
//Subroutine: Delay100ms
//==============================================================================
void Delay100ms(uchar Counter)
{while (Counter--){Delay1ms(101);}
}///
void SendBit(bit SENT_BIT) //发送一位数据
{//LCD_CS=0;LCD_DATA=SENT_BIT;LCD_WR=0;Delay2us(1);//delay100ns(2);LCD_WR=1;//LCD_CS=1;
}
void SendData(uchar dat,uchar CNT) //发送数据
{uchar i;for (i=0;i<CNT;i++){SendBit(dat&0x01);dat=dat>>1;}
}
void SendAddr(uchar addr ,uchar CNT) //发送地址
{uchar i;for (i=0;i<CNT;i++){SendBit(addr&0x80);addr<<=1;}
}
void LCD_Write_Command(uchar CMD) //写命令
{LCD_CS=0;SendAddr(0x80,3);SendAddr(CMD,9); //A5 A4 A3 A2 A1 A0LCD_CS=1;
}
void LCD_Write_Data(uchar addr ,uchar DAT) //写数据
{LCD_CS=0;SendAddr(0xA0,3);SendAddr(addr<<2,6); //A5 A4 A3 A2 A1 A0SendData(DAT,4); //D0 D1 D2 D3LCD_CS=1;
}
void LCD_Init() //初始化
{LCD_Write_Command(0x01); //SYS ENLCD_Write_Command(0x29); //set bias 1/3LCD_Write_Command(0x18); //on-chip RC oscillatorLCD_Write_Command(0x01); //SYS ENLCD_Write_Command(0x03); //dispaly on
}
void display(uchar dat) //0x00清空
{uchar i;uchar address=0;for (i=0;i<255;i++){LCD_Write_Data(address,dat);address++;}
}status=0xffff;
从左到右 分别对应0~15
//void SetSunLightStatus(uint status)
//{
// bit leve=0;
// uint i=0;
// uchar addr=1;
// uchar storge=44;
// // Delay100ms(3);
//
// for(i=0;i<16;i++)
// {
// if(i%4==0)
// addr=1;
// if(i>=0&&i<<4)
// storge=44;
// if(i>=4&&i<<8)
// storge=45;
// if(i>=8&&i<12)
// storge=40;
// if(i>=12&&i<16)
// storge=41;
// leve=status&0x8000;
// status<<=1;
// if(leve==1)
// {
//
// LCD_Write_Data(storge,addr);
//
// Delay1ms(25);//时间函数要改
//
// }
// if(i==3)
// {
// LCD_Write_Data(44,0x00);
//
// }
// if(i==7)
// {
//
// LCD_Write_Data(45,0x00);
// }
// if(i==11)
// {
// LCD_Write_Data(40,0x00);
//
// }
// if(i==15)
// {
// LCD_Write_Data(41,0x00);
//
// }
// addr<<=1;
// leve=0;
//
// }
//}void SetSunLightStatus(uint status)
{bit leve=0; //高低电平判断uchar val=0; //记录一个单元地址的总值uchar temp=0;uchar addr=1;uint i=0;for (i=0;i<16;i++){leve=status&0x8000;status<<=1;if (leve==1){val+=addr;addr<<=1;}else{addr<<=1;}leve=0;if ((i+1)%4==0) //所有变量重新初始化,以便继续下一个单元的计算{temp=val;addr=1;val=0;}if (i==3){LCD_Write_Data(44,temp);temp=0;}if (i==7){LCD_Write_Data(45,temp);temp=0;}if (i==11){LCD_Write_Data(40,temp);temp=0;}if (i==15){LCD_Write_Data(41,temp);temp=0;}}
}
void SetSoarXLightStatus(uint status)
{bit leve=0; //高低电平判断uchar val=0; //记录一个单元地址的总值uchar temp=0;uchar addr=1;uint i=0;for (i=0;i<16;i++){leve=status&0x8000;status<<=1;if (leve==1){val+=addr;addr<<=1;}else{addr<<=1;}leve=0;if ((i+1)%4==0) //所有变量重新初始化,以便继续下一个单元的计算{temp=val;addr=1;val=0;}if (i==3){LCD_Write_Data(42,temp);temp=0;}if (i==7){LCD_Write_Data(43,temp);temp=0;}if (i==11){LCD_Write_Data(38,temp);temp=0;}if (i==15){LCD_Write_Data(39,temp);temp=0;}}
}
/enum{V,KW,A,KWh,AllUnit}Choice_Unit=V;
void SetUnitValue(void)
{switch (Choice_Unit){case V:LCD_Write_Data(0,0x01);break;case KW:LCD_Write_Data(0,0x02);break;case A:LCD_Write_Data(1,0x04);break;case KWh:LCD_Write_Data(1,0x08);case AllUnit:LCD_Write_Data(0,0x03);LCD_Write_Data(1,0x0c);break;default :break;}
}/
enum{Battery,Solar,Total}BatteryOrSolarMode=Battery;
void SetBatteryOrSolarLightMode()
{switch (BatteryOrSolarMode){case Battery:LCD_Write_Data(0,0x07);break;case Solar:LCD_Write_Data(1,0x0d);break;case Total:LCD_Write_Data(1,0x0e);break;default:break;}
}
//实际应用中,SetBatteryOrSolarLightMode()应放到SetUnitValue(void)后,制版问题,两个显示在一个单元地址导致
///
void SetValue(uchar local, uchar value,bit leve) //KWh小数点位置,1表示有,0表示无
{uchar seg1[10]={13,0,14,10,3,11,15,0,15,11};uchar seg2[10]={7, 6, 3, 7,6, 5, 5,7, 7, 7};uchar addr1=0;uchar addr2=0;uchar value1=seg1[value] ;uchar value2=seg2[value];if (local==1){addr1=2;addr2=4;}else if (local==2){addr1=6;addr2=8;}else if (local==3){addr1=10;addr2=12;}else if (local==4){addr1=14;addr2=16;}else if (local==5){addr1=22;addr2=24;}else if (local==6){addr1=26;addr2=28;}else if (local==7){addr1=30;addr2=32;}else if (local==8){addr1=34;addr2=36;}else if (local<=17&&local>=9){addr1=(local-9)*4+3;addr2=addr1+2;}LCD_Write_Data(addr1,value1);if (leve==1&&(local==3||local==7||local==11||local==17||local==16))value2+=8;LCD_Write_Data(addr2,value2);
}
void main()
{LCD_Init();display(0x00);
// LCD_Write_Data(1,0x01);
// display(0x00);// LCD_Write_Data(24,7);
// while(1) ;while (1){uchar i,j;for (j=0;j<10;j++){for (i=1;i<=17;i++){if (i==17)SetValue(i, j,0) ;elseSetValue(i, j,1) ;}Delay10ms(10);}}SetValue(17, 7,1);}//
/* SetValue()测试程序while(1){uchar i,j;for(j=0;j<10;j++){for(i=1;i<=17;i++){if(i==17)SetValue(i, j,0) ;elseSetValue(i, j,1) ;}Delay10ms(10);}}
*/
//
/*Choice_Unit=AllUnit;SetUnitValue() ;BatteryOrSolarMode=Solar;SetBatteryOrSolarLightMode() ;
*/
//