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

sleep mode and pwm on 18f252

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



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

sleep mode and pwm on 18f252
PostPosted: Wed Mar 18, 2009 12:21 pm     Reply with quote

Hi

I make a little experiences and I like use sleep mode and pwm...

1º Sleep mode:

-on this chip (18F252) I can use a normal crystal with 20Mhz?

-for I can enter in sleep mode I only call the function sleep() but how I can wake up it? with an switch?

-if I need an switch where I connect them?

2ª PWM

I like add on my program some way for when I call for example flashled() put one led flash +-(500ms/500ms) and continue use program and led only stop when I give an order or something...

my start program configuration (missing pwm configuration, and sleep)
Code:

#include <18F252.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,BROWNOUT,STVREN
#use delay(clock=20000000)
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


void main()
{
       while (TRUE)
             {
             }

}

some one can help me make this two examples?

kind regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 18, 2009 4:35 pm     Reply with quote

Quote:

for I can enter in sleep mode I only call the function sleep() but how I
can wake up it? with an switch?

Download the 18F252 data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/39564c.pdf
Read these sections:
Quote:

19.3.1 WAKE-UP FROM SLEEP
19.3.2 WAKE-UP USING INTERRUPTS
3. Interrupt from INT pin, RB port change or a
Peripheral Interrupt.


Quote:

I like add on my program some way for when I call for example
flashled() put one led flash +-(500ms/500ms) and continue use
program and led only stop when I give an order or something...

This thread has sample code. It's possible that this code is too
complicated for you to use.
http://www.ccsinfo.com/forum/viewtopic.php?t=35429
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Wed Mar 18, 2009 7:15 pm     Reply with quote

hi

so for WAKE-UP is better an switch and make reset... thanks

but question about PWM I don´t understand

if I active PWM with some frequency like an CCS example:

Quote:

// For a 20 MHz clock, 1.2 kHz frequency,
duty = 512; // .000416/(16*(1/20000000))
set_pwm1_duty(duty);


1ª- if I have an led on pin CCP1 it flash with 1.2khz correct?
2ª- but if I go to other function make other task but don't change "set_pwm1_duty(duty)" led continue flashing correct?
3ª- led only stop flash if I make some like that:

Quote:

duty=0;
set_pwm1_duty(duty);


if this 3 question stay correct I have a little problem... I don´t know on my program write the setup pwm (in code) for when I call set_pwm1_duty() program know what I stay call, I don´t understand this part.

regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 18, 2009 9:32 pm     Reply with quote

Quote:
I like add on my program some way for when I call for example flashled() put one led flash +-(500ms/500ms

Here, you said you wanted 500 ms on, and 500 ms off (1 Hz flashing rate).

Quote:
if I have an led on pin CCP1 it flash with 1.2khz correct?

Here, you change this to 1200 Hz. Why ? You will not see any flashing
at 1200 Hz. The human eye cannot detect 1200 Hz. You will see an
LED that is dim or bright, depending on the duty cycle.


What do you really want ?
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu Mar 19, 2009 2:58 pm     Reply with quote

this is an example... I copy direct from CCS help

I need (500ms on/500ms off) +-
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Mar 19, 2009 3:34 pm     Reply with quote

It's no good example, cause 1200 Hz pwm frequency works, 1 Hz doesn't. You'll easily see why.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 19, 2009 3:47 pm     Reply with quote

If you need an independent flashing routine, then create a Timer0
interrupt routine. Setup Timer0 to interrupt at some suitable low
rate, such as 20 Hz (50 ms). An interrupt rate of 2 Hz (500 ms) is
not possible, with your 20 MHz oscillator. Then put a static counter
variable inside the Timer0 interrupt routine. Initialize it to 0. Then
once every interrupt, increment the variable. When it reaches a
count of 10, then you have delayed for 500 ms (10 x 50 ms).
Toggle the LED at that time. Then reset the static variable to 0.
Initialize the static variable to 0 at the start of main(). Also set the
LED pin to output a low level at the start of main().

Also test a global flag inside the Timer0 interrupt routine. If the
flag variable is set to FALSE, then skip the toggling of the LED.
Instead, turn it off. If the global flag is true, then do the toggle.
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu Mar 19, 2009 4:52 pm     Reply with quote

but how I can configure timer0, for have 20hz?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 19, 2009 4:55 pm     Reply with quote

Why don't you try to write the code ? You could learn something.
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Wed Mar 25, 2009 3:42 pm     Reply with quote

hi

I have a extract this little example, make with wizard option, but I don't understand how it work...

is example is for I control one DC engine.

in my case what I need to configure PWM?

Quote:

void frente()
{
printf("Frente\n\r");

set_pwm1_duty(1000);
set_pwm2_duty(1000);
}

void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,255,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_oscillator(False);
setup_ccp1 (CCP_PWM);
setup_ccp2 (CCP_PWM);
}
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu Mar 26, 2009 10:36 am     Reply with quote

Hi

on wizard have this options:



there tell which information stay on timer2, someone can tell me how I can make calculation there "setup_timer_2(T2_DIV_BY_16,255,1); " for have +- 500ms/500ms?

kind regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 26, 2009 11:02 am     Reply with quote

The lowest frequency for hardware PWM is 244 Hz (with 4 MHz crystal).
You can not get 1 Hz rate with hardware PWM. The Wizard can not help you.

You must use a software method. I described it in my earlier post.
But, you don't want to write any code. You only want "Wizard".
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