直流简易电子负载
ADC+负载+按键 +485+串口_最终版程序
单片机源程序如下:
- #include "config.h"
- #include "adc.h"
- #include "delay.h"
- #include "soft_uart.h"
- #include "lcd.h"
- #include "PWM.h"
- #include "intrins.h"
- #include "usart.h"
- /************* 按键******************************************************/
- sbit k1=P3^6; //定义P3.3口是k1
- sbit k2=P3^5; //定义P3.3口是k1
- sbit k3=P3^4; //定义P3.3口是k1
- sbit k4=P3^3; //定义P3.3口是k1
- sbit fan=P3^2; //定义P3.3口是k1
- int results=0;
- int DUTY,ierr=0,resulti=0;
- int num=0;
- int resultv=0;
- int cusor=0;
- int send_mode=0,receive_mode=0;
- void modbushandle1(void);
- void modbushandle2(void);
- void modbushandle3(void);
- void modbushandle4(void);
- void modbushandle5(void);
- void modbushandle6(void);
- void Uart3s(void);
- extern u8 ch;
- extern int modflag;
- extern u8 mtx[100],mrx[100];
- extern u16 send_I_V_P[50]; //4 发送电流电压功率值
- extern u16 bitsend[50]; //2 读过压保护状态
- extern u16 bitworkflag[50]; //1 5 启停标志位
- extern u16 set_current[50]; //3 6设定电流值
- unsigned int set_dis[4]={0,0,0,0};
- unsigned int a[9];//={0,0}
- unsigned int i;//,resultsvol,resultscur, u2summean,i2summean,u2sum,i2sum,u2num;
- extern unsigned char receiver_buf;
- void display(); //显示程序声明
- void keyscan();
- void voltage_test()
- {
- if(resultv>5500)
- {
- bitworkflag[0]=0;
- DUTY=0;
- bitsend[0]=1;
- }
- }
- void main()
- {
- P2M1=0X00;
- P2M0=0XFF;
- P0M1=0X00;
- P0M0=0XFF;
- P3M1=0X00;
- P3M0=0XFF;
- P4M1=0X00;
- P4M0=0XFF;
- P5M1=0X00;
- P5M0=0XFF;
- P6M1=0X00;
- P6M0=0XFF;
- P7M1=0X00;
- P7M0=0XFF;
- P0M0=0xfe;
-
- PWMInit();
- LcdInit();
- delay_ms(5);
- ADC_config();
- fan=0;//风扇
- DUTY=0;
- UartInit();
- ch=1;
-
- send_I_V_P[0]=7; //4
- send_I_V_P[1]=15;
- send_I_V_P[2]=200;
- bitworkflag[0]=0; // 1 5
- set_current[0]=1000;// 3 6
- bitsend[0]=0;
- while(1)
- {
- display();
- keyscan();
- voltage_test();
- if(crcok==1)
- {
- // DisplayOneChar(0,0,bitworkflag[0]+0x30);
- crcok = 0;
- if(mrx[0]==6)
- {
- switch (mrx[1])
- {
- case 1:modbushandle1(); //读取启停标志
- break;
-
- case 2:modbushandle2(); //读取过流保护的标志位
- break;
-
- case 3:modbushandle3(); //读取设定电流值
- break;
-
- case 4:modbushandle4(); //读电流电压功率
- break;
-
- case 5:modbushandle5(); //启动
- break;
-
- case 6:modbushandle6(); //设定电流值
- break;
- }
- }
- }
- switch (bitworkflag[0])
- {
- case 0:
- PWM2output(0);
- fan=0;
- DisplayListChar(14,0,"OF");
- break;
- case 1:
- fan=1;
- PWM2output(DUTY);
- DisplayListChar(14,0,"ON");
- break;
- }
- for(i=0;i<2;i++)
- {
- results = Get_ADC10bitResult(i); //参数0~7,查询方式做一次ADC, 返回值就是结果, == 1024 为错误
- if(i==1)
- {
- results=results*9.77;//9.98 放大2倍
- resulti=results;
- send_I_V_P[0]= resulti;
- }
- else
- {
- if(resulti>1050&resulti<1970)
- results=results*9.96;//9.98 放大2倍
- else if(resulti>=1970&resulti<2450)
- results=results*9.98;//9.98 放大2倍
- else if(resulti>=2450&resulti<3200)
- results=results*10.14;//9.98 放大2倍
- else
- results=results*9.68;//9.705 放大2倍
-
- resultv=results;
- send_I_V_P[1]=resultv;
- }
- a[0+i*4]=results/1000 + '0';
- a[1+i*4]=results%1000/100 + '0';
- a[2+i*4]=results%100/10 + '0';
- a[3+i*4]=results%10 + '0';
- DisplayOneChar(0+i*7,1,a[0+i*4]);
- DisplayListChar(1+i*7,1,".");
- DisplayOneChar(2+i*7,1,a[1+i*4]);
- DisplayOneChar(3+i*7,1,a[2+i*4]);
- DisplayOneChar(4+i*7,1,a[3+i*4]);
- delay_ms(250);
- }
- ierr = set_current[0]-resulti;
- if(ierr>300)ierr=300;
- if(ierr<-300)ierr=-300;
- DUTY+=(ierr)/30;
- }
- }
- void display()
- {
- DisplayListChar(5,1,"v");
- DisplayListChar(13,1,"A");
- /***********显示设定电流值**********/
- set_dis[0]=set_current[0]/1000 + '0';
- set_dis[1]=set_current[0]%1000/100 + '0';
- set_dis[2]=set_current[0]%100/10 + '0';
- set_dis[3]=set_current[0]%10 + '0';
-
- DisplayOneChar(2,0,set_dis[0]);
- DisplayListChar(3,0,".");
- DisplayOneChar(4,0,set_dis[1]);
- DisplayListChar(5,0,"A");
- /***********显示光标**********/
- if(cusor==1)
- {
- DisplayListChar(2,0," ");
- delay_ms(100);
- DisplayOneChar(2,0,set_dis[0]);
- delay_ms(100);
- }
- else if(cusor==2)
- {
- DisplayListChar(4,0," ");
- delay_ms(100);
- DisplayOneChar(4,0,set_dis[1]);
- delay_ms(100);
- }
- }
- void keyscan()
- {
- //*******************************k1普通显示******************************************//
- if(k1==0) //检测按键K1是否按下
- {
- delay_ms(2); //消除抖动 一般大约10ms
- if(k1==0) //再次判断按键是否按下
- {
- if(cusor==1)
- {
- set_current[0]+=1000;
- if(set_current[0]>=4000)
- set_current[0]=0;
- }
- else if(cusor==2)
- {
- set_current[0]+=100;
- if(set_current[0]>=4000)
- set_current[0]=10;
- }
- }
- while(!k1); //检测按键是否松开
- }
- //******************************k2**写*****************************************//
- if(k2==0)
- {
- delay_ms(2); //消抖处理
- if(k2==0)
- {
- if(cusor==1)
- {
- set_current[0]-=1000;
- if(set_current[0]<=0)
- set_current[0]=4000;
- }
- else if(cusor==2)
- {
- set_current[0]-=100;
- if(set_current[0]<=0)
- set_current[0]=4000;
- }
- }
- while(!k2);
- }
- //***************************k3*读**********************************************//
- if(k3==0)
- {
- delay_ms(2); //消抖处理
- if(k3==0)
- {
- cusor++;
- if(cusor>=3)
- cusor=0;
- }
- while(!k3);
- }
- //***************************k4*启动**********************************************//
- if(k4==0)
- {
- delay_ms(2); //消抖处理
- if(k4==0)
- {
- bitworkflag[0]++;
- if(bitworkflag[0]>=2)
- bitworkflag[0]=0;
- }
- while(!k4);
- }
- }
复制代码
所有资料51hei提供下载:
电子负载固件以及上位机.rar
(1.42 MB, 下载次数: 48)
|