pulse generatng in PIC

Thread Starter

fancy102

Joined Dec 21, 2007
26
hi,
i m facing defficulties in writing programme to calculate phase shift of a pulse in pic.

my input to the PIC16F877A is pulse form.how to write the PIC programme at time=0 and time=t1. i been reading a lot of websie but i still not understand. really thanks if somebody might help.

best regards
 

BlackBox

Joined Apr 22, 2007
20
Sorry, if you could be a bit more specific in your request it would be easier to help you :)

Are you trying to read the time between a High-to-low and a Low-to-high transition?

In that case I would suggest using one of the internal timers of the PIC and poll the input port.
 

nanovate

Joined May 7, 2007
666
The PIC16F877A has a capture/compare module using timer 1. This provides a way to use the hardware to time the changes on the input pin. Polling the input would not be as accurate. Without more specific info we can't help much.
 

Thread Starter

fancy102

Joined Dec 21, 2007
26
yaya..i would like to write from high to low transition. pic can capture the module by using timer1? what relate with my hardware? and sorry to ask bout the pooling input..if i use polling is it really not accurate?if so,what method should be use?

actually i am doing power factor measurement system. my input of the PIC is from the analogue switch, which is in square wave. and my problem is how to write high to low transition of the time by using PIC. Output of my PIC is connected to the LCD display.

i would like to attach my schematic draawing here...
thanks..
 

Attachments

nanovate

Joined May 7, 2007
666
Read Section 8.1 (pg65) of the datasheet:

http://ww1.microchip.com/downloads/en/DeviceDoc/39582b.pdf

This explains the CCP1 module. When an event is detected it captures the TMR1 value. TMR1 needs to be running.

Polling the pin means that you'd have to use the software check the value of the input at set intervals. This would consume a lot of processor resources especially if you need that interval to be very small. Using the CCP1 module the hardware automagically takes care of monitoring the input pin (and capturing the timer 1 value at the event). It also triggers an interrupt.
 

Thread Starter

fancy102

Joined Dec 21, 2007
26
hi,

i had write the programme based on the timer. i would like to measure my input which is in pulse form. my problem is i still can get my output. my LCD show nothing at all.

my input of pulse form is 50Hz, so the period is 1/50=0.02 second. my programme is witten as below. can any body help on y there is still no output at all.thanks.

' Hardware configuration
' ======================
DEFINE OSC 20
DEFINE LOADER_USED 1
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4

DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0

DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1

TRISD = %00000000
TRISA= %00000000
TRISC = 255 ' PORTC As input
adcon1=%00000100
CMCON = 7 ' Disable analog comparator
T1CON = 0 ' TIMER1:
' Clock Internal
' Pre-scaller 1:1

' Hardware connection
' ===================
SignalInput VAR PORTC.2

' Variables definition
' ===================
T1ON VAR T1CON.0
PulseHigh VAR WORD
PulseLow VAR WORD
degree VAR BYTE
totalpulse VAR BYTE

' Software/Hardware initialisation
' ================================
Clear
GoSub ClearTimer

Start:
' Waiting for falling edge
' ========================
While SIGNALINPUT=0 : Wend
While SIGNALINPUT=1 : Wend

' Measure low pulse
' =================
T1ON=1 ' start Timer
While SIGNALINPUT=0 : Wend ' wait for next rising edge
T1ON=0 ' stop timer
PULSElow.HIGHBYTE=TMR1H ' store results
PULSElow.LOWBYTE=TMR1L '
pulselow=pulselow / 5 ' 1 tick = 0.2 Usec @ 20MHZ
' Divide by 5 to have 1 uSec
' Resolution

GoSub Cleartimer ' reset Timer1 register (TMR1L, TMR1H)

' Waiting for rising edge
' =======================
While Signalinput=1 : Wend
While signalinput=0 : Wend


' Measure high pulse
' ==================
T1ON=1 ' Start timer
While SIGNALINPUT=1 : Wend ' wait for next falling edge
T1ON=0 ' Stop Timer
PULSEhigh.HIGHBYTE=TMR1H ' Store result
PULSEhigh.LOWBYTE=TMR1L '
pulsehigh=pulsehigh / 5 ' match results to 1 uSec resolution
GoSub Cleartimer ' reset Timer1 register (TMR1L, TMR1H)


' Send results to LCD
' =============================
LCDOut $fe, 1
LCDOut "pulse0=",DEC pulselow

LCDOut $fe,$c0
LCDOut "pulse1=",DEC pulsehigh

totalpulse=pulsehigh+pulselow
degree=pulsehigh/totalpulse*360

LCDOut $fe, 1
LCDOut "degree=",DEC degree
Pause 500
GoTo start


ClearTimer:
TMR1L=0
TMR1H=0
Return


Is the problem relate to the frequensy?or the timer?i really at a lost as what to do. i duno what the problem is.really appreciate your kindness for helping..
 

SgtWookie

Joined Jul 17, 2007
22,230
I'm not familiar with PIC programming, just re-posting his code how it's supposed to look (his original formatting)

Rich (BB code):
    '    Hardware configuration
    '    ======================
         DEFINE OSC 20
         DEFINE LOADER_USED 1
         DEFINE LCD_DREG PORTD
		 DEFINE LCD_DBIT 4

		 DEFINE LCD_RSREG PORTE
		 DEFINE LCD_RSBIT 0

		 DEFINE LCD_EREG PORTE
		 DEFINE LCD_EBIT 1
        
         TRISD = %00000000      
         TRISA=  %00000000 
         TRISC = 255            ' PORTC As input
         adcon1=%00000100
         CMCON = 7              ' Disable analog comparator      
         T1CON = 0              ' TIMER1:
                                ' Clock Internal
                                ' Pre-scaller 1:1
                     
    '    Hardware connection
    '    =================== 
         SignalInput       VAR PORTC.2
		     
    '    Variables definition 
    '    ===================
          T1ON              VAR T1CON.0
         PulseHigh          VAR WORD
         PulseLow           VAR WORD
         degree 			VAR BYTE
         totalpulse			VAR BYTE
         
    '    Software/Hardware initialisation
    '    ================================
         Clear
         GoSub ClearTimer
    
Start:
    '    Waiting for falling edge
    '    ======================== 
         While SIGNALINPUT=0 : Wend
         While SIGNALINPUT=1 : Wend
 
    '    Measure low pulse
    '    =================   
         T1ON=1                     ' start Timer
         While SIGNALINPUT=0 : Wend ' wait for next rising edge
         T1ON=0                     ' stop timer
         PULSElow.HIGHBYTE=TMR1H    ' store results 
         PULSElow.LOWBYTE=TMR1L     ' 
         pulselow=pulselow / 5      ' 1 tick = 0.2 Usec @ 20MHZ
                                    ' Divide by 5 to have 1 uSec
                                    ' Resolution
         
         GoSub Cleartimer           ' reset Timer1 register (TMR1L, TMR1H)

    '    Waiting for rising edge
    '    =======================
         While Signalinput=1 : Wend
         While signalinput=0 : Wend
    
    
    '    Measure high pulse
    '    ==================  
         T1ON=1                     ' Start timer
         While SIGNALINPUT=1 : Wend ' wait for next falling edge
         T1ON=0                     ' Stop Timer
         PULSEhigh.HIGHBYTE=TMR1H   ' Store result
         PULSEhigh.LOWBYTE=TMR1L    '
         pulsehigh=pulsehigh / 5    ' match results to 1 uSec resolution
         GoSub Cleartimer           ' reset Timer1 register (TMR1L, TMR1H)
         

    '    Send results to  LCD
    '    =============================      
         LCDOut $fe, 1
         LCDOut "pulse0=",DEC pulselow
        
         LCDOut $fe,$c0
         LCDOut "pulse1=",DEC pulsehigh

         totalpulse=pulsehigh+pulselow
         degree=pulsehigh/totalpulse*360

         LCDOut $fe, 1
         LCDOut "degree=",DEC degree
         Pause 500
         GoTo start
                  

ClearTimer:
    TMR1L=0
    TMR1H=0
    Return
 

Thread Starter

fancy102

Joined Dec 21, 2007
26
my frequensy is 50Hz and my input of pic change every time depends on the

loading...

input of pic is the phase shift between the volage and current, i am

going to develope a power factor measurement system.

so i using the ratio of time (0 and 1) to get my on time and off time. then the

phase(degree) is measured based on the ratio;

degree=time when 1/(time0+time1) *360

and my problem now is y my lcd show no outout at all though i had connect

all my hardware together.

is it the problem of time calculating?thanks!
 
Top