New to C? ... check in here
Unsoundcode
Posts: 1,532
Hi , Im guessing there will be a lot of new Propeller users and existing forum members wanting to use or at least try the C programming environment. I thought it would be a good idea to present a C primer to people completely new to C or rusty intermediates (I would say I am somewhere between). Its my intention and hope that this thread be completely devoted to the PEKitLabs Fundamentals and here new users can learn and teach from and to each other. I encourage people to post their own conversions in C from the labs and I would like to see the more advanced users give assistance and pointers (without providing the whole solution). If enough examples get posted, and are well commented, we can perhaps select a few of the best for a sticky. This should keep us going until they re-write the book.
I am attaching a primer for absolute beginners and intend to do more soon. The primer is a conversion of one of the labs first Spin files. Feel free to be critical I can probably correct any mistakes.
Jeff T.
UPDATE
Uploaded a txt version and jpeg for non MS Word users. Note the header should be corrected to be #include <propeller.h> and in the text file Int main should read int main.
UPDATE #2
Uploaded C Primer Part 2
I am attaching a primer for absolute beginners and intend to do more soon. The primer is a conversion of one of the labs first Spin files. Feel free to be critical I can probably correct any mistakes.
Jeff T.
UPDATE
Uploaded a txt version and jpeg for non MS Word users. Note the header should be corrected to be #include <propeller.h> and in the text file Int main should read int main.
UPDATE #2
Uploaded C Primer Part 2
txt
6K
Comments
Good idea. A couple of little things:
Page 1 you have a typo - Int main() - i.e. capital "I". C is case sensitive, so "Int" is not the same as "int"
Page 2 should probably say #include <Propeller.h>. The <file_name> notation means a system header file, while "file_name" means one of your own project header files. In this case I believe it should be a system header file. If either one works then it could indicate you have either a duplicate file, or a problem with your inlcude file paths - either one of which can cause you much grief down the track.
Ross.
Ross had helpful comments: I guess Word was trying to help auto-correct the first letter of a sentence. File propeller.h can be found in the system include path, and it should be all lower case: #include <propeller.h>. The latest propeller.h has been updated with many "propellery" #define macros, but it is missing cogid (fix queued for the next snapshot).
Checking in, can't wait to take a look at your primer. I just reloaded my laptop with Vista today and I am going to dual boot fedora 15 also in the middle of updating fedora 15 now and don't have an Office Program installed to read your .doc just yet, but soon. I mentioned in another post that I wasn't much of a "command line guy" well I am not much of a programmer either but I am going to jump in head first and see what happens(i'll wear a helmet).
-Ron
OpenOffice should be in the Fedora repos, and you can download LibreOffice (successor to OpenOffice) form their site.
Just finished updating and tweaking fedora going to take a look at the .doc now. Then download propgcc and see how it works with fedora. I never did get propgcc to work with Ubuntu but I had it going in Vista. It was time for me to wipe out Vista and reload it anyway (once a year) so I thought I would give fedora try, I ended up with LibreOffice.
Thanks Ron
I am having problems with he FTDI Driver or the '/dev/ttyUSB0 port'. Not sure what's wrong. I had a USB hub hooked up so I unplugged it with nothing but the Propeller plugged in. Tried BST and it cant find the Propeller either. BST worked on my Unbuntu Box with the same USB hub so I don't know. I didn't think I would have problems with fedora either I am going to have to figure this out I am kinda fond of this Gnome 3 desktop. The code compiled fine just can't load it. Its getting late I try and figure it out tomorrow. See below maybe you'll have some ideas. I tried to restart between unplugging the USB HUB also.
Its probably something silly I am overlooking.
Learning a lot Ron
Eric
Note on sudo... you can configure the sudoers file to not ask you for a password, which is my suggestion. Add yourself to the wheel group, and look for the no password line in the sudoers file. To edit the file, you can run visudo. This is a bit different than the way Ubuntu handles things, but IMO, it works more smoothly.
In Debian Users and Groups is in System->Administration. Not sure where it is in Fedora. I've used Fedora 13 with propeller-gcc although the compier generated version warnings - __red__ will be building .rpm packages for us later.
I checked if it was a permission issue per Kevin's suggestion (sudo propeller-load -r yourfilename) and it was for a quick fix I did as ersmith suggested below I will look at setting permissions in User Groups when I can find them in fedora. jazzed its working in fedora 15 now.
@Unsoundcode sorry should have started a new thread for this I guess. Nice Primer I like you explanation about the bitmasks thanks for your efforts. Just what I needed to get going.
Thanks All
Ron
You should definitely try the add-to-group thing, not sure how it's done in Fedora. On Ubuntu and Debian it's as simple as opening a terminal, then 'sudo adduser your-name dialout', then log completely out, and in again. There are presumably some GUI admin tools to do that as well, but they change all the time so I don't bother trying to learn them or find them.
On Fedora and other RedHat-derived distros there's no 'adduser' that works that way, for some reason. But for any distro (including Ubuntu and Debian, and any Unix anywhere) you can also simply log in as root, then edit /etc/group and add yourself to the dialout group. Although, on some distros, it's not 'dialout' but 'uucp'. It's whatever you see when you do 'ls -l /dev/ttyUSB0'. That's what the tools do: They edit that line in /etc/group
The reason it's better to add yourself to the correct group than to change the access with 'chmod' is that nowadays the /dev/ filesystem is dynamic, and /dev/ttyUSB0 is created and deleted automatically as you plug and unplug your propeller. That means that the 'chmod' method doesn't stick: You'll have to do it again and again whenever you unplug and plug in the propeller. Adding yourself to the correct group is permanent.
-Tor
Yes you are correct the "chmod' method doesn't stick" l am looking into the whole 'user group' thing now. Its different in Fedora that's for sure.
Thanks Ron
EDIT:
All is fine now in Gnome 3 the 'User Manger GUI' isn't readily available but if you search 'user group' an icon will show up for it, then changes can be made. I added my self to the 'dialout' group in the GUI, a restart was needed in my case.
Thanks again All,
Ron
This part of the primer expands on the use of the #define directive to create the above instructions, additional material on the use of masks and lastly to write your own header file.
The conversion at the end, LedOnP4.spin to LedOnP4.c is the beginning of the journey into programming the Propeller, it is so basic yet so rewarding.
As a last note in this post if anyone has a better solution to the functions I used in this example please post them here.
Jeff T.
Last night I was trying to figure out how to make a header file for the IO pin's. Your Primer couldn't have come at a better time for me. I was getting discouraged and was just about to give up, and go back to learning more SPIN witch is probably what I should be doing with my free time. But this is FUN. Everything in the Primer worked for me.
Appreciate the time your taking to make these Primers.
Ron
Eric
Ron I'm glad your enjoying it, I am. The header was certainly introduced at a convenient time, to implement Erics suggestion involves modifying just the one file. I plan on one more primer discussing variables, the for loop and a simple function. If you think you will take to C then you might consider a good book for reference, and how much does it cost for the expert advice we can get here...Priceless!!
regards
Jeff T.
EDIT. I wondered if there might also be a better way to express Outa[4]:=0
(or _OUTA &= ~(1<<4) if propeller.h is not included.)
The tutorial is wonderful.
Jeff T.
EDIT: Kevin Wood suggested several good programs for typing and editing code files and headers, I chose Programmers Notepad which is a free download, personally I like it very much.
I'm sure folks around here would be comfortable with that too.
Sounds like a +1 for that Programmers Notepad. I'll have to try it.
Looks good in the website. Maybe a tutorial for that will appear (Kevin?).
That's one of the first obstacles that you come up against in the PEK Labs, not only are we (the beginners amongst us) learning the features of the Propeller we have to learn to translate Spin into C. One of the aims of the header file creation is to act as an equalizer between learning C and learning the Prop, it allows us to progress with the labs and produce C code that is almost identical to Spin code.
I hope that people with more imagination than I have will give their own names to "myheader.h" and the function macros it contains, the actual content of the header I do like though.The next addition will be not(pin); the equivalent of !OUTA[pin] and it will be used in a function that takes parameters, we will really be cooking with gas then.
Jeff T.
I think the simplest definition of this would be:
(This uses C's exclusive or operator to invert bit "pin" in OUTA.)
Eric
The only thing I'd like to do additionally is be able to set multiple pins through the #define
For example, set_input_pin should be written as: #define set_input_pin(pin) _DIRA &= ~(1<<(pin))
The pin parameter is just a textual expansion. To ensure you get what you want, parens is used. This protects the user from situations like set_input_pin(25+n). In C the order of operations will cause a different result from SPIN.
Programmer's Notepad is a nice editor. I was able to define Ctrl+Shift+C for compile and Ctrl+Shift+D for download. There are some issues with that though for bigger programs and I cant get our terminal to work. I see Ctrl+Shift+K for killing an external program that may misbehave - it is useful.