Visualizza messaggio singolo
Vecchio 24 maggio 17, 01:38   #76 (permalink)  Top
dex1
User
 
L'avatar di dex1
 
Data registr.: 19-01-2005
Residenza: 33170 e dintorni
Messaggi: 10.447
Citazione:
Originalmente inviato da aero330 Visualizza messaggio
Tenendo presente tutto quello che fin'ora è stato detto (dovrei aver messo tutto):
Codice:
#define sclk 13
#define mosi 11
#define cs   10
#define dc   9
#define rst  8  //  reset

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

// number of analog samples to take per reading, per channel
#define NSAMP 128 // number of samples to take befor displaying (128=2^7)

// voltage divider calibration values
#define Dv1    11.00
#define Dv2    11.00

//number of channels
#define N_CH   2

// ADC reference voltage / calibration value
#define VREF    5.00

unsigned int u16_sum[2] = {0, 0};                // u16_sums of samples taken
unsigned char u8_Scount = 0;  // current sample number
float f32_AVvolts[2] = {0.0, 0.0};        // calculated voltages
char u8_cnt1 = 0;                  // used in 'for' loops

float f32_V_max[2] = {0.00 ,0.00};
float f32_V_min[2] = {100.00, 100.00};

void setup()
{
   tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
   tft.fillScreen(ST7735_BLACK); //  clear screen
   tft.setTextColor(ST7735_WHITE);
   tft.drawRoundRect(2, 10, 120, 110, 5,ST7735_WHITE);
   tft.setTextSize(1);
   tft.setCursor(5,0);
   tft.println("2 channel voltmeter");
   tft.setTextColor(0XFF00);
   tft.setCursor(0,140);
   tft.println(" Caution max voltage         55vdc");
}

        
void loop()
{

    ADC_Reading();          //Read voltages
    MaxMin_Detection();     //Detect MAX/MIN and mean
    
    Print_V1();     //Display V1 and V2 voltages
    Print_V2();
    
    tft.drawRoundRect(2, 10, 120, 120, 5,ST7735_WHITE);  // draw a rectangle
    
    Reset_var();    //Reset variables
}

void ADC_Reading(){
  
    while (u8_Scount < NSAMP) {
        // sample each channel A0 to A1
        for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
            u16_sum[u8_cnt1] += analogRead(A0 + u8_cnt1);
        }
        u8_Scount++;
        delay(10);
    }
}

void MaxMin_Detection(){
  
    // calculate the voltage for each channel
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
        f32_AVvolts[u8_cnt1] = ((u16_sum[u8_cnt1]>>7) * VREF) / 1024.0;
        if(f32_AVvolts[u8_cnt1] > f32_V_max[u8_cnt1]){ //get MAX value of sample
          f32_V_max[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }
        if(f32_AVvolts[u8_cnt1] < f32_V_min[u8_cnt1]){ //get MIN value of sample
          f32_V_min[u8_cnt1] = f32_AVvolts[u8_cnt1];
        }         
    }
}

void Print_V1(){
      // voltage 1 - V1(pin A0)  
    tft.setTextColor(ST7735_YELLOW,ST7735_BLACK); // set color for V1
    tft.setTextSize(3);
    tft.setCursor(15, 20);
    tft.print("V1 ");
    tft.print(f32_AVvolts[0] * Dv1, 1);
    tft.print("V ");
    // voltage 1 - mean of V1
    tft.setTextColor(ST7735_GREEN,ST7735_BLACK);// set color for V1
    tft.setTextSize(2);
    tft.setCursor(15, 46);
    tft.print("Vmax ");
    tft.print(f32_V_max[1] * Dv1, 1);
    tft.print("V ");
    tft.setCursor(15, 63);
    tft.print("Vmin ");
    tft.print(f32_V_min[1] * Dv1, 1);
    tft.print("V ");
}

void Print_V2(){
    // voltge 2 - V2 (pin A1)
    tft.setTextColor(ST7735_CYAN,ST7735_BLACK);// set color for V3
    tft.setTextSize(3);
    tft.setCursor(15, 80);
    tft.print("V2 ");
    tft.print(f32_AVvolts[2] * Dv2, 1);
    tft.print("V ");
    // voltage 2 - mean of V2
    tft.setTextColor(ST7735_WHITE,ST7735_BLACK);// set color for V4
    tft.setTextSize(2);
    tft.setCursor(15, 106);
    tft.print("Vmax ");
    tft.print(f32_V_max[2] * Dv2, 1);
    tft.print("V ");
    tft.setCursor(15, 123);
    tft.print("Vmin ");
    tft.print(f32_V_min[2] * Dv2, 1);
    tft.print("V ");
}

void Reset_var(){
      // reset variables
    u8_Scount = 0;
    for (u8_cnt1 = 0; u8_cnt1 < N_CH; u8_cnt1++) {
        u16_sum[u8_cnt1] = 0;
    }
}
Sul display dovresti vedere una cosa del genere:

2 channels voltmeter
/--------------------------------------------\
V1 0.00 V
Vmax 0.00V
Vmin 0.00V

V2 0.00 V
Vmax 0.00V
Vmin 0.00V
\--------------------------------------------/
Caution max voltage
55vdc

Se le scritte sono decentrate o non allineate correggi tu in base a quello che vedi su display agendo su tft.setTextSize(), tft.setCursor() e tft.drawRoundRect()
fantastico,domani provo.
nel frattempo ho cercato qualche tutorial per capire meglio sia arduino
che come impostare questo display.
ce ne molto da studiare.
hai qualche consiglio?
sono in debito.
grazie ancora.
__________________
Trattiamo bene la terra su cui viviamo: essa non ci è stata donata dai nostri padri, ma ci è stata prestata dai nostri figli
dex1 non è collegato   Rispondi citando