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

Interrupts, Timer

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



Joined: 15 Mar 2021
Posts: 37

View user's profile Send private message

Interrupts, Timer
PostPosted: Wed Mar 24, 2021 1:33 pm     Reply with quote

The compiler is CCS v5.015
The IDE is MPLAB X IDE v5.40

Hi everyone,
I want to change the code that will sound a buzzer once for a second to begin and then again when the time is up. I have tried it with an LED but with no success. As far I know "longer delays can be produced by using a slower clock or more count variables".

Any help will be appreciated.
Thanks,
Jake

Code:
 
void main(){
n=10;
Count=0;
output_low(PIN_B1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); //Setup timer 1 with a 1/8 prescaler
enable_interrupts(INT_TIMER1);//Enable Timer interrupt
enable_interrupts( GLOBAL);//Enable selected interrupts

while(1){ //Loop forever
  Count++;
  if(n==0){  //Reduce n by 1 and check if n=0
     printf ("Interrupts = %Lu", Count);  //if so, print interrupt message
     output_toggle (PIN_B1);
   }
else
  n=10;{
  output_toggle (PIN_B1);
            //and do nothing
  }         //Processor is free
 }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 24, 2021 4:24 pm     Reply with quote

Here are two sample programs that might help:
http://www.ccsinfo.com/forum/viewtopic.php?t=58888&start=4
http://www.ccsinfo.com/forum/viewtopic.php?t=53961&start=7
jake1272



Joined: 15 Mar 2021
Posts: 37

View user's profile Send private message

PostPosted: Thu Mar 25, 2021 3:52 am     Reply with quote

Thank you. How do I make a loop for the counter when the time
it is up to turn on the led again?

Code:
 #include <16F1847.h>
#fuses NOMCLR NOBROWNOUT NOLVP INTRC_IO
#use delay(clock = 4MHz)
 int8 led_seconds_timer = 0;
 #define INTS_PER_SECOND 2
 #define LED_PIN  PIN_B1
#INT_TIMER1

 
 //--------------------------
#INT_TIMER1
void t1_isr(void)
{
static int8 int_count = INTS_PER_SECOND;

int_count--;

if(int_count == 0)  // Has 1 second passed ?
  {
   int_count = INTS_PER_SECOND;  // If so, reload counter

   if(led_seconds_timer)  // Is the LED timer running ?
     {
      led_seconds_timer--;  // If so, decrement it

      if(led_seconds_timer == 0)  // Is it done ?
        {
        output_low(PIN_B1); // If so, turn off the LED
          delay_ms(4000);
      }
     }
  }
else {
    output_high(PIN_B1);
}
}
void Timer1_isr(void){
  output_high(PIN_B1);
  set_timer1(0);                              // Timer1 preload value
  clear_interrupt(INT_TIMER1);    // Clear Timer1 overflow bit
}

//--------------------------------
// This function turns on the LED and sets the
// number of seconds that it will stay on.

void turn_on_led(int8 seconds)
{
output_high(PIN_B1);  // Turn on the LED

led_seconds_timer = seconds; // Start the LED timer
}

//======================


void main(){
   output_low(PIN_B1);
  setup_oscillator(OSC_4MHZ);                    // Set the internal oscillator to 4MHz
  clear_interrupt(INT_TIMER1);                   // Clear Timer1 overflow bit
  enable_interrupts(INT_TIMER1);                 // Enable Timer1 interrupt
  enable_interrupts(GLOBAL);                     // Enable global interrupts
  setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);      // Timer1 configuration: internal clock source + 8 prescaler
  set_timer1(0); // Timer1 preload value
 
  turn_on_led(1);
 
  while(TRUE);                                   // Endless loop
}
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