View previous topic :: View next topic |
Author |
Message |
uri
Joined: 24 Jan 2014 Posts: 11
|
My First PIC project 12f family ABC... |
Posted: Fri Jan 24, 2014 2:16 am |
|
|
I'm sorry I lied! I don't know my ABC's. I think I don't know how to combine two applications for the following c file. When I checked the output pins only PIN_A1 in the void main application reads high, the rest read low despite the void fan application that comes after... Thank you, pls help.
Code: |
#include <12F683.h>
#device adc=10
#use delay(clock=8000000)
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO
#FUSES NOCPD //No EE protection
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOMCLR //Master Clear pin enabled / disabled
#FUSES PUT //Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
//--- set up PWM and timer
//--- see spreadsheet. At 8Mhz is 16khz with resolution of 500 steps...
void main() {
setup_oscillator(OSC_8MHZ);
output_low(PIN_A1);
while (TRUE) {
delay_us(22);
output_toggle(PIN_A1);
}
}
void fan()
{
long value=0;
setup_adc_ports(sAN3|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_16);
set_adc_channel(3); //read channel 3
delay_us(10);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_4,24,1); //24+1*4 resoluzion pwm
setup_ccp1(CCP_PWM);
setup_comparator(NC_NC);
setup_vref(FALSE);
setup_oscillator(OSC_8MHZ);
// TODO: USER CODE!!
while (true)
{
value=(read_adc()*25)/256; // 10 bits
delay_ms(500);
set_pwm1_duty(value); //value must be long
delay_ms(500);
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Fri Jan 24, 2014 3:20 am |
|
|
fan is never called, so how is it ever going to happen?...
You need (minimum changes only):
Code: |
void fan(); //A function _must_ be declared, or a prototype declared
//before it can be called. This is the prototype
void main() {
setup_oscillator(OSC_8MHZ);
fan(); //call the function
//You will never get here, since the function never exits.
output_low(PIN_A1);
while (TRUE) {
delay_us(22);
output_toggle(PIN_A1);
}
}
|
If you want the rest of the main code to execute, then the function needs to exit. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Jan 24, 2014 3:30 am |
|
|
As I see it the code never reaches void fan().
Both main() and fan() contain forever loops, so if you get into either you can never reach the other.
Please explain more clearly exactly what you are trying to achieve
Mike
Ttelmah got in as I typed. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Fri Jan 24, 2014 3:34 am |
|
|
Beat you to it!....
Best Wishes |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Jan 24, 2014 3:40 am |
|
|
Only 'cos I was doing the dishes!.....
Mike |
|
|
uri
Joined: 24 Jan 2014 Posts: 11
|
|
Posted: Fri Jan 24, 2014 11:50 am |
|
|
Thats AWESOME!! I am sure very thankful to everyone who helped me out. You made me sooo happy I decided to try asking for help.
I'm going to try it first thing. |
|
|
uri
Joined: 24 Jan 2014 Posts: 11
|
|
Posted: Fri Jan 24, 2014 6:06 pm |
|
|
The way I used suggestion from Ttelmah rendered output_toggle(PIN_A1); always off. Here is my updated code and below that I add my 12f683.h file just in case i messed up there too.
Code: |
#include <12F683.h>
#device adc=10
#use delay(clock=4000000)
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO
#FUSES NOCPD //No EE protection
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOMCLR //Master Clear pin enabled / disabled
#FUSES PUT //Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
//--- set up PWM and timer
//--- see spreadsheet. At 8Mhz is 16khz with resolution of 500 steps...
// void fan(); //A function _must_ be declared, or a prototype declared
//before it can be called. This is the prototype
void fan(); //A function _must_ be declared, or a prototype declared
//before it can be called. This is the prototype
void main() {
setup_oscillator(OSC_4MHZ);
fan(); //call the function
//You will never get here, since the function never exits.
output_low(PIN_A1);
while (TRUE) {
delay_us(22);
output_toggle(PIN_A1);
}
}
void fan()
{
long value=0;
setup_adc_ports(sAN3|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_16);
set_adc_channel(3); //read channel 3
delay_us(10);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_4,24,1); //24+1*4 resolution pwm
setup_ccp1(CCP_PWM);
setup_comparator(NC_NC);
setup_vref(FALSE);
setup_oscillator(OSC_4MHZ);
// TODO: USER CODE!!
while (true)
{
value=(read_adc()*25)/256; // 10 bits
delay_ms(500);
set_pwm1_duty(value); //value must be long
delay_ms(500);
}
} |
Begin 12F683.h file:
Code: |
//////// Standard Header file for the PIC12F683 device ////////////////
#device PIC12F683 |
++++++++++++++++++++
Header file deleted.
Reason: Forum rule #10
10. Don't post the CCS example code or drivers, or ask for such code and drivers.
This includes header files for devices (example 18f452.h).
Forum rules:
http://www.ccsinfo.com/forum/viewtopic.php?t=26245
- Forum Moderator
++++++++++++++++++++ |
|
|
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Sat Jan 25, 2014 12:31 am |
|
|
Mike Walne wrote: | Both main() and fan() contain forever loops, so if you get into either you can never reach the other. |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Sat Jan 25, 2014 1:52 am |
|
|
and as I said in the comment in the code:
"//You will never get here, since the function never exits. "
Key throughout, is things only happen, when the code sequence reaches them...
Best Wishes |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Jan 25, 2014 3:32 am |
|
|
Mike Walne wrote: | Please explain more clearly exactly what you are trying to achieve |
It's not obvious what you may be wanting to do.
Yes, you said you were merging two pieces of code.
Is this close?
a) Toggle PIN_A1 @ 22us intervals. (i.e. 22us ON, 22us OFF.)
b) Read ADC and update PWM once per second. (With 500ms between each action.)
c) Do both a & b together.
Mike
PS. Everyone with a licence has the header and example files. |
|
|
uri
Joined: 24 Jan 2014 Posts: 11
|
|
Posted: Sat Jan 25, 2014 5:25 am |
|
|
I'm wanting to do exactly what you suggested:
a) Toggle PIN_A1 @ 22us intervals. (i.e. 22us ON, 22us OFF.)
b) Read ADC and update PWM once per second. (With 500ms between each action.)
c) Do both a & b together. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Sat Jan 25, 2014 9:29 am |
|
|
Maybe think of a code 'tick'. Unless timing accuracy is very important, something like:
Code: |
#include <12F683.h>
#device adc=10
#use delay(clock=8000000)
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO
#FUSES NOCPD //No EE protection
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOMCLR //Master Clear pin enabled / disabled
#FUSES PUT //Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor enabled
void main() {
int8 counter;
int1 state=0;
int16 value;
setup_oscillator(OSC_8MHZ);
setup_adc_ports(sAN3|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_16);
set_adc_channel(3); //read channel 3
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_4,24,1); //24+1*4 resolution pwm
setup_ccp1(CCP_PWM);
setup_comparator(NC_NC);
setup_vref(FALSE);
output_low(PIN_A1);
counter=22;
while (TRUE) {
delay_us(22);
output_toggle(PIN_A1);
if (counter--==0)
{
counter=22;
if (state==0)
{
state=1;
value=(read_adc()*25)/256; // 10 bits
}
else
{
state=0;
set_pwm1_duty(value);
}
}
}
}
|
Now there will be a slight 'hiccup' in the output_toggle, every 23rd cycle (as it does the other 'task', particularly on the one where if performs the maths, but this will toggle the pin 23 times, then perform the adc read, then another 23 times, and update the PWM.
Unless you have an external clock, you cant use FCMEN, or IESO.
Best Wishes |
|
|
uri
Joined: 24 Jan 2014 Posts: 11
|
|
Posted: Sat Jan 25, 2014 11:23 am |
|
|
Thank you. My assigned partner returned home for family emergency leaving me with the whole project that counts for 5% of our grade. Plus I am learning how to do this now. I can't wait to see the pwm working Monday. THANK YOU!! |
|
|
uri
Joined: 24 Jan 2014 Posts: 11
|
|
Posted: Sun Jan 26, 2014 7:13 pm |
|
|
about this function: setup_ccp1(CCP_PWM);
How would I change the pwm output to GP5/T1CK1/OSC1 pin in order to free up the GP2/AN2/T0CK1/COUT/CCP1 for input from LM35 temperature sensor? I'm afraid this code is starting to look hopeless for our purpose. PWM fan control based on temperature as mV ADC input from silicon LM35.
I am including the schematic for our project in case I haven't already...
Wait, I don't see a way to upload the schematic pdf here. Maybe there is enough information in the previous c file though.
Also, my basic description of the 12f pin routes:
GP5/T1CK1/A5 > R330 ohm > logic level mosfet > fan motor
GP2/AN2/A2 > R330 > LED > LM35 GND pin
GP4/AN3/A4 > LM35 VOUT pin
I'm so sorry I am still new at this but that is why I am enrolled in school.
I'm extremely thankful for the other people on the forum helping me up. |
|
|
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Sun Jan 26, 2014 8:28 pm |
|
|
You won't.
Datasheet.
CCP output can't be reassigned. Table 1 lists pin functions. |
|
|
|