Ardumower arduino mega shield

Hello,

Arduino interrupt programming (concrete: for the Mega) is a little confusing - (for the Mega) only these pins (INTx) can be programmed via 'attachInterrupt':


Code:
43	PD0	SCL	INT0		Digital pin 21	SCL		(TWI Serial Clock or External Interrupt0 Input)
44	PD1	SDA	INT1		Digital pin 20	SDA		(TWI Serial Data or External Interrupt1 Input )
45	PD2	RXD1	INT2		Digital pin 19		RX1	(USART1 Receive Pin or External Interrupt2 Input)
46	PD3	TXD1	INT3		Digital pin 18		TX1	(USART1 Transmit Pin or External Interrupt3 Input)
6	PE4	OC3B	INT4		Digital pin 02	PWM		
7	PE5	OC3C	INT5		Digital pin 03	PWM



Example code (INT5):

Code:
attachInterrupt(5, rpm_interrupt, CHANGE);  // will attach interrupt routine called 'rpm_interrupt' to interrupt INT5

Corresponding interrupt routine must have the name 'rpm_interrupt':

void rpm_interrupt(){
  ...
}
http://arduino.cc/en/pmwiki.php?n=Reference/AttachInterrupt

However, if you want to program these PCINTx interrupt pins...


Code:
19	PB0	SS	PCINT0		Digital pin 53		SS	(SPI Slave Select input or Pin Change Interrupt 0)
20	PB1	SCK	PCINT1		Digital pin 52		SCK	(SPI Bus Serial Clock or Pin Change Interrupt 1)
21	PB2	MOSI	PCINT2		Digital pin 51		MOSI	(SPI Bus Master Output/Slave Input or Pin Change Interrupt 2)
22	PB3	MISO	PCINT3		Digital pin 50		MISO	(SPI Bus Master Input/Slave Output or Pin Change Interrupt 3)
23	PB4	OC2A	PCINT4		Digital pin 10	PWM		
24	PB5	OC1A	PCINT5		Digital pin 11	PWM		
25	PB6	OC1B	PCINT6		Digital pin 12	PWM		
26	PB7	OC0A	PCINT7	OC1C	Digital pin 13	PWM		
2	PE0	RXD0	PCINT8		Digital pin 00		RX0	(USART0 Receive Pin, Programming Data Input or Pin Change Interrupt 8)
63	PJ0	RXD3	PCINT9		Digital pin 15		RX3	(USART3 Receive Pin or Pin Change Interrupt 9)
64	PJ1	TXD3	PCINT10		Digital pin 14		TX3	(USART3 Transmit Pin or Pin Change Interrupt 10)
65	PJ2	XCK3	PCINT11					(USART3 External Clock  Input/Output or Pin Change Interrupt 11)
66	PJ3		PCINT12					
67	PJ4		PCINT13					
82	PK7	ADC15	PCINT23		Analog pin 15			
83	PK6	ADC14	PCINT22		Analog pin 14			
84	PK5	ADC13	PCINT21		Analog pin 13			
85	PK4	ADC12	PCINT20		Analog pin 12			
86	PK3	ADC11	PCINT19		Analog pin 11			
87	PK2	ADC10	PCINT18		Analog pin 10			
88	PK1	ADC9	PCINT17		Analog pin 09			
89	PK0	ADC8	PCINT16		Analog pin 08			
90	PF7	ADC7	PCINT15		Analog pin 07			
91	PF6	ADC6	PCINT14		Analog pin 06


... you have to directly use the Mega registers:

Example code (PCINT20):

Code:
PCICR |= (1<<PCIE2); // allow PCI2 interrupts
PCMSK2 |= (1<<PCINT20);  // enable PCINT20 interrupt

corresponding interrupt routine must have the name 'PCINT2_vect' (because it's using PCI2)

ISR(PCINT2_vect){
  ...
}
 
Hi Alexander,

thank you for your answer, but for me as progragramming beginner isn't clear.

Please can you help me how to exactly change code that digital input 51 will work for rpm measurement ?

Thank you

Alex
 
@Alex:

Example for pin 51 (not tested - your feedback would be welcome ;))

1. From above interrupt table, you find out that pin51 uses 'PCINT2'.

2. Find out which pin change register 'PCIx' to use:
2.1 The Pin change interrupt 'PCI2' will trigger if any enabled PCINT23:16 pin toggles. PCINT23:16 pins are enabled individually by the PCMSK2 Register.
2.2 The Pin change interrupt 'PCI1' will trigger if any enabled PCINT15:8 pin toggles. PCINT15:8 pins are enabled individually by the PCMSK1 Register.
2.3 The Pin change interrupt 'PCI0' will trigger if any enabled PCINT7:0 pin toggles. PCINT7:0 pins are enabled individually by the PCMSK0 Register.


Code:
PCICR |= (1<<PCIE0); // enable pin change interrupt PCIE0
PCMSK0 |= (1<<PCINT2);  // enable PCINT2 interrupt


4. Adjust interrupt routine name:

Corresponding interrupt routine must have the name 'PCINT0_vect' (because it's using 'PCI0')


Code:
ISR(PCINT0_vect){
  boolean motorMowRpmState = digitalRead(pinMotorMowRpm);
  etMotorMowRPMState(motorMowRpmState);
}
 
@Walter: Hello, I already tried to contact you - Could you please send us the Kicad project files? We would like to modify the PCBs slightly and do not want to reinvent the wheel. Thank you in advance :)

Regards,
Alexander
 
AlexanderG schrieb:
@Alex:

Example for pin 51 (not tested - your feedback would be welcome ;))

1. From above interrupt table, you find out that pin51 uses 'PCINT2'.

2. Find out which pin change register 'PCIx' to use:
2.1 The Pin change interrupt 'PCI2' will trigger if any enabled PCINT23:16 pin toggles. PCINT23:16 pins are enabled individually by the PCMSK2 Register.
2.2 The Pin change interrupt 'PCI1' will trigger if any enabled PCINT15:8 pin toggles. PCINT15:8 pins are enabled individually by the PCMSK1 Register.
2.3 The Pin change interrupt 'PCI0' will trigger if any enabled PCINT7:0 pin toggles. PCINT7:0 pins are enabled individually by the PCMSK0 Register.


Code:
PCICR |= (1<<PCIE0); // enable pin change interrupt PCIE0
PCMSK0 |= (1<<PCINT2);  // enable PCINT2 interrupt


4. Adjust interrupt routine name:

Corresponding interrupt routine must have the name 'PCINT0_vect' (because it's using 'PCI0')


Code:
ISR(PCINT0_vect){
  boolean motorMowRpmState = digitalRead(pinMotorMowRpm);
  etMotorMowRPMState(motorMowRpmState);
}


Sorry, I'm lost .....

Alex
 
Zuletzt bearbeitet von einem Moderator:
@ Alex
Modify your config.h as below (enable interrupt change on pin 51 (PCINT2), interrupt routine is PCINT0):

Code:
// remote control (RC) ppm signal change interrupt
ISR(PCINT0_vect)
{   
  if (remoteUse){
    unsigned long timeMicros = micros();
    boolean remoteSpeedState = digitalRead(pinRemoteSpeed);
    boolean remoteSteerState = digitalRead(pinRemoteSteer);
    boolean remoteMowState = digitalRead(pinRemoteMow);    
    boolean remoteSwitchState = digitalRead(pinRemoteSwitch);    
    setRemotePPMState(timeMicros, remoteSpeedState, remoteSteerState, remoteMowState, remoteSwitchState);  
  }
// mower motor speed sensor interrupt  
  boolean motorMowRpmState = digitalRead(pinMotorMowRpm);
  setMotorMowRPMState(motorMowRpmState);
}

  // R/C
  pinMode(pinRemoteMow, INPUT);
  pinMode(pinRemoteSteer, INPUT);
  pinMode(pinRemoteSpeed, INPUT); 
  pinMode(pinRemoteSwitch, INPUT);       
  PCICR |= (1<<PCIE0);
  PCMSK0 |= (1<<PCINT4);
  PCMSK0 |= (1<<PCINT5);
  PCMSK0 |= (1<<PCINT6);
  PCMSK0 |= (1<<PCINT1);
  // mower motor speed sensor interrupt
  PCMSK0 |= (1<<PCINT2);
 
Hi Fabio,

here :


sc1.jpg


Alex
Attachment: https://forum.ardumower.de/data/media/kunena/attachments/1183/sc1.jpg/
 
Zuletzt bearbeitet von einem Moderator:
Thank you Alex, I mean that I don't have 1N4007 on the electric kit, but I have 1N4448...is it the same? Can I use 1N4448 instead of 1N4007??
thank you bye
 
Hi Alex

no you can not use the 1N4448 instead of 1N4007.

The 1N4448 has an max current of 2A and the 1N4448 an Current of 30 A.

The 1N4448 must be from the Perimeter Circuit.

Stefan
 
Max schrieb:
@Alex
Hi

Have you test the code modification about rpm interrupt?

Regards.

Hi Max,

your modification from the post 4430 isn't working.
Any other idea how to get working ?

Alex
 
Zuletzt bearbeitet von einem Moderator:
Oben