无功补偿控制器程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include "derivative.h" /* include peripheral declarations */
uchar com=0x17,n=0;
int cos=1;
unsigned long Data=0;
/*
#define CS PTED_PTED4;
#define DOUT PTED_PTED5;
#define DIN PTED_PTED6;
#define SCLK PTED_PTED7;
*/
#define LCD_RS PTFD_PTFD2;
#define LCD_EN PTFD_PTFD3;
#define key1 PTBD_PTBD0;
uchar code dis1[]={"无功补偿控制器"};
uchar code dis2[]={"功率因数:"};
uchar dis3[4];
/*延时,毫秒*/
void delay_1ms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=50000;j>0;j--);
}
void ReadSpi()
{
int i=0;
//写
for(i=7;i>=0;i--)
{
PTED_PTED7 = 1;
if( 0 == (com & (1<PTED_PTED6 = 0;
else
PTED_PTED6 = 1;
PTED_PTED7 = 0;
}
//读
for(i=23,Data=0;i>=0;i--)
{
PTED_PTED7 = 1;
if( 0 == PTED_PTED5 )
Data &= ~(1<else
Data |= 1<PTED_PTED7 = 0;
}
}
/*写指令数据到LCD*/
void write_cmd(uchar cmd)
{
LCD_RS=0;
LCD_EN=0;
PTAD=cmd;
delay_1ms(5);
LCD_EN=1;
delay_1ms(5);
LCD_EN=0;
}
/*写显示数据到LCD*/
void write_dat(uchar dat)
{
LCD_RS=1;
LCD_EN=0;
PTAD=dat;
delay_1ms(5);
LCD_EN=1;
delay_1ms(5);
LCD_EN=0;
}
/*设定显示位置*/
void lcd_pos(uchar X,uchar Y)
{
uchar pos;
if(X==0)
{X=0X80;}
else if(X==1)
{X=0X90;}
else if(X==2)
{X=0X88;}
else if(X==3)
{X=0X98;}
pos=X+Y;
write_cmd(pos);//显示地址
}
/*LCD初始化*/
void lcd_init()
{
write_cmd(0x30);//基本指令操作
delay_1ms(5);
write_cmd(0x0c);//显示开,光标关
delay_1ms(5);
write_cmd(0x01);//清除LCD的显示内容
delay_1ms(5);
}
void cal_cos()
{
ReadSpi();
if(Data>0x800000)
{
Data=Data-0x1000000;
}
cos=Data/0x800000;
dis3[0]=cos/10;
dis3[1]=".";
dis3[2]=cos*10%10;
dis3[3]=cos*100%10;
}
void display(uchar j)
{
uchar i;
lcd_init();
if(j==1)
{
cal_cos();
lcd_pos(0,0);
i=0;
while(dis2[i]!='\0')
{
write_dat(dis2[i]);
i++;
}
lcd_pos(1,0);
i=0;
while(dis3[i]!='\0')
{
write_dat(dis3[i]);
i++;
}
}
else
{
lcd_pos(0,0);
i=0;
while(dis1[i]!='\0')
{
write_dat(dis1[i]);
i++;
}
}
}
void main()
{
while(1)
{
cal_cos();
if(cos<0.9)
{
PTCD_PTCD1=1;//投电容
}
else
{
PTCD_PTCD1=0;//切电容
}
if(key1==0)
{
delay_1ms(10);
if(key1==0)
{
while(!key1);//等待按键释放
display(1);
delay_1ms(5000);
}
}
else
{
display(0);
}
}
}