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 support@ccsinfo.com

I/O C_ code help

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



Joined: 30 Dec 2011
Posts: 20
Location: UK

View user's profile Send private message

I/O C_ code help
PostPosted: Fri Dec 30, 2011 3:08 pm     Reply with quote

Hi all. I am a novice to both PICs and C coding. I have this code which is meant to drive the PWM module for 50Hz output H drive as well as control inputs and outputs. It compiles fine but I get warning to say the if/else code has no effect. Can someone please confirm if there is something missing and whether my coding is up to scratch. Thanks in anticipation.
Code:

#include "16f690.h"

void main()
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PUT                      //Power Up Timer
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOBROWNOUT               //No brownout reset

#use delay(int=31000)

#use FIXED_IO( B_outputs=PIN_B5 )
#define charge_control   PIN_B5   //output to control battery charging transistor switch
#define mains_detect   PIN_C3     //input to signal mains power failure
#define low_charge   PIN_C6       //input to signal deep battery discharge
#define max_charge   PIN_C7       //input to signal maximum  battery charge level

//{while(1)

{while(PIN_C6&&!PIN_C3)
{
   setup_timer_2(T2_DIV_BY_1,154,1);     
setup_ccp1(CCP_PWM|CCP_PWM_FULL_BRIDGE|CCP_SHUTDOWN_AC_H|CCP_SHUTDOWN_BD_L);
   set_pwm1_duty((int16)0);
   setup_comparator(NC_NC_NC_NC);
{
 
   
if(!PIN_C7) 
  PIN_B5==1;
      else PIN_B5==0;     
   }

}

}

_________________
trojan07
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Dec 30, 2011 3:20 pm     Reply with quote

if(!PIN_C7)

PIN_C7 is likely the address of the pin. The address will be non-zero so always evaluate as true.

If you want the logic state of the pin you must use the input() function.
_________________
The search for better is endless. Instead simply find very good and get the job done.
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Dec 30, 2011 3:31 pm     Reply with quote

There are a number of things wrong here:

1. the fuses should not be inside main()

2. What are you trying to accomplish here?
Quote:
while(PIN_C6&&!PIN_C3)


3. What do two equals mean...
Quote:
PIN_B5==1;


4. You actually have a 31KHZ clock?
Quote:
#use delay(int=31000)


5. This section is also a problem and will create a syntax error:
Quote:
//{while(1)

{while(PIN_C6&&!PIN_C3)
{
setup_timer_2(T2_DIV_BY_1,154,1);


6.
Quote:
if(!PIN_C7)
PIN_B5==1;
else PIN_B5==0;
}


needs to be:
Code:
           if (!input(pin_c7))
                 output_high(pin_b5);
           else
                 output_low(pin_b5);

_________________
Google and Forum Search are some of your best tools!!!!


Last edited by dyeatman on Fri Dec 30, 2011 4:47 pm; edited 2 times in total
chrismdube



Joined: 30 Dec 2011
Posts: 20
Location: UK

View user's profile Send private message

I/O C_ code help
PostPosted: Fri Dec 30, 2011 4:33 pm     Reply with quote

Thanks for the quick response.
while(PIN_C6 && !PIN_C3): If pin c6 is high and pin_C3 low, PWM should run.

PIN_B5==1: Ouput B_5 high/true.

CLOCK speed is actually higher....8MHz internal.

All the I/Os have been configured either as inputs or output using the project wizard. I also think I need to set initial I/O states but can't figure out the trick.

Once again thanks for the response!
_________________
trojan07
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Dec 30, 2011 4:40 pm     Reply with quote

OK,
let's start here:
while(PIN_C6 && !PIN_C3))

Should be
while((input(PIN_C6)) && (!input(PIN_C3))

Next:
B==1 is a test for equality typically used after an if statement, not
how you set an output bit
To set an output high requires:
Output_high(pin_b5)

and to set an output low:
Output_low(pin_b5)

For port settings I would recommend getting rid of the FIXED_IO
and let the compiler handle it.

I also added another item to my previous list
_________________
Google and Forum Search are some of your best tools!!!!
chrismdube



Joined: 30 Dec 2011
Posts: 20
Location: UK

View user's profile Send private message

I/O C_ code help
PostPosted: Fri Dec 30, 2011 5:08 pm     Reply with quote

Thank you again. I have made the changes, if(!input(PIN_C7)) still returns "conditions always true." The other lines seem ok now, thanks loads. Would you say my while statement is ok to initiate the PMW?
Thanks for your patience.
_________________
trojan07
flint



Joined: 05 Jun 2010
Posts: 24
Location: Nigeria

View user's profile Send private message

Re: I/O C_ code help
PostPosted: Sat Dec 31, 2011 10:20 am     Reply with quote

chrismdube wrote:
Thank you again. I have made the changes, if(!input(PIN_C7)) still returns "conditions always true."

instead of this
Code:

if(!PIN_C7)
  PIN_B5==1;
      else PIN_B5==0;

try this:
Code:

//Note: by default, digital pins are usually of logic 1 (theoretically)
if(!input(PIN_C7))  // test if Port C7 is of logic 0 or low
{
  output_high(PIN_B5);  // turns port B5 high, if condition is met
}
else
{
   output_low(PIN_B5);  // turns port B5 low, if condition is not met
}

Best regards.
chrismdube



Joined: 30 Dec 2011
Posts: 20
Location: UK

View user's profile Send private message

I/O C_ code help
PostPosted: Sat Dec 31, 2011 12:27 pm     Reply with quote

Thank you all. I have since managed to get my 'if' statement to compile successfully after restarting my CCS compiler. My problem is with the the while condition being able to initiate the PWM module.
Code:

void main()
{
while((input(PIN_C6)) && (!input(PIN_C3))
 {  setup_timer_2(T2_DIV_BY_1,154,1);     
 setup_ccp1(CCP_PWM|CCP_PWM_FULL_BRIDGE|CCP_SHUTDOWN_AC_H|CCP_SHUTDOWN_BD_L);
   set_pwm1_duty((int16)0);
   setup_comparator(NC_NC_NC_NC);
 }
}

For some reason (which I'm daft enough not to understand), the compiler says "expecting close paren" just before the opening { for setup_timer_2. Should I make this a function then? Please advise.
_________________
trojan07
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat Dec 31, 2011 1:00 pm     Reply with quote

You are short one close parentheses, use the line below.

Code:
while((input(PIN_C6)) && (!input(PIN_C3)))


You can probably ignore the Condition always True warning since it
likely is coming from a previous line.
_________________
Google and Forum Search are some of your best tools!!!!
chrismdube



Joined: 30 Dec 2011
Posts: 20
Location: UK

View user's profile Send private message

I/O C_ code help
PostPosted: Sat Dec 31, 2011 1:54 pm     Reply with quote

My! Thank you all!! I'm over the moon! I made all the recommendations (dyetman.../flint) and compiled fine, no error no warning.
I guess I have loads to learn. Any literature recommendation is welcome too.

This platform is my new home now, I just love it! Happy New Year all
Very Happy
_________________
trojan07
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