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

16F1825 Compare Module

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



Joined: 29 Dec 2008
Posts: 18
Location: Italy

View user's profile Send private message

16F1825 Compare Module
PostPosted: Thu Dec 08, 2011 1:10 pm     Reply with quote

Hi all!

I'm having some trouble using the compare module of a 16F1825.
From the datasheet I know that the compare module uses the timer1, so I want to count the Timer0 overflows, I tried lots of configurations (setup_timer1(...)) but without results Sad
Also changed the chip but same result!

I want to toggle output pins when timer1 equals CCPR1 & CCPR2, when equal to CCPR4 the timer is erased.
The code uses the internal oscillator but I also tried with an external xt.
If working, this is a quadrature encoder simulator.

Here's the code, I'm using CCS v. 4.114.

Code:

#include <main.h>

#define CCP_COMPARE_TOGGLE_OUTPUT      0x2


// PIN Inversion Register
#byte APFCON0 =   getenv("SFR:APFCON0")
#bit RXDTSEL   = APFCON0.7
#bit SDOSEL   = APFCON0.6
#bit SSSEL   = APFCON0.5
#bit T1GSEL   = APFCON0.3
#bit TXCKSEL   = APFCON0.2

#byte APFCON1 =   getenv("SFR:APFCON1")
#bit P1DSEL = APFCON1.3
#bit P1CSEL = APFCON1.2
#bit P2BSEL = APFCON1.3
#bit CCP2SEL = APFCON1.0



unsigned long int c1, c2;

#int_CCP4
void  CCP4_isr(void)
{
   CCP_1 = c1;
   CCP_2 = c2;
   CCP_4 = c2;
}


void main()
{
   setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_PLL_OFF,0);
   // Swap pins...
      RXDTSEL = 1;
      SDOSEL = 0;
      SSSEL = 0;
      T1GSEL = 0;
      TXCKSEL = 1;
      
      P1DSEL = 0;
      P1CSEL = 0;
      P2BSEL = 0;
      CCP2SEL = 0;
      
   set_timer1(0);
   setup_timer_0(T0_INTERNAL | T0_DIV_1); // Timer0 is ok!

// Timer 1 never increase!
   setup_timer_1(T1_GATE_TIMER0| T1_GATE_SINGLE | T1_GATE_INVERTED);
     setup_ccp1(CCP_COMPARE_TOGGLE_OUTPUT);
      setup_ccp2(CCP_COMPARE_TOGGLE_OUTPUT);
      setup_ccp4(CCP_COMPARE_RESET_TIMER);
      setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
   
   set_tris_a(0xFE);
   set_tris_c(0x57);
   
   c1 = 0x100;
   c2 = 0x200;
   
   CCP_1 = c1;
   CCP_2 = c2;
   CCP_4 = c2;

   enable_interrupts(INT_TIMER0);
   enable_interrupts(INT_TIMER1_GATE);
   enable_interrupts(INT_CCP4);
   enable_interrupts(GLOBAL);
   
   while(1)
   {
     if (input(PIN_C1)==1)
      {   
         if (c1 > 0x100)
         {   
          c1=c1-100;
          c2 = c2-100;
      }
      delay_ms(50);
   }
   
   if (input(PIN_C2)==1)
      {   
         if (c1 <= 0x7FFF)
         {   
          c1=c1+100;
          c2=c2+100;
      }
      delay_ms(50);

   }
   }
}

///////////////// MAIN.H

#include <16F1825.h>
#device ICD=TRUE

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 
//#FUSES NOWDT_SW               
#FUSES NOPUT                    //No Power Up Timer
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPD                    //No EE protection
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOCLKOUT             
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOWRT                    //Program memory not write protected
#FUSES PLL                   
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES BORV19               
#FUSES DEBUG                    //Debug mode for use with ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(clock=8000000)


Thank you in advance!
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Thu Dec 08, 2011 3:33 pm     Reply with quote

As posted, can't run.
If you enable an interrupt (and global), you _must_ have a handler present, otherwise the code will hang.
You show a handler for CCP4, but not for the other two enabled interrupts....

Best Wishes
acm45



Joined: 29 Dec 2008
Posts: 18
Location: Italy

View user's profile Send private message

PostPosted: Fri Dec 09, 2011 1:47 am     Reply with quote

Hi Ttelmah,

Thank you for your fast answer!
I posted the last code I wrote, if I disable all interrupts I obtain the same behaviour, the timer1 never increases!

I saw all the configuration registers for the capture/compare module and seems all ok but isn't working!
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