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

Pic18F2620 Timer issues

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



Joined: 20 Dec 2023
Posts: 1

View user's profile Send private message

Pic18F2620 Timer issues
PostPosted: Wed Dec 20, 2023 9:25 pm     Reply with quote

Hi everyone im trying to use timers with PIC18F2620 using CCS Version 5 but i have problems implementing a code, so for debugging processes i create a new and easy program but i still dont know how to do it, i use to program in Mplab with XC8 but this project must be done in CCS but i dont know why it doesnt work

I have the Timer0 interrupt to occur every 10ms soy in this code a pin (where is a Buzzer) must be high in +/-60seg but it doesnt work

What could probably be wrong? thanks for your answers!!

Code:

#include <18F2620.h>
#fuses INTRC_IO,NOWDT,PROTECT,NOLVP //Se usara el oscilador interno
#device HIGH_INTS=TRUE //Activamos prioridad de interrupciones
#use delay(internal=32 000 000) //32[MHz]
#use rs232(baud=115200,xmit=PIN_C6, rcv=PIN_C7)

#define Buzzer PIN_B7 //Seleccionamos el pin en el que estara conectado el Buzzer

unsigned int BanderaTiempoUso;
unsigned int TiempoUsoSeg;

void main(){ //Llave de apertura del MAIN
   //Configuracion inicial
   setup_oscillator(OSC_32MHZ); //Esta statement la coloca automaticamente CCS al usar INTRC_IO y #use delay al principio de main
   setup_adc_ports(NO_ANALOGS); //Para evitar problemas con I/O de acuerdo al datasheet
   setup_adc(ADC_OFF); //Mismo proposito que la anterior linea de codigo
   //setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); //T0 como timer
   SETUP_TIMER_3(T3_INTERNAL|T3_DIV_BY_8);

   enable_interrupts(int_timer3);
   enable_interrupts(GLOBAL); //Habilitamos interrupciones globales
   
   unsigned char eInicial=1;
   while(1){ //Llave de apertura del superloop-main
      //unsigned char eInicial=1;
      if(eInicial==1){
         output_high(Buzzer);
         delay_ms(1000);
         output_low(Buzzer);
         BanderaTiempoUso=1;
         eInicial++;
      }
   }
   
}

//ISR para la interrupcion del Timer0
#INT_TIMER3
void T3_Interrupt(){
   //Se entrara a este ISR cada 1[s]
   disable_interrupts(INT_TIMER3);
   
   if(BanderaTiempoUso==1){
      TiempoUsoSeg++;
      if(TiempoUsoSeg==6000) output_high(Buzzer);
   }
   
   set_timer3(55536);
   enable_interrupts(INT_TIMER3);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19231

View user's profile Send private message

PostPosted: Thu Dec 21, 2023 3:04 am     Reply with quote

OK. Several things.
First you don't have to select INTRC_IO. Selecting INTERNAL=32MHz
in the delay setup does this for you. You also don't need the setup_oscillator
line. Again the delay setup will already do this (unless you have a very old
compiler, where this sometimes did not work).

Then BanderaTiempoUso and TiempoUsoSeg are never initialised. They
will probably contain 255, but no guarantee. Only static variables are
by default initialised.

Now you say you want the interrupt at 10mSec. On a 32MHz master oscillator
this is 80000 cycles. You show it being set to give 80008. Just a tiny error,
but remember the clock interrupts when it wraps from 65535 to zero, not
at 65535.

Then you don't need/want the disable and enable interrupt calls inside
the ISR. The interrupt is automatically disabled inside the ISR until it
exits.

Then TiempoUsoSeg is never reset anywhere. So if the buzzer went on
it'd stay on forever.
These though are all minor. The big one is below.

Now I say 'if' for one very simple reason. It'll never go on. Why?. because
TiempoUsoSeg can never get to be 6000. You declare it as an unsigned int.
An unisgned int in CCS is an 8bit integer...
So it is actually counting to 255, then resetting to 0, and never reaching
6000. Sad

Get in the habit of being explicit with variable sizes. This is good practice
all the time. So use explicitly int16, int32 etc. etc.. It avoids this type of
problem.
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