PDA

View Full Version : Simple TMR1 application example needed



flotulopex
- 3rd February 2010, 19:59
Hello,

I want to count, as an example, how much time (or instruction cycles) a FOR/NEXT loop takes to do its count using TMR1 in Timer mode.

So I made this piece of code but I can never read any result in the PIC's memory (I have the PIC connected to my PICKit2 so I can readout the memory easely).

What am I doing wrong?


<html><head></head><body><!--StartFragment--><pre><code><font color="#000080"><i>' Fuses PIC16F88
</i></font><font color="#008000">@ DEVICE INTRC_OSC_NOCLKOUT
@ DEVICE PROTECT_OFF
@ DEVICE WDT_ON
@ DEVICE PWRT_OFF
@ DEVICE MCLR_OFF
@ DEVICE BOD_OFF
@ DEVICE LVP_OFF
@ DEVICE CPD_OFF
@ DEVICE DEBUG_OFF
@ DEVICE CCPMX_OFF
@ DEVICE2 IESO_OFF
@ DEVICE2 FCMEN_OFF

</font><font color="#000080"><i>'_________________________________________________ ___________________________
' Registers 76543210
</i></font>OPTION_REG = %10000000 <font color="#000080"><i>' PORTB Pull-Ups disabled
</i></font>ANSEL = %00000000 <font color="#000080"><i>' Disable analog inputs
</i></font>ADCON0 = %00000000 <font color="#000080"><i>' A/D Module is OFF
</i></font>CMCON = %00000111 <font color="#000080"><i>' Comparator Module is OFF
</i></font>INTCON = %00000000 <font color="#000080"><i>' All Interrupts are disabled
</i></font>TRISA = %00000000 <font color="#000080"><i>' Set Input/Output
</i></font>PORTA = %00000000 <font color="#000080"><i>' Ports High/Low
</i></font>TRISB = %00000000 <font color="#000080"><i>' Set Input/Output
</i></font>PORTB = %00000000 <font color="#000080"><i>' Ports High/Low
</i></font>T1CON = %00000000 <font color="#000080"><i>' TMR1 settings

'_________________________________________________ ___________________________
' Variables
</i></font>Ctr_A <b>VAR BYTE </b><font color="#000080"><i>' counter
</i></font>Ctr_B <b>VAR WORD </b><font color="#000080"><i>' counter
</i></font>Mem_Loc <b>VAR WORD </b><font color="#000080"><i>' memory Location Pointer
</i></font>Result <b>VAR WORD</b>(10) <font color="#000080"><i>' holds the TMR1 value

</i></font>Ctr_A = 0
Ctr_B = 0
Mem_Loc = $FA8
Result = 0

<font color="#000080"><i>'_________________________________________________ ___________________________
' Program
</i></font><b>FOR </b>Ctr_A = 0 <b>TO </b>9 <font color="#000080"><i>' 10 count loop

</i></font>TMR1H = 0 <font color="#000080"><i>' reset Timer
</i></font>TMR1L = 0 <font color="#000080"><i>' reset Timer
</i></font>T1CON.0 = 1 <font color="#000080"><i>' start Timer

</i></font><b>FOR </b>Ctr_B = 0 <b>TO </b>100 : <b>NEXT </b>Ctr_B <font color="#000080"><i>' let TMR1 increment a little

</i></font>T1CON.0 = 0 <font color="#000080"><i>' stop Timer
</i></font>Result.LowByte = TMR1L <font color="#000080"><i>' store LOW TMR1's value to Result
</i></font>Result.HighByte = TMR1H <font color="#000080"><i>' store HIGH TMR1's value to Result
</i></font>Result[Ctr_A] = Ctr_A <font color="#000080"><i>' assign Result's index

</i></font><b>NEXT </b>Ctr_A <font color="#000080"><i>' loop

</i></font><b>ERASECODE </b>Mem_Loc <font color="#000080"><i>' free-up 32 mem locations

</i></font><b>FOR </b>Ctr_A = 0 <b>TO </b>9 <font color="#000080"><i>' 10 count loop
</i></font><b>WRITECODE </b>Mem_Loc, Result[Ctr_A] <font color="#000080"><i>' store Result(x) var
</i></font>Mem_Loc = Mem_Loc + 1 <font color="#000080"><i>' increment mem location
</i></font><b>NEXT </b>Ctr_A <font color="#000080"><i>' loop

</i></font><b>END</b></code></pre><!--EndFragment--></body></html>

Darrel Taylor
- 4th February 2010, 08:46
Hi Roger,

It's working..

But the 16F88 defaults to 31.25khz internal oscillator.
So it takes a while to complete.

If you read it back too quick, you don't see anything.

Adding ...
&nbsp; &nbsp; OSCCON = %01100010 ; Set Internal OSC to 4mhz.
it takes a fraction of a second to complete.

And this line overwrites the Timer results, so you might want to comment it...
Result[Ctr_A] = Ctr_A ' assign Result's index

hth,

Added:
F88's Flash memory is written in blocks of 4, so you'll need to change the length of the data.

flotulopex
- 4th February 2010, 09:52
Thanks Darrel,

After I posted my message, I added a blinky and noticed the "very slow motion".

Now I use a 20MHz crystal so I corrected the configuration fuse and added the DEFINE information.

But new problems come up...

To get the program starting, I need to approach my finger close to the xtal (?!) - it looks like some capacitance issue. I added a 10Mohms resistor in parallel with the xtal and now, the program starts everytime I power the PIC.

On the other hand, my PICKit2 programmer won't recognize my PIC when I have a 20MHz xtal intalled on the breadboard. I don't know what this comes from. Maybe one of you does?