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.

MPLAB - C18 -Timer0 trouble

Status
Not open for further replies.

binzer

Member
For some reason even though I turned off interrupts I seem to be getting one (Stack Underflow) and I am not sure why. Trying to learn to use timers. I am using MPLAB SIM, here is the code, there is a bunch of extra stuff in there for the rest of my project but I do not think that is the problem. I will take it out and retry also.

Code:
// JuneBug Info
// Buttons RB0, RB2, RB5.
// Analog AN1, AN3
// IR RB0
// LED's RA0, RA6, RA7
//
// configuration
#pragma*config OSC = INTIO2, WDT = OFF, LVP = OFF, PWRT = OFF, BOR = OFF, FSCM = OFF

#include <stdlib.h>
#include <p18f1320.h>
#include <usart.h>
#include <adc.h>
#include <delays.h>
#include <portb.h>
#include <timers.h>

// unsigned char config;
// unsigned int spbrg;

char str[8]="At Zero";
unsigned int tres;

int reslta0;
int reslts0;
char A1[5];

int reslta3;
int reslts3;
char B1[5];


void main(void)
{
	
	OSCCON = 0x72;
	
//	OSCCONbits.IRCF0=1;
// 	OSCCONbits.IRCF1=1;
// 	OSCCONbits.IRCF2=1; //set for 8Mhz

	OpenTimer0( TIMER_INT_OFF &
	T0_16BIT &
	T0_SOURCE_INT &
	T0_PS_1_256 );
	
while(1);	
	{
		}

// while(ReadTimer0 !=0) ;
//	{
//	}
//
//	putsUSART(str); //print string
}
 
Last edited:
I copied you code and created a project with it and the 18F1320i.lkr file. I did not see a problem on the simulator or the junebug.

Are you sure the chip is programming, try erasing it first.
 
Last edited:
I copied you code and created a project with it and the 18F1320i.lkr file. I did not see a problem on the simulator or the junebug.

Are you sure the chip is programming, try erasing it first.

I can try that, but for now I am running it in simulator on MPLAB, so the chip / Junebug should not really be in the picture (correct)??

When I was trying with the chip and not getting far I thought I would try the sim and see if I could track down what I did wrong. I did at first expect that it could be an interrupt and while watching it in sim it looked like it was getting one, kept looping thru the section of code in
T0open.c involving the interrupt??
Code:
void OpenTimer0(unsigned char config)
{
  T0CON = (0x7f & config);  // Configure timer, but don't start it yet
  TMR0H = 0;                // Reset Timer0 to 0x0000
  TMR0L = 0;
  INTCONbits.T0IF = 0;      // Clear Timer0 overflow flag

  if(config&0x80)           // If interrupts enabled         <-- looping
    INTCONbits.T0IE = 1;    // Enable Timer0 overflow interrupt
  else
    INTCONbits.T0IE = 0;

  T0CONbits.TMR0ON = 1; <-- looping
}
#endif

Ok, another clue, it looks like the timer makes it to a count of 3 before I get "stack underflow", then it goes back to the start of the program and restarts the timer. Since I am not using any other parts of the pic I can't see why this is happening especially in sim.

Here is the code with everything I don't need taken out.
Code:
// JuneBug Info
// Buttons RB0, RB2, RB5.
// Analog AN1, AN3
// IR RB0
// LED's RA0, RA6, RA7
//
// configuration
#pragma*config OSC = INTIO2, WDT = OFF, LVP = OFF, PWRT = OFF, BOR = OFF, FSCM = OFF

#include <stdlib.h>
#include <p18f1320.h>
#include <timers.h>

void main(void)
{	

	OSCCON = 0x72;
	INTCONbits.T0IE	= 0;

	OpenTimer0(TIMER_INT_OFF &
	T0_16BIT &
	T0_SOURCE_INT &
	T0_PS_1_1 );

while(1){
	}

}
 
Last edited:
I ran the first code you posted with both the simulator and the junebug. Neither had the problem. There should be no problem with the CODE. :D

Try it on the junebug. Maybe the simulator is the problem in your case.
 
Last edited:
I ran the first code you posted with both the simulator and the junebug. Neither had the problem. There should be no problem with the CODE. :D

Try it on the junebug. Maybe the simulator is the problem in your case.

I tried it, no dice. I put an led toggle in the while loop, which it should get to, but with the timer code in there it never gets there. Just in case of a bad timer I switched it to Timer 1, same thing on the Junebug and in sim.
While in sim I put a watch on INTCON1 to make sure int's are off. I am confused. I am new at this but...

Code:
// JuneBug Info
// Buttons RB0, RB2, RB5.
// Analog AN1, AN3
// IR RB0
// LED's RA0, RA6, RA7
//
// configuration
#pragma*config OSC = INTIO2, WDT = OFF, LVP = OFF, PWRT = OFF, BOR = OFF, FSCM = OFF

#include <stdlib.h>
#include <stdio.h>
#include <p18f1320.h>
#include <p18cxxx.h>
#include <delays.h>
#include <timers.h>

void main(void)
{	
	TRISA = 0x00;
	LATAbits.LATA0 = 0x01;
	OSCCONbits.IRCF0=1;
 	OSCCONbits.IRCF1=1;
 	OSCCONbits.IRCF2=1; //set for 8Mhz

	
//	OpenTimer1(TIMER_INT_OFF &
//	T1_8BIT_RW &
//	T1_SOURCE_INT &
//	T1_PS_1_1 );
	
while(1){
	LATAbits.LATA6 = 0x0;
	
	LATAbits.LATA6 = 0x1;
	}


}
 
Last edited:
You need to OR the Timer flags together. Because you are using AND you effectively pass zero to OpenTimer1 which selects the external oscillator. The external oscillator shares the debug pins and stops the debugger in it's tracks. It also enables interrupts.:eek:

BTW, you shouldn't have that * on the config line.

<edit> It appears you can use either AND or OR as it's user configurable. This is why I never use the Microchip libraries</edit>

Mike.
 
Last edited:
Timer1 has a setting that can cause the chip to be unusable (but fixable). Stay away from it until you are up to speed.

Again, I compiled your code and it works . I am attaching a hex file that you should be able to load and run. It is from the source provided here which is similar to yours execpt it blinks a LED on the Junebug. This source works even when the call to open_timer0 is enabled. (as did yours)

The hex file was changed from bnzr.hex to bnzr.hex.txt to get the fourm to attach it.

I hate to say this buy I suspect some form of operator error. Post the output from your build and PICkit2 windows. There has to be some problem. The source code is good.

3v0
 

Attachments

  • main.c
    769 bytes · Views: 237
  • binzr.hex.txt
    1.6 KB · Views: 166
Last edited:
It is easy enough to check what you are passing into the open timer call. Declare t0arg as a char and replace the call with this. Also you need to use the include for timers.h in the code I posted.

Code:
        t0arg = TIMER_INT_OFF &
	T1_8BIT_RW &
	T1_SOURCE_INT &
	T1_PS_1_1 ;
	OpenTimer0(t0arg);

so it becomes

Code:
// JuneBug Info
// Buttons RB0, RB2, RB5.
// Analog AN1, AN3
// IR RB0
// LED's RA0, RA6, RA7
//
// JuneBug Info
// Buttons RB0, RB2, RB5.
// Analog AN1, AN3
// IR RB0
// LED's RA0, RA6, RA7
//
// configuration
#pragma*config OSC = INTIO2, WDT = OFF, LVP = OFF, PWRT = OFF, BOR = OFF, FSCM = OFF

#include <p18f1320.h>
#include <timers.h>

void delay(long t)   
{
  int i;
  do i = i *t; 
  while(--t);
}

void main(void)
{	

  char t0arg;
  
	OSCCONbits.IRCF0=1;
 	OSCCONbits.IRCF1=1;
 	OSCCONbits.IRCF2=1; //set for 8Mhz

	TRISA = 0x01;
	LATAbits.LATA0 = 0x01;	
	
	t0arg = TIMER_INT_OFF &
	T1_8BIT_RW &
	T1_SOURCE_INT &
	T1_PS_1_1 ;
	OpenTimer0(t0arg);
	
  while(1)
  {
	  LATAbits.LATA6 = 0x0;
	  delay(10000);
	
	  LATAbits.LATA6 = 0x1;
    delay(10000);
	}


}

TIMER_INT_OFF is defined as
#define TIMER_INT_ON 0b10000000 // Interrupts enabled
The value for t0Arg from the debugger is 0x0D. The hight order bit is not set so you do not have this interrupt enabled. You could also step through the call to OpenTimer0 to see that is is not setting the interrupt.
 
Last edited:
Timer1 has a setting that can cause the chip to be unusable (but fixable). Stay away from it until you are up to speed.

OK, I am there, not sure what I did but the chip ID is reading 0x00! ARGH!!
The code was working, found a problem with one of the lib settings.
Again, I compiled your code and it works . I am attaching a hex file that you should be able to load and run. It is from the source provided here which is similar to yours execpt it blinks a LED on the Junebug. This source works even when the call to open_timer0 is enabled. (as did yours)

The hex file was changed from bnzr.hex to bnzr.hex.txt to get the fourm to attach it.

I hate to say this buy I suspect some form of operator error. Post the output from your build and PICkit2 windows. There has to be some problem. The source code is good.

3v0

Yes, an operator error, something I set for the lib's but did not stay set, not sure why.
Now I have to find how to reset the chip ID. It's been an interesting night/morning. I gotta get some sleep as we are getting a blanket of snow.

I'll do more reading and see if I can fix the chip ID.

As always thanks guys for your help, I'm getting there, maybe a little slow, but getting there.
Mike
 
You are calling OpenTimer0 with Timer1 flags.

However, something strange is happening here, the value passed is indeed 0x0d and the first line of OpenTimer1 is T1CON = (0x7e & config); which sets bit 3 of T1CON and enables the timer1 oscillator.

<edit>
The open function needs 1 more parameter,
Code:
    OpenTimer1(TIMER_INT_OFF &
    T1_8BIT_RW &
    T1_SOURCE_INT &
    [COLOR="Red"]T1_OSC1EN_OFF &[/COLOR]
    T1_PS_1_1 );
</edit>

Mike.
 
Last edited:
You are calling OpenTimer0 with Timer1 flags.

However, something strange is happening here, the value passed is indeed 0x0d and the first line of OpenTimer1 is T1CON = (0x7e & config); which sets bit 3 of T1CON and enables the timer1 oscillator.

Mike.
Using timer1 flags for timer0 is my mixup. Binzer changed the call in his code from timer0 to timer1. I changed it back without changing the flags too. I knew I should have parsed the value but was too tired/lazy. It is about 2 AM here. But that is an excuse rather then a reason :)

But I do not think it has anything to do with the original problem.

@Binzer
To fix the chip you need to erase it. Use the PICkit2 tool which lets you set the chip type manualy. (drop down box next to device). Then erase the chip a few times. It is sort of like herding pigs but after a a few tries I got MPLAB to read the correct ID.

I still want to see the output windows info from BUILD and PICkit2.
 
Last edited:
@Binzer
To fix the chip you need to erase it. Use the PICkit2 tool which lets you set the chip type manualy. (drop down box next to device). Then erase the chip a few times. It is sort of like herding pigs but after a a few tries I got MPLAB to read the correct ID.

I still want to see the output windows info from BUILD and PICkit2.


Here is the build:

----------------------------------------------------------------------
Release build of project `C:\C-PGM\TimerTest.mcp' started.
Mon Mar 02 11:44:54 2009
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\C-PGM\TimerTest.o".
Clean: Deleted file "C:\C-PGM\TimerTest.cof".
Clean: Deleted file "C:\C-PGM\TimerTest.hex".
Clean: Deleted file "C:\C-PGM\TimerTest.map".
Clean: Deleted file "C:\C-PGM\TimerTest.mcs".
Clean: Done.
Executing: "C:\MCC18\bin\mcc18.exe" -p=18F1320 "TimerTest.c" -fo="TimerTest.o" -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
MPLAB C18 v3.22 (demo)
Copyright 2000-2008 Microchip Technology Inc.
Days remaining until demo becomes feature limited: 20
Executing: "C:\MCC18\bin\mplink.exe" /l"C:\MCC18\lib" "..\MCC18\lkr\18f1320.lkr" "TimerTest.o" "C:\MCC18\lib\p18f1320.lib" /u_CRUNTIME /z__MPLAB_BUILD=1 /m"TimerTest.map" /w /o"TimerTest.cof"
MPLINK 4.22, Linker
Copyright (c) 2008 Microchip Technology Inc.
Errors : 0

MP2HEX 4.22, COFF to HEX File Converter
Copyright (c) 2008 Microchip Technology Inc.
Errors : 0

Loaded C:\C-PGM\TimerTest.cof.
----------------------------------------------------------------------
Release build of project `C:\C-PGM\TimerTest.mcp' succeeded.
Mon Mar 02 11:44:56 2009
----------------------------------------------------------------------
BUILD SUCCEEDED


Pickit2 says "No Device Detected"
I think I have a JuneBug problem.
I have to finish defrosting, have a coffee, and see if it's safe to go to work tonight.

Mike
 
You need to use the Junebug and the PICkit2 software to erase the chip as I outlined the previous post. The config mem is currently programed so that MPLAB can not ID the chip.

I did give that a shot. Pickit2 can see the 'Super Junebug' but not the target chip, tried doing an erase operation a bunch of times with no luck. I am wondering if the charge pump on the Junebug is working?
Going to take a look at it during lunch (7:00pm est).
Mike
 
I did give that a shot. Pickit2 can see the 'Super Junebug' but not the target chip, tried doing an erase operation a bunch of times with no luck. I am wondering if the charge pump on the Junebug is working?
Going to take a look at it during lunch (7:00pm est).
Mike
Maybe you did this but let me go over it again.

The PICkit/Junebug will not see the target chip till after the chip is fixes/erased. In the PICkit2 tool (not MPLAB) you need to set the chip type by hand (from the drop down menu). Then use erase, mine came back on the 2nd try at eraseing it.
 
Last edited:
I forgot to mention this :eek:
Prior to selecting the chip type in the PICkit2 software you have to do PROGRAMMER>MANUAL DEVICE SELECT

Then you can erase it. Once it is erased both MPLAB and PICkit2 software should be able to ID the chip correctly.
 
I forgot to mention this :eek:
Prior to selecting the chip type in the PICkit2 software you have to do PROGRAMMER>MANUAL DEVICE SELECT

Then you can erase it. Once it is erased both MPLAB and PICkit2 software should be able to ID the chip correctly.


I think thats what I did at about 3:30am, but I will do it again when I get back home. My computer here (old laptop) does not go online to get the .net stuff for PicKit2 (but I will try to remedy that). Thanks and I'll keep you posted.
The original problem is fixed, but I do not know why if you set a lib location in MPLAB why you need to set it with every project, operator error??
Mike
 
I installed the tools in the normal location and do not recall ever having to set any of the locations. Or are you talking about a user created lib ?

Are you using the wizzard to create new projects ?

I did a default install of MPLAB and C18.

Yes I use the wizard to create a new project. I had this problem when I first started with C18, but there was no error until I used the sim and then I remembered the previous problem, this cured the timer trouble, then I had the programming problem. I checked the Junebug over with a set of Torben?? glasses from my toolbox here at work.

Mike
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top