Gallery, Projects and General > Project Logs
X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
<< < (16/19) > >>
kwackers:
There's no point writing a project in assembler, that's for masochists. (and I've spend decades writing assembler for almost every known processor going)

Write your project in C. Then if it turns out to be too slow optimise it - usually rethinking your method is plenty, failing that have a look at the assembler the C compiler is kicking out and rewrite the slow parts then link them back (as assembler) into your C project (or inline using the asm keywords). Finally, PIC's are cheap! So just use a faster one!

PIC16's aren't particularly good targets for C compilers so you can generally optimise code for them quite well, 18's are much better targets and by the time you get to the 32's you'd struggle to beat a good C compilers optimisations anyway so would be better off optimising by changing the method.

Where the Mikro stuff scores over things like the lite version of the compiler that comes with MPLAB is the fairly large set of libraries. Need an LCD? Just make the library call, GLCD? Ditto. They provide libraries and example code for everything from pressing a button, through USB all the way to having a wireless network and serving web pages.
To be fair to use these libraries you'll probably run out of space on the free version and will need to buy the full version.
The downside is it's not the best compiler when it comes to producing optimised code, but as I mentioned above there are ways...
Bluechip:

--- Quote from: kwackers on September 11, 2010, 05:18:28 AM ---

Write your project in C.



--- End quote ---

Hi Kwackers

I don't know C, so it's a bit difficult to write it in C ...

I have never yet found any 'tutorial' or whatever that actually assumes you know nothing about it.

One has the usual 'flash a LED' stuff, gives some example code, but does not explain all of the damn symbols / directives etc.

Some can be inferred, others I've not a clue ...  :bang:

Void ??

Braces ??

// appears to be comment ?

Gave up  :(

Apologies to Chris for hi-jacking his thread  ..

Dave BC



kwackers:
If you really want to give it a go send me a PM with your questions and I'll answer them.

But some simple things to help:-

your program starts in the function 'main' so:

void main()

this means you have a function that returns no result (hence the void)

You could return an integer - eg

int fred() 

would be a function called fred that returns an integer (note that main never returns in a PIC so there's no point in it returning anything).

the braces simply collect code together so:

void main()
{
    // you're right this is a comment - until the next line

   // everything between the braces is part of main

   {
      // you can open as many braces as you like - it can help to keep bits together
      // but remember the closing ones
      // and indenting them makes the code look nice....
   }
}


Most directives have a '#' in front of them
the most common one is a string substitution, e.g.
#define A_VAR 23

whenever A_VAR appears then 23 is substituted, handy for defining registers and stuff you might want to change later.

Another is the #include directive, this simply takes the filename after it and inserts it into the file (as though you'd cut and paste the whole thing in)
It's most common use is for header files - usually a file full of #defines that are appropriate for the PIC you're using.

Hope that's of some help.

p.s Chris doesn't mind - I get the same questions off him all the time    :)
raynerd:
Bluechip - your not hijacking my thread...all good discussion is worth while! Kwackers taught me what I know ... he knows what he is talking about for sure!!

Download mikroC from the link I sent, it is the top download - MikroC compiler.

Kwacks is right with regards to the library - it is bloody fanstastic! Take me for example, a dumb arse when it comes to PICs: So OK, I can write a few lines of "if, else, while"...bla but I wouldn`t have a clue how to get the LCD to run. Well you just call on the LCD library, look at the example and change it suit your needs. If you scout about on the MikroC website there is a download containing examples for the 16F887 and if you open these up with MikroC you can pretty much see how things are working. So like you said there is an LCD example, a flashing LED, GLCD, TMR1, TRM0 example etc. Loads of them, they are really useful.

With regards to learning,... don`t give up it really is fairly easy. I`m sure Kwackers will quickly pick me up if I start spouting rubish but here is a simple code:

// <<-- these are comments so I`ll comment through it

void main()      // this is where the programme starts
{

// This is the PIC setup specifically for the 16F887

  ANSEL  = 0;            // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;          // Disable comparators
  C2ON_bit = 0;

  TRISA.b0 = 0x00;          // Bit 0 on PORTA - TRIS sets pins as output   TRIS 0XFF would be input
  PORTA.b0 = 0x00;          // initiates it at 0
  TRISA.b1 = 0xFF;          // Bit 1 on PORTA is an INPUT - lets say a button
  PORTA.b1 = 0x00;          // initiates it at 0
 
  while (1)                 // while (Statement) is true, it does everything between the braces -
                           //  1 is always true so this is an endless loop.
  {
     if (PORTA.b1)           // if we press the button
     {                      // do everything between these brackets
       PORTA = 0xFF;        // Turn ON LEDs on PORTA
       Delay_ms(1000);      // 1 second delay
       PORTA.b0 = 0x00;        // Turn OFF LEDs on PORTA
     } 
  }
}             

So this code is going to turn on the LED for 1 second each time you press the button.

Nice thing about C is you can make it a little more simple by using define - same code using define:


#define LED1 PORTA.b0   
#define button PORTA.b1

void main()      // this is where the programme starts
{

// This is the PIC setup specifically for the 16F887

  ANSEL  = 0;            // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;          // Disable comparators
  C2ON_bit = 0;

  TRISA.b0 = 0x00;          // Bit 0 on PORTA - TRIS sets pins as output   TRIS 0XFF would be input
  PORTA.b0 = 0x00;          // initiates it at 0
  TRISA.b1 = 0xFF;          // Bit 1 on PORTA is an INPUT - lets say a button
  PORTA.b1 = 0x00;          // initiates it at 0
 
  while (1)                 // while (Statement) is true, it does everything between the braces -
                            //  1 is always true so this is an endless loop.
  {
     if (button)            // if we press the button
     {                      // do everything between these brackets
       LED1 = 0xFF;         // Turn ON LED
       Delay_ms(1000);      // 1 second delay
       LED1 = 0x00;         // Turn OFF LED
     } 
  }
}


Well I hope I`ve not mucked up somewhere but if I can do it, I`m sure you certainly can!
Chris

raynerd:
:) we posted at the same time and after that effort didn`t want to delete my post. Ignore mine :D :wave:
Navigation
Message Index
Next page
Previous page

Go to full version