Jump to content
43oh

print sensor reading on the LCD sharp memory BoosterPack ?


Recommended Posts

I can not print a reading from a sensor on the LCD sharp memory BoosterPack   ,  I am using LCD_SharpBoosterPack_SPI  library,  I can only  print  the first issue of reading 

 

my  code is there

 
#include "Energia.h"
 
// Include application, user and local libraries
#include "SPI.h"
#include "LCD_SharpBoosterPack_SPI.h"
 
// Variables
 
LCD_SharpBoosterPack_SPI myScreen;
uint8_t k = 0;
char text_buffer[3];
 
 
// Add setup code
void setup() {
#if defined(__MSP430__)
    SPI.begin();
    SPI.setClockDivider(SPI_CLOCK_DIV2);
    SPI.setBitOrder(MSBFIRST);
    SPI.setDataMode(SPI_MODE0);
    
#elif defined(__LM4F120H5QR__)
    SPI.Select(2);
    SPI.begin();
    SPI.setClockDivider(SPI_CLOCK_DIV128); // for LM4F120H5QR DIV2 = 4 MHz !
#endif
    
    myScreen.begin();
    
    myScreen.setFont(1);
    myScreen.text(10, 10, "Hello!");
    myScreen.flush();  
    
    delay(1000);
    myScreen.clear();
}
 
// Add loop code
void loop() {
    
    myScreen.clearBuffer();
 
int a =analogRead(A0)/4;
 
  text_buffer[0] = '-';
  text_buffer[1] = a + '0';
  text_buffer[3] = '-';
 myScreen.setFont(1);
  myScreen.text(10,20,text_buffer );
      myScreen.flush();  
      
 

}

 

 

but if the reading is a 0-9 digit, the number is displayed fine, but when more than one digit (10-255) only shows symbols, could someone  help me?

 

I use energia 0101E0012

 

 

Link to post
Share on other sites
int a =analogRead(A0)/4;
 
  text_buffer[0] = '-';
  text_buffer[1] = a + '0';
  text_buffer[3] = '-';

You are using a trick to map a digit to an ASCII character. This trick works fine for single digit numbers, since they are in order in the ASCII table. If you need to write multiple digits, you will need to split those digits off first, before writing to the string buffer.

For example, the number 12 would need to be converted into the characters '1' and '2'. This cannot be achieved by simply adding '0' to 12, because '0' is the number 48, 48 + 12 = 60, which is the character '<'.

Now, to split the number 12 into 2 digits, we use simple division:

12 / 10 = 1

12 % 10 = 2

% is the remainder operator, so the remainder of 12/10 is 2.

Then, for each digit we can use the + '0' trick to take the correct ASCII character.

int a =analogRead(A0)/4;
 
  text_buffer[0] = '-';
  if (a >= 10)
  {
    text_buffer[1] = (a/10) + '0';
    text_buffer[2] = (a%10) + '0';
  }
  else
    text_buffer[2] = a + '0';
  text_buffer[3] = '-';

Note that this example is very crude and will only work for numbers < 100.

If you want numbers above 100 to work as well, just continue the division:

123 / 100 = 1

(123 % 100) / 10 = 2

123 % 10 = 3

 

or alternatively

 

123 / 100 = 1

123 - 100 * 1 = 23

23 / 10 = 2

23 - 10 * 2 = 3

 

this sequence can be extended to 1000 and 10000

Link to post
Share on other sites

This can be made a bit more efficient by using the C standard library function div(). It will provide the quotient (/) and remainder (%) with a single math operation rather than two.

 

For example, for two digits...

div_t d = div(n, 10);
text_buffer[0] = '0' + d.quot;  // tens
text_buffer[1] = '0' + d.rem;   // ones
For three digits...

div_t d = div(n, 100);
text_buffer[0] = '0' + d.quot;  // hundreds
d = div(d.rem, 10);
text_buffer[1] = '0' + d.quot;  // tens
text_buffer[2] = '0' + d.rem;   // ones
For four digits...

div_t d = div(n, 1000);
text_buffer[0] = '0' + d.quot;  // thousands
d = div(d.rem, 100);
text_buffer[1] = '0' + d.quot;  // hundreds
d = div(d,rem, 10);
text_buffer[2] = '0' + d.quot;  // tens
text_buffer[3] = '0' + d.rem;   // ones
etc...
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...