Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

Simple scope readout on 5110 display

$
0
0

I'm in the process of building a dual lab power supply with and the Stellaris should provide monitoring and protection shutdown.

I've ported the Adafruit LCD library to Energia LM4F (it should be easy to port to MSP430 as well) so I'm using that to display stuff in graphical format.

 

The code below does a few tricks that might be useful for some of you: a rolling buffer to hold the data, automatic vertical scaling between the minimum and maximum values.

As always, any kind of feedback is appreciated, I'm still a beginner.

 

A picture is worth a thousand words. I only have 5 points of resolution in this picture but the vertical scale is actually 20 points.

IMG_0436.JPG

 


// Include application, user and local libraries
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
 
#include "Energia.h"
 
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
 
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(PC_5, PC_6, PC_7, PD_6, PD_7);
 
#define RBUFFER_SIZE LCDWIDTH
#define PIXEL_DOUBLING 0
#define PIXELS_INSTEAD_OF_LINES 0
 
int32_t voltages[RBUFFER_SIZE];
 
void initThermometer(){
  SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
  SysCtlADCSpeedSet(SYSCTL_ADCSPEED_125KSPS); // 250
  ADCSequenceDisable(ADC0_BASE, 1);
  ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_TS);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_TS);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_TS);
  ADCSequenceStepConfigure(ADC0_BASE, 1, 3, ADC_CTL_TS | ADC_CTL_IE | ADC_CTL_END);
  ADCSequenceEnable(ADC0_BASE, 1);
}
 
int32_t getTempAdcValue(){
  unsigned long ulADC0Value[4];
 
  ADCIntClear(ADC0_BASE, 1);
  ADCProcessorTrigger(ADC0_BASE, 1);
 
  while(!ADCIntStatus(ADC0_BASE, 1, false)){
  }
 
  ADCSequenceDataGet(ADC0_BASE, 1, ulADC0Value);
  unsigned long ulTempAvg = (ulADC0Value[0] + ulADC0Value[1] + ulADC0Value[2] + ulADC0Value[3] + 2)/4;
  int32_t ulTempValueC = 1475 - ((2475 * ulTempAvg)) / 4096;
  return ulTempValueC;
}
 
 
// Add setup code
void setup() {   
  pinMode(PC_4, OUTPUT);
  digitalWrite(PC_4, HIGH);//vcc
  pinMode(PF_3, OUTPUT);
  digitalWrite(PF_3, LOW);//vss
  pinMode(PB_3, OUTPUT);
  digitalWrite(PB_3, LOW);//led
 
  display.begin();
  display.setContrast(50);
  display.clearDisplay();   // clears the screen and buffer
 
  initThermometer();
 
  Serial.begin(9600);
}
 
// Display mask
char temptext[8] = {
  ' ', ' ', ' ', '.', ' ', 0x7f, 'C', 0x00};
 
int32_t minScale;
int32_t maxScale;
 
void appendToBuffer(int32_t buffer[], int32_t value){
  //roll the buffer one value forward
  for (uint8_t i=1; i<RBUFFER_SIZE; i++){
    buffer[i-1] = buffer[i];
  }
  //replace last position
  buffer[RBUFFER_SIZE-1] = value;
}
 
void drawBuffer(uint8_t xpos, uint8_t ypos, uint8_t bufferHeight){
  minScale = 9999999; //TODO
  maxScale = -9999999;
  // retrieve min and max values
  for (uint8_t i=0; i<RBUFFER_SIZE; i++){
    minScale = min(voltages[i],minScale);
    maxScale = max(voltages[i],maxScale);
  }
 
  uint8_t lastX = xpos;
  uint8_t lastY = ypos;
  //draw pixels
  for (uint8_t i=0; i<RBUFFER_SIZE; i++){
    uint8_t x = xpos + i;
    uint8_t y = ypos + bufferHeight - (bufferHeight*(voltages[i]-minScale)/(maxScale-minScale));
    if (PIXEL_DOUBLING && (i%2==0)){
      //produces artifacts, rolling buffer increment should be set to 2 to avoid it
    }
    else{
      if (PIXELS_INSTEAD_OF_LINES){
        display.drawPixel(x, y, BLACK);
      }
      else{
        display.drawLine(lastX, lastY, x, y, BLACK);
      }
      lastX = x;
      lastY = y;
    }
  }  
}
 
uint8_t ypos = 20;
uint8_t bufferHeight = 20;
 
// Add loop code
void loop() {
  int32_t number = getTempAdcValue();
  appendToBuffer(voltages, number);
 
  display.clearDisplay();
 
  //draw graph first so it stays under
  drawBuffer(0, ypos, 20);
 
  //min and max scale texts
  {
    display.setTextSize(1);
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, ypos+bufferHeight-6);
    display.print((float)minScale/10,1);
 
    display.setCursor(0, ypos);
    display.print((float)maxScale/10,1);
  }
 
  //large readout
  display.setTextSize(2);
  display.setTextColor(BLACK);
  display.setCursor(0,0);
  display.print((float)number/10,1);
  display.print('C');
 
  display.display();
  delay(200);
}
 

Viewing all articles
Browse latest Browse all 2077

Trending Articles