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 support@ccsinfo.com

Measuring pulse width using CCP
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Wed Oct 15, 2014 1:50 pm     Reply with quote

I am sorry, I should have explained better. The test i did on some ports was a separate code just to check if the micro controller was working. Just a High and Low test on ports. The Code i have posted above has an LED at C5 however it does not turn ON even after i feed in the signal at CCP1. I dont have any signal coming out from the TX either. So there is something with the chip that i haven't enabled or configured properly. I just cant seem to figure out what it is.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 15, 2014 2:29 pm     Reply with quote

That's why I gave you this strong hint:
Quote:
If it doesn't work, then look at the pin descriptions in the PIC data sheet.

I know what the answer is. I want you to find it.
Ttelmah



Joined: 11 Mar 2010
Posts: 19244

View user's profile Send private message

PostPosted: Thu Oct 16, 2014 1:17 am     Reply with quote

There are two major problems.

The first, PCM+programmer has given you the direct 'where to look' answer to.

The second is the clock. Your chip will not be running at the 4MHz you expect. I alluded to this in an earlier reply. The oscillator setup on the 18F2550/4550 is much more complex than the '16' chips (because it needs to be able to generate the clocks for USB). With your current settings, the chip will be running at 1MHz, not 4Mhz. Look at what the CPUDIV fuses do, and what these are set to by default.

I have to ask 'why' you elected to go for the chip with the extra complexity of USB?.
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Thu Oct 16, 2014 8:13 am     Reply with quote

I saw PCM Programmer's comment last night and I reviewed the PIN description again. I then realised the "I/0 is TTL and Digital I/O" for most of the ports. Where as the 16F886 "I/O is CMOS general purpose".
To answer your question about why I chose 18F micro controller. I had this one kicking around and it had the option for Timer3. This allows me to use CCP1 with Timer1 and CCP2 with timer3 to measure the two separate incoming pulse. It seems the chip is complex and i will look online for the 16F microcontrollers if they have timer3. I appreciate that you guys encourage learning and not give answers right away.
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Thu Oct 16, 2014 8:22 am     Reply with quote

I will also be checking the 18F family but simpler than the 18F2550.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 16, 2014 8:28 am     Reply with quote

Quote:
I then realised the "I/0 is TTL and Digital I/O" for most of the ports.
Whereas the 16F886 "I/O is CMOS general purpose".

No, you still missed it. It's not the voltage levels. On the 18F2550,
pin C5 is input only. It can't be a output pin, so it can't turn
an LED on or off. Look at this table for Pin C5 in the PIC data sheet:
Quote:
TABLE 1-2: PIC18F2455/2550 PINOUT I/O DESCRIPTIONS (CONTINUED)

http://ww1.microchip.com/downloads/en/DeviceDoc/39632e.pdf
You will see in the Pin Type column it says "I" for input, and in
the Description column it says "Digital Input".

You can fix the problem by using an i/o pin for the LED.
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Thu Oct 16, 2014 9:00 am     Reply with quote

I saw that last night that pinC5 is input only and so i moved the pin to RA0 but still no go. Also the comment from Ttelmah about the clock not running at 4 Mhz is one of the reasons for the program not working. Since the internal clock is not running at 4 Mhz. I will try to see if an external 4 MHz crystal works.
Ttelmah



Joined: 11 Mar 2010
Posts: 19244

View user's profile Send private message

PostPosted: Fri Oct 17, 2014 12:35 am     Reply with quote

The code you posted, was for using an external 4Mhz crystal. However this would be being divided by 4. You are not selecting the internal clock.
It wouldn't work without the crystal, except that it should have defaulted to FCMEN, and dropped 'back' to the internal oscillator.

I'll post some setups for different configurations:
Code:

//4MHz crystal running at 4MHz.
#fuses XT, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP, CPUDIV1
#use delay(clock=4000000)

//4MHz crystal at 48MHz
#fuses XTPLL, PLL1, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP, CPUDIV1
#use delay(clock=48000000)

//4MHz internal oscillator
#fuses INTRC_IO, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use delay(internal=4MHz)


There are hundreds of other permutations (the clocking on all the USB enabled chips is quite complex), but the big problem is that the pins for the USB connection can only be used for USB, or as inputs.....

Remember that as a USB chip, it can only be used for USB with a suitable crystal (so again 'why' have a USB chip if you want to use the internal oscillator....).

A PIC18F4620 (for example), would go into the same socket as your 886, and run with almost no changes to the code.
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Fri Oct 17, 2014 7:07 am     Reply with quote

Thanks Ttelmah,
PIC16F886 is a 28pin ucontroller, and PIC18F4620 is a 40 pin ucontroller. When I was looking at the PIC18F4620 datasheet they have PIC18F2525 or PIC18F2620 with 28 pin option. So I will get the 28 pin option. Smile
temtronic



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

View user's profile Send private message

PostPosted: Fri Oct 17, 2014 9:59 am     Reply with quote

If you're still looking for a 'nice' 18F series PIC, I suggest the 18F46K22. I like 40 pins(always need 1 more I/O pin), has lots or RAM,EPROM,peripherals like TWO UARTS ! yes ther's a 28 pinner as well....

just something to consider.

I know a 40 pin device takes up more PCB space BUT ....


jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19244

View user's profile Send private message

PostPosted: Fri Oct 17, 2014 10:59 am     Reply with quote

Yes.

I suspect I'm like you. I tend to always do the initial prototype with a chip that is too large, and only once I am 'sure' what I want, work out if I can fit it into a smaller chip.
temtronic



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

View user's profile Send private message

PostPosted: Fri Oct 17, 2014 1:43 pm     Reply with quote

then...3 days after production starts...client 'needs' another feature..... Wink
Ttelmah



Joined: 11 Mar 2010
Posts: 19244

View user's profile Send private message

PostPosted: Sat Oct 18, 2014 1:23 am     Reply with quote

Again, yes.

One key thing it trying to 'second guess' what might be wanted. Getting good at this is down to studying both the specification, and what other kit in the same marketplace actually 'does'. I often code 'stubs' in place for expansions, both in hardware, and software, and have had the 'joy' when asked to add a feature of having already done 60% of the work in the original design, to allow this to be added much more easily than you tell the customer it is..... Smile
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Fri Oct 24, 2014 9:25 am     Reply with quote

Hi Guys,
So I got the 18F2620 chip and tried the code with timer1 and ccp1. The code works and the data coming to the serial port is correct. I then modified the code to use CCP2 and Timer3 to do the same thing. The data coming out of the serial port is "0 ms" instead of "2 ms" Also the LED does not turn on at A0 when using timer3 and ccp2 because of "0 ms".

I was reading the manual for the chip and in the T3CON section "bit 6, 3 T3CCP2:T3CCP1: Timer3 and Timer1 to CCPx Enable bits
1x = Timer3 is the capture/compare clock source for both CCP modules
01 = Timer3 is the capture/compare clock source for CCP2;" So i want to be using 01 =Timer3 for CCP2. How do i set that up so the timer works correctly.



Code:

#include <18F2620.h>
#fuses XT, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP, CCP2C1,
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

int8 capture_rising_edge;
int8 got_pulse_width;
int16 ccp_delta;

#define MS_2 250
#define MS_3 375

   
#int_ccp2                     //Interrupt Name
  void isr_ccp2()               //Interrupt Function
  {

static int16 t3_rising_edge;

//If current interrupt is for rising edge.
if(capture_rising_edge)
   {
   setup_CCP2(CCP_CAPTURE_FE);
   capture_rising_edge = FALSE;
   t3_rising_edge = CCP_2;
   }
else
   {
   setup_CCP2(CCP_CAPTURE_RE);
   capture_rising_edge = TRUE;
   ccp_delta = CCP_2 - t3_rising_edge;
   got_pulse_width = true;
   }

}

//+++++++++++++++++

void main()   ///
{

int16 local_ccp_delta;
int16 pulse_width_ms;

got_pulse_width = FALSE;
capture_rising_edge = TRUE;

setup_ccp2(CCP_CAPTURE_RE);
setup_timer_3(T3_INTERNAL | T3_DIV_BY_8);
enable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);


while(1)
{
   if(got_pulse_width)
   {
   disable_interrupts(GLOBAL);
   local_ccp_delta = ccp_delta;
   enable_interrupts(GLOBAL);

   pulse_width_ms = local_ccp_delta/125;
    printf("%lu ms \n\r", pulse_width_ms);
   got_pulse_width = FALSE;
}
if(local_ccp_delta>MS_2 && local_ccp_delta<MS_3)
{
   output_high(pin_a0);
}
else
{
   output_low(pin_a0);
}
   delay_ms(500);
}
}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 24, 2014 9:36 am     Reply with quote

Go to the 18F2620.h file and read this section a bit more closely to see
what you need to do:
Quote:
// Constants used for SETUP_TIMER_3() are:


The .h file lists the function parameters that you need.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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