Jump to content

Recommended Posts

I have been playing around with trying to get the timers working on the PIC16F688 and I have had zero success. Here is the code... which I think should work. Please tell me what is wrong with it.

Details of the board (including schematic) are on my blog... http://roboslave.blogspot.com/ (the schematic shows an error in the external power... but that should not hurt me here. I can get LEDs to flash.)

 

// -----------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------
#include "system.h"				// Include general file

// -----------------------------------------------------------------------
// Configuration Bits
// -----------------------------------------------------------------------
#pragma DATA _CONFIG, _PWRTE_OFF & _WDT_OFF & _CPD_OFF & _XT_OSC & _CP_OFF
#pragma CLOCK_FREQ 4000120	//Set clock frequency (20000600/4)

// -----------------------------------------------------------------------
// Defines
// -----------------------------------------------------------------------
#define	TimerLED	portc.0
#define	SenseLED	portc.1

// -----------------------------------------------------------------------
// Interrupt
// -----------------------------------------------------------------------
void interrupt( void )
{
//if( pir1 & (1<<TMR1IF) )		  // Handle timer1 interrupt
if (pir1.TMR1IF == 1)
{
	pir1.TMR1IF = 0;			// Reset interrupt flag
	TimerLED = !TimerLED;		// Toggle Timer LED
}
}

// -----------------------------------------------------------------------
// Main Program
// -----------------------------------------------------------------------
void main( void )
{
trisc = 0x00;			  //Configure port C
portc = 0x00;			  //Initialize port C

tmr1h = 0x00;			// should be cleared before enabling interrupts
tmr1l = 0x00;

intcon.GIE		= 1;	//Global Interrupt Enable bit
intcon.PEIE		= 1;	//Peripheral Interrupt Enable bit
pie1.TMR1IE		= 1;	//Timer1 Overflow Interrupt Enable bit

cmcon1.T1GSS	= 0;	//Timer1 Gate Source Select bit??????????

t1con = 0b00110001;

 while (1)
{
}
}

 

Thank you for your help.

-Chris

Link to post
Share on other sites

I think is because of the analog input AN4 multiplexed with digital I/O pin RC0.

Analog features are enabled by default and disable the digital input buffer that reads '0's as a result. Adding the line:

 

ansel = 0b11001111;

 

at the start of your program will disable AN4 and AN5 and you get full I/O features for RC<1:0> and, at the end of the day, the led will flash under timer control.

 

As an aside, the CLOCK_FREQ pragma is the FOSC not the TCY (FOSC/4) frequency!

Link to post
Share on other sites

It works!

 

I think is because of the analog input AN4 multiplexed with digital I/O pin RC0.

Analog features are enabled by default and disable the digital input buffer that reads '0's as a result. Adding the line:

 

ansel = 0b11001111;

 

at the start of your program will disable AN4 and AN5 and you get full I/O features for RC<1:0> and, at the end of the day, the led will flash under timer control.

 

As an aside, the CLOCK_FREQ pragma is the FOSC not the TCY (FOSC/4) frequency!

I am disappointed that something so simple was holding me back. Thank you for pointing that out.

 

As an aside, the CLOCK_FREQ pragma is the FOSC not the TCY (FOSC/4) frequency!

I have added delay_ms(10) between blinks and then timed them with my old oscilloscope. I was getting 5X what I was asking for. So I turned down the frequency and that solved the problem.... but I'm not sure why there is a problem. The interesting thing is the timer does not seem to be effected by the change... I don't get it.

Link to post
Share on other sites
... The interesting thing is the timer does not seem to be effected by the change...

The CLOCK_FREQ pragma change only affects the software delay routines. Timers shouldn't get effected by those change, though. The blink period will only depend by the prescaler - 1:8 with your setting - and FOSC. You should get a TMR1IF overflow every TCY*8*65536 i.e. 524ms @ FOSC=4MHz which is exactly what I did.

 

I'm not sure why you didn't get the correct time with delay_ms. Perhaps your XTAL is not oscillating to the correct frequency value ?

Try _HS_OSC rather than _XT_OSC as a config bit set.

Link to post
Share on other sites
... The interesting thing is the timer does not seem to be effected by the change...

I'm not sure why you didn't get the correct time with delay_ms. Perhaps your XTAL is not oscillating to the correct frequency value ?

Try _HS_OSC rather than _XT_OSC as a config bit set.

I tried the _HS_OSC, and get the same thing. I have been using another board (from my first project http://roboslave.blogspot.com/search/label...rolled%20Servos ) and that is working correctly. Using the same code back to the original board... it's 5x slower. I did notice that the working board xtal says "20.000N" on the top, and the non-working board has "20.0006" on the top.

 

The second, non working board xtal was bought from DigiKey part number 300-8507-ND (20.000MHz 18PF).

http://search.digikey.com/scripts/DkSearch...amp;x=0&y=0

 

However, I think I am using 22pF instead of 18pF. I don't know enough to say that is the problem. I'll find my other xtal rated for 22pF and give that a try.

Link to post
Share on other sites
I'm not sure why you didn't get the correct time with delay_ms. Perhaps your XTAL is not oscillating to the correct frequency value ?

Try _HS_OSC rather than _XT_OSC as a config bit set.

I finally figured out the problem... but I'm surprised the chip was running at all. My initial revision of this board had the regulated 5V power coming in as VSS, which is wrong (should be VDD). This schematic is incorrect. I will correct the schematic and show the 'was' 'is' versions on my blog.

Tach%20Schematic.jpg

 

VSS is supposed to be connected to ground. I added a jumper wire from the proper VSS on the chip out to ground. This fixed the timing issue.

 

Ettore, thank you for your continuing support.

 

I will show the updated code and schematic here and on the blog shortly.

Link to post
Share on other sites

Hi Chris,

 

You may want to add some decoupling capacitors to your circuit. A 100nF ceramic at least across the PIC.

 

Linear regulators sometime like some C on the input and output pins to prevent them from singing if on long supply wires.

 

Cheers

 

Reynard

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...