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.

ISR code crashing PICC compiler

Status
Not open for further replies.

Kryten

New Member
Hi Im trying to compile this code

Code:
///////////////////////////////////////////////////////////////
//                                                           //
//             Lights for bike trolly                        //
//   Turn indicators and back - brake lights                 //
//                                                           //
///////////////////////////////////////////////////////////////

//#include <pic.h>
#include <htc.h>
#include <pic12f6x.h>
__CONFIG (UNPROTECT & MCLRDIS & PWRTDIS & WDTDIS & INTIO & BOREN);

//////////////////////////////////////////////////////////////
// Definitoins                                              //
//////////////////////////////////////////////////////////////

#define _XTAL_FREQ 		4000000
#define PORT_LED_LEFT   (GPIO0)  
#define PORT_LED_RIGHT  (GPIO1)
#define PORT_LED_BRAKE  (GPIO2)
#define PORT_BTN_LEFT   (GPIO3)
#define PORT_BTN_RIGHT  (GPIO4)
#define PORT_BTN_BRAKE  (GPIO5)
#define DELAY           (__delay_ms(125))
#define DELAY_BRAKE     (__delay_ms(1))

///////////////////////////////////////////////////////
// Interrupt routine                                 //
///////////////////////////////////////////////////////

#pragma interrupt_level 0
void interrupt ISR (void)
{
	if(1 ==PORT_BTN_BRAKE )
		{
		PORT_LED_BRAKE = 1;                  //Brake light
		}
	else
		{
		PORT_LED_BRAKE = 0;    				// Brake light will flash, on 1 ms off 1 ms resulting in 50%
		DELAY_BRAKE;						// light emmison.
		PORT_LED_BRAKE = 1;
		DELAY_BRAKE;
		}
}

/////////////////////////////////////////////////////////
// Main program routine                                //
/////////////////////////////////////////////////////////      



void main (void)
{
	GPIO    = 0x00;
    CMCON    = 0x07;
    TRISIO    = 0b11111000;            	// GP0, GP1 and GP2 are outputs. GP3, GP4 and GP5 are inputs.  
	ei();
while (1)
	{
	if (1 == PORT_BTN_LEFT)				// If left butten is pressed, left turn indicator will blink
		{
		PORT_LED_LEFT = 1;
		DELAY;
		DELAY;
		DELAY;
		PORT_LED_LEFT = 0;
		DELAY;
		DELAY;
		DELAY;
		}
	else if ( 1 == PORT_BTN_RIGHT)		// If right button is pressed, right turn indicator will blink
		{
		PORT_LED_RIGHT = 1;
		DELAY;
		DELAY;	
		DELAY;
		PORT_LED_RIGHT = 0;
		DELAY;
		DELAY;
		DELAY;
		}
	else 
		{
		PORT_LED_LEFT = 0;
		PORT_LED_RIGHT = 0;
		}
	}
}

But i get this cgpic.exe has stopped working in windows. Is my code that "hard" to compile? or is my code just really wrong?

Thanks for all help
 
Ok since no one has answered yet I will try to ask in a different manner..

I want the brake light to flash with 1ms on / off time even when I use left or right turn signal. How to best do that?
I have tried with just
Code:
PORT_LED_BRAKE = 1;
DELAY_BRAKE;
PORT_LED_BRAKE = 0;
DELAY_BRAKE;

and then the
Code:
if (1 == PORT_BTN_LEFT)				// If left butten is pressed, left turn indicator will blink
		{
		PORT_LED_LEFT = 1;
		DELAY;
		DELAY;
		DELAY;
		PORT_LED_LEFT = 0;
		DELAY;
		DELAY;
		DELAY;
		}
	else if ( 1 == PORT_BTN_RIGHT)		// If right button is pressed, right turn indicator will blink
		{
		PORT_LED_RIGHT = 1;
		DELAY;
		DELAY;	
		DELAY;
		PORT_LED_RIGHT = 0;
		DELAY;
		DELAY;
		DELAY;
		}
          else if ( 1 == PORT_BTN_BRAKE)
                {
                PORT_LED_BRAKE = 1;
                }
            else 
               {
               PORT_LED_LEFT = 0;
               PORT_LED_RIGHT = 0;
               }

But that resulted in that when turn indicator is activated I could not get brake.

Should I use the brake light function in ISR or IOC?
 
The compiler compiles other code without crashing ?

Not sure why you need to use an ISR. You can easily write this without. Just poll the switches during the "delay" periods to see if anything has changed.

EDIT:

I suppose I should say a bit more.

Instead of a delay that does nothing use something like:

Code:
for (i=HALF_CYCLE; i>0; i--)
{
     // test port and adjust action or break out of loop
}
 
Last edited:
Ahh you have a quick code example?

Ill have a look in the compiler "user guide" as well
 
Code:
///////////////////////////////////////////////////////////////
//                                                           //
//             Lights for bike trolly                        //
//   Turn indicators and back - brake lights                 //
//                                                           //
///////////////////////////////////////////////////////////////

//#include <pic.h>
#include <htc.h>
#include <pic12f6x.h>
__CONFIG (UNPROTECT & MCLRDIS & PWRTDIS & WDTDIS & INTIO & BOREN);

//////////////////////////////////////////////////////////////
// Definitoins                                              //
//////////////////////////////////////////////////////////////

#define _XTAL_FREQ         4000000


/////////////////////////////////////////////////////////
// Main program routine                                //
/////////////////////////////////////////////////////////      

void main (void)
{
    GPIO    = 0x00;
    CMCON    = 0x07;
    TRISIO    = 0b11111000;                // GP0, GP1 and GP2 are outputs. GP3, GP4 and GP5 are inputs.  

    while (1);
}
 
Last edited:
What about using timer0 as flashing "controller" for back light?
 
Last edited:
I see on your tutorials (although for 18F) the reading switches
If I adapt this like this will I be able to get it working?
Code:
while(1)
  {
    // Read from GPIO
   btn1 = GPIO3
   btn2 = GPIO4
   btn3 = GPIO5
    // turn on correct LED if switch was pressed
    if(btn1) 
    {
      GPIO0 = 1;
    }
    if(btn2)
    {
      GPIO1 = 1;
    }
    if(btn3)
    {
      GPIO2 = 1;
    }
  }
}

EDIT:
Is the Cooperative Multitasking example in your tutorials what you mean I should look for?
 
Last edited:
Code using Timer0 would be a bit cleaner if you know how to set it up. You do not need to do anything as complex as the multi tasking demo for this project.

Did the compiler compile the example ?

Code:
while(1)
{
  //// blinker on cycle
  // do left light
  if left_sig or brake then turn on
  // do right light
  if right_sig or brake then turn on

  // check switches and timer0 (or check a fixed number of times
  // make adjustments as needed***
  
  //// blinker off cycle
  // do left light
  if  brake then turn on else off
  // do right light
  if  brake then turn on else off

  // check switches and timer0 (or check a fixed number of times
  // make adjustments as needed***
}
*** for example if the driver takes his foot off the break turn off the brake light now unless blinking etc
 
Last edited:
Im trying to understand the timer0

Im using HiTech picc lite if havent mentioned it ;)

I think I have forgotten to define i and HALF_CYCLE

EDIT:
So if I get you right. Using timer0 and if "things" the blinking / brake lights will not have any effect on each other..
 
Last edited:
Forget about timer0 for now. You have enough other trouble and it is not needed for this. Lets KISS for now.

If you base you code on the suggestion in post #8 the signals and break will work without messing each other up.

Let me restate the #8 code without timer0.

Code:
while(1)
{
  //// blinker on cycle
  // do left light
  if left_sig or brake then turn on
  // do right light
  if right_sig or brake then turn on

  // check switches and timer0 (or check a fixed number of times
  // to generate the delay for the 1/2 cycle***
  
  //// blinker off cycle
  // do left light
  if  brake then turn on else off
  // do right light
  if  brake then turn on else off

  // check switches and timer0 (or check a fixed number of times
  // to generate the delay for the 1/2 cycle***

}
 
Last edited:
KISS is really good for me ;)

Im trying to understand and Im not really experieced with programming, but at least im trying.

I dont quite understand how to put it in to "pure" code (if you understand).

Im having troubble with the check switches part, is that Interrupt On Change?
If so I think I will get it working. Just have to try and fail a bit :) but that is part of the learning process i mean.

I will continue to look at tutorials, I have found this to quite good.

EDIT:

Im not shure it will work at all because I try to blink two ports with different time intervall
 
Last edited:
Ok then i think I have not explained clear. So here goes..

When you dont press brake or any turn then the brake light flashes with on off 1 ms (50%)

Otherwise I would just have made it like you say. Thats no biggi
 
Last edited:
So when signaling a turn the other light will be on steady but at 50% power. This is not difficult.

First get it working like post #10

Then in the area indicated by these two lines modulate the non blinking light at 50% when required.
// check switches and timer0 (or check a fixed number of times
// to generate the delay for the 1/2 cycle***

I would use the MPLAB sim to check the timing. Replace the two lines with something like this.

This sort of program used in the NAOS demo would easily do this but I have not ported it to Hi-Tech C. If you want I will do so and the app could easily be coded. Let me know if you would like to use NAOS and I will port it to hi-tech. Note that it already works with C18 and BootC.
 
Last edited:
Yes the other light (brake light) will be on 50% all time when brake is not pressed. The problem for me is that when I press brake when turn signal is given I cant get the brake to light..

Yes I would be very grateful if you could do that for me.
 
Last edited:
I have the NAOS demo compiling with Hi-Tech but still need some time to test it. But I have other things that need doing so I will let you know when it is tested. Note that I am working on an 18F1320 and you will have to make it work on the 12F ;)
 
Im thankful and grateful for your help ;) Im on vacation for the next four weeks :D so no need to stress extra for my sake. ;)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top