MCU PID algorithm MAX6675 thermocouple temperature control project

Publisher:CoboroLatest update time:2019-10-21 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

0.jpg (58.35 KB, Downloads: 24)

Download attachments   and save them to the photo album

Uploaded on 2018-3-14 05:20

The microcontroller source program is as follows:

#include

#include//Includes random functions such as rand()


#include "STC12C5A60S2.h"

#include "zcy.h"

#include "s_12864.h"




////////////////////////////////////////////////////////

//Global variables


volatile long time0_temp1 = 0;

volatile long time0_temp2 = 0;


volatile long global_sec = 0;


int key_counter = 0 ;

int led_flash_mode_index = 2 ; //LED flash mode 1--8, starting from 1, up to 8 modes ssssssssss


volatile int time0_10ms_flag = 0;

int time0_10ms_counter = 0;

int led_active_flag = 0;


typedef void (*led_fun_str)(void); //Define the data type of a function pointer

//Then define an array with this data type

led_fun_str led_fun_bufffer[29+29];


void (*led_flash_fun_str)(void);


int led_index = 1;


int k_off = 0;

int k_on  = 0;


uchar temp_random = 0;

uchar now_temp_random = 0;

uchar last_temp_random = 0;

fly temp_diff = 0;


uchar temp_buffer_random[29];


int k_extern = 0;

int loop_temp = 0;


uchar temp_buffer_16_16_comm[32]; //Must be designed as a global variable to avoid display errors

uchar temp_buffer_8_16_comm[16]; //Must be designed as a global variable to avoid display errors


int refer_fun_flag = 0;

int key_perss_counter  = 0;

int key_once_active_flag = 0; //key action once

int key_value = 0 ;


float now_temp              = 0.0;

long dis_now_temp           = 0;

float wenkong_now_temp = 0.0; //Current temperature for temperature control


volatile int global_sec_flag = 0;

int temp_zero_below_flag = 1 ; //1 means 0 and positive temperature 0 means negative temperature


char temp_dis_num_buffer[10]; //Must be defined as a global variable, otherwise the cause of the error is unknown

char *temp_str;


uint them = 0;

int  ds_18b20_reset_ok_flag = 0;


//pid

float SV_value = 50.0; //Set temperature value

float PV_value = 0.0; //Current temperature value used for calculation

volatile float P_value = 0.0; //Proportional band, for example, 56.3 represents 56.3% 0.0--200.0

int I_value = 0; //Integral time seconds 0-3600

int D_value = 0; // differential time seconds 0-900


int comm_dis_once_flag = 1; //Initial value is 1

volatile int special_dis_once_flag = 1; //Initial value is 1

int pid_tune_flag = 0; //Initial value is 0, i.e. the default value is used in the pid stage 1 is the self-tuning process


int three_dot_dis_flag    = 0;


float Proportion = 0.0; // Proportional constant Proportional Const

float Integral = 0.0; // Integral Const        

float Derivative = 0.0; // Derivative constant Derivative Const

float  LastError   = 0.0;           //  Error[-1]

float  PrevError   = 0.0;           //  Error[-2]

float  SumError    = 0.0;           //  Sums of Errors

float  dError      = 0.0;

float  Error       = 0.0;


int   pid_result = 0;

float T_Hight = 0.0;

float T_LOW = 100.0; //temperature

long TIME_Hight = 0;

long TIME_LOW = 0; //Specific seconds

int pid_con_10ms_flag = 0;

int pid_con_counter        = 0;


float KC = 1.0; // critical scale factor initial default value

int TC = 40; //Oscillation period initial default value


int temp_pid = 0; //Set as a global variable  


volatile int get_now_temp_flag   = 0;

volatile int enable_pid_sec_flag = 0;

volatile int pid_self_sec_flag   = 0;


//uint pid_self_calc_buffer[200] _at_ 0xF000; //0xffff corresponds to the top of the flash


int zero_across_counter = 0;

int pid_self_first_status_flag = 0;


long pid_self_time_sec = 0;


float max_temp = 0.0 ; //The initial temperature is 0

float min_temp = 100.0 ; //initial temperature equals 100

float sum_temp = 0.0 ; //The initial temperature is 0

float aver_temp        = 0.0 ;


int cool_ack_counter    = 0;

int hot_ack_counter     = 0;

int once_add_1_flag     = 0;


float pid_self_calc_buffer[4];

int k_pid_self_counter = 0;


int enable_calc_min_max_flag = 0;

int k_max_min = 0;

int dis_tune_once_flag = 1;


int k_cut_off_flag = 0; //K cut-off flag

long k_reou_value = 0; 


int soft_dis_flag = 1;

int soft_counter  = 0;

int soft_end_counter = 0;


int pwm_con_time_flag = 0;


//qqqqqqqqqqqqqq

////////////////////////////////////////////////////////

//Function definition

void SendByte(uchar Dbyte); //Send byte data

void write_cmd(uchar Cbyte); //write instruction

void write_data(uchar Dbyte); //write data

void PUTchar8x8(int row,int col,int count,uchar *put);

void PUTchar8x16(int row,int col,int count,uchar *put);

void PUTchar16x16(int row,int col,int count,uchar *put); //32 bytes represent 1 Chinese character

void PUTchar24x24(int row,int col,int count,uchar *put);

void PUTBMP(void);//Picture

void PUTREVERSEBMP(void); //Reverse display of image

void LcmClear(void);//清屏

void LcmSet(void); //Display all, that is, the whole screen is black

void LcmInit(void);//initialization

void ohengxian(void); //O horizontal line program

void jihengxian(void); //Odd horizontal line program

void oshuxian(void); //O vertical line program

void jishuxian(void); //Odd vertical line program

void dianxian(void); //The point display program fills the screen with points

void zifu8x16xian(void); //Can display numbers and English

void zifu16x16xian(void); // can display specific Chinese characters

void lcd_dis_position_16_16(int line,int column,uchar zifu_16_16[2]); // 1 row 1 column specific character 

void lcd_dis_position_8_16(int line,int column,uchar zifu_8_16);// 1 row and 1 column specific character 

void lcd_s_12864_dis_8_16_str(int dis_line,int start_position,char *dis_str);//Display a line of 8*16 characters

void ds_18b20_DelayXus(int n);

void ds_18b20_init(void); //DS18B20 initialization

uchar ds_18b20_read_date(void); //read one byte

void ds_18b20_write_date(uchar date); //write a byte

float read_18b20_temp(void); //Read the temperature value of 18b20. The actual temperature value is returned and the value of temp_zero_below_flag is changed. If it is 0, it means it is below 0 degrees.

void key_pro(void);

void display_pro(void);

void pid_pro(void);

void dis_4_line_as_null(void);

void dis_pid_self_value(void);

float read_max6675_temper(void); // Use max6675 to read the temperature of the k probe and return 1 times the final temperature

void PWM_clock(uchar clock);

void PWM_start(uchar module,uchar mode);

void set_pwm_value(uchar value); //between 0--255 The larger the value, the higher the duty cycle and the larger the output voltage 40-->0.8v 237-->4.6v 




//hhhhhhhhhhhhhhhhhhhhhhhh

////////////////////////////////////////////////////////

//中断函数ttttttttttttttttttttttttttttt

void tm0_isr(void) interrupt 1 using 1  //1ms

{


TL0 = 0x20; //Set the initial timing value

TH0 = 0xD1; //Set the initial timing value

        

time0_temp1++;

if(time0_temp1 % 2 == 0 )//2ms

        {

        

        pid_con_10ms_flag = 1;

        

        }

if(time0_temp1 >= 10 )//10ms

        {

        time0_temp1 = 0;

        time0_10ms_flag = 1;

        

        }


time0_temp2++;


if(time0_temp2 % 200  == 0)//200ms

        {

        get_now_temp_flag   = 1;

        }


if(time0_temp2 % 200  == 0)//200ms

        {

        //get_now_temp_flag   = 1;

        

        pid_self_sec_flag   = 1;

        

        pwm_con_time_flag   = 1;

        enable_pid_sec_flag = 1;

        special_dis_once_flag = 1;

        

        }


if(time0_temp2 >= 1000 )//1s If you want 1000 to correspond to 1s, then no interruption can occur in the middle

        {

        time0_temp2 = 0;

        global_sec++;

        global_sec_flag = 1;

        

        three_dot_dis_flag  ^= 1;

    

        soft_dis_flag = 1; //soft start

        

        //ssr_con_1;delay_ms(10);ssr_con_0;//test

        

        }


}



void PCA_Intrrpt(void) interrupt 7 //pwm interrupt

{

if(CCF0) CCF0=0;

if(CCF1) CCF1=0; //software clear 

if(CF) CF=0; //software clear 

}




////////////////////////////////////////////////////////

//function


void Timer0Init(void) //1 millisecond@12.000MHz Timer 0

{

        AUXR |= 0x80; //Timer clock 1T mode

        TMOD &= 0xF0; //Set timer mode

        TMOD |= 0x01; //Set timer mode

        TL0 = 0x20; //Set the initial timing value

        TH0 = 0xD1; //Set the initial timing value

        TF0 = 0; // Clear TF0 flag

        TR0 = 1; //Timer 0 starts timing

        ET0 = 1;        //enable timer0 interrupt   

}



void io_init(void)

{


P3M0 = 0x00 ;   //  0000  0000  

P2M0 = 0xf0 ; // 1111 0000 The lower four bits are the buttons 

P1M0 = 0xff ; // 1111 1111 strong push-pull output lcd and ssr driver

P0M0 = 0x00 ; // 0000 0000 strong push-pull output


key_1_in;

key_2_in;

key_3_in;

key_4_in;


ssr_con_out;


lcd_s_12864_cs_out;

lcd_s_12864_reset_out;

lcd_s_12864_rs_out;

lcd_s_12864_sda_out;

lcd_s_12864_sck_out;

lcd_s_12864_light_out;


lcd_s_12864_cs_0; 

lcd_s_12864_reset_0; 

lcd_s_12864_rs_0; 

lcd_s_12864_sda_0; 

lcd_s_12864_sck_0;

lcd_s_12864_light_0;


max6675_so_in;

max6675_sck_out;

max6675_cs1_out;

cs1_1;


pwm_con_out;

pwm_con_0;


}




void power_on_event(void)//eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

{

int k;

for( k = 0; k < 2; k++ ) // flash twice

        {

        lcd_s_12864_light_0;

        delay_ms(200);

        lcd_s_12864_light_1;

        delay_ms(200);

        }


//Cite the basic functions, otherwise warning errors will always appear

if( refer_fun_flag == 1 ) // Set refer_fun_flag to 0 forever, which means never referencing these functions

        {

//        PUTchar8x8(1,1,5,zifu8x8);

//        PUTchar24x24(1,1,2,zifu24x24);

//PUTBMP();//Picture

// PUTREVERSEBMP(); //Image is reversed

// LcmClear(); //Clear screen

// LcmSet(); //Display all, that is, the whole screen is black

// LcmInit(); // Initialization

// ohengxian(); //O horizontal line program

// jihengxian(); // Odd horizontal line program

// oshuxian(); //O vertical line program

// jishuxian(); //Odd vertical line program

// dianxian(); //The point display program is full of points

// zifu8x16xian(); //Can display numbers and English

// zifu16x16xian(); // can display specific Chinese characters

//        lcd_dis_position_16_16(1,1,"郑");

//        lcd_dis_position_8_16(1,1,'8');


        }                                                                


}



//s_12864    lllllllllllllllllllllllllllllll

void SendByte(uchar Dbyte) //Send byte data

{

float i,TEMP; 

TEMP = Dbyte;

for(i=0;i<8;i++)

    {

    lcd_s_12864_sck_0;

    _nop_();        

        _nop_(); 

[1] [2] [3] [4] [5] [6] [7] [8]
Keywords:MCU Reference address:MCU PID algorithm MAX6675 thermocouple temperature control project

Previous article:Two-way learning fan remote control switch with natural wind MCU source program + circuit
Next article:Please engineer and customize your MCU code

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号