Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

question: 2 to the power of ?

Status
Not open for further replies.

jrz126

Active Member
I've got my first assignment due on tues for my microcontrollers class and Im having a hell of time with it...
the question: Create a program using do-while loop, the program will print the power of 2 from 2^0 to 2^12 on the PC screen. The printed strings should be like "2 to the power of xx = xxxx \n"

I've got the serial comm. figured out, but I cant figure out how to do the power function. here's what I thought I could use:
Code:
#include <16F877.h>
#use delay(clock=10000000) //External clock 10MHz
#fuses HS, NOWDT //High speed, no watchdog timer
#use rs232(baud=9600, xmit=PIN_c6, rcv=PIN_C7)
#include <stdio.h>
#include <math.h>
void main()
{
float i=0;
float newval;
do
{
   newval=pow(2,i);
   printf("2 to the power of %f = %f \n",i,newval);
   i=i+1;
}   while (i<12);

}

but it gives me an error about it being too big for the ROM.

Also, are there any good websites with C-based pic projects? Seems like everybody uses assembly...well everbody except my professor
 
Man does C for the PIC look funny :lol:

When are you getting the error, at compile-time or when you try to program the HEX to the PIC?

Why are you using a do {} while loop? There's no "for()" operator in PIC16 C? I'm clueless about C for the PIC, sorry if it's a dumb question.

Minor point : i = i + 1 is really a BASIC form of what we usually write in C as "++i", or "i++". You probably wouldn't lose points for that, but you won't get any bonus either ;)

Are you sure your professor expects you to use the math.h pow() function? If the goal is to write a pow() function, maybe you have to implement your own?

Edit : Sorry, now that I read your post correctly, your homework is on do {} while loops, not on implementing your own pow() :oops:
 
I've just compiled your code using CCS compiler without any problems. ( I slightly modified the parts of the file to run with the CCS but that aside your code was virtually left untouched)
And the code also looks right to me.

What compiler are you using?
Were there any compiler errors when you tried Jay's and Joel's suggestions?
 
Jay.slovak said:
Can't you just multiply it by two in each step? (or in Assembly just rotate to left).

:oops: thats the exact solution to my problem...I guess I was thinking it had to be complicated...

I was using floats because the compiler was giving me errors about using ints for the pow command... same with the i+1, I had i++ in there but it didnt like that.
 
Status
Not open for further replies.

Latest threads

Back
Top