#define pinA PB11
#define pinB PB10
#define SW PA7
#include <STM32_TM1637.h> // http://rcl-radio.ru/wp-content/uploads/2020/02/STM32_TM1637_V1_3.zip
#include <STM32ADC.h>
#include <Wire.h>
#include <STM32_Encoder.h> // http://rcl-radio.ru/wp-content/uploads/2020/01/STM32_Encoder.zip
#include <EEPROM.h>
STM32_TM1637 tm(PB1,PB0);// CLK, DIO
STM32_Encoder encoder(pinA, pinB);
STM32ADC myADC(ADC1);
const float u_k = 7.08; // калибровка регулятора напряжения
const float u_pit = 3.30; // напряжение 3,3V на плате STM32
int position = 0;
uint8 pins = PA6;
const int maxSamples = 1;
uint16_t dataPoints[maxSamples];
int menu,reg_u,u_dig,reg_i,w,www;
float u_iz;
unsigned long times;
void setup() {
Serial.begin(9600);Wire.begin();Wire.setClock(400000L);encoder.begin();
tm.brig(7); // ЯРКОСТЬ 0...7
EEPROM.init(0x801F000,0x801F800,0x400);// 1024 byte
attachInterrupt(digitalPinToInterrupt(pinA),Read,CHANGE);
attachInterrupt(digitalPinToInterrupt(pinB),Read,CHANGE);
pinMode(SW,INPUT); // SW
myADC.calibrate();
rcc_set_prescaler(RCC_PRESCALER_ADC, RCC_ADCPRE_PCLK_DIV_8);
myADC.setSampleRate(ADC_SMPR_239_5);
myADC.setScanMode();
myADC.setPins(&pins,1);
myADC.setContinuous();
myADC.setDMA(dataPoints, 1, (DMA_MINC_MODE | DMA_CIRC_MODE), NULL);
myADC.startConversion();
reg_u = EEPROM.read(0);reg_i = EEPROM.read(1);
}
void loop(){
if(digitalRead(PA7)==LOW){menu++;if(menu>1){menu=0;}delay(200);}
u_iz = dataPoints[0]*u_pit/4095;
MCP4725(u_dig);
//Serial.println(u_iz);
if(menu==0){tm.print_float(float(reg_u/10.00),1, 0,0,0,0);}
if(menu==1){tm.print_float(float(reg_i/100.00),2, 0b01110111,0,0,0);}
////////// reg U //////////////////////////////////////////////////////////
if(menu==0){
if(position != encoder.getPosition()){position = encoder.getPosition();
reg_u = reg_u + encoder.getPosition();encoder.setPosition(0);position = 0;
if(reg_u>280){reg_u=280;}if(reg_u<0){reg_u=0;}times=millis();w=1;
u_dig = float(reg_u/10.00)/u_k*4095/5.00;
Serial.println(reg_u);}}
////////// reg I //////////////////////////////////////////////////////////
if(menu==1){
if(position != encoder.getPosition()){position = encoder.getPosition();
reg_i = reg_i + encoder.getPosition();encoder.setPosition(0);position = 0;
if(reg_i>250){reg_i=250;}if(reg_i<0){reg_i=0;}times=millis();w=1;}}
//////////////////////////////////////////////////////////////////////////
//////////////// EEPROM
if(millis()-times>5000&&w==1){EEPROM.update(0,reg_u);EEPROM.update(1,reg_i);menu=0;times=millis();w=0;}
delay(100);
}
void MCP4725(int data){
byte buffer[3];
buffer[0] = 0b01000000;
// data=0;
// buffer[0] = 0b01100000; // запись в память ЦАП 0 В
buffer[1] = data >> 4;
buffer[2] = data << 4;
Wire.beginTransmission(0x60);
Wire.write(buffer[0]);
Wire.write(buffer[1]);
Wire.write(buffer[2]);
Wire.endTransmission();
}
void Read(){encoder.readAB();}