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

PWM on 12f675

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







PWM on 12f675
PostPosted: Fri Aug 08, 2003 1:32 pm     Reply with quote

I am trying to use timer0 and timer1 interrupts to generate a PWM signal on pin_A2. I used 3.169 and the wizard. My non working code follows:

#include <12F675.h>
#device adc=10
#use delay(clock=4000000)
#fuses INTRC, MCLR, BROWNOUT, NOWDT

#rom 0x3FF = {0x3470}
#define PWMout PIN_A2
#byte ANSEL=0x9f

int16 Pwidth;

#define Pwidth 35536

#int_TIMER1
void TIMER1_isr(void) {
Output_low(PWMout);
disable_interrupts(INT_TIMER1);
}

#int_TIMER0
void TIMER0_isr(void) {
Output_high(PWMout);
SET_TIMER1(Pwidth);
enable_interrupts(INT_TIMER1);
}

main()
{
ANSEL=11;
setup_adc_ports(AN1_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_counters(RTCC_INTERNAL,RTCC_DIV_64);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER0);
enable_interrupts(global);
SET_TIMER0(0);
while(true);
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516840
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: PWM on 12f675
PostPosted: Fri Aug 08, 2003 1:56 pm     Reply with quote

:=I am trying to use timer0 and timer1 interrupts to generate a PWM signal on pin_A2. I used 3.169 and the wizard. My non working code follows:
:=
:=#include <12F675.h>
:=#device adc=10
:=#use delay(clock=4000000)
:=#fuses INTRC, MCLR, BROWNOUT, NOWDT
:=
:=#rom 0x3FF = {0x3470}
:=#define PWMout PIN_A2
:=#byte ANSEL=0x9f
:=
:=int16 Pwidth;
:=
:=#define Pwidth 35536
:=
:=#int_TIMER1
:=void TIMER1_isr(void) {
:= Output_low(PWMout);
:= disable_interrupts(INT_TIMER1);
:= }
:=
:=#int_TIMER0
:=void TIMER0_isr(void) {
:= Output_high(PWMout);
:= SET_TIMER1(Pwidth);
:= enable_interrupts(INT_TIMER1);
:= }
:=
:=main()
:={
:= ANSEL=11;
:= setup_adc_ports(AN1_ANALOG);
:= setup_adc(ADC_CLOCK_INTERNAL);
:= setup_counters(RTCC_INTERNAL,RTCC_DIV_64);
:= setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
:= setup_comparator(NC_NC_NC_NC);
:= setup_vref(FALSE);
:= enable_interrupts(INT_TIMER0);
:= enable_interrupts(global);
:= SET_TIMER0(0);
:= while(true);



I perform PWM for motor control using this routine and only use the timer to regulate the loop period. I can assign values for on and off time. This is based on a loop that runs at a fixed speed. The bit test can be performed on a lesser bit to make it run faster. You could also run a variation of this code inside an interupt.

if(bit_test (PWM_Counter_lo++, 7))
{ PWM_Counter_lo = 0;
PWM_Counter++;
}
if(PWM_Cycle)
{ if(PWM_Counter > on_time)
{ PWM_Counter = 0;
PWM_Cycle = 0;
}
}
else
{ if(PWM_Counter > off_time)
{ PWM_Counter = 0;
PWM_Cycle = 1;
}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516843
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