|
|
View previous topic :: View next topic |
Author |
Message |
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
[Help] Capture mode in PIC18F46K22 not run? |
Posted: Wed Dec 18, 2013 1:49 am |
|
|
Hi all.
I'm trying to work with capture mode on PIC18F46K22.
I used CCS C ver 5.015
I used CCP1 Pin to read input capture.
I don't know this config capture mode in CCS is enough for it run?
I config code in bellow:
Code: |
#include <18F46K22.h>
#fuses HSM,PUT,NOPROTECT,NOLVP,NOWDT,NOPLLEN,NOPBADEN
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, errors, stream= RF) // HARD UART_1
#use rs232(baud=4800, xmit=PIN_D6, rcv=PIN_D7, errors, stream= PC) // HARD UART_2
|
RDA Interrupts:
Code: |
#int_rda
void ngat_rs232(void)
{
c=fgetc(RF);
switch(c)
{
case 2:
index=0;
break;
case 3:
if (index==10) read_1key=1;
break;
default:
str_rs232[index]=c;
index++;
break;
}
}
//=====================================================================================================
#int_ccp1
void ccp1_isr(void)
{
static int16 t1_falling_edge;
// If current interrupt is for falling edge.
if(capture_falling_edge)
{
setup_ccp1(CCP_CAPTURE_RE);
capture_falling_edge = FALSE;
t1_falling_edge = CCP_1;
}
else
{
setup_ccp1(CCP_CAPTURE_FE);
capture_falling_edge = TRUE;
ccp_delta = CCP_1 - t1_falling_edge;
got_pulse_width = TRUE;
}
}
//=====================================================================================================
#int_HLVD
void ngat_hlvd(void)
{
write_eeprom(10,make8(count,1));
write_eeprom(11,make8(count,0));
delay_ms(100);
output_low(PIN_E2);
}
|
Main code:
Code: |
void main()
{
set_tris_a(0x20); // en_LED + RA5: detect high low volatge
set_tris_b(0x00); // data_LED
set_tris_c(0x84); // UART_1 TO RF
set_tris_d(0x80); // UART_2 TO PC
set_tris_e(0x00);
output_a(0xff); // CLR DISP LED7
output_b(0xff); // CLR DISP LED7
output_e(0xff); // CLR DISP LED
got_pulse_width = FALSE;
capture_falling_edge = TRUE;
delay_ms(2000);
setup_low_volt_detect( LVD_TRIGGER_BELOW | LVD_41 ); // detect high low voltage: below 4.2V
setup_ccp1(CCP_CAPTURE_FE);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8 );
enable_interrupts(INT_CCP1);
enable_interrupts(int_rda);
enable_interrupts(INT_HLVD);
enable_interrupts(GLOBAL);
clr_buf_rs232();
clr_buf_idkey();
clr_buf_idkey_end();
count= make16(read_eeprom(10), read_eeprom(11)); // Restore count in eeprom
if(count>9999) count=0;
while(1)
{
if(read_1key==1)
{
strcpy(str_idkey, str_rs232);
for(i_count=0; i_count<=3; i_count++)
{
str_idkey_end[i_count] = str_idkey[i_count+6];
}
read_1key=0;
idkey_end =atol(str_idkey_end);
display_7segment(idkey_end);
}
if(got_pulse_width)
{
disable_interrupts(GLOBAL);
local_ccp_delta = ccp_delta;
enable_interrupts(GLOBAL);
clear_interrupt(int_ccp1);
pulse_width_ms = (local_ccp_delta * 32) /10000;
if(pulse_width_ms >=7 && pulse_width_ms<= 12) check_pulse_10ms=1;
got_pulse_width= FALSE;
}
if( check_pulse_10ms==1) //&& (idkey_end==1627) )
{
check_pulse_10ms=0;
count++;
}
display_7segment(count);
}
}
|
I try ported code into PIC18F4680 is OK, but in PIC18F46K22, it not run.
Pls show me problem in this.
Thanks all. _________________ Begin Begin Begin !!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Wed Dec 18, 2013 3:00 am |
|
|
It looks fairly correct, but with some comments/questions.
Get rid of the delay in INT_HLVD. Use a loop instead. The idea is 'right' (you don't want to exit the routine, if the power comes back 'on' in a few moments, so need to wait for a while until either the power has gone 'off', and the code ends here, or the power has come back 'on', and be sure it has stabilised). Problem is the classic CCS one, that a delay here, means interrupts will be disabled in all delays outside. Just have a int16 variable, and do a for/next loop for a lot of counts instead.
Then you don't show fast_io being enabled. If not, the TRIS settings will be being overridden on any port where you perform I/O.
There may be a problem with the defaults on fuses/peripherals. Look at the .lst file, and verify that the peripheral fuses are being set how you expect, and then explicitly disable peripherals (CTMU on the CCP1 pin for example).
Again be explicit on the timer selection. Don't assume it'll default to what you want. So CCP_USE_TIMER1_AND_TIMER2, or'ed with the edge selection.
I'd setup your UART, with BRGH1OK. The high bit needs to be on on these chips there is an erratum on the UART if it is not....
Best Wishes |
|
|
|
|
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
|