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

need help with PWM,Interrupt and Timer0 coding in C language

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



Joined: 03 Mar 2009
Posts: 1

View user's profile Send private message

need help with PWM,Interrupt and Timer0 coding in C language
PostPosted: Tue Mar 03, 2009 12:40 pm     Reply with quote

I want to produce a frequency (I use PWM) for a certain period of time(timer0), then interrupt-on-overflow to stop the frequency for another period of time. So I should have tones play at certain interval. Below is my attempt on coding for that purpose and it isn't working...Please help me find where has gone wrong. Thanks !
Code:

/** I N C L U D E S **************************************************/
#include "p18f46k20.h"
#include "delays.h"
#include "12 CCP PWM.h"  // header file
#include "08 Interrupts.h"

/** V A R I A B L E S *************************************************/
#pragma udata   // declare statically allocated initialized variables


/** D E C L A R A T I O N S *******************************************/
void timer0(void);

#pragma code                            // declare executable instructions

void main (void)
{

 
   // set internal oscillator to 1MHz   
    //OSCCON = 0b10110110;                  // IRCFx = 101
    //OSCTUNEbits.PLLEN = 0;                //PLL disabled


//PWM settings for a frequency

   TRISDbits.TRISD7 = 0;            //PWM-pin ON
    T2CON  = 0b00000101;            //timer2 on
   PR2    = 240;
   CCPR1L = 0x78;
   CCP1CON= 0b01001100;
   
timer0();

while(1);
}


//---------------------------------------------------------------------------
void timer0(void)
{

T0CON=0b10100010;         //enable timer0
                     //timer is configured as 16-bits counter
                     //transition on TOCKl pin
                     //increment on low-high transition on T0CKl pin
                     //set prescaler               
                     //1:8 prescaler



   INTCONbits.TMR0IF = 0;          //clear roll-over interrupt flag
   INTCON2bits.INTEDG0=0;          // interrupt is falling edge
   INTCONbits.INT0IE=1;         // enable  interrupt
     INTCONbits.GIEH = 1;           //enable global interrupts
   
   TMR0H = 0;                      // clear timer - always write upper byte first
    TMR0L = 0;
}


//----------------------------------------------------------------------------
//Low priority interrupt vector

#pragma code InterruptVectorLow = 0x18
void
InterruptVectorLow (void)
{
  _asm
    goto InterruptHandlerLow
  _endasm
}


//-------------------------------------------------------------------------
// Low priority interrupt routine

#pragma code
#pragma interrupt InterruptHandlerLow
void
InterruptHandlerLow ()
{   
    if (INTCONbits.TMR0IF)
   {
        LATDbits.LATD7=0;
      Delay1KTCYx(10);
        INTCONbits.TMR0IF=0;
    }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 12:50 pm     Reply with quote

This the CCS compiler forum. Please ask C18 compiler questions on
their forum:
http://www.microchip.com/forums/tt.aspx?forumid=3
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