6 years, 7 months ago.

narrow the cycle of a wave displayed on the lcd with low frequency

below is the program that shows frequency from 1hz to 350hz on a C12832 LCD. But it only shows a full cycle for high frequency wave. while i need it to show a full cycle for low frequency wave. please do advise me to improve my program to factor in wave size.

  1. include "mbed.h"
  2. include "rtos.h"
  3. include "stdio.h"
  4. include "C12832_lcd.h"

C12832_LCD lcd;

AnalogIn Ain(p20);

Mutex lcd_mutex;

void thread(void const *args) { int col, height; float ADCdata;

while(1) { lcd_mutex.lock();

for(col=0; col<128; col++) { ADCdata = Ain*3.3;

height=31-int(ADCdata*10);

lcd.pixel(col, height, 1); }

lcd.copy_to_lcd(); lcd_mutex.unlock(); Thread::wait(2000); lcd.cls(); } }

int main() { lcd.cls(); Thread t(thread); while(1); }

Question relating to:

Rapid Prototyping for general microcontroller applications, Ethernet, USB and 32-bit ARM® Cortex™-M3 based designs

1 Answer

6 years, 7 months ago.

If you don't mind the screen update thread taking longer then it is simple, add a delay to the code that reads the analog in:

Timer updateRateTimer;
float samplePeriod = 1.0/(128*frequency); 
updateRateTimer.start();
for(col=0; col<128; col++) {
  updateRateTimer.reset();
  ADCdata = Ain*3.3;
  height=31-int(ADCdata*10);
  lcd.pixel(col, height, 1); 
  while (updateRateTimer < samplePeriod) {}
}

where frequency is the lowest frequency you want to be able to see a full cycle of.

The downside to this method is that for a 1Hz wave your screen update will be blocking the CPU for 1 second, since you are using a thread I'd assume you want to have something else going on at the same time.

The other option would be to have a ticker interrupt doing the sampling and set a flag once there is a full buffer of data. The LCD thread could set up the ticker and then loop waiting for short time periods until the buffer is full and then draw the screen.

Oh and please use <<code>> and <</code>> around your code (each one on its own line) so that code posted here is correctly formatted. See the editing tips for more details.

Accepted Answer

how do i integrate this code into my own code to improve because i have already tried and it does not display it on the lcd screen

posted by Shariq Azeem 16 Jun 2015