CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with FLOAT & IF-ELSE LOOP

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
RamEtrics



Joined: 19 Mar 2014
Posts: 4

View user's profile Send private message Send e-mail

Problem with FLOAT & IF-ELSE LOOP
PostPosted: Mon Mar 24, 2014 9:11 am     Reply with quote

Hi everybody, I am using PIC16F877A to drive an AC motor based on the input voltage to the motor. But my coding is not working correctly.
Please help me.
Code:

#if defined(__PCM__)
#include <16F877a.H>
#device adc=10
#fuses  NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
 
#define LCD_EN      PIN_E0
#define LCD_RS      PIN_E1
#define LCD_RW      PIN_E2
 
//LCD->R/S R/W EN D0 D1 D2 D3 D4 D5 D6 D7
//PIN->RE0 RE1 RE2 RD0 RD1 RD2 RD3 RD4 RD5 RD6 RD7
 
void lcd_write(char data);
void lcd_init();
void lcd_newline();
void clr_lcd();
 
 
void main()
{
    float voltsCurrentVal = 0.0, voltsPreviousVal = 0.0;
    int i;
    delay_ms(10);
    output_bit(LCD_RW,0);
 
    delay_ms(100);
    setup_adc_ports(AN0);
    setup_adc(ADC_CLOCK_DIV_8);
    set_adc_channel(0);
 
    lcd_init();
    delay_us(20);
 
    while(1)
    {
       
       
        voltsCurrentVal = read_adc();
   
        if(voltsPreviousVal!=voltaCurrentVal)
          {  voltsCurrentVal = (voltsCurrentVal * 5.0) / 1023.0;
 
            clr_lcd();
            printf(lcd_write,"\fVoltage = %3.2f", voltsCurrentVal);
 
    if(voltsCurrentVal==5)
            {
            for(i=0;i<10000;i++)
            {
              output_bit(pin_b1,1);   
              output_bit(pin_b2,0);
              delay_ms(20);
              output_bit(pin_b2,1);
              output_bit(pin_b1,0);
              delay_ms(20);
            }
            }
    else if(4.87<=voltsCurrentVal<5.00)
            {
            for(i=0;i<10000;i++)
            {
            output_bit(pin_b1,1);   
            output_bit(pin_b2,0);   
            delay_ms(20.5);
            output_bit(pin_b1,0);
            output_bit(pin_b2,1);
            delay_ms(20.5);
            }
            }
    else if(4.87>voltsCurrentVal>=4.67)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(21);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(21);
   }}
    else if(4.67>voltsCurrentVal>=4.46)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(21.5);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(21.5);
   }}
    else if(4.46>voltsCurrentVal>=4.35)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(23);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(23);
   }}
    else if(4.35>voltsCurrentVal>=4.25)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(23.5);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(23.5);
   }}
   else if(4.25>voltsCurrentVal>=4.14)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(24);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(24);
   }}
   else if(4.14>voltsCurrentVal>=3.93)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(24.5);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(24.5);
   }}
   else if(3.93>voltsCurrentVal>=3.83)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(26.04);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(26.04);
   }}
   else if(3.83>voltsCurrentVal>=3.72)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(26.7);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(26.7);
   }}
   else if(3.72>voltsCurrentVal>=3.62)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(27.5);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(27.5);
   }}
   else if(3.62>voltsCurrentVal>=3.52)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(28.4);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(28.4);
   }}
   else if(3.52>voltsCurrentVal>=3.41)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(29.14);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(29.14);
   }}
   else if(3.41>voltsCurrentVal>=3.31)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(30);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(30);
   }}
   else if(3.31>voltsCurrentVal>=3.20)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(30.9);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(30.9);
   }}
   else if(3.20>voltsCurrentVal>=3.10)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(32.33);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(32.33);
   }}
   else if(3.10>voltsCurrentVal>=3)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(33.33);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(33.33);
   }}
   else if(3>voltsCurrentVal>=2.89)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(34.5);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(34.5);
   }}
   else if(2.89>voltsCurrentVal>=2.79)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(35.77);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(35.77);
   }}
   else if(2.79>voltsCurrentVal>=2.68)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(36.97);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(36.97);
   }}
   else if(2.68>voltsCurrentVal>=2.58)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(38.61);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(38.61);
   }}
   else if(2.58>voltsCurrentVal>=1.45)
   {
   for(i=0;i<=10000;i++)
   {output_bit(pin_b1,1);
   output_bit(pin_b2,0);
   delay_ms(40);
   output_bit(pin_b1,0);
   output_bit(pin_b2,1);
   delay_ms(40);
   }}
   else
   {
   output_bit(pin_b1,0);
   output_bit(pin_b2,0);
   }
          }
 
        delay_ms(1000);
 
        output_bit(LCD_RW,0);
 
    }
}
 
 
 
void lcd_init()
{
    output_bit(LCD_RS,0);
    delay_ms(10);
    lcd_write(0x01);
    lcd_write(0x38);
    lcd_write(0x06);
    lcd_write(0x0f);
    lcd_write(0x80);
    delay_ms(100);
    output_bit(LCD_RS,1);
}
 
void clr_lcd()
{
    output_bit(LCD_RS,0);
    delay_ms(10);
    lcd_write(0x01);
    output_bit(LCD_RS,1);
    delay_ms(1);
}
 
void lcd_newline()
{
    output_bit(LCD_RS,0);
    delay_ms(10);
    lcd_write(0xc0);
    output_bit(LCD_RS,1);
    delay_ms(1);
}
 
void lcd_write(char data)
{
    output_d(data);
    delay_ms(1);
    output_bit(LCD_EN,1);
    delay_ms(1);
    output_bit(LCD_EN,0);
    delay_ms(1);
}
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Mar 24, 2014 9:39 am     Reply with quote

here's one possible reason....

if(voltsPreviousVal!=voltaCurrentVal)

pretty sure you meant =voltsCurrentVal


I didn't look anymore after that...
change code/compile/report back
hth
jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 24, 2014 9:48 am     Reply with quote

Quote:
int i;

for(i=0;i<10000;i++)

Learn CCS data types for your compiler.

Download the manual for the PCM compiler:
https://www.ccsinfo.com/downloads/ccs_c_manual.pdf

Read the section titled: Basic and Special types
What does it say is the size of an 'int' ? Then look at your code above.

Also read this line in that section:
Quote:
unsigned -
Data is always positive. This is the default data type if not specified.


These comments are for the PCB, PCM and PCH compiler. PCD is different.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 24, 2014 9:50 am     Reply with quote

Quote:
delay_ms(33.33);

Again, read the manual. Don't code anymore until you read the CCS
manual for every function that you're going to use. Currently, you're
not reading the manual then doing what's called "debugging by forum".
Hint: We get impatient with this, especially if you don't read the manual.
Battery David



Joined: 01 Feb 2010
Posts: 25

View user's profile Send private message

PostPosted: Tue Mar 25, 2014 10:58 am     Reply with quote

There's also a MISRA rule that you should never use '==' with floats. If you want to check if it's 5.0, use:

Code:
if(( voltsPreviousVal > 4.95 ) && (voltsPreviousVal < 5.05))
{
    // Do some really cool things
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group