Forum Index
ANSI C Compatibility

 
Author Message
liam.zbasic



Joined: 25 Mar 2008
Location: California

Posted: 06 July 2008, 0:46 AM    Post subject: ANSI C Compatibility

Is it possible to combine ANSI C code with Zbasic? The intent is to take advantage of Matlab autocode for complex control systems. Comments appreciated.

P.
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Location: Portland, OR

Posted: 06 July 2008, 1:02 AM    Post subject: Re: ANSI C Compatibility

liam.zbasic wrote:
Is it possible to combine ANSI C code with Zbasic?
Yes but only with native mode devices, e.g. ZX-24n.

You can use inline C code within ZBasic source code, separate C source modules, compiled object modules and/or object libraries.
Back to top
liam.zbasic



Joined: 25 Mar 2008
Location: California

Posted: 06 July 2008, 2:19 AM    Post subject:

Excellent. Is all the capability you outlined administered by the Zbasic IDE? Do you have a simple "hello world" example using separate C source modules? Thank you for your quick response.

P.
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Location: Portland, OR

Posted: 06 July 2008, 3:14 AM    Post subject:

liam.zbasic wrote:
Do you have a simple "hello world" example using separate C source modules?
The attached .zip file contains a simple project containing one ZBasic module (c_source.bas) and a C source module (factorial.c). It is somewhat contrived since the functionality implemented in C could just as easily have been done in ZBasic but it illustrates the technique nonetheless.

The content of c_source.bas is reproduced below. The key to making a call to a function implemented in C is to prepare a Declare statement that informs the ZBasic compiler as to the parameters and type of the C function. The Alias clause tells the compiler the actual name of the C function being called. The declared name (used in ZBasic code) needn't be the same. For more information, see the ZBasic Reference manual section Defining and Using External Subroutines, Functions and Variables

Code:
Declare Function Factorial(ByVal val as UnsignedInteger) as UnsignedInteger Alias "factorial"

Sub Main()
  Dim val as UnsignedInteger
  val = 5
  Debug.Print val; "! is "; Factorial(val)
End Sub


The content of the module factorial.c (reproduced below) is a straightforward recursive implementation of the factorial function.
Code:
unsigned
factorial(unsigned val)
{
  return((val <= 1) ? 1 : factorial(val - 1) * val);
}


When working with external C modules, object modules or object libraries, it may be a good idea to add the compiler option --verbose to the project file (before the module names). With that option present, the output from the backend compiler and linker will be displayed in the ZBasic IDE output window so you'll be able to see error messages that are issued. Without that, the ZBasic compiler will simply report that an error occurred during the build without any clues as to what the error might be.

This technique is recommended for more experienced programmers and, particularly, those with experience programming in C.



c_source.zip
 Description:
ZBasic example using a C source module.

Download
 Filename:  c_source.zip
 Filesize:  562 Bytes
 Downloaded:  271 Time(s)

Back to top
Display posts from previous:   
Page 1 of 1

 



ZBasic Microcontrollers Home
All content Copyright © 2005, 2006, 2007, 2008 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group