View previous topic :: View next topic |
Author |
Message |
chrismdube
Joined: 30 Dec 2011 Posts: 20 Location: UK
|
I/O C_ code help |
Posted: Fri Dec 30, 2011 3:08 pm |
|
|
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
|
|
Posted: Fri Dec 30, 2011 3:20 pm |
|
|
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: 1941 Location: Norman, OK
|
|
Posted: Fri Dec 30, 2011 3:31 pm |
|
|
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...
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
|
I/O C_ code help |
Posted: Fri Dec 30, 2011 4:33 pm |
|
|
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: 1941 Location: Norman, OK
|
|
Posted: Fri Dec 30, 2011 4:40 pm |
|
|
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
|
I/O C_ code help |
Posted: Fri Dec 30, 2011 5:08 pm |
|
|
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
|
Re: I/O C_ code help |
Posted: Sat Dec 31, 2011 10:20 am |
|
|
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
|
I/O C_ code help |
Posted: Sat Dec 31, 2011 12:27 pm |
|
|
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: 1941 Location: Norman, OK
|
|
Posted: Sat Dec 31, 2011 1:00 pm |
|
|
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
|
I/O C_ code help |
Posted: Sat Dec 31, 2011 1:54 pm |
|
|
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
_________________ trojan07 |
|
|
|