test

Dependencies:   ad5422_arduino mbed LT1446 ADS1248-1 LM35-1 Flash FT813 PGA280_ADS1259

Committer:
vitlog
Date:
Mon Jun 22 10:06:00 2020 +0000
Revision:
1:5d28312892aa
Parent:
0:98fcc06c66bf
Child:
2:04090a362742
22.06.2020

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vitlog 0:98fcc06c66bf 1 #include "PerifConfig.h"
vitlog 0:98fcc06c66bf 2 #include "PGA280.h"
vitlog 0:98fcc06c66bf 3 #include "ADS1259.h"
vitlog 0:98fcc06c66bf 4 #include "PGA280ADS1259.h"
vitlog 0:98fcc06c66bf 5 #include "main.h"
vitlog 1:5d28312892aa 6 #include "ad5422_arduino.h"
vitlog 1:5d28312892aa 7 #include "lm35.h"
vitlog 1:5d28312892aa 8 #include "LT1446.h"
vitlog 1:5d28312892aa 9
vitlog 1:5d28312892aa 10 #define PID_SAMPLE_TIME 0.25 //Период ПИД
vitlog 1:5d28312892aa 11 #define Kp (E_PCOEFF) //Получение коэффициентов ПИД
vitlog 1:5d28312892aa 12 #define Ki (E_PCOEFF / E_ICOEFF * PID_SAMPLE_TIME)
vitlog 1:5d28312892aa 13 #define Kd (E_PCOEFF * E_DCOEFF / PID_SAMPLE_TIME)
vitlog 1:5d28312892aa 14
vitlog 1:5d28312892aa 15 #define E_PCOEFF 0.11F //Пропорциональный коэффициент ПИД регулятора (Из EEPROM)
vitlog 1:5d28312892aa 16 #define E_ICOEFF 3000.0F //Постоянная времени интегрирования ПИД регулятора в секундах (Из EEPROM) (был 3000)
vitlog 1:5d28312892aa 17 #define E_DCOEFF 0.1F
vitlog 1:5d28312892aa 18 #define TEMP 35
vitlog 1:5d28312892aa 19
vitlog 0:98fcc06c66bf 20
vitlog 0:98fcc06c66bf 21 /*Таймер для вызова функции каждые 40 мс*/
vitlog 1:5d28312892aa 22 Ticker lm_temp;
vitlog 0:98fcc06c66bf 23 Ticker adctask;
vitlog 1:5d28312892aa 24 Ticker PID1;
vitlog 0:98fcc06c66bf 25 /*Конец*/
vitlog 1:5d28312892aa 26 InterruptIn button(USER_BUTTON);
vitlog 0:98fcc06c66bf 27
vitlog 1:5d28312892aa 28 volatile float Error = 0, dError = 0, last_Error = 0; //Ошибка и предыдущее значение для ПИД
vitlog 1:5d28312892aa 29 volatile float Integral = 0.0; //Интегральная составляющая ПИД-регулятора
vitlog 1:5d28312892aa 30 volatile uint8_t temp=0;
vitlog 0:98fcc06c66bf 31 char mbflag;
vitlog 0:98fcc06c66bf 32 unsigned char pga280gain;
vitlog 0:98fcc06c66bf 33 char UComandFlag;
vitlog 0:98fcc06c66bf 34 volatile unsigned char UComand;
vitlog 0:98fcc06c66bf 35 volatile long tempdata=0;
vitlog 0:98fcc06c66bf 36 char ch=0; //адрес на плате ТЭД-2 = 6
vitlog 0:98fcc06c66bf 37 float x=0;
vitlog 0:98fcc06c66bf 38 float k;
vitlog 0:98fcc06c66bf 39 unsigned char str[];
vitlog 0:98fcc06c66bf 40
vitlog 0:98fcc06c66bf 41
vitlog 0:98fcc06c66bf 42
vitlog 0:98fcc06c66bf 43 //функция чтения АДС для выполнения в задаче
vitlog 0:98fcc06c66bf 44 void readADC()
vitlog 0:98fcc06c66bf 45 {
vitlog 0:98fcc06c66bf 46 tempdata=ads1259_readData(5); //функция может зависать (надо избавиться от бесконечного цикла)
vitlog 0:98fcc06c66bf 47 x=NormADC(tempdata);
vitlog 1:5d28312892aa 48 UART.printf("%08f\r\n",x);
vitlog 0:98fcc06c66bf 49 }
vitlog 0:98fcc06c66bf 50
vitlog 1:5d28312892aa 51 void readtemp()
vitlog 1:5d28312892aa 52 {
vitlog 1:5d28312892aa 53 float t;
vitlog 1:5d28312892aa 54 /*Обработка ошибки*/
vitlog 1:5d28312892aa 55 LM35_start(0.25);
vitlog 1:5d28312892aa 56 if (LM35_0.ready) //если данные готовы то считать и сбросить флаг (способ многозадачности)
vitlog 1:5d28312892aa 57 {
vitlog 1:5d28312892aa 58 LM35_0.ready=0;
vitlog 1:5d28312892aa 59 t=LM35_0.temp;
vitlog 1:5d28312892aa 60 Error=temp-t;
vitlog 1:5d28312892aa 61 Integral += Error;
vitlog 1:5d28312892aa 62 dError = Error - last_Error;
vitlog 1:5d28312892aa 63 last_Error = Error;
vitlog 1:5d28312892aa 64 //временно записать в калиброванное значение (нельзя так делать)
vitlog 1:5d28312892aa 65 LT1446_0.dacA.Code=(uint16_t)(PID() * 4095);
vitlog 1:5d28312892aa 66 LT1446_Write(&LT1446_0);
vitlog 1:5d28312892aa 67 UART.printf("Power %04d temp %0.1f\r\n",LT1446_0.dacA.Code,t);
vitlog 1:5d28312892aa 68 }
vitlog 1:5d28312892aa 69
vitlog 1:5d28312892aa 70 }
vitlog 1:5d28312892aa 71 void printtemp()
vitlog 1:5d28312892aa 72 {
vitlog 1:5d28312892aa 73 if (LM35_0.ready) //если данные готовы то считать и сбросить флаг (способ многозадачности)
vitlog 1:5d28312892aa 74 {
vitlog 1:5d28312892aa 75 UART.printf("TempCol %0.1f\r\n",LM35_0.temp);
vitlog 1:5d28312892aa 76 LM35_0.ready=0;
vitlog 1:5d28312892aa 77 }
vitlog 1:5d28312892aa 78
vitlog 1:5d28312892aa 79 }
vitlog 0:98fcc06c66bf 80
vitlog 0:98fcc06c66bf 81 float inline NormADC(long data)
vitlog 0:98fcc06c66bf 82 {
vitlog 0:98fcc06c66bf 83 x=data*2.5/0x800000;
vitlog 0:98fcc06c66bf 84 //x=x*8/(1<<pga280gain);
vitlog 0:98fcc06c66bf 85 return x;
vitlog 0:98fcc06c66bf 86 }
vitlog 0:98fcc06c66bf 87
vitlog 0:98fcc06c66bf 88 void ComandCheck()
vitlog 0:98fcc06c66bf 89 {
vitlog 0:98fcc06c66bf 90 if(UART.getc()=='/')
vitlog 0:98fcc06c66bf 91 {
vitlog 0:98fcc06c66bf 92 UART_gets(16); //моя функция на время
vitlog 0:98fcc06c66bf 93 UART.printf("%c\r\n",str[0]);
vitlog 0:98fcc06c66bf 94 switch (str[0]){
vitlog 1:5d28312892aa 95 case 'k':{
vitlog 1:5d28312892aa 96 PID1.detach();
vitlog 1:5d28312892aa 97 temp=(str[1]-'0')*10+(str[2]-'0');
vitlog 1:5d28312892aa 98 UART.printf("temp= %d\r\n",temp);
vitlog 1:5d28312892aa 99 break;}
vitlog 1:5d28312892aa 100 case 't':{
vitlog 1:5d28312892aa 101 //снять показания термодатчика
vitlog 1:5d28312892aa 102 LM35_start(0.25);
vitlog 1:5d28312892aa 103 lm_temp.attach(&printtemp,0.25);
vitlog 1:5d28312892aa 104 break;}
vitlog 0:98fcc06c66bf 105 case 'p':{
vitlog 0:98fcc06c66bf 106 pga280gain=str[1]; //присваиваем числовое значение команды взятое из символа цифры
vitlog 0:98fcc06c66bf 107 pga280gain-=0x30;
vitlog 0:98fcc06c66bf 108 ch=5;
vitlog 0:98fcc06c66bf 109 pga280_setGAIN(pga280gain,ch);
vitlog 0:98fcc06c66bf 110 ch=0;
vitlog 0:98fcc06c66bf 111 break;}
vitlog 0:98fcc06c66bf 112 case 'g': {
vitlog 0:98fcc06c66bf 113 //запустить задачу чтения АЦП каждые 40 мс (25 герц)
vitlog 0:98fcc06c66bf 114 adctask.attach(&readADC,0.04);
vitlog 0:98fcc06c66bf 115 break;}
vitlog 1:5d28312892aa 116 case's': {//остановить постоянное отображение
vitlog 1:5d28312892aa 117 adctask.detach();
vitlog 1:5d28312892aa 118 lm_temp.detach();
vitlog 1:5d28312892aa 119 LM35_stop();
vitlog 0:98fcc06c66bf 120 break;}
vitlog 0:98fcc06c66bf 121 case 'd':
vitlog 0:98fcc06c66bf 122 //MBFlagRegister.DAC=1;
vitlog 0:98fcc06c66bf 123 break;
vitlog 0:98fcc06c66bf 124 case 'e':{
vitlog 0:98fcc06c66bf 125 tempdata=pga280_readOneRegisterDevice(PGA280_ERROR_ADR,5);//чтение ошибок
vitlog 0:98fcc06c66bf 126 printf("ERRORS %X\n",tempdata);
vitlog 0:98fcc06c66bf 127 break;}
vitlog 0:98fcc06c66bf 128 case 'r':{
vitlog 0:98fcc06c66bf 129 //перезагрука
vitlog 0:98fcc06c66bf 130 printf("System Reset\r\n");
vitlog 0:98fcc06c66bf 131 NVIC_SystemReset();
vitlog 0:98fcc06c66bf 132 break;}
vitlog 0:98fcc06c66bf 133 default:
vitlog 0:98fcc06c66bf 134 //MBFlagRegister.TMLS=0; //починка зависания (не работает)
vitlog 0:98fcc06c66bf 135 break;
vitlog 0:98fcc06c66bf 136 }
vitlog 0:98fcc06c66bf 137 }
vitlog 0:98fcc06c66bf 138 }
vitlog 1:5d28312892aa 139 float PID ()
vitlog 1:5d28312892aa 140 {
vitlog 1:5d28312892aa 141 float P = Kp * Error;
vitlog 1:5d28312892aa 142 float I = Ki * Integral;
vitlog 1:5d28312892aa 143 float D = Kd * dError;
vitlog 1:5d28312892aa 144 float control = P + I + D;
vitlog 1:5d28312892aa 145 if (control > 1.0) return 1.0;
vitlog 1:5d28312892aa 146 if (control < 0.0) return 0.0;
vitlog 1:5d28312892aa 147 return control;
vitlog 1:5d28312892aa 148 }
vitlog 1:5d28312892aa 149
vitlog 1:5d28312892aa 150 void pressed()
vitlog 1:5d28312892aa 151 {
vitlog 1:5d28312892aa 152 PID1.attach(&readtemp,0.25);
vitlog 1:5d28312892aa 153 UART.printf("PID is ON\r\n");
vitlog 1:5d28312892aa 154 }
vitlog 0:98fcc06c66bf 155
vitlog 0:98fcc06c66bf 156 int main()
vitlog 0:98fcc06c66bf 157 {
vitlog 1:5d28312892aa 158
vitlog 1:5d28312892aa 159 //Инициализация периферийных устройств
vitlog 1:5d28312892aa 160 SPI1_MasterInitMode(2);//работают режимы 1 и 2
vitlog 0:98fcc06c66bf 161 //запустить задачу проверки команды по прерыванию УАРТ
vitlog 0:98fcc06c66bf 162 UART.attach(&ComandCheck,Serial::RxIrq);
vitlog 1:5d28312892aa 163 EN1=1;
vitlog 1:5d28312892aa 164
vitlog 0:98fcc06c66bf 165
vitlog 0:98fcc06c66bf 166 ch=5;
vitlog 1:5d28312892aa 167 pga280_setAdress(ch);
vitlog 0:98fcc06c66bf 168 pga280_ads1259_init(ch); //инициализировать АЦП по адресу на плате ТЭД-2 = 5 (4)
vitlog 0:98fcc06c66bf 169 printf("\r\n");
vitlog 0:98fcc06c66bf 170 tempdata=pga280_readOneRegisterDevice(PGA280_BUF_TIMEOUT_ADR,ch);
vitlog 0:98fcc06c66bf 171 printf("Timeout %X\n",tempdata);
vitlog 0:98fcc06c66bf 172 wait_ms(20);
vitlog 0:98fcc06c66bf 173 pga280_setGAIN(GAIN_1_PGA280,ch);
vitlog 0:98fcc06c66bf 174 pga280_setMUX(2,ch);
vitlog 0:98fcc06c66bf 175 pga280gain=3; // gain=1 (0x03)
vitlog 0:98fcc06c66bf 176 tempdata=pga280_readOneRegisterDevice(PGA280_ERROR_ADR,ch);//чтение ошибок
vitlog 0:98fcc06c66bf 177 printf("Errors %X\n",tempdata);
vitlog 1:5d28312892aa 178 /*Конец*/
vitlog 0:98fcc06c66bf 179
vitlog 1:5d28312892aa 180 button.fall(&pressed);
vitlog 0:98fcc06c66bf 181
vitlog 1:5d28312892aa 182
vitlog 1:5d28312892aa 183 uint16_t q1=0;
vitlog 0:98fcc06c66bf 184 while(1)
vitlog 1:5d28312892aa 185 {
vitlog 1:5d28312892aa 186 q1=4;
vitlog 1:5d28312892aa 187 while(q1--){
vitlog 1:5d28312892aa 188 SPI1_MasterInitMode(q1);
vitlog 1:5d28312892aa 189 Cs=0;
vitlog 1:5d28312892aa 190 TED2.write(0x00);
vitlog 1:5d28312892aa 191 TED2.write(0x00);
vitlog 1:5d28312892aa 192 TED2.write(0x00);
vitlog 1:5d28312892aa 193 Cs=1;
vitlog 1:5d28312892aa 194 UART.printf("%d\r\n",q1);
vitlog 1:5d28312892aa 195 wait_ms(3000);
vitlog 1:5d28312892aa 196 }
vitlog 0:98fcc06c66bf 197 }
vitlog 0:98fcc06c66bf 198 }
vitlog 0:98fcc06c66bf 199