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 CCS Technical Support

creating a function to output a string into LCD ?

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



Joined: 17 Jul 2012
Posts: 53

View user's profile Send private message

creating a function to output a string into LCD ?
PostPosted: Wed Nov 14, 2012 6:43 pm     Reply with quote

I'd like to create a function to output a string into LCD, but there's a problem here.
lcd.h code:
Code:

#define      LCD_module
#ifdef      LCD_module
/********************************************/
#define   LCD_RS      RD1
#define   LCD_EN      RD3
#define   LCD_RW      RD2
#define   LCD_DATA4   RD4
#define   LCD_DATA5   RD5
#define   LCD_DATA6   RD6
#define   LCD_DATA7   RD7

#define   LCD_RS_TRIS      TRISD1
#define   LCD_EN_TRIS      TRISD3
#define   LCD_RW_TRIS      TRISD2
#define   LCD_DATA4_TRIS   TRISD4
#define   LCD_DATA5_TRIS   TRISD5
#define   LCD_DATA6_TRIS   TRISD6
#define   LCD_DATA7_TRIS   TRISD7
/*------------------------------------------*/
typedef union _BYTE_VAL
{
    unsigned char Val;
    struct
    {
        unsigned char b0:1;
        unsigned char b1:1;
        unsigned char b2:1;
        unsigned char b3:1;
        unsigned char b4:1;
        unsigned char b5:1;
        unsigned char b6:1;
        unsigned char b7:1;
    } bits;
} BYTE_VAL;
/*------------------------------------------*/
unsigned char lcd_get_byte(unsigned char rs);
void lcd_put_byte(unsigned char rs, unsigned char b);
int1 lcd_busy();
void lcd_gotoxy(unsigned char col, unsigned char row);
void lcd_putc(char c);
void lcd_init();
void lcd_clear();
/********************************************/
#endif

lcd.c code:
Code:

#include "lcd.h"
#include "IO_module.h"
#use delay(clock = 4000000)
/*------------------------------------------*/
unsigned char lcd_get_byte(unsigned char rs)
{
   BYTE_VAL b;
   
   LCD_DATA4_TRIS = 1;
   LCD_DATA5_TRIS = 1;
   LCD_DATA6_TRIS = 1;
   LCD_DATA7_TRIS = 1;
   
   LCD_RW = 1;
   LCD_RS = 0;
   if(rs) LCD_RS = 1;
   delay_us(20);
   LCD_EN = 1;
   delay_us(20);
   
   b.bits.b7 = LCD_DATA7;
   b.bits.b6 = LCD_DATA6;
   b.bits.b5 = LCD_DATA5;
   b.bits.b4 = LCD_DATA4;
   
   LCD_EN = 0;
   delay_us(20);
   LCD_EN = 1;
   delay_us(20);
   b.bits.b3 = LCD_DATA7;
   b.bits.b2 = LCD_DATA6;
   b.bits.b1 = LCD_DATA5;
   b.bits.b0 = LCD_DATA4;
   LCD_EN = 0;
   
   return b.Val;
}
/*------------------------------------------*/
void lcd_put_byte(unsigned char rs, unsigned char b)
{
   BYTE_VAL temp;
   
   LCD_DATA4_TRIS = 0;
   LCD_DATA5_TRIS = 0;
   LCD_DATA6_TRIS = 0;
   LCD_DATA7_TRIS = 0;
   
   LCD_RS = 0;
   if(rs) LCD_RS = 1;
   
   delay_us(20);
   LCD_RW = 0;
   delay_us(20);
   LCD_EN = 0;
   
   temp.Val = b;
   
   // send the high nibble
   LCD_DATA4 = temp.bits.b4;
   LCD_DATA5 = temp.bits.b5;
   LCD_DATA6 = temp.bits.b6;
   LCD_DATA7 = temp.bits.b7;
   delay_us(20);
   LCD_EN =  1;
   delay_us(20);
   LCD_EN = 0;
   // send the low nibble
   LCD_DATA4 = temp.bits.b0;
   LCD_DATA5 = temp.bits.b1;
   LCD_DATA6 = temp.bits.b2;
   LCD_DATA7 = temp.bits.b3;
   delay_us(20);
   LCD_EN =  1;
   delay_us(20);
   LCD_EN = 0;
}
/*------------------------------------------*/
int1 lcd_busy() 
{
   int1 busy;
   
   LCD_DATA4_TRIS = 1;
   LCD_DATA5_TRIS = 1;
   LCD_DATA6_TRIS = 1;
   LCD_DATA7_TRIS = 1;
   
   LCD_RW = 1;
   LCD_RS = 0;
   delay_us(20);
   LCD_EN = 1;
   delay_us(20);
   
   busy = LCD_DATA7;
   
   LCD_EN = 0;
   delay_us(20);
   LCD_EN = 1;
   delay_us(20);
   LCD_EN = 0;
   
   return busy;
}
/*------------------------------------------*/
void lcd_gotoxy(unsigned char col, unsigned char row)
{
   unsigned char address;
   
   if(row!=0)
      address=0x40;
   else
      address=0;
   
   address += col;
   lcd_put_byte(0,0x80|address);
   while(lcd_busy());
}
/*------------------------------------------*/
void lcd_putc(char c)
{
 switch(c)
   {
    case '\f':
      lcd_put_byte(0,1);
      delay_ms(2);
      break;
   
    case '\n':
       lcd_gotoxy(1,2);
       break;
   
    case '\b':
       lcd_put_byte(0,0x10);
       break;
   
    default:
       lcd_put_byte(1,c);
       break;
   }
}
/*------------------------------------------*/
void lcd_init()
{
   int8 temp;
   LCD_EN_TRIS = 0;
   LCD_RS_TRIS = 0;
   LCD_RW_TRIS = 0;
   LCD_DATA4_TRIS = 0;
   LCD_DATA5_TRIS = 0;
   LCD_DATA6_TRIS = 0;
   LCD_DATA7_TRIS = 0;
   LCD_EN = 0;
   LCD_RS = 0;
   LCD_RW = 0;

   delay_ms(100);            // delay for power on
   
   // reset LCD
   lcd_put_byte(0,0x30);
   delay_ms(50);
   lcd_put_byte(0,0x30);
   delay_ms(50);
   lcd_put_byte(0,0x32);
   delay_ms(100);            // delay for LCD reset
   delay_ms(100);            // delay for LCD reset
   delay_ms(100);            // delay for LCD reset
   
   while(lcd_busy());
   /*Function set:4 bit interface + 2 lines + 5x7 dot format
   */
   lcd_put_byte(0,0x28);         
   while(lcd_busy());
   /*display off + cursor underline & cursor blink off
   */
   lcd_put_byte(0,0x08);      
   while(lcd_busy());
   /*display on + cursor underline & cursor blink off
   */
   lcd_put_byte(0,0x0C);      
   while(lcd_busy());
   /*clear display and move cursor to home
   */
   lcd_put_byte(0,0x01);      
   while(lcd_busy());
   /*Character Entry mode:increment + display shift off
   */
   lcd_put_byte(0,0x06);         
   while(lcd_busy());
}
/*------------------------------------------*/
void lcd_clear()
{
   lcd_put_byte(0,1);      // display off
   while(lcd_busy());   
}
/*------------------------------------------*/
void lcd_puts(const char* s)
{
   while(*s)
   {
      lcd_putc(*s++);
   }
}


and error statements from MPLAB:
*** Error 28 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 196(21,25): Expecting an identifier
*** Error 48 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 196(27,28): Expecting a (
*** Error 43 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 197(1,2): Expecting a declaration
*** Error 43 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 198(1,6): Expecting a declaration
*** Error 43 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 198(6,7): Expecting a declaration
*** Error 43 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 198(7,8): Expecting a declaration
*** Error 48 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 198(8,9): Expecting a (
*** Error 43 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 199(1,2): Expecting a declaration
*** Error 32 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 200(11,12): Expecting a , or )
*** Error 43 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 200(14,15): Expecting a declaration
*** Error 43 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 200(15,16): Expecting a declaration
*** Error 43 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 201(1,2): Expecting a declaration
*** Error 43 "D:\document\Study\university of technology\sesmester_7\Embedded System Design\Big_assignment\lcd.c" Line 202(1,2): Expecting a declaration
13 Errors, 0 Warnings.
Halting build on first failure as requested.
BUILD FAILED: Wed Nov 14 19:39:52 2012
////////////////////////////////////////////////////////////////
problem here is "lcd_puts", i don't know why CCS can't accept "const char* s" ? this function works well in Hi-tech compiler !Looking forward to receiving your support !
Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 14, 2012 8:28 pm     Reply with quote

CCS doesn't do constant strings in the same way as Hi-Tech.

Solution:
Don't use 'const' in the function declaration, and add the #device
statement: PASS_STRINGS=IN_RAM
small_chick



Joined: 17 Jul 2012
Posts: 53

View user's profile Send private message

PostPosted: Wed Nov 14, 2012 9:58 pm     Reply with quote

it's well-done now, PCM programmer ! thank you very much ! Very Happy
small_chick



Joined: 17 Jul 2012
Posts: 53

View user's profile Send private message

PostPosted: Thu Nov 15, 2012 6:23 pm     Reply with quote

I've tried lcd.h &.c with PIC16F887 and dsPIC30F4011.Then they work well with only PIC16F887 .
Here the code for PIC16F887:
Code:

#include <16F887.h>
#device PASS_STRINGS = IN_RAM
#fuses   XT,NOWDT,PUT,BROWNOUT,BORV40
#include "lcd.c"

void main()
{
   lcd_init();
   lcd_puts("adc");
   while(1);   
}

when using dsPIC30F4011, I only changed in lcd.h as following:
Code:

#define   LCD_RS      RB1
#define   LCD_EN      RB3
#define   LCD_RW      RB2
#define   LCD_DATA4   RB4
#define   LCD_DATA5   RB5
#define   LCD_DATA6   RB6
#define   LCD_DATA7   RB7

#define   LCD_RS_TRIS      TRISB1
#define   LCD_EN_TRIS      TRISB3
#define   LCD_RW_TRIS      TRISB2
#define   LCD_DATA4_TRIS   TRISB4
#define   LCD_DATA5_TRIS   TRISB5
#define   LCD_DATA6_TRIS   TRISB6
#define   LCD_DATA7_TRIS   TRISB7

a part of code in IO_module.h (included in lcd.c):
Code:

....
#word    TRISB = 0x02C6
#word   PORTB = 0x02C8
#word   LATB = 0x02CA

#bit   TRISB8 = 0x02C6.8
#bit   TRISB7 = 0x02C6.7
#bit   TRISB6 = 0x02C6.6
#bit   TRISB5 = 0x02C6.5
#bit   TRISB4 = 0x02C6.4
#bit   TRISB3 = 0x02C6.3
#bit   TRISB2 = 0x02C6.2
#bit   TRISB1 = 0x02C6.1
#bit   TRISB0 = 0x02C6.0

#bit   RB0 = 0x02C8.0
#bit   RB1 = 0x02C8.1
#bit   RB2 = 0x02C8.2
#bit   RB3 = 0x02C8.3
#bit   RB4 = 0x02C8.4
#bit   RB5 = 0x02C8.5
#bit   RB6 = 0x02C8.6
#bit   RB7 = 0x02C8.7
#bit   RB8 = 0x02C8.8

#bit   LATB0 = 0x02CA.0
#bit   LATB1 = 0x02CA.1
#bit   LATB2 = 0x02CA.2
#bit   LATB3 = 0x02CA.3
#bit   LATB4 = 0x02CA.4
#bit   LATB5 = 0x02CA.5
#bit   LATB6 = 0x02CA.6
#bit   LATB7 = 0x02CA.7
#bit   LATB8 = 0x02CA.8
...

code in main.c:
Code:

#include    <30F4011.h>
#device PASS_STRINGS = IN_RAM
#include   "lcd.c"
#fuses XT,NOWDT,PUT64,BROWNOUT,BORV42

void main()
{
   ADPCFG = 0xFFFF;//config all ANx as digital
   lcd_init();
   lcd_puts("adc");
   while(1);
}

I suppose the problem here is in lcd_init() because when i try it, it's always stuck in lcd_busy() ! I can't understand why this happened with dsPIC30F4011 but didn't with PIC16F887 ???? Please help me ! Sad
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Fri Nov 16, 2012 2:11 am     Reply with quote

Fractionally puzzled what code you are actually using?. The standard lcd.c, that comes with the compiler, doesn't have an lcd.h, or an IO_module.h, nor does it use register bit definitions for the ports. Switch to the standard driver as a 'first thing to do'. In all honesty, flex_lcd, is more flexible, and possibly more likely to work.
The general thing that causes a problem on a particular chip, is that the pins being used 'wake up' configured to something else. Try making sure that analog functions are turned off on these pins before starting.

Best Wishes
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