Thread: My microcontroller cant do maths!

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    22

    My microcontroller cant do maths!

    Heres my code:

    Code:
    #include <htc.h>
    #include <stdint.h>
    
    #define _XTAL_FREQ 8000000
    
    __CONFIG (FOSC_XT & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_OFF);
    
    void initDigitalOut() {
    	PORTA = PORTB = PORTC = PORTD = 0;
    	TRISA = TRISB = TRISC = TRISD = 0;
    	ANSEL = ANSELH = 0;
    }
    
    unsigned int math() {
    	float revolutionTime_S = 3000/31250;
    	float horizontalDelay_US = revolutionTime_S/276;
    	float horizontalDelay_US = horizontalDelay_US*1000000;
    	return (unsigned int) horizontalDelay_US;
    }
    
    void main() {
    	initDigitalOut();
    	while(1) {
    	PORTB = math();
    	PORTD = math() >> 8;
    	}
    }
    I seperated horizontal delay into 2 sections because revolutionTime_S ranges from about 0.032 to 2, and I dont think float would like to deal with numbers like 2,000,000? Anyway, I get no output on the ports at all. When I return a constant of say 350, the ports light up nicely. The value the function should be outputting ranges from about 350 to about 7500. All within the range of a 16 bit number. Can anyone shed light onto what's happening here?

    Thanks!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > float revolutionTime_S = 3000/31250;
    This will always evaluate to 0, since the division is done in integer math.

    Try
    float revolutionTime_S = 3000.0/31250.0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Or like this
    Code:
    float revolutionTime_S = (float) 3000/31250;

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    22
    you sir have just given me the solution to a much bigger problem. Thanks alot!!!!

    You both*

    Code:
    float revolutionTime_S = 3000/31250;
    "This will always evaluate to 0, since the division is done in integer math."

    Salem, would you care to explain what is going on in this statement?
    Last edited by dannybeckett; 03-09-2011 at 09:56 AM.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    The numbers 3000 and 31250 both look like integers, so the compiler treats them as such and performs an integer division, which truncates any fractional part and has an integer result. When you use 3000.0 and 31250.0 instead, both numbers look like floating point types. As long as at least one operand of a division is a floating point type, the compiler does floating point division.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    One way'd be to scale the output so the fractional part is insignificant:
    Code:
    unsigned int math()
    {
        return ((3000 * 1000000) / (276 * 31250));
    }

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    22
    And since 3000/31250 is all fractional, the output is 0. I was getting confused thinking that any integer maths when assigned to a float would come out as 0. Thanks for the help everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maths knowledge
    By progcomputeach in forum General Discussions
    Replies: 28
    Last Post: 08-03-2010, 06:45 AM
  2. Replies: 11
    Last Post: 02-18-2009, 06:10 AM
  3. Replies: 27
    Last Post: 10-25-2007, 10:47 AM
  4. maths???
    By nerdyneo in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2003, 01:04 PM
  5. C for microcontroller
    By phernyt in forum C Programming
    Replies: 2
    Last Post: 12-08-2001, 01:01 PM