View Single Post
Old 03-23-2005, 08:58 PM   #7
Chris31
Raw Wave
 
Join Date: May 2003
Posts: 2,021
The PWM being mentioned is the CCP module. This can be setup to time the pulse width and return the value in the CCP registers, useful for measuring the pulse width.

Hmm interrupts? I wont bother with it. The bit change interrupt can be a problem when at the same time trying to access the port, very unlikely you be doing this anyway.

From microchip apps note >

Note: If a change on the I/O pin should occur
when a read operation is being executed
(start of the Q2 cycle), then the RBIF
interrupt flag may not get set.

The interrupt-on-change feature is recommended for
wake-up on key depression operation and operations
where PORTB is only used for the interrupt-on-change
feature. Polling of PORTB is not recommended while
using the interrupt-on-change feature.


Applying interrupt, your code wont be doing anything apart from waiting for the line to go low for ~9ms then back high again for ~5ms to indicate a start condition. Once you are in the start theres not much for you to do apart from decoding the stream of data which is going to be part of the interrupt routine.

Your main will look like >

Main
goto main

Look abit silly. The rest are done in the interrupt routine.

As for the CCP module, not all PICs have this. Infact you dont even need it.
If you really want to optimise your design, use a 12C508 or something along those line.

For measuring the pulse width you can make it this way >

Setup a 0.1ms timer that set a flag every 0.1ms using interrupts.


On part of the decoding routine on the main >
Each time an interrupt occured a counter is incremented. If this counter contains ~5 after a bit changed then you know ~0.5ms has passed. You reset this counter every bit change and of course you have to reset the "0.1ms" elapsed flag every time you increment.

You now have a pulse width measurement routine accurate to ~0.1ms.

So from the beginning, your main loop will start incrementing the pulse width counter as soon as a LOW is detected. If the counter does not equate to ~9ms after it goes high then you know this is not a start condition, the code should reset looking for the ~9ms LOW again. If successful, look for a ~5ms high this time.

All you have to do is keep a track of the pulse width counter and at which state it is measuring, a HIGH or a LOW state. From there you can decode on the fly.

You can either save all the data and decode from the RAM or just decode whats needed on the fly.

How are you going to output the decoded bits? Using 4 pins from the PIC or output it to the serial port?

Outputing it to serial port should be fairly easy.


How you gonna program it? ASM? C? BASIC?

Interrupt or polling, hardware or software pulse width measurement thats your choice. But this little project can definitely be done with some of the most basic PIC you can buy.

Thats about it for now I guess

Last edited by Chris31; 03-23-2005 at 09:14 PM.
Chris31 is offline   Reply With Quote