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

Converting String to ascii hex to be output to LCD Via I2C.

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



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

Converting String to ascii hex to be output to LCD Via I2C.
PostPosted: Sun Nov 12, 2006 5:42 pm     Reply with quote

Ok, I'm trying to write to an LCD with i2c. I got that part working ok, I can write to the lcd one letter at a time. Now I'm trying to make a function that will take a string from the user and convert it letter by letter to it's ascii equivalent in hex and output to the lcd. Here's my code, I know this is a simple problem but I'm new to programming and I'm getting a lot of errors. If anyone can help out I would really appreciate it.
Code:
#include <16F877A.h>
#use delay(clock=20000000)
#fuses HS,WDT,BROWNOUT,PUT,NOLVP
//set up i2c peripheral and use hardware SSP
#use i2c(Master,sda=PIN_C4,scl=PIN_C3,RESTART_WDT,FORCE_HW)

void LCD(const char* text)
{  delay_ms(10);
   i2c_start();               //set i2c to start condition
   i2c_write(0xC6);           //send lcd address
   i2c_write(0x00);           //write to register 0
   i2c_write(0x05);           //cursor type: underline
   i2c_write(0x0C);           //clears screen and sets cursor to home position
   char i=0;
   while( text[i] != '\0' )
   { i2c_write(text[i++]); }
   i2c_stop();
}
#int_TIMER1
TIMER1_isr()
{  //communicate every 50 ms
   LCD('Voltage');
}
void main()
{  setup_counters(RTCC_INTERNAL,WDT_1152MS);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
   port_b_pullups(TRUE);       //pull ups for input pins
   enable_interrupts(INT_TIMER1);
   enable_interrupts(global);
   while(1)
   {  //use WDT in case i2c 'hangs up' due to faulty slave
      restart_wdt();
   }
}


I'm using the LCD02 from devantech incase anyone needs to know.
http://www.robot-electronics.co.uk/htm/Lcd02tech.htm
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sun Nov 12, 2006 6:15 pm     Reply with quote

The printf command in CCS allows you to specify a function where a char is to be sent.
totalnoob



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

PostPosted: Sun Nov 12, 2006 7:31 pm     Reply with quote

I don't think printf works with the i2c commands.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 12, 2006 8:49 pm     Reply with quote

To do what Mark is talking about, you would have to re-write your
code to create an lcd_putc() function that can accept a single character
as the input. In essence, you would have to write a driver.
Instead, I'm going to propose that you fix your existing code, as follows:

Initially, you shouldn't be using the Watchdog Timer or interrupts.
Just make a very simple program that hopefully can display data
on the LCD. I'm sure you know that your program doesn't compile.
You can't pass a pointer to a constant string in CCS. Also, putting
single quotes around text does not create a string. The compiler
won't accept it. You have to use double quotes.
If you want to pass a pointer to a string, you need to copy the constant
string into a RAM array first. Then pass a pointer to the RAM array.
Example:
Code:

char buffer[20];   

void main()
{
strcpy(buffer, "Voltage: ");
LCD(buffer);

while(1);
}


You need to fix your LCD() routine. You know a lot of it won't compile.
Remove the 'const'. Move the declaration of 'i' up to the top of the
routine.
totalnoob



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

PostPosted: Tue Nov 14, 2006 7:33 pm     Reply with quote

I tried what you suggested with the strcpy function and I was able to get it working. Thanks a lot for the help. I'm trying to make the the functions more robust now and put them into a .h file. I'll see how that works out and maybe I'll have a few more questions later. Thanks again for the help.
totalnoob



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

PostPosted: Tue Nov 14, 2006 8:33 pm     Reply with quote

Okay, I tried to put my lcd functions into a separate .h file and it compiles but the main function isn't working correctly. When I run the program the LCD_write function always works but the init_LCD function gets skipped. Can anyone tell me why it gets skipped if no errors came up when I compiled it? Here's my code for the main .c file and the .h file:
Code:
#include <16F877A.h>
#include <string.h>
#include <LCD.h>
#use delay(clock=20000000)
#fuses HS,BROWNOUT,PUT,NOLVP
//set up i2c peripheral and use hardware SSP
#use i2c(Master,sda=PIN_C4,scl=PIN_C3,FORCE_HW)

unsigned char buffer1[20];

void main()
{   
   delay_ms(20);
   init_LCD();
   strcpy(buffer1,"Voltage: ");
   LCD_write(buffer1);
   i2c_stop();
   while(1)
   {   }
}

Code:
#include <string.h>
#use delay(clock=20000000)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3,FORCE_HW)

#define lcd_addr 0xC6            //LCD Address
unsigned char i, max;
unsigned char buffer[20];

void LCD_write(text)
{   //function to write a string to i2c LCD
   i2c_start();
   i2c_write(lcd_addr);
   max=strlen(text);   
   for(i=0; i<=max; i++)
   {
      i2c_write(text[i-1]);
   }
   i2c_write(0x0D);            //carriage return
}

void init_LCD()
{   //startup message for LCD Display
   i2c_start();               //set i2c to start condition
   i2c_write(lcd_addr);         //send lcd address
   i2c_write(0x00);                 //write to register 0
   i2c_write(0x05);             //cursor type: underline
   i2c_write(0x0C);                 //clears screen and sets cursor to home position
   strcpy(buffer,"Name: Unknown");   //copies name of project to string
   LCD_write(buffer);            //writes name of project to LCD
   strcpy(buffer,"Name1");         //copies name of team member 1 to string
   LCD_write(buffer);            //writes string to LCD
   strcpy(buffer,"Name2");
   LCD_write(buffer);
   delay_ms(256);               //delay to read startup message
   delay_ms(256);
   delay_ms(256);
   delay_ms(256);
   i2c_write(0x0C);            //clears screen and sets cursor to home position
   i2c_stop();
}

This is my first time trying to make a .h file so maybe this is a stupid problem but I've been stuck on it all day. I would really appreciate some help on it.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 14, 2006 10:57 pm     Reply with quote

I think the reason it's "skipping" the init is because the LCD takes a
long time to initialize itself after power-up. This company, Robot-
Electronics, doesn't publish any real data sheets. You have to look
at their example programs to get some idea of how to do things.

They have a BS2p program here:
http://www.robot-electronics.co.uk/htm/lcd02bs2p.htm
It shows a delay of 500 ms at the start of the program.

They have a Hi-Tech C program for the LCD03, a similar product, here:
http://www.robot-electronics.co.uk/files/p877lcd03.c
In main(), it has a delay loop with 60K iterations. I'm not sure
exactly what the total delay is, but it might be as much as 1 second.

Right now, you have a delay at the start of main() of only 20 ms.
I'd say you should increase that to a minimum of 500 ms.

There are other things that I don't like about your code.
Instead of writing a complete LCD_Write routine, you've got "i2c_stop()"
tacked on the code in main(). You should move it to the end of the
LCD_Write() function.
totalnoob



Joined: 22 Oct 2006
Posts: 24

View user's profile Send private message

PostPosted: Wed Nov 15, 2006 7:50 pm     Reply with quote

That was exactly what the problem was, thanks a lot. Now my programs work perfectly. I appreciate the help. Are you an employee of CCS? or are you just a knowledgeable forum goer, because it seems like you have all the answers. Laughing
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 15, 2006 8:30 pm     Reply with quote

No, I'm not employed by CCS. I don't have all the answers, either.
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