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.

MCC18 Driving me crazy

Status
Not open for further replies.

AtomSoft

Well-Known Member
Ok for some reason i keep getting a halt on my second delay

Code:
  	#pragma	config OSC=INTIO2, WDT=OFF, LVP=OFF, DEBUG=ON
  	#include <p18f1320.h>
// ----------------------------------------------------------	
	void InterruptHandlerHigh(void);
	void InterruptVectorHigh(void);
	void MyDelay(int x);
	void MyFlash(int qTimes);
	
// ----------------------------------------------------------

void main(void)
{
	int count;	
	int countA = 0;

    ADCON1 = 0; // make RA0 digital
    TRISA = 0b10111110;


    while(1)    // loop forever
    {
      	LATA=0b00000001; // Turn LED on
		MyDelay(200);

      	LATA=0b00000000; // Turn LED off
[b]      	MyDelay(200);     // The program halts here. Any thoughts?[/b] 
		
		countA = countA + 1;		
		if (countA > 3) {
			MyFlash(3);
			countA = 0;	
		}

    }
}
// ----------------------------------------------------------

void MyDelay(int xD)
	{
		int countD = 0;
      	
		while (countD < xD)
      	{
        	countD = countD + 1;
      	}
	}
// ----------------------------------------------------------
void MyFlash (int qTimes)
	{
		int x = qTimes;
		while(x != 0)  {
			TRISA = 0b10111110;
			LATA = 0b00000001;
			MyDelay(10);

			LATA = 0b01000000;
			MyDelay(10);

			TRISA = 0b00111111;
			LATA = 0b01000000; 
			MyDelay(10);

			LATA = 0b10000000;
			MyDelay(10);

			TRISA = 0b01111110;
			LATA = 0b10000000;
			MyDelay(10);

			LATA = 0b00000001;
			MyDelay(10);
			
			x--;
		}
		TRISA = 0b10111110;
	}
//----------------------------------------------------------------------------
// High priority interrupt vector

#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}

//----------------------------------------------------------------------------
// High priority interrupt routine

#pragma code
#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh ()
{
	
	int countF;
  	
	if (INTCONbits.RBIF) {          
	
	INTCONbits.RBIF = 0;            //clear interrupt flag
    
	countF = 15;
	INTCON3 = 0;

 	MyFlash(20);
    }
    }
}

//----------------------------------------------------------------------------
 
Last edited:
in the "c012i.c" file it points to:

Code:
  _asm
    // Initialize the stack pointer
[b]    lfsr 1, _stack  // Points here[/b]
    lfsr 2, _stack

    clrf TBLPTRU, 0 // 1st silicon doesn't do this on POR

    bcf __FPFLAGS,RND,0 // Initialize rounding flag for floating point libs
    
    _endasm
 
I posted a reply to this thread but it seems to have got lost.

I asked, what happens if you cut it down to it's absolute minimum. The smaller the code the easier to identify the problem.

Mike.
 
This
Code:
void MyDelay(int xD)
	{
		int countD = 0;
      	
		while (countD < xD)
      	{
        	countD = countD + 1;
      	}
	}
is horrible! Do this instead
Code:
void MyDelay(int xD)
{
	int countD = 0;
	while (countD < xD)
	{
		countD = countD + 1;
	}
}
You're still mixing spaces and tabs, and your indentation is all over the place. Neat, correct indentation makes things so much more understandable.

These
Code:
  	#pragma	config OSC=INTIO2, WDT=OFF, LVP=OFF, DEBUG=ON
  	#include <p18f1320.h>
// ----------------------------------------------------------	
	void InterruptHandlerHigh(void);
	void InterruptVectorHigh(void);
	void MyDelay(int x);
	void MyFlash(int qTimes);
shouldn't be indented at all.
 
AtomSoft said:
ok lol i usually dont worry about spacing till finished thats all
Hehehe :D Like code commenting, indentation, if left till the end, never gets done. Six months later you'll try to read your messy code and you'll kick yourself for not making it more readable. :) At this point your code is still small. Wait till it starts getting BIG. If you aren't organized you're lost.
 
i think something is wrong with my junebug buttons. I just slightly touch not press a button and it goes off. Eveything seems to work fine except that.
 
Is this neater i tried my best lol

My code:
Code:
#pragma	config OSC=INTIO2, WDT=OFF, LVP=OFF, DEBUG=ON
#include <p18f1320.h>

void InterruptHandlerHigh(void);
void InterruptVectorHigh(void);
void MyDelay(int x);
void MyFlash(int qTimes);

void main(void)
{
	int count;	
	char countA = 0;

	ADCON1 = 0; // make RA0 digital
	TRISA = 0b10111110;
	INTCON = 0b11011000;
	INTCON2 = 0b11110000;
	INTCON3 = 0;

	while(1)    // loop forever
	{
		LATA=0b00000001; // Turn LED on
		MyDelay(200);

		LATA=0b00000000; // Turn LED off
		MyDelay(200);
		
		countA = countA + 1;		
		if (countA > 3) {
			MyFlash(3);
			countA = 0;	
		}

	}
}
// ----------------------------------------------------------

void MyDelay(int xD)
{
	int countD = 0;
     	
	while (countD < xD)
   	{
		countD = countD + 1;
	}
}
// ----------------------------------------------------------
void MyFlash (int qTimes)
{
	int x = qTimes;
	while(x != 0)  
	{
		TRISA = 0b10111110;
		LATA = 0b00000001;
		MyDelay(10);

		LATA = 0b01000000;
		MyDelay(10);

		TRISA = 0b00111111;
		LATA = 0b01000000; 
		MyDelay(10);

		LATA = 0b10000000;
		MyDelay(10);

		TRISA = 0b01111110;
		LATA = 0b10000000;
		MyDelay(10);

		LATA = 0b00000001;
		MyDelay(10);
			
		x--;
	}

	TRISA = 0b10111110;
}
//----------------------------------------------------------------------------
// High priority interrupt vector

#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
	_asm
	goto InterruptHandlerHigh //jump to interrupt routine
	_endasm
}

//----------------------------------------------------------------------------
// High priority interrupt routine

#pragma code
#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh ()
{
	if (INTCONbits.RBIF) 
	{          
		INTCONbits.RBIF = 0;     //clear interrupt flag
		INTCONbits.INT0IF = 0;
		MyFlash(10);
	}
}

//----------------------------------------------------------------------------
 
Last edited:
AtomSoft said:
Is this neater i tried my best lol
Only a tiny bit. You're still all over the place. Ah well, you'll improve with practice... maybe.

i think something is wrong with my junebug buttons. I just slightly touch not press a button and it goes off. Eveything seems to work fine except that.
Sounds like you've got some pins floating.
 
Ok i found the floating pin and it was PORTB7 which calls a interrupt everytime i was my have over the board lol but when i set it in TRISB to 0 i get NO interrupts. I went over it like 20 times changing things but no luck plz any info.. thx

Code:
#pragma	config OSC=INTIO2, WDT=OFF, LVP=OFF, DEBUG=ON
#include <p18f1320.h>

void InterruptHandlerHigh(void);
void InterruptVectorHigh(void);
void MyDelay(int x);
void MyFlash(int qTimes);

void main(void)
{
	int count;	
	char countA = 0;

	ADCON1 = 0; // make RA0 digital
	TRISA = 0b10111110;
	TRISB = 0x00000001;
	LATB = 0;
	INTCON = 0b11001000;  //11011000;
	INTCON2 = 0b11110101; //10000001
	INTCON3 = 0b11001000;

	while(1)    // loop forever
	{
		LATA=0b00000001; // Turn LED on
		MyDelay(200);

		LATA=0b00000000; // Turn LED off
		MyDelay(200);
		
		countA = countA + 1;		
		//if (countA > 3) {
		//	MyFlash(3);
		//	countA = 0;	
		//}

	}
}
// ----------------------------------------------------------

void MyDelay(int xD)
{
	int countD = 0;
     	
	while (countD < xD)
   	{
		countD = countD + 1;
	}
}
// ----------------------------------------------------------
void MyFlash (int qTimes)
{
	int x = qTimes;
	while(x != 0)  
	{
		TRISA = 0b10111110;
		LATA = 0b00000001;
		MyDelay(10);

		LATA = 0b01000000;
		MyDelay(10);

		TRISA = 0b00111111;
		LATA = 0b01000000; 
		MyDelay(10);

		LATA = 0b10000000;
		MyDelay(10);

		TRISA = 0b01111110;
		LATA = 0b10000000;
		MyDelay(10);

		LATA = 0b00000001;
		MyDelay(10);
			
		x--;
	}

	TRISA = 0b10111110;
}
//----------------------------------------------------------------------------
// High priority interrupt vector

#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
	_asm
	goto InterruptHandlerHigh //jump to interrupt routine
	_endasm
}

//----------------------------------------------------------------------------
// High priority interrupt routine

#pragma code
#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh () [b]//I have a breakpoint here[/b]
{
	if (INTCONbits.RBIF) 
	{          
		INTCONbits.RBIF = 0;     //clear interrupt flag
		INTCONbits.INT0IF = 0;
		MyFlash(1);
	}
}

//----------------------------------------------------------------------------
 
INTCON2 controls the RBPU (weak pullup) it has to be set to zero to enable it. Just enable it and leave it enabled. No need to turn it off.

Code:
[FONT=Crystal, monospace]INTCON2bits.RBPU=0;  // enable weak pullups on PORTB input pins[/FONT]
I'm starting to like C18 :)
 
Last edited:
Still a no go. Im going over every line but am dieing here lol I set trisb 7 and i get no breakpoints dont and i get that 1 breakpoint. I took the interrupt code from the C18 User Guide
New code
Code:
#pragma	config OSC=INTIO2, WDT=OFF, LVP=OFF, DEBUG=ON
#include <p18f1320.h>

void main(void);
//void MyInterr(void);
//void InterruptVectorHigh(void);
//void InterruptHandlerHigh(void);
void interrupt_at_high_vector(void);
void high_isr (void);
void MyDelay(int x);
void MyFlash(int qTimes);

void main(void)
{
	int count;	
	char countA = 0;

	ADCON1 = 0; // make RA0 digital
	TRISA = 0b10111110;
	TRISB = 0x00000001;
	LATB = 0;
	INTCON = 0b11010000;  //11011000;
	INTCON2bits.RBPU=0;

	while(1)    // loop forever
	{
		LATA=0b00000001; // Turn LED on
		MyDelay(200);

		LATA=0b00000000; // Turn LED off
		MyDelay(200);
		
		countA = countA + 1;		
		//if (countA > 3) {
		//	MyFlash(3);
		//	countA = 0;	
		//}

	}
}
// ----------------------------------------------------------

void MyDelay(int xD)
{
	int countD = 0;
     	
	while (countD < xD)
   	{
		countD = countD + 1;
	}
}
// ----------------------------------------------------------
void MyFlash (int qTimes)
{
	int x = qTimes;
	while(x != 0)  
	{
		TRISA = 0b10111110;
		LATA = 0b00000001;
		MyDelay(10);

		LATA = 0b01000000;
		MyDelay(10);

		TRISA = 0b00111111;
		LATA = 0b01000000; 
		MyDelay(10);

		LATA = 0b10000000;
		MyDelay(10);

		TRISA = 0b01111110;
		LATA = 0b10000000;
		MyDelay(10);

		LATA = 0b00000001;
		MyDelay(10);
			
		x--;
	}

	TRISA = 0b10111110;
}
// -------------------------------------------------
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
_asm GOTO high_isr _endasm     [b]//   brakpoint here [/b]
}

#pragma code /* return to the default code section */
#pragma interrupt high_isr
void high_isr (void)
{
	/* ... */               
}
 
Last edited:
I cut it down to a bare minimum and still no good:
Code:
#pragma	config OSC=INTIO2, WDT=OFF, LVP=OFF, DEBUG=ON
#include <p18f1320.h>

void interrupt_at_high_vector(void);
void high_isr (void);

void main(void)
{
	INTCON2bits.RBPU=0;
	INTCON2bits.RBIP=1;

	INTCON = 0b11010000; //[b] Setting Bit 3 to 1 gives a automatic interrupt [/b]
	TRISB = 0b00000001;

	while(1){

	}
}

// -------------------------------------------------
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
_asm GOTO high_isr _endasm     //   brakpoint here 
}

#pragma code /* return to the default code section */
#pragma interrupt high_isr
void high_isr (void)
{
	/* ... */    
	INTCONbits.INT0IE=0;
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top