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 » first LCD row

January 23, 2012
by Pr0ject
Pr0ject's Avatar

My LCD that came with the nerdkits works fine if I write in all it's characters. Though when I try to replace the first row a couple of problems happen, the first character becomes a 't' and the rest of the line either flickers as if it's changing really fast or it's fine. Please note that this happens only with the first row

January 23, 2012
by treymd
treymd's Avatar

Can we see your code?

January 23, 2012
by Pr0ject
Pr0ject's Avatar

If you must, but be warned its in a mess at the moment since I'm still working on it. Also it has a lot of copy and pasting... Also I deleted everything concerning the first row in order to work on other things without it interfering (except fort he part where it just puts in the first row blanks).

#define F_CPU 14745600

#include <avr/io.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>       // needed for uint8_t
#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <math.h>
#include "../libnerdkits/uart.h"
#define MYPIN  (1<<PC5)    
#define SWITCH (1<<PC4)
#define LOL (MYPIN | SWITCH)
#define RAND_MAX   0x7FFF

// this code scans ADC1 for an analog signal upon request, using 8Mhz processor clock

int ADCsingleREAD(uint8_t adctouse)
{
    int ADCval;

    ADMUX = adctouse;         // use #1 ADC
    ADMUX |= (1 << REFS0);    // use AVcc as the reference
    ADMUX &= ~(1 << ADLAR);   // clear for 10 bit resolution

    ADCSRA|= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);    // 128 prescale for 8Mhz
    ADCSRA |= (1 << ADEN);    // Enable the ADC

    ADCSRA |= (1 << ADSC);    // Start the ADC conversion

    ADCval = ADCL;
    ADCval = (ADCH << 8) + ADCval;    // ADCH is read so ADC can be updated again

    return ADCval;
}
void adc_init() {
  // set analog to digital converter
  // for external reference (5v), single ended input ADC0
  ADMUX = 0;

  // set analog to digital converter
  // to be enabled, with a clock prescale of 1/128
  // so that the ADC clock runs at 115.2kHz.
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);

  // fire a conversion just to get the ADC warmed up
  ADCSRA |= (1<<ADSC);
}

void realtimeclock_setup() {
  // setup Timer0:
  // CTC (Clear Timer on Compare Match mode)
  // TOP set by OCR0A register
  TCCR0A |= (1<<WGM01);
  // clocked from CLK/1024
  // which is 14745600/1024, or 14400 increments per second
  TCCR0B |= (1<<CS02) | (1<<CS00);
  // set TOP to 143
  // because it counts 0, 1, 2, ... 142, 143, 0, 1, 2 ...
  // so 0 through 143 equals 144 events
  OCR0A = 143;
  // enable interrupt on compare event
  // (14400 / 144 = 100 per second)
  TIMSK0 |= (1<<OCIE0A);
}
volatile int32_t the_time;
SIGNAL(SIG_OUTPUT_COMPARE0A) {
  // when Timer0 gets to its Output Compare value,
  // one one-hundredth of a second has elapsed (0.01 seconds).
  the_time++;
}

int main(void){
PORTC |= LOL; // turn on pull-up resistor
realtimeclock_setup();
  // start up the LCD
  lcd_init();
 FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
  lcd_home();

  // start up the Analog to Digital Converter
  adc_init();

  // start up the serial port
  uart_init();
  FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  stdin = stdout = &uart_stream;
  sei();

    int ADCvalue;
    int min;
    int max;
    int A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X;
    while (1)
    {

           ADCvalue = ADCsingleREAD(5);
            // ADCvalue now contains an 10bit ADC read

            lcd_line_one();
            lcd_write_string(PSTR("                             "));
            lcd_line_two();
            lcd_write_string(PSTR("light level :  "));
            lcd_write_int16(ADCvalue);

            lcd_line_three();
   fprintf_P(&lcd_stream, PSTR("%16.2f sec"), (double) the_time / 100.0);

            //if ((double) the_time / 100.0 > 5) {

            //lcd_line_one();
            //lcd_write_string(PSTR(" apple pie "));
            //lcd_line_four();
            //lcd_write_string(PSTR("this is a replacement"));
            //}
            //else {
            //lcd_line_four();
            //lcd_write_string(PSTR("silen"));
            //}
            if (ADCvalue > max){
                if (ADCvalue - max > 50){
                    max = ADCvalue;
                }
            }
            if (ADCvalue < min) {
                min = ADCvalue
            }
            if ((double) the_time / 100.0 = 1800){
            A = ADCvalue;
            }
            if ((double) the_time / 100.0 = 3600){
            B = ADCvalue;
            }
            if ((double) the_time / 100.0 = 5400){
            C = ADCvalue;
            }
            if ((double) the_time / 100.0 = 7200){
            D = ADCvalue;
            }
            if ((double) the_time / 100.0 = 9000){
            E = ADCvalue;
            }
            if ((double) the_time / 100.0 = 10800){
            F = ADCvalue;
            }
            if ((double) the_time / 100.0 = 12600){
            G = ADCvalue;
            }
            if ((double) the_time / 100.0 = 14400){
            H = ADCvalue;
            }
            if ((double) the_time / 100.0 = 16200){
            I = ADCvalue;
            }
            if ((double) the_time / 100.0 = 18000){
            J = ADCvalue;
            }
            if ((double) the_time / 100.0 = 19800){
            K = ADCvalue;
            }
            if ((double) the_time / 100.0 = 21600){
            L = ADCvalue;
            }
            if ((double) the_time / 100.0 = 23400){
            M = ADCvalue;
            }
            if ((double) the_time / 100.0 = 25200){
            N = ADCvalue;
            }
            if ((double) the_time / 100.0 = 27000){
            O = ADCvalue;
            }
            if ((double) the_time / 100.0 = 28800){
            P = ADCvalue;
            }
            if ((double) the_time / 100.0 = 30600){
            Q = ADCvalue;
            }
            if ((double) the_time / 100.0 = 32400){
            R = ADCvalue;
            }
            if ((double) the_time / 100.0 = 34200){
            S = ADCvalue;
            }
            if ((double) the_time / 100.0 = 36000){
            T = ADCvalue;
            }
            if ((double) the_time / 100.0 = 37800){
            U = ADCvalue;
            }
            if ((double) the_time / 100.0 = 39600){
            V = ADCvalue;
            }
            if ((double) the_time / 100.0 = 41400){
            W = ADCvalue;
            }
            if ((double) the_time / 100.0 = 43200){
            X = ADCvalue;
            }
            lcd_line_four();
            lcd_write_string(PSTR("Maximum: "));
            lcd_write_int16(max);

    }
}
January 23, 2012
by Pr0ject
Pr0ject's Avatar

Never mind, I found out that the problem was that the program was running in an endless loop so it kept on changing between what the row has on it.

Yet I still need help figuring out why on the first character on the first row it prints out a 't'.

January 23, 2012
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Pr0ject,

Thats certainly a little weird. The problem could have something to do with the fact that you are using %f instead of %lf to print doubles. The other issue could be that you are printing too many characters to line #3. You only have 20 characters to print to and I believe your string will print 22 characters. Let us know if you find the problem.

Humberto

January 24, 2012
by Pr0ject
Pr0ject's Avatar

Thank you all for the help! I don't have any problems now, and I am almost done with my little project :D

Post a Reply

Please log in to post a reply.

Did you know that an electroluminescent backlight for an LCD panel requires hundreds of volts AC to run? Learn more...