LoadCell_STM32_SDRAM_v1

Dependencies:   BSP_DISCO_F746NG DISCO-F746NG_SDRAMBlockDevice LCD_DISCO_F746NG mbed

Fork of LoadCell_STM32 by Tapio Valli

Committer:
tapiov
Date:
Sat Feb 24 16:18:46 2018 +0000
Revision:
5:f8d3bcb187de
Parent:
3:22b1719c2b92
Child:
6:641b171407c9
Data size halved (16 bit data). Improved operation and output.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tapiov 1:c3c61d08f31b 1 // LoadCell_STM32_RAM v2 functions
tapiov 1:c3c61d08f31b 2 // (C) Tapio Valli 2018-02-17
tapiov 1:c3c61d08f31b 3
tapiov 1:c3c61d08f31b 4 #include "mbed.h"
tapiov 1:c3c61d08f31b 5 #include "LCD_DISCO_F746NG.h"
tapiov 1:c3c61d08f31b 6
tapiov 5:f8d3bcb187de 7 #include <stdio.h>
tapiov 5:f8d3bcb187de 8 #include <errno.h>
tapiov 1:c3c61d08f31b 9 #include <stdint.h>
tapiov 1:c3c61d08f31b 10 #include <stdio.h>
tapiov 1:c3c61d08f31b 11 #include <stdlib.h>
tapiov 1:c3c61d08f31b 12 #include <string.h>
tapiov 1:c3c61d08f31b 13 #include <assert.h>
tapiov 1:c3c61d08f31b 14 #include <ctype.h>
tapiov 1:c3c61d08f31b 15
tapiov 1:c3c61d08f31b 16 #include "main.h"
tapiov 1:c3c61d08f31b 17
tapiov 1:c3c61d08f31b 18 // define the Serial object
tapiov 1:c3c61d08f31b 19 Serial pc2(USBTX, USBRX);
tapiov 1:c3c61d08f31b 20
tapiov 1:c3c61d08f31b 21 LCD_DISCO_F746NG lcd2;
tapiov 1:c3c61d08f31b 22
tapiov 1:c3c61d08f31b 23 void initArray(Array *a, size_t initialSize) {
tapiov 5:f8d3bcb187de 24 a->array = (uint16_t *)malloc(initialSize * sizeof(uint16_t));
tapiov 1:c3c61d08f31b 25 a->used = 0;
tapiov 1:c3c61d08f31b 26 a->size = initialSize;
tapiov 1:c3c61d08f31b 27 }
tapiov 1:c3c61d08f31b 28
tapiov 5:f8d3bcb187de 29 void insertArray(Array *a, uint32_t newsize) {
tapiov 3:22b1719c2b92 30 a->size = newsize;
tapiov 5:f8d3bcb187de 31 a->array = (uint16_t *)realloc(a->array, a->size * sizeof(uint16_t));
tapiov 3:22b1719c2b92 32 a->used = 0;
tapiov 3:22b1719c2b92 33 }
tapiov 3:22b1719c2b92 34
tapiov 1:c3c61d08f31b 35 void freeArray(Array *a) {
tapiov 1:c3c61d08f31b 36 free(a->array);
tapiov 1:c3c61d08f31b 37 a->array = NULL;
tapiov 1:c3c61d08f31b 38 a->used = a->size = 0;
tapiov 1:c3c61d08f31b 39 }
tapiov 1:c3c61d08f31b 40
tapiov 1:c3c61d08f31b 41 size_t string_parser(char *input, char ***word_array)
tapiov 1:c3c61d08f31b 42 {
tapiov 1:c3c61d08f31b 43 size_t n = 0;
tapiov 1:c3c61d08f31b 44 const char *p = input;
tapiov 1:c3c61d08f31b 45
tapiov 1:c3c61d08f31b 46 while ( *p )
tapiov 1:c3c61d08f31b 47 {
tapiov 1:c3c61d08f31b 48 while ( isspace( ( unsigned char )*p ) ) ++p;
tapiov 1:c3c61d08f31b 49 n += *p != '\0';
tapiov 1:c3c61d08f31b 50 while ( *p && !isspace( ( unsigned char )*p ) ) ++p;
tapiov 1:c3c61d08f31b 51 }
tapiov 1:c3c61d08f31b 52
tapiov 1:c3c61d08f31b 53 if ( n )
tapiov 1:c3c61d08f31b 54 {
tapiov 1:c3c61d08f31b 55 size_t i = 0;
tapiov 1:c3c61d08f31b 56
tapiov 1:c3c61d08f31b 57 *word_array = (char**)malloc( n * sizeof( char * ) );
tapiov 1:c3c61d08f31b 58
tapiov 1:c3c61d08f31b 59 p = input;
tapiov 1:c3c61d08f31b 60
tapiov 1:c3c61d08f31b 61 while ( *p )
tapiov 1:c3c61d08f31b 62 {
tapiov 1:c3c61d08f31b 63 while ( isspace( ( unsigned char )*p ) ) ++p;
tapiov 1:c3c61d08f31b 64 if ( *p )
tapiov 1:c3c61d08f31b 65 {
tapiov 1:c3c61d08f31b 66 const char *q = p;
tapiov 1:c3c61d08f31b 67 while ( *p && !isspace( ( unsigned char )*p ) ) ++p;
tapiov 1:c3c61d08f31b 68
tapiov 1:c3c61d08f31b 69 size_t length = p - q;
tapiov 1:c3c61d08f31b 70
tapiov 1:c3c61d08f31b 71 ( *word_array )[i] = ( char * )malloc( length + 1 );
tapiov 1:c3c61d08f31b 72
tapiov 1:c3c61d08f31b 73 strncpy( ( *word_array )[i], q, length );
tapiov 1:c3c61d08f31b 74 ( *word_array )[i][length] = '\0';
tapiov 1:c3c61d08f31b 75
tapiov 1:c3c61d08f31b 76 ++i;
tapiov 1:c3c61d08f31b 77 }
tapiov 1:c3c61d08f31b 78 }
tapiov 1:c3c61d08f31b 79 }
tapiov 1:c3c61d08f31b 80
tapiov 1:c3c61d08f31b 81 return n;
tapiov 1:c3c61d08f31b 82 }
tapiov 1:c3c61d08f31b 83
tapiov 1:c3c61d08f31b 84 void PlotData(uint32_t XCoordinate,uint32_t YCoordinate)
tapiov 1:c3c61d08f31b 85 {
tapiov 1:c3c61d08f31b 86 // Plot at x,y
tapiov 1:c3c61d08f31b 87 lcd2.DrawHLine(XCoordinate,YCoordinate,1);
tapiov 1:c3c61d08f31b 88 }
tapiov 1:c3c61d08f31b 89
tapiov 1:c3c61d08f31b 90 void InitScreen(uint32_t BackGroundColor,uint32_t ForeGroundColor)
tapiov 1:c3c61d08f31b 91 {
tapiov 1:c3c61d08f31b 92
tapiov 1:c3c61d08f31b 93 // #define LCD_COLOR_BLUE ((uint32_t)0xFF0000FF)
tapiov 1:c3c61d08f31b 94 // #define LCD_COLOR_GREEN ((uint32_t)0xFF00FF00)
tapiov 1:c3c61d08f31b 95 // #define LCD_COLOR_RED ((uint32_t)0xFFFF0000)
tapiov 1:c3c61d08f31b 96 // #define LCD_COLOR_CYAN ((uint32_t)0xFF00FFFF)
tapiov 1:c3c61d08f31b 97 // #define LCD_COLOR_MAGENTA ((uint32_t)0xFFFF00FF)
tapiov 1:c3c61d08f31b 98 // #define LCD_COLOR_YELLOW ((uint32_t)0xFFFFFF00)
tapiov 1:c3c61d08f31b 99 // #define LCD_COLOR_LIGHTBLUE ((uint32_t)0xFF8080FF)
tapiov 1:c3c61d08f31b 100 // #define LCD_COLOR_LIGHTGREEN ((uint32_t)0xFF80FF80)
tapiov 1:c3c61d08f31b 101 // #define LCD_COLOR_LIGHTRED ((uint32_t)0xFFFF8080)
tapiov 1:c3c61d08f31b 102 // #define LCD_COLOR_LIGHTCYAN ((uint32_t)0xFF80FFFF)
tapiov 1:c3c61d08f31b 103 // #define LCD_COLOR_LIGHTMAGENTA ((uint32_t)0xFFFF80FF)
tapiov 1:c3c61d08f31b 104 // #define LCD_COLOR_LIGHTYELLOW ((uint32_t)0xFFFFFF80)
tapiov 1:c3c61d08f31b 105 // #define LCD_COLOR_DARKBLUE ((uint32_t)0xFF000080)
tapiov 1:c3c61d08f31b 106 // #define LCD_COLOR_DARKGREEN ((uint32_t)0xFF008000)
tapiov 1:c3c61d08f31b 107 // #define LCD_COLOR_DARKRED ((uint32_t)0xFF800000)
tapiov 1:c3c61d08f31b 108 // #define LCD_COLOR_DARKCYAN ((uint32_t)0xFF008080)
tapiov 1:c3c61d08f31b 109 // #define LCD_COLOR_DARKMAGENTA ((uint32_t)0xFF800080)
tapiov 1:c3c61d08f31b 110 // #define LCD_COLOR_DARKYELLOW ((uint32_t)0xFF808000)
tapiov 1:c3c61d08f31b 111 // #define LCD_COLOR_WHITE ((uint32_t)0xFFFFFFFF)
tapiov 1:c3c61d08f31b 112 // #define LCD_COLOR_LIGHTGRAY ((uint32_t)0xFFD3D3D3)
tapiov 1:c3c61d08f31b 113 // #define LCD_COLOR_GRAY ((uint32_t)0xFF808080)
tapiov 1:c3c61d08f31b 114 // #define LCD_COLOR_DARKGRAY ((uint32_t)0xFF404040)
tapiov 1:c3c61d08f31b 115 // #define LCD_COLOR_BLACK ((uint32_t)0xFF000000)
tapiov 1:c3c61d08f31b 116 // #define LCD_COLOR_BROWN ((uint32_t)0xFFA52A2A)
tapiov 1:c3c61d08f31b 117 // #define LCD_COLOR_ORANGE ((uint32_t)0xFFFFA500)
tapiov 1:c3c61d08f31b 118 // #define LCD_COLOR_TRANSPARENT ((uint32_t)0xFF000000)
tapiov 1:c3c61d08f31b 119
tapiov 1:c3c61d08f31b 120 lcd2.Clear(BackGroundColor);
tapiov 1:c3c61d08f31b 121 lcd2.SetBackColor(BackGroundColor);
tapiov 1:c3c61d08f31b 122 lcd2.SetTextColor(ForeGroundColor);
tapiov 1:c3c61d08f31b 123 lcd2.SetFont(&Font20);
tapiov 1:c3c61d08f31b 124 }
tapiov 1:c3c61d08f31b 125
tapiov 1:c3c61d08f31b 126 void LCDWrite(uint32_t Line,char Str[],Text_AlignModeTypdef AlignMode)
tapiov 1:c3c61d08f31b 127 {
tapiov 1:c3c61d08f31b 128 char IntStr[50];
tapiov 1:c3c61d08f31b 129
tapiov 1:c3c61d08f31b 130 // InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE,Font20);
tapiov 1:c3c61d08f31b 131
tapiov 1:c3c61d08f31b 132 lcd2.ClearStringLine(Line);
tapiov 1:c3c61d08f31b 133 snprintf(IntStr,50,Str);
tapiov 1:c3c61d08f31b 134 lcd2.DisplayStringAt(0, LINE(Line), (uint8_t *)IntStr, AlignMode);
tapiov 1:c3c61d08f31b 135 }
tapiov 1:c3c61d08f31b 136
tapiov 1:c3c61d08f31b 137 void CountDown(uint32_t millisecs)
tapiov 1:c3c61d08f31b 138 {
tapiov 1:c3c61d08f31b 139 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
tapiov 1:c3c61d08f31b 140
tapiov 1:c3c61d08f31b 141 LCDWrite(5," ",CENTER_MODE);
tapiov 1:c3c61d08f31b 142 wait_ms(1);
tapiov 1:c3c61d08f31b 143
tapiov 1:c3c61d08f31b 144 LCDWrite(5,"Starting in 3... ",CENTER_MODE);
tapiov 1:c3c61d08f31b 145 wait_ms(millisecs);
tapiov 1:c3c61d08f31b 146
tapiov 1:c3c61d08f31b 147 LCDWrite(5,"Starting in 2... ",CENTER_MODE);
tapiov 1:c3c61d08f31b 148 wait_ms(millisecs);
tapiov 1:c3c61d08f31b 149
tapiov 1:c3c61d08f31b 150 LCDWrite(5,"Starting in 1... ",CENTER_MODE);
tapiov 1:c3c61d08f31b 151 wait_ms(millisecs);
tapiov 1:c3c61d08f31b 152
tapiov 1:c3c61d08f31b 153 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
tapiov 1:c3c61d08f31b 154 LCDWrite(5,"GO!",CENTER_MODE);
tapiov 1:c3c61d08f31b 155 }
tapiov 1:c3c61d08f31b 156
tapiov 1:c3c61d08f31b 157 void SamplePoints(Array *Data,uint32_t NoOfPoints,uint32_t Period_us)
tapiov 1:c3c61d08f31b 158 {
tapiov 1:c3c61d08f31b 159 AnalogIn ain(A0);
tapiov 1:c3c61d08f31b 160 uint32_t i;
tapiov 1:c3c61d08f31b 161
tapiov 1:c3c61d08f31b 162 // Measure NoOfPoints values (f.ex. 19200)
tapiov 1:c3c61d08f31b 163 for(i=0;i<NoOfPoints;i++) {
tapiov 5:f8d3bcb187de 164 Data->array[i]=(uint16_t)(ain.read_u16());
tapiov 1:c3c61d08f31b 165 wait_us(Period_us);
tapiov 1:c3c61d08f31b 166 }
tapiov 1:c3c61d08f31b 167
tapiov 1:c3c61d08f31b 168 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
tapiov 1:c3c61d08f31b 169 LCDWrite(5,"DONE!",CENTER_MODE);
tapiov 1:c3c61d08f31b 170
tapiov 2:386f65563144 171 pc2.printf("Sampling done.\r\n",i);
tapiov 1:c3c61d08f31b 172 }
tapiov 1:c3c61d08f31b 173
tapiov 1:c3c61d08f31b 174 void AvgAndPlotPoints(Array *Data,uint32_t NoOfPoints, uint32_t AvgSize) {
tapiov 1:c3c61d08f31b 175
tapiov 1:c3c61d08f31b 176 uint32_t i1,i2;
tapiov 1:c3c61d08f31b 177
tapiov 1:c3c61d08f31b 178 uint32_t BufferSum,BufferAvg;
tapiov 1:c3c61d08f31b 179 uint32_t XCoord,YCoord;
tapiov 1:c3c61d08f31b 180 char MyStr[50];
tapiov 1:c3c61d08f31b 181
tapiov 1:c3c61d08f31b 182 pc2.printf("Start reading... \r\n");
tapiov 1:c3c61d08f31b 183 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_RED);
tapiov 1:c3c61d08f31b 184
tapiov 1:c3c61d08f31b 185 for(i1=0;i1<NoOfPoints;i1++) {
tapiov 1:c3c61d08f31b 186 BufferSum=0;
tapiov 1:c3c61d08f31b 187
tapiov 1:c3c61d08f31b 188 // Read AvgSize samples
tapiov 1:c3c61d08f31b 189 for(i2=i1;i2<i1+AvgSize;i2++) {
tapiov 5:f8d3bcb187de 190 BufferSum=BufferSum+(uint32_t)Data->array[i2];
tapiov 1:c3c61d08f31b 191 }
tapiov 1:c3c61d08f31b 192
tapiov 1:c3c61d08f31b 193 BufferAvg=BufferSum/AvgSize;
tapiov 1:c3c61d08f31b 194
tapiov 1:c3c61d08f31b 195 // Calculate two coords and plot
tapiov 1:c3c61d08f31b 196 XCoord=((i1*480.0)/NoOfPoints);
tapiov 1:c3c61d08f31b 197 YCoord=(272.0*(BufferAvg/65536.0));
tapiov 1:c3c61d08f31b 198
tapiov 1:c3c61d08f31b 199 PlotData(XCoord,YCoord);
tapiov 1:c3c61d08f31b 200 }
tapiov 1:c3c61d08f31b 201
tapiov 1:c3c61d08f31b 202 pc2.printf("Done all, Points = %u Avg = %u \r\n", i1,AvgSize);
tapiov 1:c3c61d08f31b 203
tapiov 1:c3c61d08f31b 204 LCDWrite(0,"",CENTER_MODE);
tapiov 1:c3c61d08f31b 205 snprintf(MyStr,50,"Pnts = %d Avg = %d",NoOfPoints,AvgSize);
tapiov 1:c3c61d08f31b 206 LCDWrite(0,MyStr,RIGHT_MODE);
tapiov 1:c3c61d08f31b 207 }