View previous topic :: View next topic |
Author |
Message |
jake1272
Joined: 15 Mar 2021 Posts: 37
|
Interrupts, Timer |
Posted: Wed Mar 24, 2021 1:33 pm |
|
|
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
|
|
|
jake1272
Joined: 15 Mar 2021 Posts: 37
|
|
Posted: Thu Mar 25, 2021 3:52 am |
|
|
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
} |
|
|
|
|