dip204.c File Reference


Detailed Description

LCD DIP204 driver.

This file defines a useful set of functions for the DIP204 interface on AVR32 devices.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file dip204.c.

#include <stdarg.h>
#include <stdio.h>
#include "board.h"
#include "dip204.h"
#include "gpio.h"
#include "compiler.h"
#include "spi.h"
#include "pwm.h"
#include "delay.h"

Go to the source code of this file.

Defines

#define DIP204_CGRAM_BASE_ADDR   0x40
#define DIP204_PERIOD_MAX   50
#define DIP204_READ_COMMAND   0xFC
#define DIP204_READ_DATA   0xFE
#define DIP204_WRITE_COMMAND   0xF8
#define DIP204_WRITE_DATA   0xFA

Functions

void dip204_clear_display (void)
void dip204_create_char (char ascii_code, const unsigned char data[8])
void dip204_hide_cursor (void)
void dip204_init (backlight_options option, Bool backlight_on)
void dip204_printf_string (const char *format,...)
static void dip204_read_byte (unsigned char *byte)
 hardware abstraction layer to read a byte from LCD depends if LCD is plugged on SPI or on EBI
void dip204_read_data (unsigned char *data)
static void dip204_select (void)
 function to select the LCD
void dip204_set_backlight (backlight_power power)
void dip204_set_cursor_position (unsigned char column, unsigned char line)
void dip204_show_cursor (void)
static void dip204_unselect (void)
 function to unselect the LCD
static void dip204_wait_busy (void)
 function to wait for LCD becomes not busy
static void dip204_write_byte (unsigned char byte)
 hardware abstraction layer to send a byte to LCD depends if LCD is plugged on SPI or on EBI
void dip204_write_data (unsigned char data)
void dip204_write_string (const char *string)

Variables

static int channel_id = -1
static avr32_pwm_channel_t pwm_channel
static unsigned short pwm_duty


Define Documentation

#define DIP204_CGRAM_BASE_ADDR   0x40

Definition at line 77 of file dip204.c.

Referenced by dip204_create_char().

#define DIP204_PERIOD_MAX   50

Definition at line 75 of file dip204.c.

Referenced by dip204_init(), and dip204_set_backlight().

#define DIP204_READ_COMMAND   0xFC

Read Command start byte, for Busy-flag checking

Definition at line 70 of file dip204.c.

Referenced by dip204_create_char(), dip204_wait_busy(), and dip204_write_byte().

#define DIP204_READ_DATA   0xFE

Read Data from DDRAM

Definition at line 73 of file dip204.c.

Referenced by dip204_read_data(), and dip204_write_byte().

#define DIP204_WRITE_COMMAND   0xF8

#define DIP204_WRITE_DATA   0xFA

Write Data start byte

Definition at line 67 of file dip204.c.

Referenced by dip204_create_char(), dip204_printf_string(), dip204_write_byte(), dip204_write_data(), and dip204_write_string().


Function Documentation

void dip204_clear_display ( void   ) 

Clear the LCD screen (need void delay_ms(unsigned short time_ms) function to perform active wait)

Definition at line 333 of file dip204.c.

References dip204_select(), dip204_unselect(), dip204_wait_busy(), dip204_write_byte(), and DIP204_WRITE_COMMAND.

00334 {
00335   dip204_select();
00336   /* Send Command Start Byte */
00337   dip204_write_byte(DIP204_WRITE_COMMAND);
00338   /* Send Display Clear Command */
00339   dip204_write_byte(0x01);
00340   /* Wait for command execution */
00341   delay_ms(4);
00342   dip204_wait_busy();
00343   dip204_unselect();
00344 }

void dip204_create_char ( char  ascii_code,
const unsigned char  data[8] 
)

Create a new ASCII character

Parameters:
ascii_code Input. ascii code of the new character. Must fit in the range [0; 7].
data Input. pixel map of the character. It is composed of 5 columns and 8 lines.

Definition at line 271 of file dip204.c.

References DIP204_CGRAM_BASE_ADDR, dip204_read_byte(), DIP204_READ_COMMAND, dip204_select(), dip204_set_cursor_position(), dip204_unselect(), dip204_wait_busy(), dip204_write_byte(), DIP204_WRITE_COMMAND, and DIP204_WRITE_DATA.

00272 {
00273   unsigned char i;
00274   unsigned char column, line;
00275 
00276   /* select the LCD chip */
00277   dip204_select();
00278 
00279   /* save cursor position */
00280   /* Send Read Command Start-Byte */
00281   dip204_write_byte(DIP204_READ_COMMAND);
00282   /* Read status */
00283   dip204_read_byte(&i);
00284   /* Extract and save line and column cursor information */
00285   line = ((i&0x60) >> 5) + 1;
00286   column = (i&0x1F) + 1;
00287 
00288   /* Send Command Start Byte */
00289   dip204_write_byte(DIP204_WRITE_COMMAND);
00290   /* Send CGRAM Address Set command */
00291   dip204_write_byte(DIP204_CGRAM_BASE_ADDR + ((ascii_code << 3)&0x38));
00292   /* wait for LCD */
00293   dip204_wait_busy();
00294 
00295   /* To proceed the 8 lines */
00296   for(i=0; i<8; i++)
00297   {
00298     /* Send Data Start Byte */
00299     dip204_write_byte(DIP204_WRITE_DATA);
00300     /* send data */
00301     dip204_write_byte(data[i] & 0x1F); //data[i]);
00302     /* wait for LCD */
00303     dip204_wait_busy();
00304   }
00305 
00306   /* unselect chip */
00307   dip204_unselect();
00308 
00309   /* Reset cursor position */
00310   dip204_set_cursor_position(column, line);
00311 }

void dip204_hide_cursor ( void   ) 

Hide cursor

Definition at line 232 of file dip204.c.

References dip204_select(), dip204_unselect(), dip204_wait_busy(), dip204_write_byte(), and DIP204_WRITE_COMMAND.

Referenced by main().

00233 {
00234   /* select the LCD chip */
00235   dip204_select();
00236   /* Send Command Start Byte */
00237   dip204_write_byte(DIP204_WRITE_COMMAND);
00238   /* Send "Display On Command: Display On, Cursor On, Blink On" */
00239   dip204_write_byte(0x0C);
00240   dip204_wait_busy();
00241   /* unselect chip */
00242   dip204_unselect();
00243 }

void dip204_init ( backlight_options  option,
Bool  backlight_on 
)

Initialize the LCD (need void delay_ms(unsigned short time_ms) function to perform active wait)

Parameters:
option backlight_IO if no PWM needed, backlight_PWM if PWM needed...
backlight_on Whether to start with backlight on or off.

Definition at line 103 of file dip204.c.

References backlight_PWM, channel_id, DIP204_PERIOD_MAX, dip204_select(), dip204_unselect(), dip204_wait_busy(), dip204_write_byte(), DIP204_WRITE_COMMAND, pwm_channel, and pwm_duty.

Referenced by main().

00104 {
00105   pwm_opt_t pwm_opt;  // pwm option config
00106 
00107   if (option == backlight_PWM)
00108   {
00109     channel_id = DIP204_PWM_CHANNEL;
00110     gpio_enable_module_pin(AVR32_PWM_6_PIN, AVR32_PWM_6_FUNCTION);
00111 
00112     // PWM controller configuration
00113     pwm_opt.diva=0;
00114     pwm_opt.divb=0;
00115     pwm_opt.prea=0;
00116     pwm_opt.preb=0;
00117 
00118     pwm_init(&pwm_opt);
00119     pwm_duty = (backlight_on) ? DIP204_PERIOD_MAX - 1 : 1;
00120     pwm_channel.CMR.calg = PWM_MODE_LEFT_ALIGNED;   // channel mode
00121     pwm_channel.CMR.cpol = PWM_POLARITY_LOW;   // channel polarity
00122     pwm_channel.CMR.cpd = PWM_UPDATE_PERIOD;   // not used the first time
00123     pwm_channel.CMR.cpre = AVR32_PWM_CPRE_MCK_DIV_256;   // channel prescaler
00124     pwm_channel.cdty = pwm_duty;  // channel duty cycle, should be < CPRD
00125     pwm_channel.cprd = DIP204_PERIOD_MAX;  // channel period
00126     pwm_channel.cupd = 0;  // channel update is not used here.
00127 
00128     pwm_channel_init(channel_id, &pwm_channel);
00129     // start PWM
00130     pwm_start_channels(1 << channel_id);
00131   }
00132   else
00133   {
00134     if (backlight_on)
00135     {
00136       gpio_clr_gpio_pin(DIP204_BACKLIGHT_PIN);
00137     }
00138     else
00139     {
00140       gpio_set_gpio_pin(DIP204_BACKLIGHT_PIN);
00141     }
00142   }
00143   // delay for power on
00144   delay_ms(20);
00145   // select the LCD chip
00146   dip204_select();
00147   // Send Command Start Byte
00148   dip204_write_byte(DIP204_WRITE_COMMAND);
00149   // Send "extended Function Set" Command  (RE=1)
00150   dip204_write_byte(0x34);
00151   // Wait for command execution
00152   delay_ms(1);
00153   // Send "Enter 4-Line Mode" Command
00154   dip204_write_byte(0x09);
00155   // Wait for command execution
00156   delay_ms(1);
00157   // Send "Function Set" Command (RE=0)
00158   dip204_write_byte(0x30);
00159   // Wait for command execution
00160   delay_ms(1);
00161   // Send "Display On Command: Display On, Cursor On, Blink On"
00162   dip204_write_byte(0x0F);
00163   // Wait for command execution
00164   delay_ms(1);
00165   // Send "Display Clear" Command
00166   dip204_write_byte(0x01);
00167   // Wait for command execution
00168   delay_ms(5);
00169   // Send "Entry Mode Set Command: Increment Mode, Entire Shift off"
00170   dip204_write_byte(0x06);
00171   // Wait for command execution
00172   delay_ms(1);
00173   dip204_wait_busy();
00174   // unselect chip
00175   dip204_unselect();
00176 }

void dip204_printf_string ( const char *  format,
  ... 
)

Write a formatted string

Parameters:
format Input. Formatted null terminated string to display

Definition at line 367 of file dip204.c.

References dip204_select(), dip204_unselect(), dip204_wait_busy(), dip204_write_byte(), and DIP204_WRITE_DATA.

00368 {
00369   va_list arg;
00370   char string[21];
00371   unsigned char i=0;
00372 
00373   va_start(arg, format);
00374   i = vsprintf(string, format, arg);
00375   while (i < sizeof(string) - 1) string[i++] = '\0';
00376   va_end(arg);
00377   dip204_select();
00378   /* for all chars in string */
00379   i = 0;
00380   while(string[i]!='\0')
00381   {
00382     /* Send Write Data Start Byte */
00383     dip204_write_byte(DIP204_WRITE_DATA);
00384     /* Send byte */
00385     dip204_write_byte(string[i]);
00386     /* go to next char */
00387     i++;
00388     dip204_wait_busy();
00389   }
00390   dip204_unselect();
00391 }

static void dip204_read_byte ( unsigned char *  byte  )  [static]

hardware abstraction layer to read a byte from LCD depends if LCD is plugged on SPI or on EBI

Parameters:
byte Input. byte read from the LCD (D7 .. D0)

Definition at line 473 of file dip204.c.

Referenced by dip204_create_char(), dip204_read_data(), and dip204_wait_busy().

00474 {
00475   unsigned short reverse = 0x00;
00476 
00477   /* dummy write */
00478 #ifdef _ASSERT_ENABLE_
00479   spi_status =
00480 #endif
00481   spi_write(DIP204_SPI, 0x00);
00482   Assert( SPI_OK==spi_status );
00483   /* read RSR register */
00484 #ifdef _ASSERT_ENABLE_
00485   spi_status =
00486 #endif
00487   spi_read(DIP204_SPI, &reverse);
00488   Assert( SPI_OK==spi_status );
00489   /* Revert received byte (issued LSB first by the LCD) */
00490   *byte = bit_reverse8(reverse);
00491 }

void dip204_read_data ( unsigned char *  data  ) 

Read data at current position

Parameters:
data Output. data read at current position

Definition at line 258 of file dip204.c.

References dip204_read_byte(), DIP204_READ_DATA, dip204_select(), dip204_unselect(), dip204_wait_busy(), and dip204_write_byte().

00259 {
00260   dip204_select();
00261   /* Send Read Data Start-Byte */
00262   dip204_write_byte(DIP204_READ_DATA);
00263   /* read SPI data */
00264   dip204_read_byte(data);
00265   /* wait for LCD */
00266   dip204_wait_busy();
00267   dip204_unselect();
00268 }

static void dip204_select ( void   )  [static]

function to select the LCD

Definition at line 399 of file dip204.c.

Referenced by dip204_clear_display(), dip204_create_char(), dip204_hide_cursor(), dip204_init(), dip204_printf_string(), dip204_read_data(), dip204_set_cursor_position(), dip204_show_cursor(), dip204_write_data(), and dip204_write_string().

00400 {
00401   spi_selectChip(DIP204_SPI, DIP204_SPI_NPCS);
00402 }

void dip204_set_backlight ( backlight_power  power  ) 

Change the backlight power

Parameters:
power increase or decrease the backlight...

Definition at line 179 of file dip204.c.

References backlight_power_decrease, backlight_power_increase, channel_id, DIP204_PERIOD_MAX, pwm_channel, and pwm_duty.

Referenced by dip204_example_PB_int_handler().

00180 {
00181   if (channel_id != -1)
00182   {
00183     if (power == backlight_power_decrease)
00184     {
00185       // update channel duty cycle using double buffering to prevent unexpected waveform.
00186       pwm_duty = Max(pwm_duty - (DIP204_PERIOD_MAX / 10), 1);
00187       pwm_channel.CMR.cpd = PWM_UPDATE_DUTY;
00188       // new duty cycle
00189       pwm_channel.cupd = pwm_duty;
00190       // set channel configuration.
00191       pwm_sync_update_channel(channel_id, &pwm_channel);
00192     }
00193     else if (power == backlight_power_increase)
00194     {
00195       // update channel duty cycle using double buffering to prevent unexpected waveform.
00196       pwm_duty = Min(pwm_duty + (DIP204_PERIOD_MAX / 10), DIP204_PERIOD_MAX - 1);
00197       pwm_channel.CMR.cpd = PWM_UPDATE_DUTY;
00198       // new duty cycle
00199       pwm_channel.cupd = pwm_duty;
00200       // set channel configuration.
00201       pwm_sync_update_channel(channel_id, &pwm_channel);
00202     }
00203   }
00204   else
00205   {
00206     if (power == backlight_power_decrease)
00207     {
00208       gpio_set_gpio_pin(DIP204_BACKLIGHT_PIN);
00209     }
00210     else if (power == backlight_power_increase)
00211     {
00212       gpio_clr_gpio_pin(DIP204_BACKLIGHT_PIN);
00213     }
00214   }
00215 }

void dip204_set_cursor_position ( unsigned char  column,
unsigned char  line 
)

Set cursor to given position

Parameters:
column Input. Column where to set cursor (from 1 to 20).
line Input. Line where to set cursor (from 1 to 4).

Definition at line 314 of file dip204.c.

References dip204_select(), dip204_unselect(), dip204_wait_busy(), dip204_write_byte(), and DIP204_WRITE_COMMAND.

Referenced by dip204_create_char(), dip204_example_Joy_int_handler(), dip204_example_PB_int_handler(), and main().

00315 {
00316   unsigned char address = 0;
00317 
00318   dip204_select();
00319   if ((column <= 20) && (line <= 4))
00320   {
00321     /* Calculate DDRAM address from line and row values */
00322     address = ( (line-1) * 32 ) + ( column-1 ) + 128;
00323   }
00324   /* Send Command Start Byte */
00325   dip204_write_byte(DIP204_WRITE_COMMAND);
00326   /* Send Adress lower Nibble */
00327   dip204_write_byte(address);
00328   dip204_wait_busy();
00329   dip204_unselect();
00330 }

void dip204_show_cursor ( void   ) 

Show blinking cursor

Definition at line 218 of file dip204.c.

References dip204_select(), dip204_unselect(), dip204_wait_busy(), dip204_write_byte(), and DIP204_WRITE_COMMAND.

00219 {
00220   /* select the LCD chip */
00221   dip204_select();
00222   /* Send Command Start Byte */
00223   dip204_write_byte(DIP204_WRITE_COMMAND);
00224   /* Send "Display On Command: Display On, Cursor On, Blink On" */
00225   dip204_write_byte(0x0F);
00226   dip204_wait_busy();
00227   /* unselect chip */
00228   dip204_unselect();
00229 }

static void dip204_unselect ( void   )  [static]

function to unselect the LCD

Definition at line 408 of file dip204.c.

Referenced by dip204_clear_display(), dip204_create_char(), dip204_hide_cursor(), dip204_init(), dip204_printf_string(), dip204_read_data(), dip204_set_cursor_position(), dip204_show_cursor(), dip204_write_data(), and dip204_write_string().

00409 {
00410 #ifdef _ASSERT_ENABLE_
00411   spi_status =
00412 #endif
00413   spi_unselectChip(DIP204_SPI, DIP204_SPI_NPCS);
00414   Assert( SPI_OK==spi_status );
00415 }

static void dip204_wait_busy ( void   )  [static]

function to wait for LCD becomes not busy

Definition at line 497 of file dip204.c.

References dip204_read_byte(), DIP204_READ_COMMAND, and dip204_write_byte().

Referenced by dip204_clear_display(), dip204_create_char(), dip204_hide_cursor(), dip204_init(), dip204_printf_string(), dip204_read_data(), dip204_set_cursor_position(), dip204_show_cursor(), dip204_write_data(), and dip204_write_string().

00498 {
00499   unsigned char status = 0x00;
00500 
00501   /* send read commd to LCD */
00502   dip204_write_byte(DIP204_READ_COMMAND);
00503   /* read next byte */
00504   do {
00505   dip204_read_byte(&status);
00506   /* keep D7 to know status */
00507   }while (status & 0x80);
00508 }

static void dip204_write_byte ( unsigned char  byte  )  [static]

hardware abstraction layer to send a byte to LCD depends if LCD is plugged on SPI or on EBI

Parameters:
byte Input. byte to write to the LCD (D7 .. D0)

Definition at line 424 of file dip204.c.

References DIP204_READ_COMMAND, DIP204_READ_DATA, DIP204_WRITE_COMMAND, and DIP204_WRITE_DATA.

Referenced by dip204_clear_display(), dip204_create_char(), dip204_hide_cursor(), dip204_init(), dip204_printf_string(), dip204_read_data(), dip204_set_cursor_position(), dip204_show_cursor(), dip204_wait_busy(), dip204_write_data(), and dip204_write_string().

00425 {
00426   unsigned char reverse;
00427 
00428   switch (byte)
00429   {
00430     /* MSB first for command */
00431     case DIP204_READ_COMMAND:
00432     case DIP204_WRITE_COMMAND:
00433     case DIP204_READ_DATA:
00434     case DIP204_WRITE_DATA:
00435     {
00436       /* send D7 to D0 */
00437 #ifdef _ASSERT_ENABLE_
00438       spi_status =
00439 #endif
00440       spi_write(DIP204_SPI, byte);
00441       Assert( SPI_OK==spi_status );
00442       break;
00443     }
00444     /* LSB first for all other data */
00445     default:
00446     {
00447       /* reverse byte */
00448       reverse = bit_reverse8(byte);
00449       /* send D0 to D3 */
00450 #ifdef _ASSERT_ENABLE_
00451       spi_status =
00452 #endif
00453       spi_write(DIP204_SPI, (reverse & 0xF0));
00454       Assert( SPI_OK==spi_status );
00455       /* send D4 to D7 */
00456 #ifdef _ASSERT_ENABLE_
00457       spi_status =
00458 #endif
00459       spi_write(DIP204_SPI, ((reverse << 4) & 0xF0));
00460       Assert( SPI_OK==spi_status );
00461       break;
00462     }
00463   }
00464 }

void dip204_write_data ( unsigned char  data  ) 

Write a byte at current position

Parameters:
data Input. data to display

Definition at line 246 of file dip204.c.

References dip204_select(), dip204_unselect(), dip204_wait_busy(), dip204_write_byte(), and DIP204_WRITE_DATA.

Referenced by dip204_example_Joy_int_handler(), and dip204_example_PB_int_handler().

00247 {
00248   dip204_select();
00249   /* Send Write Data Start-Byte */
00250   dip204_write_byte(DIP204_WRITE_DATA);
00251   /* send data */
00252   dip204_write_byte(data);
00253   dip204_wait_busy();
00254   dip204_unselect();
00255 }

void dip204_write_string ( const char *  string  ) 

Write a string

Parameters:
string Input. null terminated string to display

Definition at line 347 of file dip204.c.

References dip204_select(), dip204_unselect(), dip204_wait_busy(), dip204_write_byte(), and DIP204_WRITE_DATA.

Referenced by dip204_example_Joy_int_handler(), dip204_example_PB_int_handler(), and main().

00348 {
00349   unsigned char i=0;
00350 
00351   dip204_select();
00352   /* for all chars in string */
00353   while(string[i]!=0)
00354   {
00355     /* Send Write Data Start Byte */
00356     dip204_write_byte(DIP204_WRITE_DATA);
00357     /* Send byte */
00358     dip204_write_byte(string[i]);
00359     /* go to next char */
00360     i++;
00361     dip204_wait_busy();
00362   }
00363   dip204_unselect();
00364 }


Variable Documentation

int channel_id = -1 [static]

The channel number

Definition at line 94 of file dip204.c.

Referenced by dip204_init(), and dip204_set_backlight().

avr32_pwm_channel_t pwm_channel [static]

the PWM channel config

Definition at line 91 of file dip204.c.

Referenced by dip204_init(), and dip204_set_backlight().

unsigned short pwm_duty [static]

the duty cycle that will be updated to change backlight power

Definition at line 88 of file dip204.c.

Referenced by dip204_init(), and dip204_set_backlight().


Generated on Fri Feb 19 02:23:29 2010 for AVR32 UC3 - DIP204 LCD Driver by  doxygen 1.5.5