View previous topic :: View next topic |
Author |
Message |
lambk89
Joined: 21 Oct 2012 Posts: 8
|
RTOS and Interrupt Timer, CCP2 |
Posted: Thu Sep 17, 2015 8:04 am |
|
|
Hi!
Can you help me?
I use RTOS and Interrupt Timer 1. When I turn off Timer 1 then ccp2 happen but when i turn on Timer 2 then Interrupt ccp2 can't do.
This is my code:
Code: |
#include "E:\reading\Lab\Distant sensor\example\CCP\RTOS.h"
#define TRIGGER PIN_A3
int16 fall, rise;
int echo = 0;
#use rtos(timer = 0, minor_cycle = 10ms)
#task (rate = 1000ms, max = 10ms)
void Init_Sensor();
/*
#task (rate = 100ms, max = 10ms)
void display();
*/
void trig()
{
output_high(TRIGGER);
delay_us(10);
output_low(TRIGGER);
}
/*
double dis()
{
return (double)(fall - rise)*0.8/56;
}
void display()
{
printf("Khoang cach = %f \n",dis() );
}
*/
void Init_Sensor()
{
echo = 0;
setup_ccp2(CCP_CAPTURE_RE);
trig();
//printf("Init \n");
}
#int_CCP2
void CCP2_isr(void)
{
if(echo == 0)
{
rise = CCP_2;
setup_ccp2(CCP_CAPTURE_FE);
echo = 1;
printf("echo = 0 \n");
}
else
{
fall = CCP_2;
printf("echo = 1 \n");
}
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
// TODO: USER CODE!!
rtos_run();
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Sep 18, 2015 9:46 pm |
|
|
Quote: | but when i turn on Timer 2 then Interrupt ccp2 can't do. |
I don't see any place in your code where you turn on Timer2. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Sat Sep 19, 2015 12:28 am |
|
|
There is also the old classic 'wrong line':
setup_spi(SPI_SS_DISABLED);
This _enables_ the SPI, with the slave select disabled.
The command to disable the SPI, is:
setup_spi(FALSE);
Can affect how other parts work.... |
|
|
lambk89
Joined: 21 Oct 2012 Posts: 8
|
|
Posted: Sun Sep 20, 2015 7:55 am |
|
|
Sorry! That's timer 1 not timer 2. |
|
|
lambk89
Joined: 21 Oct 2012 Posts: 8
|
|
Posted: Sun Sep 20, 2015 7:57 am |
|
|
Ttelmah wrote: | There is also the old classic 'wrong line':
setup_spi(SPI_SS_DISABLED);
This _enables_ the SPI, with the slave select disabled.
The command to disable the SPI, is:
setup_spi(FALSE);
Can affect how other parts work.... |
I setup_spi like you but CCP2 not work. |
|
|
lambk89
Joined: 21 Oct 2012 Posts: 8
|
|
Posted: Sun Sep 20, 2015 7:58 am |
|
|
PCM programmer wrote: | Quote: | but when i turn on Timer 2 then Interrupt ccp2 can't do. |
I don't see any place in your code where you turn on Timer2. |
Sorry! I write wrong. That's timer 1 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 20, 2015 10:07 am |
|
|
Quote: |
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);
|
The reason is because you don't have an interrupt routine for Timer1.
You have enabled INT_TIMER1 interrupts, but without an #int_timer1
routine, the Timer1 interrupt flag bit will never be cleared. So, you will
get continuous Timer1 interrupts and your program will appear to be
locked up. Your program will spend all of its time executing the CCS
global interrupt handler code.
At a minimum, you need to add the following code to your program:
Code: |
#int_timer1
void timer1_isr(void)
{
}
|
|
|
|
lambk89
Joined: 21 Oct 2012 Posts: 8
|
|
Posted: Mon Sep 21, 2015 11:26 am |
|
|
PCM programmer wrote: | Quote: |
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);
|
The reason is because you don't have an interrupt routine for Timer1.
You have enabled INT_TIMER1 interrupts, but without an #int_timer1
routine, the Timer1 interrupt flag bit will never be cleared. So, you will
get continuous Timer1 interrupts and your program will appear to be
locked up. Your program will spend all of its time executing the CCS
global interrupt handler code.
At a minimum, you need to add the following code to your program:
Code: |
#int_timer1
void timer1_isr(void)
{
}
|
|
Thanks! |
|
|
|