View previous topic :: View next topic |
Author |
Message |
Boyko
Joined: 11 Oct 2004 Posts: 13 Location: Sofia
|
CCS functions and Picbasic subroutines |
Posted: Sat Nov 13, 2004 4:26 am |
|
|
Everyone says that C is better than Basic, because it uses functions. But I can't undesrtand what exactlty is the advantage of the functions compared to the subroutines. Yes I know that in CCS we have local vars, but I program on 18F452 and the RAM is not a problem at all.
Can you tell me what is the difference between theese two small codes:
CCS C
Code: |
int8 my_funct(int8 a, int8 b) {
int8 result
result = 5 * a - 4 * b;
return result;
}
main() {
while (true) {
delay_ms(500);
printf(%my_funct(25,10)\r\n);
}
}
|
In Picbasic:
Code: |
a var byte
b var byte
result var byte
While 1 = 1
pause 500
a = 25
b = 10
gosub my_funct
hserout [Dec result, 13, 10]
Wend
my_funct:
result = 5 * a - 4 * b
Return
|
Thanks in advance. _________________ Regards
Boyko |
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Sat Nov 13, 2004 8:42 am |
|
|
Hi,
For small and simple code like this there is no difference, but when you start to complicate things it does have.
An example, do a menu system for a 16x2 LCD.
Basic is nice, but has its weakness. |
|
|
Boyko
Joined: 11 Oct 2004 Posts: 13 Location: Sofia
|
|
Posted: Sat Nov 13, 2004 9:24 am |
|
|
But I have very good programs for 16x2 LCD menus. Picbasic Proton+ has an excellent data table support.
Yes, I agree there are weaknesses, but I can't find them. That's why I ask you. Could you please show me some of them?
I use Proton+ & Picbasic pro and now CCS C. _________________ Regards
Boyko |
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Sat Nov 13, 2004 11:14 am |
|
|
How would you do something like this:
Code: | struct {
int8 a;
int8 b;
int16 c;
int32 d;
} data; |
now send them via serial port with tx interrupts.
Once I tried with proton and at that time I couldnt.
With C it's easy. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sat Nov 13, 2004 11:19 am |
|
|
How about pointers. |
|
|
Boyko
Joined: 11 Oct 2004 Posts: 13 Location: Sofia
|
|
Posted: Sat Nov 13, 2004 2:39 pm |
|
|
Quote: |
How would you do something like this:
Code:
struct {
int8 a;
int8 b;
int16 c;
int32 d;
} data;
now send them via serial port with tx interrupts.
|
What do you mean with Tx interrupts? I've always rceived data with interrupt, but never sending.
I'm not sure, but cant' we do it like this
Code: |
a var byte
b var byte
c var word
d var dword
while 1 = 1
pause 1000
hserout [Dec a, Dec b , Dec c , Dec d, 13, 10]
wend
|
Quote: |
How about pointers.
|
Yes there are no pointers, but what I miss? What can't I do?
Can you give an example please. _________________ Regards
Boyko |
|
|
mcafzap
Joined: 07 Sep 2003 Posts: 46 Location: Manchester, UK
|
|
Posted: Sat Nov 13, 2004 3:20 pm |
|
|
The difference between C and BASIC is that C is almost a macro language. This means much greater control over every aspect of the hardware and this in turn results in smaller code. The keywords often do less than in BASIC so this is a trade-off. Perhaps C's biggest failing is in string manipulation (just my opinion).
In your last example for outputting variables over an RS232 link using BASIC, it is not clear to me whether interrupts are being used - indeed this remoteness from the hardware might be why you'd want to use it.
HTH
Steve |
|
|
Boyko
Joined: 11 Oct 2004 Posts: 13 Location: Sofia
|
|
Posted: Sun Nov 14, 2004 4:24 am |
|
|
Yes I didn't use interrupt for outputtining the vars. But it's not a problem to be done. Many times I used interrupt driven circular buffer array.
Now I'm learning CCS C and I'm wondering do I need it. Is there a program which can not be done with Picbasic Proton+, but with C only. I mean I have to spend a month or more to learn CCS well enough and I afraid that I'll receive not so much from C. Do you think the effort is justified? _________________ Regards
Boyko |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Sun Nov 14, 2004 4:38 am |
|
|
One more thing you need to do is to compare the assembly code generated by both the basic and the C compiler for the same code. There is a high chance that C results in a more optimized code. |
|
|
Boyko
Joined: 11 Oct 2004 Posts: 13 Location: Sofia
|
|
Posted: Sun Nov 14, 2004 5:09 am |
|
|
Well may be 10 to 15% more code for Picbasic. But if I use 18F452 with 16k flash, I don't see any problem. My programs are more often 3-4k big. The rest 12-13 are not utilized.
Hitech has the most optimized code, but CCS C is the most popular compiler. I don't think this is a big advantage. _________________ Regards
Boyko |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Nov 14, 2004 8:20 am |
|
|
Then stick with your BASIC. There are BASIC programmers and C programmers. Pick which one you want to be. |
|
|
Boyko
Joined: 11 Oct 2004 Posts: 13 Location: Sofia
|
|
Posted: Sun Nov 14, 2004 9:22 am |
|
|
Well, CCS is the most popular compiler. I'd like to program on it. I've already read the reference manual, the PICmicro MCU C book by Nigel Gardner, and the 20 lessons given by CCS. The next few weeks I'll try the programs from the exaples folder.
It's easy, because I just have to convert in my mind the programs I did with Picbasic to the C syntaxis.
Yes, for now I can write only small and easy programs in C, but I think that 95% of the C programs could be written with Picbasic. Actually I'm not sure what percent. That's why I ask this forum. May be someone already did the same migration like me and could tell me: "Picbasic is not a professional compiler because..... and I recommend you to stick with CCS because...." _________________ Regards
Boyko
Last edited by Boyko on Mon Nov 15, 2004 2:23 am; edited 1 time in total |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Sun Nov 14, 2004 2:38 pm |
|
|
As Mark said, If you are comfortable with PicBasic and it can do all that you need, then stick with it. Even if people say it is not a professional compiler. I remember in the early versions of VB people were saying it wasn't a professional programming language and applications should be written in BC++ or VC++. Now look how VB has changed the way people write programs for Windows. Who knows, some day PicBasic may prove to be better than C for small to medium applications.
Still knowledge of C is something useful to have. |
|
|
|