NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Getting blank screen when running program

April 04, 2010
by Ralphxyz
Ralphxyz's Avatar

My code is a modification of the temp sensor program. I have modified it to handle multiple temp sensors. This is my first C program since I wrote a simple measurement program 15 years ago so I do not have any idea if my program even works but the immediate thing is that after it compiles successfully there is only a blank screen when the program runs.

Here is a the code that I have changed, everything else is straight from the tempsensor project:

uint16_t sensor0 = 0; 
uint16_t sensor1 = 0; 
uint16_t sensor2 = 0; 
uint16_t sensor3 = 0; 
uint16_t sensor4 = 0; 
uint16_t sensor5 = 0;
int16_t difference = 0;
while(1) {
// take 100 samples and average them!
temp_avg = 0.0;
uint8_t mux;    // place holder for the ADMUX value
//char sensor[1];
for(mux=0; mux<=5; mux++)
{
ADMUX=mux;

for(i=0; i<1000; i++) 
{
last_sample = adc_read();
this_temp = sampleToFahrenheit(last_sample);

// add this contribution to the average
temp_avg = temp_avg +  this_temp/100.0;

     switch(mux)
        {
        case 0:
          sensor0 = temp_avg;
          break;
        case 1:
          sensor1 = temp_avg;
          break;
        case 2:
          sensor2 = temp_avg;
          break;
        case 3:
          sensor3 = temp_avg;
          break;
        case 4:
          sensor4 = temp_avg;
          break;
        case 5:
          sensor5 = temp_avg;
          break;
        }                       
}

}

// write message to LCD
lcd_home();

//lcd_clear_and_home();
//lcd_line_one();

printf_P(PSTR("Hello, world!\r\n"));  //from NerdKit guys

printf_P(PSTR("%.2f degrees F\r\n"), temp_avg);
    }

return 0; }

I haven't the slightest idea if the code even runs. Is there a debugger that steps through the code?

Thanks I sure appreciate (and need ) your help and would really appreciate a detailed critique .

Ralph

April 04, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Ralph, where are you trying to view the output, and what do you mean by blank screen?

In the code above you are not sending your output to a specific output stream. This will work correctly if you map the uart output to stdin with the following lines

FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
stdin = stdout = &uart_stream;

This creates a stream for the uart and maps stdout to that stream. At that point you should be able to see the output if you set up putty on your computer to act as a terminal emulator. If you don't have the lines above the output to printf_P() won't be defined.

Humberto

April 05, 2010
by Ralphxyz
Ralphxyz's Avatar

Humberto, the uart_stream comes from your tempsensor code.

I only showed the code I modified everything else comes straight from your working code.

the blank screen is the LCD, sorry about not saying that.

Does my modified code look correct? Should it work?

I am just trying to get "Hello World" to appear on the LCD.

Once I get some output I'll format the sensor readings.

Ralph

April 05, 2010
by Ralphxyz
Ralphxyz's Avatar

One other thing. Whenever I modify the tempsensor project code I always get the blank LCD.

If I change the averaging code:

for(i=0; i<100; i++) 
{
last_sample = adc_read();
this_temp = sampleToFahrenheit(last_sample);

// add this contribution to the average
temp_avg = temp_avg + this_temp/100.0;
    }

To:

for(i=0; i<1000; i++) 
{
last_sample = adc_read();
this_temp = sampleToFahrenheit(last_sample);

// add this contribution to the average
temp_avg = temp_avg + this_temp/100.0;
    {

Just changing i<100 to i<1000 gets the blank LCD.

Ralph

April 05, 2010
by Ralphxyz
Ralphxyz's Avatar

oops, the problem was with the change to the averaging i<1000, I had left 1000 in my code. Once I restored the i<100 I get the display on the LCD as desired.

Why in the world would that change make a difference?

Ralph

April 05, 2010
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi Ralph,

At least in our sample code, we have the variable i defined as being of type "uint8_t". This means that it can only hold values 0 through 255. As your inner for loop tries to keep counting until it gets to 1000, it fails to ever exit that for loop, because the condition i<1000 is always true. (When i=255 and is then incremented by one, it rolls around back to 0.)

You can change "uint8_t i;" to "uint16_t i;", which will let you count up to 65535.

Mike

April 05, 2010
by Ralphxyz
Ralphxyz's Avatar

Ah, that explains it! In the averaging for loop when I changed i<100 to i<1000 I got the blank LCD because it never exited the loop as you say.

That is great. As long as I use i<100 everything works great!

I was thinking of using the greater averaging loop as a delay to stop the LCD from flickering but I have since learned how to use a delay_ms(100000), which appears to be working.

Thanks so much for your explanation.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that NerdKits make a great gift? Learn more...