View previous topic :: View next topic |
Author |
Message |
JimT
Joined: 22 Feb 2007 Posts: 4
|
How Does It Work? |
Posted: Thu Mar 22, 2007 6:50 am |
|
|
Folks,
While reviewing code for a port our team will be doing, I ran accross these function definitions:
Code: | void LCD_write_nibble(byte n)
{
output_bit(LCD_D4, n & 1);
output_bit((LCD_D5, (n>>1) & 1);
output_bit(LCD_D6, (n>>2) & 1);
output_bit(LCD_D7, (n>>3) & 1);
delay_cycles(1)
output_high(LCD_E);
delay_us(4);
output_low(LCD_E);
}
void LCD_write_byte(byte addr, b)
{
delay_us(80);
output_low(LCD_E);
delay_cycles(1);
output_bit(LCD_RS, addr);
LCD_write_nibble(b>>4);
LCD_write_nibble(b & 0x0F);
}
void LCD_putc(char c)
{
if(c)
LCD_write_byte(1,c);
} |
In the main() function the LCD_putc() function is envoked by:
Code: | LCD_putc("Some String"); |
I expected a compiler error or warning about type mismatch, but I did not get it. Instead, all the characters of the string showed up on the LCD. I checked the map file to find that the compiler generated a loop for the LCD_putc() function. Why did it do this?
The compiler is version 4.025 and the processor is a PIC 18F452. Any insight would be welcome.
Thanks,
Jim. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Mar 22, 2007 7:40 am |
|
|
That is one of the CCS extensions to standard C. If you give a string to a function that takes a char the compiler creates a loop to execute the function on each character of the string. It is non-standard but sometimes very useful. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
JimT
Joined: 22 Feb 2007 Posts: 4
|
|
Posted: Thu Mar 22, 2007 8:06 am |
|
|
SherpaDoug
Thanks for the reply. We'll make sure we document it in the code.
Thanks,
Jim. |
|
|
JimT
Joined: 22 Feb 2007 Posts: 4
|
|
Posted: Thu Mar 22, 2007 4:50 pm |
|
|
Closing question:
Is this extension covered in the CCS documentation? I'm looking for something to reference.
Thanks,
Jim. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Mar 22, 2007 5:04 pm |
|
|
The March 2006 manual says this:
Quote: |
A (non-standard) feature has been added to the compiler to help get
around the problems created by the fact that pointers cannot be created
to constant strings. A function that has one CHAR parameter will accept
a constant string where it is called. The compiler will generate a loop
that will call the function once for each character in the string.
Example:
void lcd_putc(char c )
{
... }
lcd_putc ("Hi There.");
|
I can't find it in the current manual. |
|
|
JimT
Joined: 22 Feb 2007 Posts: 4
|
|
Posted: Fri Mar 23, 2007 6:27 am |
|
|
PCM Programmer,
Thanks for the reply, that's just what I was looking for.
JimT. |
|
|
|