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.

Microcontroller Programming » Strange Text Blinking help

April 24, 2011
by missle3944
missle3944's Avatar

Hi, I am veryy confused on my code because when I have a button pushed I want text to be displayed but for some reason it blinks on the LCD screen. Here is my code

    // for NerdKits with ATmega168
    // LCD GAME
    #define F_CPU 14745600

    #include <stdio.h>

    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include <avr/pgmspace.h>
    #include <inttypes.h>
    #include "../libnerdkits/delay.h"
    #include "../libnerdkits/lcd.h"
    #include "../libnerdkits/delay.h"
    #include "../libnerdkits/lcd.h"
    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include <avr/pgmspace.h>
    #include <inttypes.h>

    #include "../libnerdkits/delay.h"
    #include "../libnerdkits/lcd.h"
    #include "../libnerdkits/uart.h"
    // PIN DEFINITIONS:
    //
    // PC4 -- LED anode

    int down (int x)
    {

        return 1 + x;   //calculate the sum
        //return the answer
    }

    int up (int y)
    {

        return 1 + y;   //calculate the sum
        //return the answer
    }

    int main() {
    uint8_t mode_selectb;
    uint8_t mode_select;

         lcd_init();
      FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
      lcd_write_string(PSTR("MANGAME"));
      delay_ms(10);
      // init LCD

    // Set the pin to input mode for Pushbutton
      DDRC &= ~(1<<PC3); // set PC2 as input

      // turn on the internal resistor for the pin
      PORTC |= (1<<PC3); // turn on internal pull up resistor

    // Set the pin to input mode for Pushbutton
      DDRC &= ~(1<<PC2); // set PC2 as input

      // turn on the internal resistor for the pin
      PORTC |= (1<<PC2); // turn on internal pull up resistor

    // declare the variables to represent pushbutton input

      // print message to screen
      //             20 columns wide:
      //                     01234567890123456789

      int x = 1;
      int y = 0;

      //WHHHHHHHHHHHHHHHHHHHHHIIIIIIIIIIIIIIIIIIIIIIILLLLLLLLLLLLLLLLLLLLLLLLEEEEEEEEEEEEEEEE***************
      while(1) {

      lcd_init();
      lcd_home();

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

    mode_selectb = (PINC & (1<<PC3)) >> PC3;
    mode_select = (PINC & (1<<PC2)) >> PC2;

    if(mode_selectb == 0) // Pushbutton switch "******************OPEN*******************" (tied to GND) 
    {

      lcd_line_one();
      lcd_goto_position(up(y),down(x));
    lcd_write_string(PSTR("0"));

     y ++;

     }
    if (mode_select == 0) // Pushbutton switch "******************OPEN*******************" (tied to GND) 
    {

     x ++;

      }

      lcd_line_one();
      lcd_goto_position(1,5);
    lcd_write_string(PSTR("|"));
    lcd_goto_position(1,6);
    lcd_write_string(PSTR("|"));

    delay_ms(100);

    lcd_goto_position(1,7);
    lcd_write_string(PSTR("|"));

    delay_ms(90);
    lcd_goto_position(1,8);
    lcd_write_string(PSTR("|"));

    delay_ms(80);
    lcd_goto_position(1,9);
    lcd_write_string(PSTR("|"));

    delay_ms(70);

      }

      return 0;
    }
April 25, 2011
by xodin
xodin's Avatar

Try moving lcd_init before the while loop.

April 25, 2011
by xodin
xodin's Avatar

Actually, it looks like you have it in two places. You only need to init once. Same thing with uart_init, you should move this whole block up above the while loop:

uart_init();   FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);   stdin = stdout = &uart_stream;
April 25, 2011
by missle3944
missle3944's Avatar

Hi xodin, I never thought of that. I'll give that a try.

Missle3944

Post a Reply

Please log in to post a reply.

Did you know that you can make a spooky Halloween Jack-O-Lantern with a microcontroller? Learn more...