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.

PIC16F877A Timer1 problem

Status
Not open for further replies.

lignin

Junior Member level 2
Junior Member level 2
Joined
Apr 27, 2013
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,436
my aim is simple : flash led every 1 second ; I am using mplab and my code is below. I think my code is correct but in simulation , led is not flashing. please help me whats the problem
Code:
#include<pic.h>
#include<PIC16F877A.h>

#define _XTAL_FREQ 4000000;
int i=0;

void init(void);

void main()
{
	init();
	while(1);
}

void init()
{
	TRISB=0X00;
	PORTB=0X00;
	GIE=1;	
	T1OSCEN=0;
	T1SYNC=1;	
	T1CKPS1=0;	
	T1CKPS0=0;
	TMR1CS=0;
	TMR1H=-50000/256;
	TMR1L=-50000%256;
	TMR1IE=1;
	TMR1IF=0;
	TMR1ON=1;
	
}

static void interrupt
timer1_int(void)
{
	if(TMR1IF)
	{
	i++;
	if(i==20)
	{
	RB1=!RB1;
	i=0;
	}
	TMR1IF=0;
	TMR1ON=0;
	TMR1H=-50000/256;
	TMR1L=-50000%256;
	TMR1ON=1;	
	}
}
 

Hello lignin ,

Could you please explain what is this

{
i++;
if(i==20)
{
RB1=!RB1;
i=0;
}

RB1=!RB1;??

If you want to toggle you pin use RB1=~RB1;

!= is a Relational Operator. please correct me if I am wrong.

Best regards,
 

I can add a comment: there is a potential danger in using the "RB1=~RB1;" method that the bit may not reliably toggle. It depends on the instructions produced by the compiler. The reason is that it reads RB1 then writes it straight back, if anything, such as capacitance on the pin prevents the level changing during the length of the instruction being executed, it may read the wrong pin state back. Read up on "RMW" for more details.

A reliable method is to use the code to toggle a bit in an internal register then write the bit out to the port. In other words, instead of reading the present state from the pin and reversing it, keep an independent bit elsewhere and just write it to RB1.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top