If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.
Special Thanks To:
Aaron Holmes
Aaron Wheeler
Adam Horden
Alan Scrimgeour
Andre
Andrew Haynes
Anonymous000
asabase
Austin Weil
barney
Barry
Bert Hickman
Bill Kukowski
Blitzorn
Brandon Paradelas
Bruce Bowling
BubeeMike
Byong Park
Cesiumsponge
Chris F.
Chris Hooper
Corey Worthington
Derek Woodroffe
Dalus
Dan Strother
Daniel Davis
Daniel Uhrenholt
datasheetarchive
Dave Billington
Dave Marshall
David F.
Dennis Rogers
drelectrix
Dr. John Gudenas
Dr. Spark
E.TexasTesla
eastvoltresearch
Eirik Taylor
Erik Dyakov
Erlend^SE
Finn Hammer
Firebug24k
GalliumMan
Gary Peterson
George Slade
GhostNull
Gordon Mcknight
Graham Armitage
Grant
GreySoul
Henry H
IamSmooth
In memory of Leo Powning
Jacob Cash
James Howells
James Pawson
Jeff Greenfield
Jeff Thomas
Jesse Frost
Jim Mitchell
jlr134
Joe Mastroianni
John Forcina
John Oberg
John Willcutt
Jon Newcomb
klugesmith
Leslie Wright
Lutz Hoffman
Mads Barnkob
Martin King
Mats Karlsson
Matt Gibson
Matthew Guidry
mbd
Michael D'Angelo
Mikkel
mileswaldron
mister_rf
Neil Foster
Nick de Smith
Nick Soroka
nicklenorp
Nik
Norman Stanley
Patrick Coleman
Paul Brodie
Paul Jordan
Paul Montgomery
Ped
Peter Krogen
Peter Terren
PhilGood
Richard Feldman
Robert Bush
Royce Bailey
Scott Fusare
Scott Newman
smiffy
Stella
Steven Busic
Steve Conner
Steve Jones
Steve Ward
Sulaiman
Thomas Coyle
Thomas A. Wallace
Thomas W
Timo
Torch
Ulf Jonsson
vasil
Vaxian
vladi mazzilli
wastehl
Weston
William Kim
William N.
William Stehl
Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Registered Member #2008
Joined: Tue Mar 03 2009, 05:11AM
Location: USA, Frederick, MD
Posts: 118
Good evening. I had recently ordered parts for my projects, finally.
In C, do I need to specify the upper limit of the dimension? Why do I see this in the example in my book: function(a[], ... ) and a[i] occures later in the function. How can the computer know the size of a[] ahead of time?
As I understand, PIC 18 supports only 2 hardware PWM outputs, but can be programmed to output as many PWM signals as you want using soft PWM.
Soft PWM is PWM, improvised by programming, while hard PWM uses a function to call a special built-in PWM device within the PIC.
Every communication protocol has to be declared in a .h file?
Registered Member #2099
Joined: Wed Apr 29 2009, 12:22AM
Location: Los Altos, California
Posts: 1716
[quote] In C, do I need to specify the upper limit of the dimension? Why do I see this in the example in my book: function(a[], ... ) and a[i] occures later in the function. How can the computer know the size of a[] ahead of time? [/quote1286924354]
The function doesn't need to know the size of the array. As you will learn upon further study of C, the actual argument passed into the example function is simply the base address. a[i] refers to an address that is greater by i times the size of one array element.
C does not protect the programmer from declaring an array of 5 elements and then referencing it with a computed index value of 7. In a simple case, that would read or write the memory location for some other variable that was allocated soon after the array.
Registered Member #1334
Joined: Tue Feb 19 2008, 04:37PM
Location: Nr. London, UK
Posts: 615
In C you need to be extremely careful - it is a language that allows you to do pretty much anything without picking you up on any unintentional errors - an example already quoted is to do with array bounds.
Arrays in C are dimensioned by the number of elements in the array. e.g. "int a[6];" declares an array of 6 integers, however, they are referenced from a base of zero, i.e. a[0] up to a[5]. Should you consider writing to a[23], you will get no warning and your program may well fail in subtle and unexpected ways! Also note that string literals such as "ABC" have a hidden terminating NULL character, so "ABC" needs 4 characters of storage, not 3. Some C development environments will help you a bit with obvious and glaring issues, and may well flag up some such issues with warning such as "Do you really mean/want to do this?" but no environment can trap all abuses.
A simple example of the subtlety that can be used are statements such as:
char c;
int i;
...
i = 42;
...
c = "0123456789ABCDEF"[ i & 0xF ];
This declares an integer variable i and a character variable c and then assigns a value to i and the hex equivalent character of the low 4 bits of i to c. The reason I use this example is that is shows that a string is the same as a character array, and with array, the variable used is just a pointer to the first element, so "ABC" returns a pointer to the letter 'A' (a "char *"). You can therefore index a string literal just like a character array - C is full of this sort of stuff.
C is great for embedded programmers as it allows great flexibility due to its simplicity and lack of implicit or explicit constraints. However, its strengths are also its weaknesses - the scope for subtle errors and abuse is immense, and most IDEs are not helpful to beginners.
There is a HUGE amount of rubbish/dangerous C out there... Use the language with caution/respect. Use comments! Look at simple examples, do the tutorials, start simple. Very simple. Flash an LED or similar and then work your way up. Be wary of examples on the WWW - many are just BAD ! Don't cut-n-paste too much - try to understand absolutely what an example does and always, always, question why something is done a particular way and research the answer. This is simply the best way to learn a language - questioning, research & experimentation.
If you are learning programming and don't need C for performance or some other reason, try using a "safer" language like BASIC e.g. from OshonSoft or similar.
Registered Member #30
Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
cavemen wrote ...
How can the computer know the size of a[] ahead of time?
As others have pointed out, the compiler doesn't care, it leaves all the hard stuff up to you.
When you pass an array as an argument to a function, what actually gets passed is a pointer to the first element of it. No information about the size is passed in. When writing C I don't use the a[] syntax, I always just pass pointers explicitly to remind myself of this. It's so easy to accidentally go over array bounds and "smash the stack".
To be a really good C programmer, you probably had to start with assembler so you understand all the dirty work that's going on behind the scenes. Or rather, the lack of dirty work. If you started with Visual Basic, PHP, or whatever, you take for granted a lot of things that C simply doesn't do for you. (And C++ doesn't either, before you get your hopes up.)
Software PWM doesn't work as well as hardware PWM. The maximum frequency you can generate is limited, as is the duty cycle resolution, and unless you're a really good programmer, it can glitch as the processor gets interrupted to do other things.
If you need more channels, the PIC18F4431 has four channels of high-res hardware PWM, and internal logic for driving half-bridges and so on. It's intended for motor drives where it can drive a three-phase bridge and a DC link chopper. I used it to build a four-channel LED dimmer, which I had to do in assembler because I didn't have time to get the C compiler working.
PICs are kind of last century, everyone is using AVRs and ARMs now.
wrote ... Every communication protocol has to be declared in a .h file?
It's good practice to write your program in modules like this, yes. Microchip will have written what are essentially drivers for all of the on-chip hardware.
So if I want to use the PIC18F4431's Power Control PWM module from my C program, I #include <pcpwm.h> and that allows my program to access the functions that Microchip already wrote for me. You can open up pcpwm.h in a text editor and see what functions are available to you, and sometimes there are comments in the .h file to clarify what the functions do.
When designing large programs you should do the same, split them up into convenient modules with header files that you #include in your main program.
Registered Member #33
Joined: Sat Feb 04 2006, 01:31PM
Location: Norway
Posts: 971
Steve McConner wrote ...
PICs are kind of last century, everyone is using AVRs and ARMs now.
Actually, I would say it's opposite. AVRs used to be more powerful than PICs, but the PICs have caught up now with all the new series of chips. I don't see what large advantages there are to AVRs.
ARMs however, are immensely more powerful (at least compared to the 8-bit PICs and AVRs). But the most important thing to remember is to use the right tools for the job. You wouldn't use an 8-bit PIC to generate 3D graphics (unless you're doing it for the challenge), and you wouldn't use a 80MHz ARM to flash an LED.
Registered Member #2008
Joined: Tue Mar 03 2009, 05:11AM
Location: USA, Frederick, MD
Posts: 118
Thank you for all your answers. I am trying to use the right tool for the right job and that is why a PIC18 with a built-in USB and a pair of PWM outputs will do the job of controlling a CNC machine, driving a Tesla coil at a specified frequency and making small usb-controlled toys.
As I know the CNC machine is usually moving one motor at a time. The trick here is that PWM can be generated separately and the other output pins can rout it to a motor that is running. I am not sure if this will work.
So I had seen program examples in my book where it appeared that the size of the dimension s[i] was not specified anywhere. Not in the declarations and not anywhere else.
Page 30 of the "C programming language"
Void is used in functions without the return command, ones that place a value in the external variable?
What is Unary VS Binary? Unary addition and subtraction vs binary addition and subtraction. ]the_c_programming_language_-
_by_kernighan_and_ritchie_30.pdf[/file]
Registered Member #2099
Joined: Wed Apr 29 2009, 12:22AM
Location: Los Altos, California
Posts: 1716
What is Unary VS Binary? Unary addition and subtraction vs binary addition and subtraction.
Google can lead you to the answer quickly. [edit] sorry, maybe not so simple. I think Wikipedia has a very poorly written article about the subject. Better call it a night; I'm old & tired, rusty at C and don't want to give a wrong answer. K&R should define all actual arithmetic operators, even if it does not use the term "unary addition".
Registered Member #30
Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
The examples use arrays of size "MAXLINE" which is defined equal to 1000.
This is the kind of nasty old code that breeds "buffer overflow" bugs.
If you declare a function void, as in: void Myfunction() {...} then you can't use "return" inside it.
If you declare it of some type other than void, then you have to use "return" to send back something of that type.
For instance int main(void) {do stuff; return 0;}
Here is an example of how return values are used.
// First I define a function for squaring a number
float squared(float x)
{ return x*x; }
// Then I use it
float i;
i=2.0;
printf("%f squared is %f", i, squared(i));
The unary operators are ones that only take one argument and modify it. Like ++ to increment something.
i++ would be unary addition, it means increase the value of i by one.
i = j+k would be binary addition, because it has two arguments.
This is a specialized use of the word "binary" that has nothing to do with 1s and 0s. Another word I like from K&R is "Variadic"
Registered Member #2008
Joined: Tue Mar 03 2009, 05:11AM
Location: USA, Frederick, MD
Posts: 118
Thank you. That makes sense.
I have to tell that the way PWM is used in stepper motors is to make smteps more smooth. The 'more smooth' is probably going to require some research.
PWM is necessery on every step to make it smooth.
Smooth accel and decel is a different story and that is done by the code.
Every step of the stepper motor is acceleration and deceleration. step#1 and lest step produce acceleration near 100G according to my math. The system will absorb the impact, but it will wear the lead screw and the lead nut more.
There is a different story for Tesla Coil. But I won't be there e in a while.
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.