Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Help with Timer 0 of pic16f505

Status
Not open for further replies.

ankitvirdi4

Member level 4
Member level 4
Joined
Mar 13, 2012
Messages
70
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,288
Location
India
Visit site
Activity points
1,928
Hello guys I wrote a simple code in Mplab to turn all leds on PORTC ON the code is

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
#include <htc.h> 
#include "delay.h"  
void main()   
{   
TRISC=0x00;       // Set port to output     
PORTC=0xff; 
DelayMs(100); 
PORTC=0x00; 
DelayMs(100); 
 }


All the Leds seem to blink except the one connected to RC5/T0CKI that is the last one. I tried setting and clearing T0SC in OPTION registor still that led wont turn on.. No pull ups are mentioned in the data sheet. please help.Thank you.
 

All the Leds seem to blink except the one connected to RC5/T0CKI that is the last one. I tried setting and clearing T0SC in OPTION registor still that led wont turn on.. No pull ups are mentioned in the data sheet.

I do not see any indication you've cleared T0CS (bit 5) of the OPTION Register in the code you've posted.

Are you sure you cleared bit 5 and not bit 4, the register bits are numbered starting with zero?

Code:
OPTION &= 0xDF;

Reference: PIC12F508/509/16F505 Datasheet, Section: 5.2 PORTC (PIC16F505 Only), Page: 31
Note:
On power-up, TOCKI functionality is
enabled in the OPTION register and must
be disabled to allow RC5 to be used as
general purpose I/O.

And Section: 5.3 TRIS Registers
5.3 TRIS Registers

The Output Driver Control register is loaded with the
contents of the W register by executing the
TRIS f instruction. A ‘1’ from a TRIS register bit puts
the corre-sponding output driver in a High-Impedance mode.
A‘0’ puts the contents of the output data latch on the
selected pins, enabling the output buffer. The excep-
tions are RB3/GP3, which is input only and the T0CKI
pin, which may be controlled by the OPTION register.
See Register 4-3 and Register 4-4

You'll need to clear bit 5 of the OPTION Register for the TRISC Register to have an effect.

bit 5
T0CS
: Timer0 clock Source Select bit
1 = Transition on T0CKI pin (overrides TRIS on the T0CKI pin)
0 = Transition on internal instruction cycle clock, Fosc/4

BigDog
 
Clearing T0CS does it I might have not done it correctly last time. Thanks a lot BigDog :) Although
Code:
OPTION &= 0xDF;
gives an error "can't generate code for this expression"
Code:
#include <htc.h>
#include "delay.h" 
void main()  
{  
    OPTION = 0xDF;
    TRISC=0x00;       // Set port to output 
    PORTC=0xff;
    DelayMs(1);        // Turn them all on (Assuming a high on the pin will light it).  
    while(1) ; 
}
Works fine.
can you please tell me why I got error for &= I am using MPLAB IDE v8.91 with High Tech C compiler.
 

Although
Code:
OPTION &= 0xDF;
gives an error "can't generate code for this expression"
can you please tell me why I got error for &= I am using MPLAB IDE v8.91 with High Tech C compiler.

Please pardon me, it has been a while since I last utilized a Baseline PIC16F series device.

Alright, I believe I have determined the source of that particular compiler error, however before we cover that topic, lets review the purpose and affects of the &= operator.

Code:
OPTION &= 0xDF;

Which is equivalent to:

Code:
OPTION = OPTION & 0xDF;

A bitwise AND (&) used with a bitmask (0xDF in this case), clears only those bits in OPTION which correspond to 0's in the bitmask, while leaving the other bits of OPTION unchanged.

Notice in the long form:

Code:
OPTION = OPTION & 0xDF;

The contents of the OPTION register must first be READ, then bitwise AND with the bitmask value of 0xDF, before being WRITTEN back to the OPTION register.

Reference: PIC12F508/509/16F505 Datasheet, Section: 4.5 OPTION Register, Page: 24
4.5 OPTION Register

The OPTION register is a 8-bit wide, write-only register,
which contains various control bits to configure the
Timer0/WDT prescaler and Timer0.

By executing the OPTION instruction, the contents of
the W register will be transferred to the OPTION regis-
ter
. A Reset sets the OPTION<7:0> bits.

The above section of the devices datasheet indicate the OPTION register is WRITE only, therefore statements like the following:

Code:
OPTION = OPTION & 0xDF;
or
Code:
OPTION &= 0xDF;

Will produce a compiler error as they require the contents of the OPTION register be READ acted upon before being WRITTEN back to the OPTION register.

Obviously, as the OPTION register is WRITE ONLY, any attempts to READ it will result in an error.

It should also be noted, the reason for the OPTION register being WRITE ONLY is the register is not memory mapped as indicated in the Address column of the table below:



As such, Microchip has provide special instructions to deal with these WRITE ONLY registers, such as OPTION, which copies the contents of the W register to the OPTION register.

Therefore the following C code:

Code:
OPTION = 0xDF;

Actually translates into assembly code as the following:

Code:
MOVLW             0xDF
OPTION

The WRITE ONLY registers, TRISB and TRISC are handled in a similar fashion with the TRIS instruction.



I should also point out the statement:

Code:
T0CS = 0;

Does not function as expected, due to the fact T0CS is not mapped to a bit position of the OPTION register, but actual a numerical value representing the bits position within the OPTION register.

Normally you would use it as such:

Code:
OPTION = nGPWU | nGPPU  | T0CS | T0SE | PSA | PS2 |PS1 |PS0;  // Sets all the bits of the OPTION register

Or

Code:
OPTION = nGPWU | nGPPU  | T0SE | PSA | PS2 |PS1 |PS0;  // Sets all the bits of the OPTION register except the T0CS which is cleared


BigDog
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top