More ISR Questions

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include 
#include 
#include 
char passes,TF,cycles;	
volatile char GPLF,TS;

int main()
{

DDRD  |= 0x40;		//set PD6 as output (motor drive)
DDRB  |= 0x02;		//set PB1 as output (motor brake)

PORTD &= ~0x40;		//turns off PD6(motor drive)
PORTC &= ~0x40;		//turns off PC6(motor brake)
PORTC |= 0x07;		//pullup PC0(Tappet Sensor Input),1,2
PORTD |= 0xBF;		//pullup PD0,1,2,3,4,5,7
PORTB |= 0x05;		//pullup PB0,2

ADCSRA |= 0x06;  	//ADC Prescalar set to 64 - 312.5kHz@20MHz 
ADMUX  |= 0x20;		//changes ADC to 8-bit resolution, read from ADCH, sets Vref to AVCC		
ADCSRA |= 0x80;  	//enable ADC

PCICR  |= 0x02;		//enable PCIE1 for PCINT 14-8
PCMSK1 |= 0x05;		//enable PCINT8 on PC0 (Tappet Sensor), PCINT10 on PC2 (Trigger), PCMSKx enables individual pin interrupts
sei();				//enable global interrupts 

	while(1)		//main loop
	{//Main loop==================================================

		while( GPLF )	//start of cycle counting loop
		{//CCL----------------------------------------------------
		
		if(TF != TS)				//increments cycles each time tappet sensor is true
			{
			if(TS)
				passes = passes + 1;
				TF=TS;
			}	
		
		if(passes == 1)						//this segment breaks the loop after 1 full gearbox cycle 
			{								//is completed unless PINC2 is still low, in wich case it will
				passes = 0;					//continue to cycle until PINC2 is high or cycls = x.
				cycles = cycles + 1;	
					if( PINC&_BV(PINC2) )
					{
						PORTD &= ~(1<< 6);
						GPLF = 0;
					}
					if( cycles >= 3)
					{
						PORTD &= ~(1<< 6);
//						TCharge = 0;
						cycles = 0;
						GPLF = 0;
					}
			}		
				
		}//CCL----------------------------------------------------
		PORTD &= ~(1<< 6);

	}//Main loop==================================================
		
}

ISR(PCINT1_vect)
{
	if( !(PIND&_BV(PINC2)) )		//Starts gearbox if trigger is pulled 
	{
		PORTD |= (1 << 6);
		GPLF = 1;
	}

	if( !(PINC&_BV(PINC0)) )		//Allows passes to be incremnted by 1 each time tappet plate passes the sensor
		TS=1;
	else
		TS=0;					
}

Taking a look at the following code, I'll give a brief description of what it does. The microcontroller is controlling a mechanical gearbox that drives an air piston. I am using PINC0 to monitor when the piston gets to a specific point via digital output photo sensor. I also monitoring when the momentary push button is pressed to turn the gearbox on. The code basically makes sure that the drive motor is on until the gearbox has completed its cycle after the button is released. Now previously I had everything in the main loop but was missing critical points when the sensors were tripped.

I moved the reading of the sensors over to an interrupt which detects the pin change of EITHER of those pins, which can change at random. I don't care which one changes when, I just need to know when one changes and mechanically I don't believe either will ever change at the same time. I know that I can set PORTx on or off from an interrupt because I've tried that before in a more basic version of the program. However this does not turn on PORTD, bit 6 when PINC2 goes low, it only stopped working after I added in the if statements and set GPLF to 1 inside the interrupt. GPLF and TS are both declared as volatile and they are global values, what is wrong that is preventing PINC2 and 0 from being read when this interrupt occurs?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

While you didn't say I seem to remember this is a 48/88/168? Now you do:

PCMSK1 |= 0x05;      //enable PCINT8 on PC0 (Tappet Sensor), 

which enables PCINT8 and PCINT10 which are bits 0 and 2 of PORTC.

Then in the ISR you have:

   if( !(PIND&_BV(PINC2)) )      //Starts gearbox if trigger is pulled 
   { 
      PORTD |= (1 << 6); 
      GPLF = 1; 
   }

so when did PCINT10 magically move from PORTC to PORTD?

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It didn't, I cannot believe I didn't see that. I guess some times when something that should be working doesn't, the best thing to do is look for typos.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, that requires a double face palm.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is this a typo?

if( !(PIND & _BV(PINC2)) )

EDIT - I see I'm a day late and a dollar short...