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

Timer miss counting

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



Joined: 17 Feb 2006
Posts: 59
Location: Argentina

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

Timer miss counting
PostPosted: Mon Aug 29, 2011 11:54 am     Reply with quote

Hi , I am trying to count cycles with timer0 (8 bits) in 18f4550. CCS Ver 4.108.
It should interrupt every 51.2us and in 1 second I should count someting aroung 20K. But after I delay 1sec I only count 3000. What could be wrong? I was reading about, corrected some lines , but no effect so far. Thanks a lot, here is the code.

Code:


#include <18F4550.h>
#device adc=8
#FUSES NOWDT                     //No Watch Dog Timer
#FUSES WDT128                    //Watch Dog Timer uses 1:128 Postscale
#FUSES HSPLL                     //High Speed Crystal/Resonator with PLL enabled
#FUSES NOPROTECT                 //Code not protected from reading
#FUSES NOBROWNOUT                //No brownout reset
#FUSES BORV20                    //Brownout reset at 2.0V
#FUSES PUT                       //Power Up Timer
#FUSES NOCPD                     //No EE protection
#FUSES STVREN                    //Stack full/underflow will cause reset
#FUSES NODEBUG                   //No Debug mode for ICD
#FUSES NOLVP                     //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                     //Program memory not write protected
#FUSES NOWRTD                    //Data EEPROM not write protected
#FUSES IESO                      //Internal External Switch Over mode enabled
#FUSES FCMEN                     //Fail-safe clock monitor enabled
#FUSES NOPBADEN                  //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC                    //configuration not registers write protected
#FUSES NOWRTB                    //Boot block not write protected
#FUSES NOEBTR                    //Memory not protected from table reads
#FUSES NOEBTRB                   //Boot block not protected from table reads
#FUSES NOCPB                     //No Boot Block code protection
#FUSES NOMCLR                    //Master Clear pin used for I/O
#FUSES LPT1OSC                   //Timer1 configured for low-power operation
#FUSES NOXINST                   //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL12                     //Divide By 12(48MHz oscillator input)
#FUSES CPUDIV4                   //System Clock by 4
#FUSES USBDIV                    //USB clock source comes from PLL divide by 2
#FUSES VREGEN                    //USB voltage regulator enabled
#FUSES ICPRT                     //ICPRT enabled

#use delay(crystal=20000000,  clock=5000000)

//////////////////////////////////////////////////////////////////////////////////

#include <velocidad.h>
// Definicion de DEBUG.
#define DEBUG
//#define USB


#ZERO_RAM
#id CHECKSUM

#define MAX_TIMER_COUNT 0xFF     // Si el timer es de 16bits 0xFFFF
#define LED PIN_C2

// Definicion de los registros de calculo
unsigned int16 contador_tmr_int =0;
unsigned long time1 =0;
unsigned long time2 =0;
unsigned int velocidad =0;

//

#int_TIMER0
void  TIMER0_isr(void)
{
   contador_tmr_int++;
}

// Definiciones de Pines del LCD
#define LCD_ENABLE_PIN PIN_D2
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#include <lcd.c>
//

#include <funciones.h>


void main()
{
   
   lcd_init();                   //Inicializo el LCD
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2);
   //setup_psp(PMP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   //setup_wdt(WDT_ON);
   //setup_timer_0(RTCC_INTERNAL); // 16 bits
   setup_timer_0(RTCC_INTERNAL|RTCC_8_bit);  // RES .2us OVRFLW 51.2us
   //setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // RES .2us OVRFLW 13.1us - 3.6 m/s -> km/h
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // RES 1.6us OVRFLW 104ms - 0.28 km/h -> m/s
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

       
   while (TRUE) {
   
   contador ();
   delay_ms (1000);
   output_toggle (LED);
   
 
   }

}

//////////////////////////////////////////////////////////////////////////

long contador (void){

   int16 total,loc_copy;
     
      //disable_interrupts(INT_TIMER0);
      loc_copy = contador_tmr_int;
       
#ifdef DEBUG
printf (lcd_putc, "\nO %Lu ",loc_copy);
#endif
     
      // Inicializo el contador de interrupciones.
      contador_tmr_int =0;
      //enable_interrupts(INT_TIMER0);
     
   return total;
}



Thanks in advance
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 29, 2011 12:04 pm     Reply with quote

Quote:
#FUSES HSPLL //High Speed Crystal/Resonator with PLL enabled

#FUSES PLL12 //Divide By 12(48MHz oscillator input)

#FUSES CPUDIV4

#use delay(crystal=20000000, clock=5000000)


What is the frequency of the external crystal or oscillator that you have connected to the PIC ?
Is it a crystal, or is it an oscillator ?

What frequency do you want PIC to run at ?
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Aug 29, 2011 12:43 pm     Reply with quote

my very quick calc says:
for "RES 1.6us OVRFLW 104ms"
on timer one

you would use a 2.5MHZ ext clock or rock
and NO PLL to make that timer math work out

how did you come to expect those params with a 20mhz clock
and a PLL multiplier no less ??????
javi.ar



Joined: 17 Feb 2006
Posts: 59
Location: Argentina

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Mon Aug 29, 2011 2:20 pm     Reply with quote

Well those math came out from the PIC wizard.
I have a 20Mhz crystal attached and I want to use the config for USB output.
So I have no limits for clock speed as long as I know the overflow/interrupt timing and USB working full speed.
Thanks a lot.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 29, 2011 2:30 pm     Reply with quote

These are the fuse settings for a 20 MHz crystal, with the PLL, to give
a CPU clock of 48 MHz. These fuses will also work with the USB module.
Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48M)

//==========================
void main()
{

while(1)
  {
   output_high(PIN_B0);
   delay_ms(500);
   output_low(PIN_B0);
   delay_ms(500);
  }

}
 
 
 
javi.ar



Joined: 17 Feb 2006
Posts: 59
Location: Argentina

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Mon Aug 29, 2011 2:41 pm     Reply with quote

Ok thanks a lot for the reply, I will give a try right now, and let you know. Thanks as usual for the invaluable support.
javi.ar



Joined: 17 Feb 2006
Posts: 59
Location: Argentina

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Mon Aug 29, 2011 4:43 pm     Reply with quote

Yes, now the readings are as predicted. Thanks again. I should keep on reading.
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