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

Pulse with capture pic18f26k22

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



Joined: 15 Sep 2023
Posts: 7

View user's profile Send private message Send e-mail

Pulse with capture pic18f26k22
PostPosted: Mon Jan 13, 2025 1:44 pm     Reply with quote

Code:
Hi Guys! ( - PIC16F88 -Ccs PCM 5.115)

i'm using a follow code to read a train pulse (us) {100,200,300,400,500}

#include <16F88.h>
#fuses NOWDT,NOPROTECT,PUT,NOLVP, NOMCLR,NOBROWNOUT,INTRC_IO   //
#use delay(clock= 8MHZ)
#use rs232(baud=9600, xmit=PIN_b5, rcv=PIN_b2,stop=1,parity=n,bits=8)
#include <string.h>
#include <stdlib.h>
#define LED PIN_A3
#define Buffer 10   
#define TotEdg 10

#USE TIMER(TIMER=1,TICK=1us,BITS=16,ISR)   

#int_ext   
void ext_isr()   
 {   
    if(Edges>0)
     {             
      Time_Width = get_ticks();  // L_H 
      set_timer1(0);
      set_ticks(0);
      arry[Edges-1]=Time_Width;
      Time_Width =0;
     }
    else {set_timer1(0);set_ticks(0);} 
   
    if(e==0) {e=1; ext_int_edge(H_TO_L);} else {e=0; ext_int_edge(L_TO_H);}   //<==           
     
    Edges++;
    CountInt0++;
   
    if(Edges==TotEdg)   
     {
      disable_interrupts(int_ext); 
     }
 }

void main()  //  *  *  *  *  *  *  *  *  *  *  *  *  *

   
  IniProcessador();     
  delay_ms(100); 
  ext_int_edge(L_TO_H);  //<==   
  set_timer1(0);
  set_ticks(0); 
  clear_interrupt(INT_TIMER1);   
  enable_interrupts(int_ext);
  clear_interrupt(INT_EXT);     
  enable_interrupts(global);
  Edges=0;
 
  while(TRUE)
  {       
    if(Edges==TotEdg) 
       {   
       int i=0;                   
       for(i=0;i<=Edges;i++)
         {         
          printf("%lu,",arry[i]);
         }
          printf("\n%lu,%lu\n",Edges,CountInt0 );                 
       Edges=0;
       CLEAR_UART();         
       clear_array();
       enable_interrupts(int_ext);
       clear_interrupt(INT_EXT);
       }
}
  Its work fine, but when I migrate code to pic18f26k22 like that:

#include <18F26k22.h>
#FUSES hsm, NOWDT,NOBROWNOUT,NOPUT,NOMCLR
#use delay(internal=64mhz)
#use rs232 (baud=9600,xmit=pin_c6, rcv=pin_c7,stop=1,parity=n,bits=8,ERRORS)

The rest of the code is the same........

The time (us) of the read pulses changes to more, such as 180, 260, 390, 480 and 550.

Could anyone tell me why?  Mny thanks..


Last edited by AlbertCharles on Tue Jan 14, 2025 6:22 am; edited 2 times in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19649

View user's profile Send private message

PostPosted: Tue Jan 14, 2025 5:34 am     Reply with quote

First, learn to use the code buttons, so we can see the code more sensibly
laid out.
Then the code is incomplete. Doesn't show a couple of routines you call,
or the variable declarations.
Your clock setups are rather dubious. Get rid of the clock fuses, and use
the INTERLAL=8MHz and INTERNAL=64MHz settings, which then handle
these.
You are telling the compiler to handle timer1 for you, then making changes
to it using direct access. Don't. Either handle the timer yourself, or use
the compiler functions. Don't mix the two methods.
Obvious fault is with the interrupts. You need to clear the interrupt before
enabling it. You do this in the initial setup, but not when you re-enable.
Because of the higher processor speed, there is a higher chance that
this will give counts from the wrong edge as the interrupt will have already
been set and not handled when you enable it.
AlbertCharles



Joined: 15 Sep 2023
Posts: 7

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 14, 2025 6:10 am     Reply with quote

Mr. Ttelmah...

I´ll fix according you sugestion, and i´ll come back here to expose the results.

Mny thnks.
temtronic



Joined: 01 Jul 2010
Posts: 9372
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jan 14, 2025 8:36 am     Reply with quote

just a comment...
I see the 1st pic is 8MHz, the 2nd 64Mhz
at least for test purposes, clock the 2nd at 8Mhz ( same speed)

I know shouldn't be a problem but makes it a bit easier to see on a scope when you compare the two.

also might want to dump both listings and see IF there's something 'different' as to how the compiler created the code.
Ttelmah



Joined: 11 Mar 2010
Posts: 19649

View user's profile Send private message

PostPosted: Tue Jan 14, 2025 11:30 am     Reply with quote

They will be different. Remember he is switching PIC16 to PIC18.
I think he is getting extra counts, because the code gets to the relaunch
a little quicker (larger buffer for the UART, and the execution speed),
so because he re-enables the interrupt before clearing it he gets a
call that isn't actually synchronised to the waveform edge. The original
values seem suspiciously exact actually.
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