#include "C8051F020.h" #include "intrins.h" #include "process.h" #include "timer.h" #include "lcd.h" #include "adda.h" unsigned char SysTime,KeyCmd; unsigned intVoltageData=120,CurrentData=100; unsigned char xdata DataLine0[16]="V:00.0V "; unsigned char xdata DataLine1[16]="A:0.00A "; unsigned int VoltageAdc,CurrentAdc; float VoltageData_f,CurrentdData_f; unsigned int DacVoltage,DacCurrent; main() { InitTimer0(); LCD_INITIALIZE(); ADC_init(); for(;;) { StartPro(0); KeyCmd= GetKey(); if(KeyCmd== 1) { VoltageData++; } elseif(KeyCmd == 2) { VoltageData--; } elseif(KeyCmd == 3) { DacCurrent++; } elseif(KeyCmd == 4) { DacCurrent--; } VoltageAdc= ADC_Conver(7); CurrentAdc= ADC_Conver(6); VoltageData_f= VoltageAdc; VoltageData_f= VoltageData_f/4096*3.3; if(VoltageData_f> VoltageData) { DacVoltage--; } elseif(VoltageData_f < VoltageData) { DacVoltage++; } CurrentdData_f= CurrentAdc; CurrentdData_f= CurrentdData_f/4096*3.3; if(CurrentdData_f> CurrentData) { DacVoltage--; } elseif(CurrentdData_f < CurrentData) { DacVoltage++; } DataLine0[3]= VoltageData/100%10+'0'; DataLine0[4]= VoltageData/10%10+'0'; DataLine0[6]= VoltageData/1%10+'0'; DataLine1[3]= CurrentData/100%10+'0'; DataLine1[5] = CurrentData/10%10+'0'; DataLine1[6]= CurrentData/1%10+'0'; LCD_Disp(DataLine0,DataLine1); Set_DACs(); EndPro(10); } } #include "C8051F020.h" #include "intrins.h" #include "timer.h" //12M:FC17 11.0592M:FC66 void InitTimer0(void) { TMOD|= 0x01; TH0= 0xFC; //fbc2 ec78 TL0= 0x17; EA= 1; ET0= 1; TR0= 1; } void InitTimer1(void) { TMOD|= 0x10; TH1= 0xFC; //fbc2 ec78 TL1= 0x17; EA= 1; ET1= 1; TR1= 1; } void et0() interrupt 1 using 0 //将此定时器设置成5ms的定时器 { TH0= 0xFC; //fc17 ec78 TL0= 0x17; SysTime++; } void et1(void) interrupt 3 using 1 { TH1=0xFC;////重新设置定时初值 TL1=0x17;// } #include "C8051F020.h" #include "intrins.h" #include "lcd.h" void Dealy1602(void) { unsignedchar i=5; while(i--); } bit LCD_BUSY() //监测LCD是否繁忙,忙的情况下无法识别命令 { bitresult; LCD_RS=0; LCD_RW=1; LCD_EN=1; Dealy1602(); result=(bit)(LCD_PORT&0x80); LCD_EN=0; returnresult; } void LCD_Write(unsigned char dat,unsignedchar type) //向LCD写命令或者数据 { unsignedchar xdata i; while((LCD_BUSY()&&i<100)){i++;} LCD_RS=type; LCD_RW=0; LCD_EN=0; Dealy1602(); LCD_PORT= dat; Dealy1602(); LCD_EN=1; Dealy1602(); LCD_EN=0; } void LCD_INITIALIZE(void) //初始LCD显示屏 { LCD_Write(0x06,LCD_COMMAND); //设置光标移动方式,写完字符后后退一位 LCD_Write(0x38,LCD_COMMAND); //将显示屏设置成16*2的显示模式 LCD_Write(0x0C,LCD_COMMAND); //开显示、不显示光标 0C LCD_Write(0x01,LCD_COMMAND); //清屏 } void LCD_Prints(unsigned char *dat,unsignedchar add) { unsignedchar xdata i=0; LCD_Write(add|0x80,LCD_COMMAND); while((*dat!='\0')&&(i<16)) { i++; LCD_Write((*(dat++)),LCD_DATA); } } void LCD_Disp(unsigned char*DataLine0,unsigned char *DataLine1) { LCD_Prints(DataLine0,0); LCD_Prints(DataLine1,0x40); } void SetCurser(unsigned charXPoint,unsigned char YPoint) //设置光标的位置 { unsignedchar xdata PointTemp; PointTemp= (XPoint<<6)|0x80|YPoint; LCD_Write(PointTemp,0); } void WriteOneCharacter(unsigned charXpoint,unsigned char Ypoint,unsigned char DataToWrite) { SetCurser(Xpoint,Ypoint); LCD_Write(DataToWrite,LCD_DATA); } #include "C8051F020.h" #include "intrins.h" #include "key.h" unsigned charOldKey0=0,OldKey1=0,OldKey2=0,OldKey3=0; unsigned char GetKey(void) { unsigned char KeyCmd=0; if((OldKey0==1)&&(Key0==0)) { KeyCmd= 1; } OldKey0= Key0; if((OldKey1==1)&&(Key1==0)) { KeyCmd= 2; } OldKey1= Key1; if((OldKey2==1)&&(Key2==0)) { KeyCmd= 3; } OldKey2= Key2; if((OldKey3==1)&&(Key3==0)) { KeyCmd= 4; } OldKey3= Key3; } #include "C8051F020.h" #include "intrins.h" #include "adda.h" void delay_ms(unsigned int count) { unsignedint ii,jj; for(ii=0;ii<count;ii++) { for(jj=0;jj<250;jj++) _nop_(); } } void ADC_init(void) { ADC0CN = 0x04; REF0CN = 0x07; AMX0CF = 0x00; AMX0SL = 0x01; ADC0CF = (SYSCLK/SAR_CLK) << 3; ADC0CF |= 0x00; EIE2 |= 0x02; } unsigned int ADC_Conver(unsigned char Num) { unsignedint temp; delay_ms(1); AD0INT=Num; AD0BUSY=1; while(!AD0INT); temp=ADC0H; temp=temp<<8; temp=temp+ADC0L; returntemp; } void DAC0_Init(void) { DAC0CN = 0x94; REF0CN |= 0x03; } void DAC1_Init(void) { DAC1CN = 0x94; REF0CN |= 0x03; } void Set_DACs(void) { DAC1 = DacVoltage; DAC0 = DacCurrent; }
|