View previous topic :: View next topic |
Author |
Message |
totalnoob
Joined: 22 Oct 2006 Posts: 24
|
Converting String to ascii hex to be output to LCD Via I2C. |
Posted: Sun Nov 12, 2006 5:42 pm |
|
|
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
|
|
Posted: Sun Nov 12, 2006 6:15 pm |
|
|
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
|
|
Posted: Sun Nov 12, 2006 7:31 pm |
|
|
I don't think printf works with the i2c commands. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Nov 12, 2006 8:49 pm |
|
|
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
|
|
Posted: Tue Nov 14, 2006 7:33 pm |
|
|
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
|
|
Posted: Tue Nov 14, 2006 8:33 pm |
|
|
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
|
|
Posted: Tue Nov 14, 2006 10:57 pm |
|
|
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
|
|
Posted: Wed Nov 15, 2006 7:50 pm |
|
|
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. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 15, 2006 8:30 pm |
|
|
No, I'm not employed by CCS. I don't have all the answers, either. |
|
|
|