CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

LCD for debugging

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

LCD for debugging
PostPosted: Mon May 28, 2007 8:37 am     Reply with quote

Hello,

I read so many things about LCD drivers, LCD programs, communication with LCD that I am very confused.

All I would like to do is to have a 2 line 2x16 character LCD display which reads data over serial communication and display it on the screen.

The reason for this is, that sometimes I would like to see values of the variables on the pic in the circuit. This would help so much in debugging.

Now, I already purchased a Hitachi 77480 compatible 2x16 character display, it has 14 ports on it.

What else do I need to make this work?

Thanks, Oxy.
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Mon May 28, 2007 12:06 pm     Reply with quote

Hello,

I just make the same experience.

Read the 'lcd.c' driver. You will find it in the driver folder.

Connect the display as described in the driver file.
The bus is 7 wires between the pic and display (4 datas and 3 controles).
Connect the power pin 1 to ground, pin 2 to 5V
Connect VO ( pin 3 for contrast) to the ground.

you can include this in your code :

Include the 'lcd.c' driver file.
Initialize RS/232 as you want.
Use the following sample code.

Code:

  // call lcd_init() one time before use LCD
  lcd_init();

  // Clear the LCD.
  printf( lcd_putc, "\f") ;
  delay_ms( 500 ) ;

  printf(lcd_putc, "Hello");

  while (true) {
    if ( kbhit() ) {
      c = getc() ;
      lcd_putc(c);
    }
  }



More explanations are in the 'lcd.c' driver file.

This code is working actually in my board.

Best regards,
dro
_________________
in médio virtus
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Mon May 28, 2007 12:30 pm     Reply with quote

Hello,

Thank you, you see here is your chance... Smile

I am not sure if I was specific enough. I would like to build a board with the LCD on top of it, and to have 3 wires coming out. One tor +VCC, one for the Serial line and one for GND.

I would use this board whenever I want to debug something on any of my projects.

I did understand what you said, one thing though - nerither you or the LCD.c file specifies what kind of Pic should I use for this...

I have a few on stock: 12F675, 12F629, 16F627A, 16F628, 18F4320.
Can I use any of this forthis reason, or should I go and get a different one?

Thanks.
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Tue May 29, 2007 12:34 am     Reply with quote

Hello oxxyfx,


I just have the same project. Until now, i used serial lcd from Parallax or serial backpack like this: http://www.seetron.com/bpk000_1.htm

Now i decided to make this 'backpack' myself. Not only for debugging but also as user interface for my different projects. They are also very usefully for debugging.

So, i will save about 20$ for each.

About the PIC witch can be used, i am not sure that the 12F can be used but the 16F and 18F you have will probably be more than enough.

Now i have a prototype with a 18F4685. Of course, the 18F4685 is only for prototype. For final project, i will probably use a 16F... with at least 10 IO (in the absolute, 7 are enough), ICSP and a few memory. I think that a 16F84 will be enough.

For the software, i used the driver Flex_LCD420.c found at : http://www.ccsinfo.com/forum/viewtopic.php?t=28268
As you see, this driver support 20X4 chars. It is simple to add parameters for chose between 2 or 4 lines. There is also a 2 lines version.

I just make some modifications for have a blinking cursor on/off and i transformed the gotoXY by gotoLC (line, column). The main modifications are in the lcd_putc() function.

The main program accept characters from serial at 9600 Baud then send they to the lcd.
A chr 27 (ESCAPE) clear the screen.

I need to add some functionalities in the main program:
- A communication protocol compatible with the backpacks i already used.
- Support for some rs/232 speed.
- ...

The last step will be to make PCB layout. This step is very easy with my program Target 3001.

I'm ready to work with you for finishing this project.

Here are the actual program code:

Code:

#OPT 11
#include <18F4685.h>

#device adc=8
#FUSES NOWDT                   //No Watch Dog Timer
#FUSES WDT128                  //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                      //High speed Osc (> 4mhz)
#FUSES NOPROTECT               //Code not protected from reading
#FUSES BROWNOUT                //Reset when brownout detected
#FUSES BORV45                  //Brownout reset at 2.0V
#FUSES PUT                     //Power Up Timer
#FUSES NOCPD                   //No EE protection
#FUSES STVREN                  //Stack full/underflow will cause reset
#FUSES NODEBUG                 //No Debug mode for ICD
#FUSES NOLVP                   //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                   //Program memory not write protected
#FUSES NOWRTD                  //Data EEPROM not write protected
#FUSES NOIESO                  //Internal External Switch Over mode enabled
#FUSES FCMEN                   //Fail-safe clock monitor enabled
#FUSES NOPBADEN                //PORTB pins are configured as analog input channels on RESET
#FUSES BBSIZ4K                 //4K words Boot Block size
#FUSES NOWRTC                  //configuration not registers write protected
#FUSES NOWRTB                  //Boot block not write protected
#FUSES NOEBTR                  //Memory not protected from table reads
#FUSES NOEBTRB                 //Boot block not protected from table reads
#FUSES NOCPB                   //No Boot Block code protection
#FUSES LPT1OSC                 //Timer1 configured for low-power operation
#FUSES MCLR                    //Master Clear pin enabled
#FUSES XINST                   //Extended set extension and Indexed Addressing mode enabled

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


#include <Flex_LCD420.c> 

int8 int_sec ;
#int_TIMER2
void  TIMER2_isr(void) {
  #bit LCD_CURSOR_BIT = LCD_FUNCTION.LCD_CURSOR // 1

  if (++int_sec == 50 ) {
    int_sec = 0 ;
    if (LCD_CURSOR_BLINK) {
      LCD_CURSOR_BIT = !LCD_CURSOR_BIT ;
      lcd_send_byte (0, LCD_FUNCTION ); //0b000111x);
    }
  }
   
}



//===================================
void main(){

int8 i;

   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16,208,15);
   setup_psp(PSP_DISABLED);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(GLOBAL);


// The lcd_init() function should always be called once,
// near the start of your program.
lcd_init();

// Clear the LCD.
printf(lcd_putc, "\f");
delay_ms(500);


while(true) {

  int8 i = 0;
  char c ;
 
  printf(lcd_putc,"\t1"); // for start blinking cursor

  while (true) {
    if ( kbhit() ) {
      disable_interrupts(GLOBAL);
      c = getc() ;
      enable_interrupts(GLOBAL);
      if ( c == 27 ) {
        // Clear the LCD.
        printf(lcd_putc, "\f");
        delay_ms(500);
      } else  lcd_putc(c);

    }
  }



  }   

}


and the modified driver:
Code:

// from PCM programmer, modified for cursor blinking
// -- prototype version --

#define LCD_DB4   PIN_D4
#define LCD_DB5   PIN_D5
#define LCD_DB6   PIN_D6
#define LCD_DB7   PIN_D7
#define LCD_RS    PIN_D1 //PIN_E0
#define LCD_RW    PIN_D2 //PIN_E1
#define LCD_E     PIN_D0 //PIN_E2

// IF you want only a 6-pin interface to your LCD, then
// connect the R/W pin on the LCD to ground, and comment
// out the following line.  Doing so will save one PIC
// pin, but at the cost of losing the ability to read from
// the LCD.  It also makes the write time a little longer
// because a STATIC delay must be used, instead of polling
// the LCD's busy bit.  Normally a 6-pin interface is only
// used IF you are running out of PIC pins, and you need
// to use as few as possible FOR the LCD.
#define USE_RW_PIN   1     

// These are the line addresses FOR most 4x20 LCDs.
#define LCD_LINE_1_ADDRESS 0x00
#define LCD_LINE_2_ADDRESS 0x40
#define LCD_LINE_3_ADDRESS 0x14
#define LCD_LINE_4_ADDRESS 0x54

// These are the line addresses FOR LCD's which use
// the Hitachi HD66712U controller chip.
/*
#define LCD_LINE_1_ADDRESS 0x00
#define LCD_LINE_2_ADDRESS 0x20
#define LCD_LINE_3_ADDRESS 0x40
#define LCD_LINE_4_ADDRESS 0x60

*/
//========================================
#define lcd_type 2   // 0=5x7, 1=5x10, 2=2 lines(or more)

int8 lcd_line;

int8 const LCD_INIT_STRING[4] ={
   0x20| (lcd_type << 2), // Set mode: 4 - bit, 2 + lines, 5x8 dots
   0xc,     // Display on
   1,      // Clear display
   6      // Increment cursor
};

#define LCD_BLINK   0
#define LCD_CURSOR  1
#define LCD_ONOFF   2

int8 LCD_FUNCTION  = LCD_INIT_STRING[1] ;
int8 LCD_SetMode = 0 ;
int8 LCD_CURSOR_BLINK = 0 ;


//-------------------------------------
void lcd_send_nibble(int8 nibble){
   // Note:  !! converts an integer expression
   // to a boolean (1 or 0).
   output_bit (LCD_DB4, !! (nibble&1));
   output_bit (LCD_DB5, !! (nibble&2));
   output_bit (LCD_DB6, !! (nibble&4));
   output_bit (LCD_DB7, !! (nibble&8));
   delay_cycles (1);
   output_high (LCD_E);
   delay_us (2);
   output_low (LCD_E);
}

//-----------------------------------
// This sub-routine is only called by lcd_read_byte().
// It's not a stand-alone routine.  FOR example, the
// R/W signal is set high by lcd_read_byte() before
// this routine is called.     
#ifdef USE_RW_PIN
int8 lcd_read_nibble(void) {
   int8 retval;

   // Create bit variables so that we can easily set
   // individual bits in the retval variable.
   #bit retval_0 = retval.0
   #bit retval_1 = retval.1
   #bit retval_2 = retval.2
   #bit retval_3 = retval.3
   
   retval = 0;
   
   output_high (LCD_E);
   delay_us (1);
   
   retval_0 = input (LCD_DB4);
   retval_1 = input (LCD_DB5);
   retval_2 = input (LCD_DB6);
   retval_3 = input (LCD_DB7);
   
   output_low (LCD_E);
   delay_us (1);
   
   return (retval);
}
#endif

//---------------------------------------
// Read a byte from the LCD and RETURN it.
#ifdef USE_RW_PIN
int8 lcd_read_byte(void){
   int8 low;
   int8 high;
   
   output_high (LCD_RW);
   delay_cycles (1);
   
   high = lcd_read_nibble ();
   
   low = lcd_read_nibble ();
   
   return ((high<<4)|low);
}
#endif

//----------------------------------------
// Send a byte to the LCD.
void lcd_send_byte(int8 address, int8 n){
   output_low(LCD_RS);

   #ifdef USE_RW_PIN
      while(bit_test(lcd_read_byte(),7)) ;
   #ELSE
      delay_us(60);
   #endif

   if(address) output_high (LCD_RS);
     else output_low (LCD_RS);
   
   delay_cycles (1);

   #ifdef USE_RW_PIN
     output_low(LCD_RW);
     delay_cycles(1);
   #endif

   output_low(LCD_E);
   lcd_send_nibble(n >> 4);
   lcd_send_nibble(n & 0xf);
}

//----------------------------
void lcd_init(void){
   int8 i;
   lcd_line = 1;
   output_low(LCD_RS);

   #ifdef USE_RW_PIN
     output_low(LCD_RW);
   #endif

   output_low(LCD_E);

   // Some LCDs require 15 ms minimum delay after
   // power-up.  Others require 30 ms.  I'm going
   // to set it to 35 ms, so it should work with
   // all of them.
   delay_ms(35);
   for (i = 0; i < 3; i++){
      lcd_send_nibble (0x03);
      delay_ms (5);
   }

   lcd_send_nibble(0x02);
   for (i = 0; i < SIZEOF (LCD_INIT_STRING); i++){
      lcd_send_byte (0, LCD_INIT_STRING[i]);
     
      // IF the R / W signal is not used, then
      // the busy bit can't be polled. One of
      // the init commands takes longer than
      // the hard - coded delay of 50 us, so in
      // that case, lets just DO a 5 ms delay
      // after all four of them.
      #ifndef USE_RW_PIN
        delay_ms (5);
      #endif
   }
}

//----------------------------
void lcd_gotolc(int8 y, int8 x){
   int8 address;

   switch (y){
      case 1:
        address = LCD_LINE_1_ADDRESS;
        break;

      case 2:
        address = LCD_LINE_2_ADDRESS;
        break;

      case 3:
        address = LCD_LINE_3_ADDRESS;
        break;

      case 4:
        address = LCD_LINE_4_ADDRESS;
        break;

      default:
        address = LCD_LINE_1_ADDRESS;
        break;
   }

   address += x-1;
   lcd_send_byte(0, 0x80 | address);
}

//-----------------------------
void lcd_putc(char c){

  #bit LCD_BLINK_BIT  = LCD_FUNCTION.LCD_BLINK  // 0
  #bit LCD_CURSOR_BIT = LCD_FUNCTION.LCD_CURSOR // 1
  #bit LCD_ONOFF_BIT  = LCD_FUNCTION.LCD_ONOFF  // 2
 
  #bit LCD_SetMode_BLINK     = LCD_SetMode.LCD_BLINK  // 0
  #bit LCD_SetMode_CURSOR    = LCD_SetMode.LCD_CURSOR // 1
  #bit LCD_SetMode_ONOFF_BIT = LCD_SetMode.LCD_ONOFF  // 2

  if ( LCD_SetMode ) {

    switch ( LCD_SetMode ){
      case 1: // blink
        LCD_BLINK_BIT = 0 ;
        if (c=='1') LCD_BLINK_BIT = 1 ;
        break;

      case 2: // cursor
        LCD_CURSOR_BIT = 0 ;
        if (c=='1') LCD_CURSOR_BIT = 1 ;
        LCD_CURSOR_BLINK = LCD_CURSOR_BIT ;
        break;
 
      case 4: // On/Off
        LCD_ONOFF_BIT = 0 ;
        if (c=='1') LCD_ONOFF_BIT = 1 ;
        break;
    }

    LCD_SetMode = 0 ;
    lcd_send_byte (0, LCD_FUNCTION );

  } else {
    switch (c){
      case '\f':
        lcd_send_byte (0, 1);
        lcd_line = 1;
        delay_ms (2);
        break;
       
      case '\n':
        lcd_gotolc ( ++lcd_line, 1);
        break;
       
      case '\b':
        lcd_send_byte (0, 0b0010000); //0x10);
        break;
 
      case '\t': // cursor
        LCD_SetMode_CURSOR = 1 ;
        break;
 
      case '\a': // blink
        LCD_SetMode_BLINK = 1 ;
        break;

      default:
       lcd_send_byte(1,c);
       break;

    }
 
  }


}

//------------------------------
#ifdef USE_RW_PIN

char lcd_getc(int8 x, int8 y){
 char value;
   lcd_gotolc(y,x);

   // Wait until busy flag is low.
   while(bit_test(lcd_read_byte(),7));
   output_high(LCD_RS);
   value = lcd_read_byte();
   output_low(LCD_RS);
   return(value);
}

#endif


_________________
in médio virtus
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Tue May 29, 2007 2:40 am     Reply with quote

If its debugging you want the CCS debugger is very nice.
Chances are you will want to do ICSP ..program and reprogram your chip while it is in your circuit so you will be dedicating B7 and B6 to the ICSP and if you further give up PIN_b3 for the CCS debugger it will do printfs to CCS's built in PC monitor.
The CCS debugger has mouse over display of variables in your code and line by line tracing.
grasspuddle



Joined: 15 Jun 2006
Posts: 66

View user's profile Send private message

PostPosted: Tue May 29, 2007 8:39 am     Reply with quote

If you want extremely easy and simple use this:

http://www.sparkfun.com/commerce/product_info.php?products_id=813

It has 3 inputs, +5V, GND, and RCV

Whatever it receives (9600baud if I remember right) it displays.
I've used it once or twice when I didn't have a max232 to plug in and test on comp.
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Tue May 29, 2007 10:51 am     Reply with quote

Thank you. This last one is exactly what I need.

Ox.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group