博主是华东理工大学信工的一名学生,没想到大三下学期期末考试之后却被这个电子系统设计实践摧残得焦头烂额,连续14天梦里都是单片机,特此记录一篇博客。内容为整个项目的代码,包含一个不太行的PID,即PID部分需要修改。
工具:keil5 带51工具库 (C51包、整个项目见我博客中的下载页),keil5可以在淘宝中花点钱买一个,因为安全无毒啊
1.Keil5配置
请把所有.c文件加到一个工程中,然后看我配置
2.各部分代码
我一共15个文件。由于时间确实紧迫所以代码写得略乱了一点,运行起来吧还有点不对头的地方,Con,Pid小风机可能有点异常。别忘了把第5个菜单删掉!因为最后说我代码量太大,超出单片机RAM了,所以我把第5个菜单里的内容都注释掉了,它是个算数游戏,玩法是判断题,0x3a是“是”,0x3b是“否”,0x39是“返回”,0x38是“确定”。我一共6个菜单,除了第5菜单是为了让我练口算自己编写的,其余菜单都是老师要求的。
main.c
放主函数和中断定时器(run要用)
#include <REG52.H>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <intrins.h>
#include <global.h>
#include <hd7279.h>
#include <uart.h>
#include <ds18b20.h>
#include <motor.h>
#include <ee24c16.h>
#include <pid.h>
int menu_num=0,layer=1,motorNow = 0,run_num=0,con_num=0;
unsigned char keyvalue=0xff;
unsigned char LED[8]={0xa9,0xf1,0x01,0x00,0x44,0x00,0x00,0x00};
float Tset = 27.0,kp=4.0,ki=0.33,kd=4.0;
unsigned int pwm=50;
void main(void){
unsigned char tP[8]={0xa9,0xf1,0x01,0x00,0x44,0x00,0x00,0x00};
unsigned char run[8]={0x21,0x2c,0x25,0x01,0x79,0x00,0x00,0x00};
unsigned char Con[8]={0xb8,0x2d,0x25,0x01,0x5d,0x00,0x00,0x00};
unsigned char PA[8]={0xf1,0xf5,0x01,0x00,0xc5,0x00,0x00,0x00};
unsigned char Game[8]={0xd0,0x08,0xd0,0x01,0x9d,0x00,0x00,0x00};
unsigned char PID[8]={0xf1,0x44,0x6d,0x01,0xbd,0x00,0x00,0x00};
unsigned char run_all[8]={0x00,0x00,0x00,0x00,0x21,0x01,0x00,0x00};
unsigned char temparray[10]={0xFC,0x44,0x79,0x5D,0xC5,0x9D,0xBD,0x54,0xFD,0xDD};
unsigned char dat1;
unsigned char key,i;
int a,ans,run_menu_num=-1,pa_menu_num=-1;
unsigned int pw_now;
unsigned int t_down,t_up,temp_now_int;
init_hd7279();
init_24c16();
//init_uart();
motor = 0;
save_all_data(); //只让它执行一次 存EEPROM数据 以后要注释掉
while(1){
display();
key = readKey();
//ÐèÒª²»¶ÏÏÔʾµÄÄ£¿é
if(layer==2 && menu_num==0){
EA = 0;
show_temperature();
}
else if(layer==2 && menu_num==2){
EA = 0;
motor = 0;
temp_now = show_temperature();
if(temp_now>t_down && temp_now<t_up){
pw_now=(temp_now-t_down)/(t_up-t_down)*50+50;
LED[1] = 0x00;
LED[2] = temparray[(pw_now%100)/10];
LED[3] = temparray[(pw_now%100)%10];
}
else if(temp_now>=t_up){//100
LED[1] = 0x44;
LED[2] = 0xfc;
LED[3] = 0xfc;
}
else{
LED[1] = 0x00;
LED[2] = 0x00;
LED[3] = 0xfc;
}
LED[0] = 0x00;
}
else if(layer==2 && menu_num==5){
EA=1;
pwm = pid_getpwm(pwm,show_temperature(),Tset,kp,ki,kd);
//show_temperature();
LED[0]=0x00;
LED[1]=0x00;
LED[2]=temparray[(pwm%100)/10]; //10
LED[3]=temparray[(pwm%100)%10]; //1
}
else if(layer==3 && menu_num==1){//run
EA = 1;
if(motorNow<(unsigned int)(dat1)){
motor = 1;
P2 = 0x00;
}
else{
motor = 0;
P2 = 0x02;
}
}
if(key==0xff){
keyvalue = 0xff;
}
else{
if(keyvalue==0xff){
keyvalue = key;
if(layer==1){
menu_switch();
switch(menu_num){
case 0:for(i=0;i<8;i++)LED[i]=tP[i];break;
case 1:for(i=0;i<8;i++)LED[i]=run[i];break;
case 2:for(i=0;i<8;i++)LED[i]=Con[i];break;
case 3:for(i=0;i<8;i++)LED[i]=PA[i];break;
case 4:for(i=0;i<8;i++)LED[i]=Game[i];break;
case 5:for(i=0;i<8;i++)LED[i]=PID[i];break;
}
}
else if(layer == 2){
switch(menu_num){
case 0:
{
if(keyvalue==0x39){
layer = 1;
keyvalue=0xff;
}
break;
}
case 1:
{
run_menu_num=run_menu_switch(run_menu_num);
for(i=0;i<8;i++)LED[i]=run_all[i];
switch(run_menu_num){
case 0:{
LED[7] = 0x44;
dat1=readbyte_24c16(0);
break;
}
case 1:{
LED[7] = 0x79;
dat1=readbyte_24c16(1);
break;
}
case 2:{
LED[7] = 0x5d;
dat1=readbyte_24c16(2);
break;
}
case 3:{
LED[7] = 0xc5;
dat1=readbyte_24c16(3);
break;
}
case 4:{
LED[7] = 0x9d;
dat1=readbyte_24c16(4);
break;
}
case 5:{
LED[7] = 0xbd;
dat1=readbyte_24c16(5);
break;
}
case 6:{
LED[7] = 0x54;
dat1=readbyte_24c16(6);
break;
}
case 7:{
LED[7] = 0xfd;
dat1=readbyte_24c16(7);
break;
}
case 8:{
LED[7] = 0xdd;
dat1=readbyte_24c16(8);
break;
}
case 9:{
LED[6] = 0x44;
LED[7] = 0xfc;
dat1=readbyte_24c16(9);
break;
}
}
if((dat1/100)!=0)LED[1]=temparray[dat1/100];
else LED[1]=0x00;
LED[2]=temparray[(dat1%100)/10];
LED[3]=temparray[(dat1%100)%10];
break;
}
case 2:
{
t_down = readbyte_24c16(10);
t_up =readbyte_24c16(11);
if(keyvalue==0x39){
layer = 1;
keyvalue=0xff;
}
break;
}
case 3:
{
pa_menu_num=pa_menu_switch(pa_menu_num);
switch(pa_menu_num){
case 0:LED[0]=0x00;LED[1]=0x21;LED[2]=0x2c;LED[3]=0x25;LED[4]=0xf1;LED[5]=0xf5;LED[6]=0x01;LED[7]=0x00;break;//run
case 1:LED[0]=0x00;LED[1]=0xb8;LED[2]=0x2d;LED[3]=0x25;LED[4]=0xf1;LED[5]=0xf5;LED[6]=0x01;LED[7]=0x00;break;//con
}
break;
}
case 4:
{
// if(keyvalue==0x3b)a = 1;
// else if(keyvalue==0x3a)a = 0;
// else if(keyvalue==0x39){
// layer = 1;
// keyvalue=0xff;
// }
// else ans = showmathgame();
//
// if(keyvalue==0x3b || keyvalue==0x3a){
// if(a!=ans){
// LED[0]=0x00;
// LED[1]=0x00;
// LED[2]=0x00;
// LED[3]=0x00;
// LED[4]=0xc1;
// LED[5]=0x25;
// LED[6]=0xc1;
// LED[7]=0x00;
// }
// else{
// LED[0]=0x00;
// LED[1]=0x00;
// LED[2]=0x00;
// LED[3]=0x00;
// LED[4]=0xd0;
// LED[5]=0x2c;
// LED[6]=0xd0;
// LED[7]=0x00;
// }
// display();
// delay(1000);
// ans = showmathgame();
// }
break;
}
case 5:{
init_motor();
if(keyvalue==0x39){
layer = 1;
keyvalue=0xff;
}
break;
};
}
}
else if(layer==3){
switch(menu_num){
case 0:
{
break;
}
case 1:
{
if(keyvalue==0x39){
layer = 2;
run_menu_num = -1;
keyvalue=0xff;
motor = 0;
P2 = 0x02;
}
init_motor();
for(i=0;i<8;i++)LED[i]=run_all[i];
switch(run_menu_num){
case 0:{
dat1 = readbyte_24c16(0);
break;
}
case 1:{
dat1 = readbyte_24c16(1);
break;
}
case 2:{
dat1 = readbyte_24c16(2);
break;
}
case 3:{
dat1 = readbyte_24c16(3);
break;
}
case 4:{
dat1 = readbyte_24c16(4);
break;
}
case 5:{
dat1 = readbyte_24c16(5);
break;
}
case 6:{
dat1 = readbyte_24c16(6);
break;
}
case 7:{
dat1 = readbyte_24c16(7);
break;
}
case 8:{
dat1 = readbyte_24c16(8);
break;
}
case 9:{
dat1 = readbyte_24c16(9);
break;
}
}
if((dat1/100)!=0)LED[1]=temparray[dat1/100];
else LED[1]=0x00;
LED[2]=temparray[(dat1%100)/10];
LED[3]=temparray[(dat1%100)%10];
LED[4]=0x01;
LED[5]=0x2c;//u
LED[6]=0x25;//n
break;
}
case 2:break;
case 3:
{
if(flag==0){
//run
run_menu_num=run_menu_switch(run_menu_num);
if(LED[4]==0xf5){
run_menu_num = run_num;
}
for(i=0;i<8;i++)LED[i]=run_all[i];
switch(run_menu_num){
case 0:{
LED[7] = 0x44;
dat1=readbyte_24c16(0);
break;
}
case 1:{
LED[7] = 0x79;
dat1=readbyte_24c16(1);
break;
}
case 2:{
LED[7] = 0x5d;
dat1=readbyte_24c16(2);
break;
}
case 3:{
LED[7] = 0xc5;
dat1=readbyte_24c16(3);
break;
}
case 4:{
LED[7] = 0x9d;
dat1=readbyte_24c16(4);
break;
}
case 5:{
LED[7] = 0xbd;
dat1=readbyte_24c16(5);
break;
}
case 6:{
LED[7] = 0x54;
dat1=readbyte_24c16(6);
break;
}
case 7:{
LED[7] = 0xfd;
dat1=readbyte_24c16(7);
break;
}
case 8:{
LED[7] = 0xdd;
dat1=readbyte_24c16(8);
break;
}
case 9:{
LED[6] = 0x44;
LED[7] = 0xfc;
dat1=readbyte_24c16(9);
break;
}
}
if((dat1/100)!=0)LED[1]=temparray[dat1/100];
else LED[1]=0x00;
LED[2]=temparray[(dat1%100)/10];
LED[3]=temparray[(dat1%100)%10];
LED[4]=0xf1;
}
else{
pa_menu_num=pa_menu_switch(pa_menu_num);
switch(pa_menu_num){
case 0:dat1=readbyte_24c16(10);LED[0]=0x00;LED[1]=0x00;LED[2]=temparray[(readbyte_24c16(10)%100)/10];LED[3]=temparray[(readbyte_24c16(10)%100)%10];LED[4]=0xf1;LED[5]=0xf5;LED[6]=0x01;LED[7]=0xad;break;//b
case 1:dat1=readbyte_24c16(11);LED[0]=0x00;LED[1]=0x00;LED[2]=temparray[(readbyte_24c16(11)%100)/10];LED[3]=temparray[(readbyte_24c16(11)%100)%10];LED[4]=0xf1;LED[5]=0xf5;LED[6]=0x01;LED[7]=0xb1;break;//f
}
break;
}
break;
}
case 4:break;
case 5:break;
}
}
else if(layer==4){
if(flag==0){
//run
if(keyvalue==0x3a){
if(dat1>=0 && dat1<100){
dat1++;
if((dat1/100)!=0)LED[1]=temparray[dat1/100];
else LED[1]=0x00;
LED[2]=temparray[(dat1%100)/10];
LED[3]=temparray[(dat1%100)%10];
}
}
else if(keyvalue==0x3b){
if(dat1>0 && dat1<=100){
dat1--;
if((dat1/100)!=0)LED[1]=temparray[dat1/100];
else LED[1]=0x00;
LED[2]=temparray[(dat1%100)/10];
LED[3]=temparray[(dat1%100)%10];
}
}
else if(keyvalue==0x39){
layer = 3;
keyvalue=0xff;
}
else{
if(LED[4] == 0xf5){
writebyte_24c16(dat1,run_num);
layer = 3;
keyvalue=0xff;
}
}
}
else{
//con
if(keyvalue==0x3a){
if(dat1>=20 && dat1<40){
dat1++;
LED[1]=0x00;
LED[2]=temparray[(dat1%100)/10];
LED[3]=temparray[(dat1%100)%10];
}
}
else if(keyvalue==0x3b){
if(dat1>20 && dat1<=40){
dat1--;
LED[2]=temparray[(dat1%100)/10];
LED[3]=temparray[(dat1%100)%10];
}
}
else if(keyvalue==0x39){
layer = 3;
keyvalue=0xff;
}
else{
if(LED[4] == 0xf5){
if(con_num==0){
writebyte_24c16(dat1,10);
}
else writebyte_24c16(dat1,11);
layer = 3;
keyvalue=0xff;
}
}
}
LED[4] = 0xf5;
LED[5] = 0x01;
LED[6] = 0x00;
}
}
}
}
}
void timer0(void) interrupt 1{
TH0 = 0xee;
TL0 = 0x00;
motorNow++;
if(motorNow>=100){
motorNow = 0;
}
//if(layer==2 && menu_num==5) pwm = pid_getpwm(pwm,show_temperature(),Tset,kp,ki,kd);
}
hd7279.c
#include <REG52.H>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <intrins.h>
#include <hd7279.h>
#include <global.h>
unsigned int flag = 0;
void init_hd7279(void){
send_byte(HD7279_RESET);
}
void send_byte(unsigned char byte){
unsigned char i;
cs = 0;
long_delay();
for(i=0;i<8;i++){
if(byte&0x80){
dat = 1;
}
else{
dat = 0;
}
clk = 1;
short_delay();
clk = 0;
short_delay();
byte = byte*2;
}
dat = 0;
}
unsigned char receive_byte(void){
unsigned char i,byte;
dat = 1;
long_delay();
for(i=0;i<8;i++){
clk = 1;
short_delay();
byte = byte*2;
if(dat){
byte = byte|0x01;
}
clk = 0;
short_delay();
}
dat = 0;
return(byte);
}
unsigned char readKey(void){
unsigned char key;
//cs = 0;
send_byte(0x15);
key = receive_byte();
//cs = 1;
return(key);
}
void display(){
unsigned char i;
//cs = 0;
for(i=0;i<8;i++){
write_hd7279(0x90+i,LED[i]);
}
//cs = 1;
}
void write_hd7279(unsigned char position,unsigned char dat){
send_byte(position);
send_byte(dat);
}
void menu_switch(void){
switch(keyvalue){
case 0x3b:
{
menu_num++;
menu_num = menu_num%6;
break;
}
case 0x3a:
{
menu_num=(menu_num+5)%6;
break;
}
case 0x39:
{
layer = 1;
break;
}
case 0x38:
{
layer = 2;
keyvalue = 0xff;
break;
}
}
}
int run_menu_switch(int num){
switch(keyvalue){
case 0x3b:
{
num++;
num = num%10;
break;
}
case 0x3a:
{
num=(num+9)%10;
break;
}
case 0x39:
{
if(num!=-1){
layer--;
keyvalue = 0xff;
run_num = num;
num = -1;
}
else num = 0;
break;
}
case 0x38:
{
if(num!=-1){
layer++;
keyvalue = 0xff;
run_num = num;
num = -1;
}
else num = 0;
break;
}
}
return num;
}
int pa_menu_switch(int num){
switch(keyvalue){
case 0x3b:
{
num++;
num = num%2;
break;
}
case 0x3a:
{
num=(num+1)%2;
break;
}
case 0x39:
{
if(num!=-1){
layer--;
keyvalue = 0xff;
con_num = num;
num = -1;
}
else num = 0;
break;
}
case 0x38:
{
if(num!=-1){
if(layer==2){
if(num==0)flag=0;
else flag = 1;
}
layer++;
keyvalue = 0xff;
con_num = num;
num = -1;
}
else num = 0;
break;
}
}
return num;
}
//int showmathgame(){
// int a,b,a1,a2,b1,b2,c,c0,c1,d;
// int answere[3];
// unsigned char matharray[10]={0xFC,0x44,0x79,0x5D,0xC5,0x9D,0xBD,0x54,0xFD,0xDD};
// a = rand()%100;
// if(a>10){
// a1 = a/10;
// a2 = a%10;
// b = rand()%9+1;
// LED[4]=matharray[a1];
// LED[5]=matharray[a2];
// LED[6]=0x01;
// LED[7]=matharray[b];
// }
// else{
// b=rand()%100;
// b1 = b/10;
// b2 = b%10;
// LED[4]=matharray[a];
// LED[5]=0x01;
// LED[6]=matharray[b1];
// LED[7]=matharray[b2];
// }
// c = a-b;
// c1 = c+(rand()%9+1);
// c0 = c-(rand()%9+1);
// answere[0]=c0;
// answere[1]=c;
// answere[2]=c1;
// d=answere[rand()%3];
//
// if(d<-9){
// LED[0]=0x01;
// LED[1]=matharray[abs(d)/10];
// LED[2]=matharray[abs(d)%10];
// LED[3]=0x00;
// }
// else if(d>-10 && d<0){
// LED[0]=0x01;
// LED[1]=matharray[abs(d)];
// LED[2]=0x00;
// LED[3]=0x00;
// }
// else if(d>0 && d<10){
// LED[0]=matharray[d];
// LED[1]=0x00;
// LED[2]=0x00;
// LED[3]=0x00;
// }
// else{
// LED[0]=matharray[d/10];
// LED[1]=matharray[d%10];
// LED[2]=0x00;
// LED[3]=0x00;
// }
// if(d==c)return 1;
// else return 0;
//}
hd7279.h
#define HD7279_RESET 0xa4
sbit cs = P1^4;
sbit dat = P1^7;
sbit clk = P1^5;
sbit key = P1^6;
extern unsigned int flag;
void init_hd7279(void);
void send_byte(unsigned char);
unsigned char receive_byte(void);
unsigned char readKey(void);
void display(void);
void write_hd7279(unsigned char,unsigned char);
void menu_switch(void);
int showmathgame(void);
int run_menu_switch(int);
int pa_menu_switch(int num);
ds18b20.c
#include <REG52.H>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <intrins.h>
#include <global.h>
#include <hd7279.h>
#include <ds18b20.h>
#include <uart.h>
#include <ee24c16.h>
float temp_now=0.0;
void init_ds1820(void){
reset_ds1820();
writeData_ds1820(0xCC);
writeData_ds1820(0x4E);
writeData_ds1820(0x20);
writeData_ds1820(0x00);
writeData_ds1820(0x7F);
reset_ds1820();
}
void reset_ds1820(void){
unsigned char i;
ds1820_dq = 0;
for(i=240;i>0;i--);
ds1820_dq = 1;
for(i=240;i>0;i--);
}
void writeData_ds1820(unsigned char dat){
unsigned char i,j;
for(i=8;i>0;i--){
ds1820_dq = 0;
for(j=2;j>0;j--);
ds1820_dq = dat&0x01;
for(j=30;j>0;j--);
ds1820_dq = 1;
dat>>=1;
}
}
unsigned char readData_ds1820(void){
unsigned char i,j,s;
for(i=8;i>0;i--){
s>>=1;
ds1820_dq = 0;
for(j=2;j>0;j--);
ds1820_dq = 1;
for(j=4;j>0;j--);
if(ds1820_dq==1) s|=0x80;
for(j=30;j>0;j--);
ds1820_dq = 1;
}
return s;
}
float show_temperature(void){
unsigned char temp_data,temp_data2,i;
unsigned short TempDec;
unsigned char temperature[2];
unsigned char temp[4];
unsigned char temparray[10]={0xFC,0x44,0x79,0x5D,0xC5,0x9D,0xBD,0x54,0xFD,0xDD};
unsigned char tempdparray[10]={0xfe,0x46,0x7b,0x5f,0xC7,0x9f,0xBf,0x56,0xFf,0xDf};
float T=0.0;
reset_ds1820();
writeData_ds1820(0xcc);
writeData_ds1820(0x44);
reset_ds1820();
writeData_ds1820(0xcc);
writeData_ds1820(0xbe);
for(i=0;i<2;i++){
temperature[i]=readData_ds1820();
}
reset_ds1820();
temp_data=temperature[1];
temp_data&=0xf0;
if(temp_data==0xf0){
if(temperature[0]==0){
temperature[0]=~temperature[0]+1;
temperature[1]=~temperature[1]+1;
}
else{
temperature[0]=~temperature[0]+1;
temperature[1]=~temperature[1];
}
}
temp_data = temperature[1]<<4;
temp_data2 = temperature[0]>>4;
temp_data = temp_data|temp_data2;
temp[0]=(temp_data%100)/10;//10
temp[1]=(temp_data%100)%10;//1
temperature[0]&=0x0f;
TempDec = temperature[0]*625;
temp[2] = TempDec/1000;//1
temp[3] = (TempDec%1000)/100;//10
T = 10*(float)temp[0]+(float)temp[1]+0.1*(float)temp[2]+0.01*(float)temp[3];
if(T==85.0)return 0;
if(temp_now==0){
temp_now = T;
}
if(abs(T-temp_now)<1){
LED[0] = 0xd1;
LED[1] = 0xb8;
LED[2] = 0x00;
LED[3] = 0x00;
LED[4] = temparray[temp[0]];
LED[5] = tempdparray[temp[1]];
LED[6] = temparray[temp[2]];
LED[7] = temparray[temp[3]];
temp_now = T;
}
//if((T<40.0) || (abs(T-temp_now)<1)){}
return temp_now;
}
ds18b20.h
sbit ds1820_dq = P1^3;
extern float temp_now;
void init_ds1820(void);
void reset_ds1820(void);
void writeData_ds1820(unsigned char);
float show_temperature(void);
unsigned char readData_ds1820(void);
motor.c
#include <REG52.H>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <intrins.h>
#include <hd7279.h>
#include <global.h>
#include <motor.h>
#include <uart.h>
void init_motor(void){
TMOD = 0x01;
TH0 = 0xee;
TL0 = 0x00;
//EA = 1;
ET0 = 1;
TR0 = 1;
motor = 0;
P2 = 0x02;
}
motor.h
sbit motor = P1^2;
extern int motorNow;
void init_motor(void);
ee24c166.c
#include <REG52.H>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <intrins.h>
#include <hd7279.h>
#include <global.h>
#include <ee24c16.h>
#include <uart.h>
#include <ds18b20.h>
void init_24c16(void){
sda=1;
scl=1;
}
void I2C_start(void){
sda=1;
delay_us(5);
scl=1;
delay_us(5);
sda=0;
delay_us(5);
}
void I2C_stop(void){
sda=0;
delay_us(5);
scl=1;
delay_us(5);
sda=1;
delay_us(5);
}
void I2C_ack(void){
unsigned char i;
scl=1;
delay_us(5);
while((sda==1)&&(i<200))i++;
scl=0;
delay_us(5);
}
void I2C_noack(){
sda = 1;
delay_us(5);
scl = 1;
delay_us(5);
scl = 0;
delay_us(5);
}
void writebyte_I2C(unsigned char input){
unsigned char i;
scl = 0;
for(i=0;i<8;i++){
if(input&0x80)sda = 1;
else sda = 0;
input = input<<1;
delay_us(5);
scl = 1;
delay_us(5);
scl = 0;
delay_us(5);
}
sda = 1;
delay_us(5);
}
unsigned char readbyte_I2C(void){
unsigned char i,rbyte;
scl = 0;
delay_us(5);
sda = 1;
delay_us(5);
for(i=0;i<8;i++){
scl = 1;
delay_us(5);
rbyte = rbyte<<1;
if(sda)rbyte++;
scl = 0;
delay_us(5);
}
return rbyte;
}
void writebyte_24c16(unsigned char dat,unsigned char addr){
init_24c16();
I2C_start();
writebyte_I2C(WriteDeviceAddress);
I2C_ack();
writebyte_I2C(addr);
I2C_ack();
writebyte_I2C(dat);
I2C_ack();
I2C_stop();
}
unsigned char readbyte_24c16(unsigned char addr){
unsigned char output;
init_24c16();
I2C_start();
writebyte_I2C(WriteDeviceAddress);
I2C_ack();
writebyte_I2C(addr);
I2C_ack();
I2C_start();
writebyte_I2C(ReadDeviceAddress);
I2C_ack();
output = readbyte_I2C();
I2C_noack();
I2C_stop();
return output;
}
void save_all_data(void){
//Ö»µ÷ÓÃÒ»´Î
delay_us(150);
writebyte_24c16(10,0);
delay_us(150);
writebyte_24c16(20,1);
delay_us(150);
writebyte_24c16(30,2);
delay_us(150);
writebyte_24c16(40,3);
delay_us(150);
writebyte_24c16(50,4);
delay_us(150);
writebyte_24c16(60,5);
delay_us(150);
writebyte_24c16(70,6);
delay_us(150);
writebyte_24c16(80,7);
delay_us(150);
writebyte_24c16(90,8);
delay_us(150);
writebyte_24c16(100,9);
delay_us(150);
writebyte_24c16(23,10);//t_down
delay_us(150);
writebyte_24c16(32,11);//t_up
delay_us(150);
}
ee24c16.h
sbit scl = P1^1;
sbit sda = P1^0;
#define WriteDeviceAddress 0xa0
#define ReadDeviceAddress 0xa1
unsigned char readbyte_24c16(unsigned char);
void writebyte_24c16(unsigned char,unsigned char);
unsigned char readbyte_I2C(void);
void writebyte_I2C(unsigned char);
void I2C_noack(void);
void I2C_ack(void);
void I2C_stop(void);
void I2C_start(void);
void init_24c16(void);
void save_all_data(void);
pid.c
公式照着老师发的打的,参数找的网上说的工程参数,但是出来的结果不行,你们加油
#include <REG52.H>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <intrins.h>
#include <global.h>
#include <ds18b20.h>
#include <uart.h>
#include <pid.h>
float e_bebefore=0.0,e_before=0.0,e=0.0;
unsigned int pid_getpwm(int ui,float Tnow,float Tset,float kp,float ki,float kd){
//ui = 50,Tset = 28.0,kp=4.0,ki=0.33,kd=4.0
float A,B,C;
int u_delta;
A = kp*(1+ki+kd);
B = -kp*(1+2*kd);
C = kp*kd;
e_bebefore = e_before;
e_before = e;
e = Tnow-Tset;
//u_delta = 4*e-16*e_before;
u_delta = A*e+B*e_before+C*e_bebefore;
ui = ui+u_delta;
//if(ui>100) ui=100;
return ui;
}
pid.h
extern float e_bebefore,e_before,e;
unsigned int pid_getpwm(int,float,float,float,float,float);
global.c
#include <REG52.H>
#include <stdio.h>
#include <math.h>
#include <intrins.h>
#include <global.h>
void long_delay(void){
unsigned char d;
for(d=0;d<0x30;d++);
}
void short_delay(void){
unsigned char d;
for(d=0;d<8;d++);
}
void delay(unsigned int z){
//ms
unsigned int x,y;
for(x=z;x>0;x--) for(y=110;y>0;y--);
}
void delay_us(unsigned int z){
//微秒级延时 延时z微秒
unsigned int x;
for(x = 0;x<z;x++){
_nop_();
}
}
global.h
extern int menu_num,layer,run_num,con_num;
extern unsigned char keyvalue;
extern unsigned char LED[8];
extern float Tset,kp,ki,kd;
extern unsigned int pwm;
void long_delay(void);
void short_delay(void);
void delay(unsigned int);
void delay_us(unsigned int);
uart.c
这个模块是为了调试方便做的
如调用send_string_com(0xbd)就可以在串口调试助手中看到0xbd出现,即单片机向PC机发送了数据
#include <REG52.H>
#include <stdio.h>
#include <math.h>
#include <intrins.h>
#include <global.h>
#include <uart.h>
//void init_uart(void){
// TMOD = 0x20;
// TH1 = 0xfd;
// TL1 = 0xfd;
// TR1 = 1;
// SM0 = 0;
// SM1 = 1;
// REN = 1;
// EA = 1;
// ES = 1;
//}
void send_string_com(unsigned char str){
TMOD = 0x20;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1;
SM0 = 0;
SM1 = 1;
REN = 1;
EA = 1;
ES = 1;
SBUF = str;
while(!TI);
TI = 0;
}
uart.h
//void init_uart(void);
void send_string_com(unsigned char);
将以上所有.c文件添加到一个工程下,将所有.h文件和.c文件添加到一个文件夹下即可。
感谢paq ryh smc的帮助~大家加油