Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
07/27/06 10:19
Modified:
  07/27/06 10:26

Read: times


 
#121152 - An example for 8051/AVR
Responding to: ???'s previous message
Follwing an example for LCD driver in 4bit mode, running on 8051 or AVR:

There are only less differences:

- other port naming P0..P3 - PORTA.. PORTD
- AVR need direction setting
- other delay loop timing

/****************************** differences *****************************/

#ifdef __C51__

#include <reg51.h>                      // 8051 io definitiones

#define LCD_RS          2               // P1.2
#define LCD_E           3               // P1.3
#define LCD_PORT        P1

#define SetLcdOutputs()                 // not needed on 8051

#define RELOAD60us      (XTAL / 12.0 / 2 * 60e-6) // Keil C51: 2 cycle

#else

#include <io.h>                         // AVR io definitions

#define LCD_PORT        PORTB
#define LCD_DDR         DDRB
#define LCD_RS          PB2
#define LCD_E           PB3

#define SetLcdOutputs() LCD_DDR |= 1<<LCD_E^1<<LCD_RS^0xF0

#define RELOAD60us      (XTAL / 3 * 60e-6)      // WINAVR: 3 cycle

#endif


/****************************** common code *****************************/

typedef unsigned char  u8;
typedef   signed char  s8;
typedef unsigned short u16;
typedef   signed short s16;
typedef unsigned long  u32;
typedef   signed long  s32;


#define XTAL    11.0592e6

u8 position;


void wait_60us( u8 n )                  // wait n * 60µs (max 15ms)
{
  u8 i;
  do{
    i = RELOAD60us;
    while( --i );
  }while( --n );
}


u8 lcd_nibble( u8 d )
{
  LCD_PORT &= 0x0F;
  LCD_PORT |= d & 0xF0;
  LCD_PORT |= 1<<LCD_E;
  d <<= 4;
  LCD_PORT &= ~(1<<LCD_E);
  return d;
}


void lcd_byte( u8 d )
{
  lcd_nibble( lcd_nibble( d ));
  wait_60us( 1 );                       // wait > 46us
}


void lcd_command( u8 d )
{
  LCD_PORT &= ~(1<<LCD_RS);             // RS = 0
  lcd_byte( d );
  if( d & 0x80 )
    position = d;
  switch( d ){
    case 1:
    case 2:
    case 3: wait_60us( 27 );            // wait > 1.6ms
  }
}


void lcd_data( u8 d )
{
  LCD_PORT |= 1<<LCD_RS;                // RS = 1
  lcd_byte( d );
  position++;
  if( position & 0x10 ){
    position ^= 0x50;                   // switch line 1<->2
    lcd_command( position );
  }
}


void lcd_init( void )
{
  SetLcdOutputs();                      // set direction bits
  LCD_PORT &= ~(1<<LCD_E);              // E = 0
  LCD_PORT &= ~(1<<LCD_RS);             // RS = 0
  wait_60us( 250 );                     // wait >15ms
  lcd_nibble( 0x30 );
  wait_60us( 69 );                      // wait >4.1ms
  lcd_nibble( 0x30 );
  wait_60us( 2 );                       // wait >100µs
  lcd_nibble( 0x30 );                   // 8 bit mode
  wait_60us( 2 );                       // wait >100µs
  lcd_nibble( 0x20 );                   // 4 bit mode
  wait_60us( 2 );
  lcd_command( 0x28 );                  // 2 lines 5*7
  lcd_command( 0x08 );                  // display off
  lcd_command( 0x01 );                  // display clear
  lcd_command( 0x06 );
  lcd_command( 0x0C );                  // no cursor, no blink
  position = 0x80;
}



As you can see, only some definitions are different but the code was mostly identical (direction setting was added).

But in assembler every line of code must be rewritten.


Peter


List of 26 messages in thread
TopicAuthorDate
How long we see 8051            01/01/70 00:00      
   unofficial history of 8051            01/01/70 00:00      
      History, or prediction?            01/01/70 00:00      
         It seems to gain, not lose            01/01/70 00:00      
   Does it matter?            01/01/70 00:00      
      Double negative?            01/01/70 00:00      
      to C or not to C            01/01/70 00:00      
         Defintely 'C'!            01/01/70 00:00      
            yeah, let's flame!            01/01/70 00:00      
          Defintely 'C'!            01/01/70 00:00      
            arguments            01/01/70 00:00      
               An example for 8051/AVR            01/01/70 00:00      
                  contra-example for 8051/AVR            01/01/70 00:00      
                  A slight mistake            01/01/70 00:00      
                     stdint.h            01/01/70 00:00      
               That's your trouble, then!            01/01/70 00:00      
                  Compiler independant            01/01/70 00:00      
                  C is not better            01/01/70 00:00      
                  bringing 2 togethert            01/01/70 00:00      
                  Opening pandora's box            01/01/70 00:00      
               Learning C            01/01/70 00:00      
                  for a beginner ...            01/01/70 00:00      
            are You sure?            01/01/70 00:00      
            Portability and scars            01/01/70 00:00      
   I'm an ASM Guru but            01/01/70 00:00      
      why discuss the rare exception tha same apply            01/01/70 00:00      

Back to Subject List