Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Timing with no WAIT

Status
Not open for further replies.

camerart

Well-Known Member
Hi,

I want to time a count in PIC basic. 18F2431

Like this:

count:
LET A = A +1
waitms 100
goto count

I want to add it into a program LOOP that is time critical, so I don't want to use WAIT. Can someone tell me a better way please.

Camerart.
 
Assorted comments:

Time saved in not using interrupts will be spent in polling a flag.

If you are looking for precise timing, interrupts have more chances to provide so than any non-interrupt based option.

A 10 ms tick based on a TMR2 interrupt with high priority is the most precise you could get and you will be free to do something else in between ticks.

How precise those 10ms should be? Or you want them to ocurre at always the same frequency even not 10ms exactly?
 
Hi,

Thanks both.

I haven't calculated it yet, or tested and example, but I think each step will be between 1/10 to 1/30th of a second, and I think constant. I dont think the timing has to be absolutely precise, but it would help.

Do you have an example of the TMR2 set-up bits and configs for 18F2431 please? I've actually had this explained before, but I can't find my example. From memory, this sounds good though.

C.
 
Polling or interrupt??? The timer settings are the same

T2CON = 64 ' bits 6-3 Post scaler 1:1 thru 1:16
T2CON.TMR2ON = 1 'bit 2 turn timer2 on;
T2CON.T2CKPS1 = 1 ' bits 1-0 Prescaler Rate Select bits
T2CON.T2CKPS0 = 0
PR2 = 139

This will give 10mS.... or as near as possible.... I seem to recall 8Mhz... If it isn't it needs changing.

This web site is where I get my timer info from. http://eng-serve.com/pic/pic_timer.html
 
Hi Ian,

Which part changes the timing? (10ms), is it PR2 number? Yes, 8Mhz.

I'm more used to a variable in the loop where I can change as I need to.

I looked at the website you posted, but even though it looks good, it is a bit advanced for me, but thanks.

C.
 
Hi Ian,

I'm a bit baffled, so would you be good enough to add it into the attached program, so I can get an idea how it works, please?
I'm trying to 'time = time + 1' while affecting the timings of the LOOP as little as possible

C
 

Attachments

  • Timer.txt
    1.9 KB · Views: 295
Last edited:
Hola camerart

I do not want to interfere with the solution that Ian is suggesting.

It seems to me that you are mixing the part where you set with that repeated reading of yours. I dare to say that you have no clear idea yet of what you need.

Here below there is the code I use to set TMR2 to tick every 5ms. Of course I use interrupts and my main loop is completely free for me to do that "something else" I mentioned before.

Code:
;10 - TMR2 module

;Used to materialize a nominal [5 ms] time base. On the next cycle after
;matching PR2, TMR2 resets to 0 and requests an interrupt. The respective ISR
;sets a flag, used by the DECR_COUNTERS routine, called continuously from the
;main loop. Prior exiting the ISR, the interrupt flag is reset. This sequency
;repeats indefinitely.
;******************************************************************************


  IF CLOCK ==4000000        ;4 MHz

;With a 4 MHz xtal, the clock feeding TMR2 through the prescaler is
;4/4 MHz = 1 MHz so the Tcy = 1us.
;To get a 5 ms overflow, we need: 5ms/1us = 5.000 counts.
;We make [4*250*5] =5.000 counts (5 ms), so we set:
;prescaler 1:4  -  PR2 =250  -  postscaler  1:5

  BCF T2CON,T2CKPS1       ;0
  BSF T2CON,T2CKPS0       ;1 prescaler 1:4

  LOADREG PR2,250

  BCF T2CON,T2OUTPS3      ;0
  BSF T2CON,T2OUTPS2      ;1
  BCF T2CON,T2OUTPS1      ;0
  BCF T2CON,T2OUTPS0      ;0 postscaler 1:5

  BSF T2CON,TMR2ON        ;start TMR2
  ENDIF
;******************************************************************************


LOADREG   MACRO REG,VAL
            MOVLW VAL
            MOVWF REG
            ENDM

Maybe, prior deciding on interrupts or polling you should make really clear one thing: what do you actually want to do. After that you should go to see how to have it done.
 
Sorry for the delay!! Really busy day at work today!!! I am going bald VERY quickly!!

I'm just running through all the data!!! This chip has PWM control... Full H bridge...!!!

You are using CCP1 and CCP2 for your bridge.... Unfortunately TM2 is out of bounds as the CCP modules are using them..

We must use timer 0 timer 1 or timer 5...

Timer 1 isn't the same as timer 2 so I'll need to a bit of homework on this... It's a real shame Oshonsoft doesn't support the PWM modules.... Maybe I could write the low level functions for you!!

I'll get back to you later on this..
 
Hola atferrari,

True I am forming an idea, and I have difficulty explaining my thoughts, so I'll try again:)

I have a program that moves a servo motor/arm to received data input angles. (I haven't attached the actual program, as it would complicate things even more) I have enclosed a test program, just to play with the idea.

Because of various reasons, I'm having difficulty stopping the motor at the SET-POINT (from each data input) I've tried a dead band, but this needs such variation depending on motor voltage, speed etc, that it doesn't work properly.

My idea is to add what I am calling a PACE. This will move as I would like the motor to move, but slightly slower. Then I will make the motor follow it within a 1-2 degrees. At >=2 degrees, I will try full PWM. At 1 degree I will try 0 or slow PWM and at the SET-POINT I will brake the motor, and see what results, I get.

I hope this clarifies things.

C.
 
Using timer 1

Code:
'182431 INT 8Mhz TIMER TEST 10.00 14.12.15
'This needs both sides of each H-Bridge pair connected together
Define SIMULATION_WAITMS_VALUE = 1
Define CONFIG1L = 0x00
Define CONFIG1H = 0x08
Define CONFIG2L = 0x0c
Define CONFIG2H = 0x20
Define CONFIG3L = 0x04
Define CONFIG3H = 0x80
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40

'PORTC.2 as CCP1 [pwm1] Forward
'PORTC,1 as CCP2 [pwm2] Reverse
'When running in Oshonsoft Sim
'to view PWM use Scope, Normal , Settings, display width 1000
'to check use Hardware UART

ADCON0 = %01000001
ADCON1 = %00000000
OSCCON = %01110010  'internal 8Mhz clock

Dim duty1 As Byte
Dim duty2 As Byte
Dim pwm1 As Byte
Dim pwm2 As Byte
Dim time As Byte

TRISA = %00000001
TRISB = %00000000
TRISC = %10001000

'assign PWM pins
PWMon 1, 8  'RC1 CCP1, Mode at 8MHz at 7812Hz, to change this look at the Manual Tables
PWMon 2, 8  'RC2 CCP2

duty1 = 100
duty2 = 155

'set up timer 1
T1CON.T1CKPS1 = 0  'bits 5 - 4 prescaler rate Select bits
T1CON.T1CKPS0 = 1     ' Bit 4
T1CON.T1OSCEN = 1     ' Bit 3 Timer1 Oscillator Enable Control Bit 1 = On
T1CON.T1SYNC = 1     ' Bit 2 Timer1 External Clock Input Synchronization Control Bit...1 = Do Not synchronize external clock Input
T1CON.TMR1CS = 0     ' Bit 1 Timer1 Clock Source Select Bit...0 = Internal clock (FOSC/4)




'testing only
main:
If RA0 = 1 Then
   RB4 = 1
Else
   RB4 = 0
Endif
Toggle RB6
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'wait for 10mS
TMR1H = 216
TMR1L = 242   ' Prescale for 10mS
T1CON.TMR1ON = 1
While
   PIR1.TMR1IF
Wend
T1CON.TMR1ON = 0
time = time + 1
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
' carry on

If PORTA.3 = 0 Then
PWMduty 2, 0
'ASM:  bcf CCP2CON,DC2B0
'ASM:  bcf ccp2con,dc2b1
'ASM:  movlw 0x00
'ASM:  movwf ccpr2l
'Adcin 0, duty1>>>>>>>>>>>>>>>>>>>>>>>>>>>>.Not supported
PWMduty 1, duty1
Endif

If PORTA.3 = 1 Then
PWMduty 1, 0
'ASM:  bcf CCP1CON,DC1B0
'ASM:  bcf ccp1con,dc1b1
'ASM:  movlw 0x00
'ASM:  movwf ccpr1l

'Adcin 0, duty1>>>>>>>>>>>>>>>>>>>>>>>Not supported
PWMduty 2, duty1
Endif

Goto main
 
Hi Ian,

Too late! I'm bald already:)

The REAL program has a PWM section written by Eric Gibbs, and works ok. (Hero worship) Eric also wrote a test program, for QUAD simulated inputs, and this is also working fine. (More hero worship) To save you time, effort and hair, would you please ignore PWM and use on/off, my motor will be happy, also the QUAD. If you could add a TIMER section into the TEST program I posted, or another example, I can then play with it, then add it into the REAL program.

Thanks, C.

Sorry for the delay!! Really busy day at work today!!! I am going bald VERY quickly!!

I'm just running through all the data!!! This chip has PWM control... Full H bridge...!!!

You are using CCP1 and CCP2 for your bridge.... Unfortunately TM2 is out of bounds as the CCP modules are using them..

We must use timer 0 timer 1 or timer 5...

Timer 1 isn't the same as timer 2 so I'll need to a bit of homework on this... It's a real shame Oshonsoft doesn't support the PWM modules.... Maybe I could write the low level functions for you!!

I'll get back to you later on this..
 
To be honest... I don't really know why you need a timer to measure 10mS and not waitms 10..

I should imagine you just need a heartbeat... Each 10mS that passes the time is incremented!!!... Is this what you need??
 
Hi I,

The program is not compiling in Oshonsoft (Is it an Oshonsoft problem?)

It doesn't like:
While
PIR1.TMR1IF
Wend

What should happen is 'say' the input reads 30, then a string of degrees: 1-20ms-2-20ms-3-20ms-----------27-20ms-28-50ms-29-100ms-30-stop. (Remember, I'm making it up as I go along).

I think the REAL program seems sensitive to time changes, as it seemed to change when I removed unwanted lines. I'm no expert so it could have been something else??

C.

C.
To be honest... I don't really know why you need a timer to measure 10mS and not waitms 10..

I should imagine you just need a heartbeat... Each 10mS that passes the time is incremented!!!... Is this what you need??
To be honest... I don't really know why you need a timer to measure 10mS and not waitms 10..

I should imagine you just need a heartbeat... Each 10mS that passes the time is incremented!!!... Is this what you need??
 
Hi Ian,

It wouldn't compile.

EDIT: Changed to PIR1.TMR1IF and now it simulates. I'll be back.
Thanks.

C
 

Attachments

  • TMR1IF.gif
    TMR1IF.gif
    18 KB · Views: 277
Last edited:
Hi,

I am trying to get a simulation in Oshonsoft, with the above program, but it appears to me that TIME is incrementing every LOOP not as the timer set-up, am I correct?

See attached image, 4 counts in 100ish uS.

EDIT: Today I tried a simple 50ms wait (My original post asked for 100ms) The counter was ok, but there was hunting around stopping at the SET-POINT. I might be able to iron it out if there's difficulty getting a TMR to work.

C.
 

Attachments

  • 10ms.gif
    10ms.gif
    118.1 KB · Views: 271
Last edited:
While NOT PIR1.TMR1IF

My mistake sorry... If you want to run the program as fast as possible, but only updating time once every 10mS

if PIR1.TMR1IF then
time = time + 1
PIR1,TMR1IF = 0
TMR1H = 216
TMR1L = 242​
endif
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top