Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
vilvide2
PostPosted: Dec 23, 2007 - 05:23 PM
Rookie


Joined: Dec 19, 2007
Posts: 48
Location: Denmark

Hallo all.

I have tried to adapt a LCD c code for my development board stk200.

now it is almost working but i can only print one charter and only the last one....??

Most I send a commando that tells the lcd to shift right????

se my code below.

best regard
denmark


#include "lcd_driver_backup.h"



int main(void)

{
LCD_init();

LCD_write("hallo");// display the message

while(1);
}









#include <avr/io.h>
#include <avr/delay.h>

#define LCD_RS 6 //2 is the number of the pin portc.6
#define LCD_RW 7 //3 is the number of the pin portd.7
#define LCD_RD 6 //3 is the number of the pin portd.6
#define LCD_E 7 //4 is the number of the pin portc.7




// LCD_putchar writes a character to the LCD at the current address, no busy flag check is done before or after
//the character is written!
//usage: LCD_putchar('A'); or LCD_putchar(0x55);
void LCD_putchar(char data)
{

DDRA = 0xFF;//PortD is output
PORTC &= ~((1<<LCD_RS)|(1<<LCD_E));
PORTD &= ~(1<<LCD_RW);
PORTD &= ~(1<<LCD_RD);
PORTA = data;//put data on bus

PORTC |= (1<<LCD_E);
PORTC |= (1<<LCD_RS);

asm volatile ("nop");//the number of nops required varies with your clock frequency, try it out!
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");//the number of nops required varies with your clock frequency, try it out!
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");//the number of nops required varies with your clock frequency, try it out!
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");




asm volatile ("nop");//the number of nops required varies with your clock frequency, try it out!
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");//the number of nops required varies with your clock frequency, try it out!
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");//the number of nops required varies with your clock frequency, try it out!
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");

PORTC &= ~(1<<LCD_E);
PORTC &= ~(1<<LCD_RS);



DDRA = 0;//release bus
}///////////////////////////// End of Put Char

//LCD_getaddress reads the address counter and busy flag. For the address only, mask off bit7 of the return value.
char LCD_getaddr(void)
{
char address;//make var for the return value
DDRA = 0;//PortD is input
PORTD |= (1<<LCD_RD);
PORTC |= (1<<LCD_E);//RW high, strobe enable
asm volatile ("nop");
asm volatile ("nop");
address = PINA;//while E is high, get data from LCD
PORTD &= ~(1<<LCD_RD);
PORTC &= ~(1<<LCD_E);//reset RW to low, E low (for strobe)

return address;//return address and busy flag
}//////////////////////// End of Get ADDRESS

//LCD_wait reads the address counter (which contains the busy flag) and loops until the busy flag is cleared.
void LCD_wait(void)
{
while((LCD_getaddr() & 0x80) == 0x80);//get address and busy flag and loop until busy flag cleared
}/////////////// End Of LCD Wait

//LCD_command works EXACTLY like LCD_putchar, but takes RS low for accessing the command reg
//see LCD_putchar for details on the code
void LCD_command(char command)
{
DDRA = 0xFF;
PORTC &= ~((1<<LCD_RS)|(1<<LCD_E));
PORTD &= ~(1<<LCD_RW);
PORTD |= (1<<LCD_RW);
PORTC |= (1<<LCD_E);


PORTA = command;


asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");


PORTC &= ~(1<<LCD_E);
PORTD &= ~(1<<LCD_RW);
DDRA = 0;
}///////////////////// End Of LCD Command

/*LCD_init initialises the LCD with the following paramters:
8 bit mode, 5*7 font, 2 lines (also for 4 lines)
auto-inc cursor after write and read
cursor and didsplay on, cursor blinking.
*/
void LCD_init(void)
{


DDRC |= ((1<<LCD_E)|(1<<LCD_RS));

PORTC = 0x00;

DDRD |= ((1<<LCD_RW)|(1<<LCD_RD));//setup the LCD control signals on PortA
PORTD = 0x00;
LCD_wait();
LCD_command(0x30);
LCD_wait();
LCD_command(0x01);
LCD_wait();//LCD_wait();//_delay_loop_2(0xFFFF);
LCD_command(0x38);//now: 8 bit interface, 5*7 font, 2 lines.
LCD_wait();//LCD_wait();//wait until command finished
LCD_command(0x06);//display on, cursor on (blinking)
LCD_wait();
LCD_command(0x10);//display on, cursor on (blinking)
LCD_wait();
LCD_command(0x0F);
LCD_wait();

}/////////////////////// End of LCD Init

//now it's time for a simple function for showing strings on the LCD. It uses the low-level functions above.
//usage example:
void LCD_write(char* dstring)
{

while(*dstring)//is the character pointed at by dstring a zero? If not, write character to LCD


{
LCD_wait();//if the LCD is bus, let it finish the current operation
//LCD_putchar(*sa++);//the write the character from dstring to the LCD, then post-inc the dstring is pointing at.
LCD_putchar(*dstring++);//the write the character from dstring to the LCD, then post-inc the dstring is pointing at.
}
}////////////////// End of LCD Write

//*
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Dec 23, 2007 - 07:34 PM
Raving lunatic


Joined: Mar 27, 2002
Posts: 7758
Location: Lund, Sweden

Advice: No-one will bother to read that code until you learn to use the code-tags so that the code keeps the formatting it has on your development system.c We'd like to see something like this...
Code:

#include "lcd_driver_backup.h"

int main(void)
{
   LCD_init();
   LCD_write("hallo");// display the message
   while(1);
}

// and so on...
 
 View user's profile Send private message  
Reply with quote Back to top
microcarl
PostPosted: Dec 23, 2007 - 08:38 PM
Raving lunatic


Joined: May 30, 2004
Posts: 7777
Location: Cincinnati, Ohio

Okay!!! As this is the Christmas season, I started to do you a favor by formatting and indenting your code for you. I also started to fix your problems...

But after a while, I realized that your code is missing so much, out of sequence, has wrong bit polarities and the wrong time, etc..., and because I don't really know how you have the LCD E, RS & R/W bits wired up, I said to my self... "Self, why am I doing this??? " So, here's what I'm going to do for you, as it is the Christmas season, and all.

I'll just post some working 8 bit code and you'll have an example that has been verified to work properly by many here on the forum.

And too, feel free to use my code, if you want.

Code:

// My_LCD.c

/*
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the below copyright notice appears in all copies and that both the copyright notice and this permission notice appear in supporting documentation. Livingston Electronics makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.


8 BIT LCD control program
Written by Carl W. Livingston
Copyright Carl W. Livingston
microcarl@roadrunner.com
AVR Freaks member - microcarl
January 02, 2007
*/

/*************************************************************/
/*************************************************************/
#include <iot2313v.h>
/*************************************************************/
/*************************************************************/

/*   //// From the LCD perspective \\\\
      RxD --> PORTD:0
      LED <-- PORTD:1 // High = ON, Low = OFF
      J_1 --> PORTD:2 // BAUD rate select
      J_2 --> PORTD:3 // BAUD rate select
      LCD:R/W <-- PORTD:4
      LCD:RS <-- PORTD:5
      LCD:E <-- PORTD:6
      N/A <-> PORTD:7

      LCD:R/W <-- PORTD:4
      LCD:RS <-- PORTD:5
      LCD:E <-- PORTD:6
      LCD:Vee <-- CONTRAST
      LCD:DB0 <-- PORTB:0
      LCD:DB1 <-- PORTB:1
      LCD:DB2 <-- PORTB:2
      LCD:DB3 <-- PORTB:3
      LCD:DB4 <-- 4.7K Ohm <-- PORTx:4
      LCD:DB5 <-- 4.7K Ohm <-- PORTx:5
      LCD:DB6 <-- 4.7K Ohm <-- PORTx:6
      LCD:DB7 <-- 4.7K Ohm <-- PORTx:7

   //// From the I/O PORTx perspective \\\\
      PORTD:0 <--RxD
      PORTD:1 --> LED // High = ON, Low = OFF
      PORTD:2 <-- J_1 // BAUD rate select
      PORTD:3 <-- J_2 // BAUD rate select
      PORTD:4 --> LCD:R/W
      PORTD:5 --> LCD:RS
      PORTD:6 --> LCD:E
      PORTD:7 <-> N/A   
   
      PORTB:0 --> LCD:DB0
      PORTB:1 --> LCD:DB1
      PORTB:2 --> LCD:DB2
      PORTB:3 --> LCD:DB3
      PORTB:4 --> 4.7K Ohm --> LCD:DB4
      PORTB:5 --> 4.7K Ohm --> LCD:DB5
      PORTB:6 --> 4.7K Ohm --> LCD:DB6
      PORTB:7 --> 4.7K Ohm --> LCD:DB7
*/

/*************************************************************/
/*************************************************************/
// If you want to use a different I/O port for LCD control & data,
// do it here!!!
#define LCD_DATA_OUT PORTB
#define LCD_DATA_IN PINB
#define LCD_DATA_DDR DDRB

#define LCD_CONTROL_OUT PORTD
#define LCD_CONTROL_IN PIND
#define LCD_CONTROL_DDR DDRD

// Define LCD Read/Write as PORTx, 0x10;
#define LCD_RW 4
// Define LCD Register Select as PORTx, 0x20; 
#define LCD_RS 5
// Define LCD Enable as PORTx, 0x40; 
#define LCD_E 6
/*************************************************************/
/*************************************************************/

// LCD busy status bit
#define LCD_BUSY 7
// LED control bit
#define LED 1
// BAUD rate control bits
#define J_1 2
#define J_2 3
#define CGRAM 6 

// Turn on power to the display, no cursor
#define   PWR_ON   0x0C
// Set 8 data bits
#define   DATA_8   0x30
// Set 8 data bits, 4 display lines
#define   LINE_8x4   0x38
// Clear display command      
#define CLR_DSP 0x01
// Character generator RAM command
#define REG_MODE 0xFE

#define NULL 0x00

// Decimal 252 turns on LCD LED backlight
#define LED_SW_ON 252
// Decimal 253 turns off LCD LED backlight
#define LED_SW_OFF 253
                     
void LCD_Delay (unsigned int);
void LCD_PutCmd (char);
void LCD_PutChar (char);
char LCD_BusyWait (void);
void IO_INIT (void);
void LCD_INIT (void);
void USART_Init (void);
char USART_SetBAUD (void);

char USART_DATA = NULL;

void main (void) {
          // Initialize the Tiny2313 I/O
          IO_INIT ();
          // Initialize the HD44780 LCD controller
          LCD_INIT ();
          // Initialize the USART   
          USART_Init ();
   
          while (1) {
                while (!(UCSRA & (1 << RXC)))  // RXC: USART Receive Complete
                ;
                USART_DATA = UDR; // Get USART data

                if (USART_DATA == LED_SW_OFF)
                   LCD_CONTROL_OUT &= ~(1<<LED);
                else if (USART_DATA == LED_SW_ON)
                        LCD_CONTROL_OUT |= (1<<LED);
                else if (USART_DATA == REG_MODE) {
                        while (!(UCSRA & (1 << RXC))) // RXC: USART Receive Complete
                        ;   
                        LCD_PutCmd (UDR); // Send LCD command character
                }
                else LCD_PutChar (USART_DATA); // Send LCD data character
          }         
}

// Clock cycle = 67nS @ 14.7456MHz   
// Delay resolution ~ 1uS @ 14.7456MHz
void LCD_Delay (unsigned int d) {
               while (d-- != 0)
               ;
               asm("nop"); // Make the delay time a bit closer to the predicted delay time.
                           // Experimentally setup with an Oscilloscope.
}

void LCD_PutCmd (char Cmd) {
    LCD_CONTROL_OUT &= ~(1<<LCD_RS);
    LCD_BusyWait();
    LCD_DATA_OUT = Cmd;   
    asm ("nop");   
    LCD_CONTROL_OUT |= (1<<LCD_E);
    asm ("nop"); // PWeh must be 230nS minimum
    asm ("nop"); // nop = 67nS @ 14.7456MHz   
    asm ("nop");
    asm ("nop"); // Here, E = 271nS @ 14.7456MHz   
    LCD_CONTROL_OUT &= ~(1<<LCD_E);
    asm ("nop");
    LCD_CONTROL_OUT |= (1<<LCD_RS);
}

void LCD_PutChar (char c) {
    LCD_CONTROL_OUT &= ~(1<<LCD_RS);
    LCD_BusyWait();
    LCD_CONTROL_OUT |= (1<<LCD_RS);
    LCD_DATA_OUT = c;
    asm ("nop");         
    LCD_CONTROL_OUT |= (1<<LCD_E);
    asm ("nop"); // PWeh must be 230nS minimum
    asm ("nop"); // nop = 67nS @ 14.7456MHz   
    asm ("nop");
    asm ("nop"); // Here, E = 271nS @ 14.7456MHz
    LCD_CONTROL_OUT &= ~(1<<LCD_E);
}

char LCD_BusyWait (void) {
    unsigned char LCDStatus;
    LCD_DATA_DDR = 0x00; // Set LCD data port to inputs
    LCD_CONTROL_OUT |= (1<<LCD_RW);
    asm ("nop");
    do {
       LCD_CONTROL_OUT |= (1<<LCD_E);   
      asm ("nop"); // PWeh must be 230nS minimum
      asm ("nop"); // nop = 67nS @ 14.7456MHz   
      asm ("nop");    
      asm ("nop"); // Here, E = 271nS @ 14.7456MHz
      LCDStatus = LCD_DATA_IN;
      LCD_CONTROL_OUT &= ~(1<<LCD_E);
    } while ((LCDStatus & (1<<LCD_BUSY)) == (1<<LCD_BUSY));
    LCD_CONTROL_OUT &= ~(1<<LCD_RW);   
    LCD_DATA_DDR |= 0xFF; // Set LCD data port to outputs      
    return (LCDStatus);
}         

void IO_INIT (void) {
    LCD_Delay (60000); // Wait for the LCD display to boot up
    // Set J_2:J_1 PULL-UPS active
    LCD_CONTROL_OUT = (1<<J_2) | (1<<J_1);
    // Set LCD_control J_2:J_1 to inputs   
    LCD_CONTROL_DDR = 0xF2;
    LCD_CONTROL_OUT |= (1<<LCD_RS); // Set LCD_RS HIGH   
    // Delay resolution ~ 1uS @ 14.7456MHz         
    LCD_Delay (15000); // Need 15mS delay for LCD to power up

    // Initialize the AVR controller I/O
    LCD_DATA_DDR = 0xFF; // Set LCD_DATA_OUT as all outputs
    LCD_DATA_OUT = 0x00; // Set LCD_DATA_OUT to logic low
}

void LCD_INIT (void) {
    // Initialize the LCD controller
    LCD_PutCmd (LINE_8x4); // Set 8 bit data, 4 display lines
    LCD_PutCmd (CLR_DSP); // Power up the display
    LCD_PutCmd (PWR_ON); // Power up the display
    LCD_Delay (30000);
    LCD_BusyWait();
}

// Initialize UART0
void USART_Init(void) {
    UCSRB = NULL; // Disable while setting baud rate
    UCSRA = NULL;
    UCSRC = (1<<UCSZ1) | (1<<UCSZ0); // 8 bit data
    UBRR = USART_SetBAUD (); // Set baud rate
    UCSRB = (1<<RXEN); // RXEN = Enable
}

// Actual baud rate = 9600 BAUD, (0.0% error) @ 14.7456MHz
// Actual baud rate = 19.2K BAUD, (0.0% error) @ 14.7456MHz
// Actual baud rate = 38.4K BAUD, (0.0% error) @ 14.7456MHz
// Actual baud rate = 115.2K BAUD, (0.0% error) @ 14.7456MHz
#define BAUD_9600 95
#define BAUD_19200 47
#define BAUD_38400 23
#define BAUD_115200 7

char USART_SetBAUD (void) {
    char BaudSelectJumpersValue;
    static char BaudLookupTable[] = {BAUD_115200, BAUD_38400, BAUD_19200, BAUD_9600};

    // Get BAUD rate jumper settings      
    BaudSelectJumpersValue  = LCD_CONTROL_IN;
    // Mask off unwanted bits
    BaudSelectJumpersValue &= (1<<J_2) | (1<<J_1);
    // J_2 = PD.3, J_1 = PD.2
    // This is two bits too far to the left and the array index will
    // increment by 4, instead of 1.
    // Shift J_2 & J_1 right by two bit positions for proper array indexing
    BaudSelectJumpersValue = (BaudSelectJumpersValue >> J_1);

    return BaudLookupTable[BaudSelectJumpersValue];
}


Merry Christmas!!!

_________________
Carl W. Livingston, KC5OTL
microcarl@roadrunner.com

It's a fundamental law of nature... All things gravitate toward total chaos!!!

The original Dragon Slayer !

Long live the AVR!!!
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
vilvide2
PostPosted: Dec 23, 2007 - 10:30 PM
Rookie


Joined: Dec 19, 2007
Posts: 48
Location: Denmark

microcarl wrote:
Okay!!! As this is the Christmas season, I started to do you a favor by formatting and indenting your code for you. I also started to fix your problems...

But after a while, I realized that your code is missing so much, out of sequence, has wrong bit polarities and the wrong time, etc..., and because I don't really know how you have the LCD E, RS & R/W bits wired up, I said to my self... "Self, why am I doing this??? " So, here's what I'm going to do for you, as it is the Christmas season, and all.

I'll just post some working 8 bit code and you'll have an example that has been verified to work properly by many here on the forum.

And too, feel free to use my code, if you want.

Code:

// My_LCD.c

/*
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the below copyright notice appears in all copies and that both the copyright notice and this permission notice appear in supporting documentation. Livingston Electronics makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.


8 BIT LCD control program
Written by Carl W. Livingston
Copyright Carl W. Livingston
microcarl@roadrunner.com
AVR Freaks member - microcarl
January 02, 2007
*/

/*************************************************************/
/*************************************************************/
#include <iot2313v.h>
/*************************************************************/
/*************************************************************/

/*   //// From the LCD perspective \\\\
      RxD --> PORTD:0
      LED <-- PORTD:1 // High = ON, Low = OFF
      J_1 --> PORTD:2 // BAUD rate select
      J_2 --> PORTD:3 // BAUD rate select
      LCD:R/W <-- PORTD:4
      LCD:RS <-- PORTD:5
      LCD:E <-- PORTD:6
      N/A <-> PORTD:7

      LCD:R/W <-- PORTD:4
      LCD:RS <-- PORTD:5
      LCD:E <-- PORTD:6
      LCD:Vee <-- CONTRAST
      LCD:DB0 <-- PORTB:0
      LCD:DB1 <-- PORTB:1
      LCD:DB2 <-- PORTB:2
      LCD:DB3 <-- PORTB:3
      LCD:DB4 <-- 4.7K Ohm <-- PORTx:4
      LCD:DB5 <-- 4.7K Ohm <-- PORTx:5
      LCD:DB6 <-- 4.7K Ohm <-- PORTx:6
      LCD:DB7 <-- 4.7K Ohm <-- PORTx:7

   //// From the I/O PORTx perspective \\\\
      PORTD:0 <--RxD
      PORTD:1 --> LED // High = ON, Low = OFF
      PORTD:2 <-- J_1 // BAUD rate select
      PORTD:3 <-- J_2 // BAUD rate select
      PORTD:4 --> LCD:R/W
      PORTD:5 --> LCD:RS
      PORTD:6 --> LCD:E
      PORTD:7 <-> N/A   
   
      PORTB:0 --> LCD:DB0
      PORTB:1 --> LCD:DB1
      PORTB:2 --> LCD:DB2
      PORTB:3 --> LCD:DB3
      PORTB:4 --> 4.7K Ohm --> LCD:DB4
      PORTB:5 --> 4.7K Ohm --> LCD:DB5
      PORTB:6 --> 4.7K Ohm --> LCD:DB6
      PORTB:7 --> 4.7K Ohm --> LCD:DB7
*/

/*************************************************************/
/*************************************************************/
// If you want to use a different I/O port for LCD control & data,
// do it here!!!
#define LCD_DATA_OUT PORTB
#define LCD_DATA_IN PINB
#define LCD_DATA_DDR DDRB

#define LCD_CONTROL_OUT PORTD
#define LCD_CONTROL_IN PIND
#define LCD_CONTROL_DDR DDRD

// Define LCD Read/Write as PORTx, 0x10;
#define LCD_RW 4
// Define LCD Register Select as PORTx, 0x20; 
#define LCD_RS 5
// Define LCD Enable as PORTx, 0x40; 
#define LCD_E 6
/*************************************************************/
/*************************************************************/

// LCD busy status bit
#define LCD_BUSY 7
// LED control bit
#define LED 1
// BAUD rate control bits
#define J_1 2
#define J_2 3
#define CGRAM 6 

// Turn on power to the display, no cursor
#define   PWR_ON   0x0C
// Set 8 data bits
#define   DATA_8   0x30
// Set 8 data bits, 4 display lines
#define   LINE_8x4   0x38
// Clear display command      
#define CLR_DSP 0x01
// Character generator RAM command
#define REG_MODE 0xFE

#define NULL 0x00

// Decimal 252 turns on LCD LED backlight
#define LED_SW_ON 252
// Decimal 253 turns off LCD LED backlight
#define LED_SW_OFF 253
                     
void LCD_Delay (unsigned int);
void LCD_PutCmd (char);
void LCD_PutChar (char);
char LCD_BusyWait (void);
void IO_INIT (void);
void LCD_INIT (void);
void USART_Init (void);
char USART_SetBAUD (void);

char USART_DATA = NULL;

void main (void) {
          // Initialize the Tiny2313 I/O
          IO_INIT ();
          // Initialize the HD44780 LCD controller
          LCD_INIT ();
          // Initialize the USART   
          USART_Init ();
   
          while (1) {
                while (!(UCSRA & (1 << RXC)))  // RXC: USART Receive Complete
                ;
                USART_DATA = UDR; // Get USART data

                if (USART_DATA == LED_SW_OFF)
                   LCD_CONTROL_OUT &= ~(1<<LED);
                else if (USART_DATA == LED_SW_ON)
                        LCD_CONTROL_OUT |= (1<<LED);
                else if (USART_DATA == REG_MODE) {
                        while (!(UCSRA & (1 << RXC))) // RXC: USART Receive Complete
                        ;   
                        LCD_PutCmd (UDR); // Send LCD command character
                }
                else LCD_PutChar (USART_DATA); // Send LCD data character
          }         
}

// Clock cycle = 67nS @ 14.7456MHz   
// Delay resolution ~ 1uS @ 14.7456MHz
void LCD_Delay (unsigned int d) {
               while (d-- != 0)
               ;
               asm("nop"); // Make the delay time a bit closer to the predicted delay time.
                           // Experimentally setup with an Oscilloscope.
}

void LCD_PutCmd (char Cmd) {
    LCD_CONTROL_OUT &= ~(1<<LCD_RS);
    LCD_BusyWait();
    LCD_DATA_OUT = Cmd;   
    asm ("nop");   
    LCD_CONTROL_OUT |= (1<<LCD_E);
    asm ("nop"); // PWeh must be 230nS minimum
    asm ("nop"); // nop = 67nS @ 14.7456MHz   
    asm ("nop");
    asm ("nop"); // Here, E = 271nS @ 14.7456MHz   
    LCD_CONTROL_OUT &= ~(1<<LCD_E);
    asm ("nop");
    LCD_CONTROL_OUT |= (1<<LCD_RS);
}

void LCD_PutChar (char c) {
    LCD_CONTROL_OUT &= ~(1<<LCD_RS);
    LCD_BusyWait();
    LCD_CONTROL_OUT |= (1<<LCD_RS);
    LCD_DATA_OUT = c;
    asm ("nop");         
    LCD_CONTROL_OUT |= (1<<LCD_E);
    asm ("nop"); // PWeh must be 230nS minimum
    asm ("nop"); // nop = 67nS @ 14.7456MHz   
    asm ("nop");
    asm ("nop"); // Here, E = 271nS @ 14.7456MHz
    LCD_CONTROL_OUT &= ~(1<<LCD_E);
}

char LCD_BusyWait (void) {
    unsigned char LCDStatus;
    LCD_DATA_DDR = 0x00; // Set LCD data port to inputs
    LCD_CONTROL_OUT |= (1<<LCD_RW);
    asm ("nop");
    do {
       LCD_CONTROL_OUT |= (1<<LCD_E);   
      asm ("nop"); // PWeh must be 230nS minimum
      asm ("nop"); // nop = 67nS @ 14.7456MHz   
      asm ("nop");    
      asm ("nop"); // Here, E = 271nS @ 14.7456MHz
      LCDStatus = LCD_DATA_IN;
      LCD_CONTROL_OUT &= ~(1<<LCD_E);
    } while ((LCDStatus & (1<<LCD_BUSY)) == (1<<LCD_BUSY));
    LCD_CONTROL_OUT &= ~(1<<LCD_RW);   
    LCD_DATA_DDR |= 0xFF; // Set LCD data port to outputs      
    return (LCDStatus);
}         

void IO_INIT (void) {
    LCD_Delay (60000); // Wait for the LCD display to boot up
    // Set J_2:J_1 PULL-UPS active
    LCD_CONTROL_OUT = (1<<J_2) | (1<<J_1);
    // Set LCD_control J_2:J_1 to inputs   
    LCD_CONTROL_DDR = 0xF2;
    LCD_CONTROL_OUT |= (1<<LCD_RS); // Set LCD_RS HIGH   
    // Delay resolution ~ 1uS @ 14.7456MHz         
    LCD_Delay (15000); // Need 15mS delay for LCD to power up

    // Initialize the AVR controller I/O
    LCD_DATA_DDR = 0xFF; // Set LCD_DATA_OUT as all outputs
    LCD_DATA_OUT = 0x00; // Set LCD_DATA_OUT to logic low
}

void LCD_INIT (void) {
    // Initialize the LCD controller
    LCD_PutCmd (LINE_8x4); // Set 8 bit data, 4 display lines
    LCD_PutCmd (CLR_DSP); // Power up the display
    LCD_PutCmd (PWR_ON); // Power up the display
    LCD_Delay (30000);
    LCD_BusyWait();
}

// Initialize UART0
void USART_Init(void) {
    UCSRB = NULL; // Disable while setting baud rate
    UCSRA = NULL;
    UCSRC = (1<<UCSZ1) | (1<<UCSZ0); // 8 bit data
    UBRR = USART_SetBAUD (); // Set baud rate
    UCSRB = (1<<RXEN); // RXEN = Enable
}

// Actual baud rate = 9600 BAUD, (0.0% error) @ 14.7456MHz
// Actual baud rate = 19.2K BAUD, (0.0% error) @ 14.7456MHz
// Actual baud rate = 38.4K BAUD, (0.0% error) @ 14.7456MHz
// Actual baud rate = 115.2K BAUD, (0.0% error) @ 14.7456MHz
#define BAUD_9600 95
#define BAUD_19200 47
#define BAUD_38400 23
#define BAUD_115200 7

char USART_SetBAUD (void) {
    char BaudSelectJumpersValue;
    static char BaudLookupTable[] = {BAUD_115200, BAUD_38400, BAUD_19200, BAUD_9600};

    // Get BAUD rate jumper settings      
    BaudSelectJumpersValue  = LCD_CONTROL_IN;
    // Mask off unwanted bits
    BaudSelectJumpersValue &= (1<<J_2) | (1<<J_1);
    // J_2 = PD.3, J_1 = PD.2
    // This is two bits too far to the left and the array index will
    // increment by 4, instead of 1.
    // Shift J_2 & J_1 right by two bit positions for proper array indexing
    BaudSelectJumpersValue = (BaudSelectJumpersValue >> J_1);

    return BaudLookupTable[BaudSelectJumpersValue];
}


Merry Christmas!!!
 
 View user's profile Send private message  
Reply with quote Back to top
microcarl
PostPosted: Dec 23, 2007 - 10:37 PM
Raving lunatic


Joined: May 30, 2004
Posts: 7777
Location: Cincinnati, Ohio

And your point???

Were you so excited over the code, that you forgot to make any comment?

_________________
Carl W. Livingston, KC5OTL
microcarl@roadrunner.com

It's a fundamental law of nature... All things gravitate toward total chaos!!!

The original Dragon Slayer !

Long live the AVR!!!
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
vilvide2
PostPosted: Dec 24, 2007 - 11:53 AM
Rookie


Joined: Dec 19, 2007
Posts: 48
Location: Denmark

microcarl wrote:
And your point???

Were you so excited over the code, that you forgot to make any comment?
 
 View user's profile Send private message  
Reply with quote Back to top
microcarl
PostPosted: Dec 24, 2007 - 04:46 PM
Raving lunatic


Joined: May 30, 2004
Posts: 7777
Location: Cincinnati, Ohio

vilvide2 wrote:
microcarl wrote:
And your point???

Were you so excited over the code, that you forgot to make any comment?


You have got to be a troll!!!

_________________
Carl W. Livingston, KC5OTL
microcarl@roadrunner.com

It's a fundamental law of nature... All things gravitate toward total chaos!!!

The original Dragon Slayer !

Long live the AVR!!!
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
vilvide2
PostPosted: Dec 25, 2007 - 11:38 AM
Rookie


Joined: Dec 19, 2007
Posts: 48
Location: Denmark

problem solved. thanks to the all.........
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Dec 25, 2007 - 12:35 PM
Raving lunatic


Joined: Mar 27, 2002
Posts: 7758
Location: Lund, Sweden

Carl!

This is the tird thread that the OP starts, then talks absolute jibberish, and ends with
Quote:

problem solved. thanks to the all.........

As far as I can se to the letter the same (including the dots). OP is a troll.
 
 View user's profile Send private message  
Reply with quote Back to top
pwillard
PostPosted: Dec 25, 2007 - 02:12 PM
Wannabe


Joined: Dec 30, 2005
Posts: 54


Or at least "not ready to be playing with LCD", maybe try lighting up a few LED's and simple sensors first.
 
 View user's profile Send private message  
Reply with quote Back to top
Guddleif
PostPosted: Jan 28, 2008 - 08:54 PM
Newbie


Joined: Mar 01, 2007
Posts: 12


Hi. I see this thread ended kind of bad, but the code posted might be just what i need =)

I have a "16 x 2 Blue Character LCD Display with Backlight" which uses an hd44780. I want to connect this to my atmega128-board. Would the code from microcarl work for me? What in the code would i have to change to adapt it to my controller?

Also, would the code work with AVR Studio and the AVR GCC compiler?

Thank you
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Jan 28, 2008 - 09:03 PM
Raving lunatic


Joined: Feb 12, 2005
Posts: 5440
Location: Cratfield, England

1. You appear to have a RW line so the Busy flag will work.
2. Edit MicroCarl's code to match your port bits.
3. #include <avr/io.h>
4. change the syntax of the asm() macro
5. change the number of nops for your F_CPU
6. or use _delay_us() and _delay_ms() with <delay.h>
Carl's long delays will get optimised out by avr-gcc.
7. Compile. You should have a working LCD !!!

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jan 28, 2008 - 10:13 PM
Raving lunatic


Joined: Mar 27, 2002
Posts: 7758
Location: Lund, Sweden

Or try my code here: http://www.avrfreaks.net/index.php?name ... 551#200551

Written for avr-gcc, should be good for up to 8 MHz. Fairly adaptable when it comes to wire-up of the LCD to AVR pins. The file you need to change is myLcd.h, and although it lacks any comments it should be quite clear what to do with it (if not - return here with questions!). Does not handle the Busy-flag but instead uses timed delays (delay_ms() and delay_us()) for timing constraints.
 
 View user's profile Send private message  
Reply with quote Back to top
Guddleif
PostPosted: Jan 29, 2008 - 08:19 PM
Newbie


Joined: Mar 01, 2007
Posts: 12


Thanks to the both of you! I will try it out as soon as my connection cable arrives in the mail. Very Happy
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits