Go Down

Topic: External interrupt on Atmega168 (Read 3380 times) previous topic - next topic

Wahrani31

Apr 16, 2016, 09:50 pm Last Edit: Apr 17, 2016, 12:26 am by Wahrani31
Hi every one

I'm trying to control 2 dc motor EMG30 12v using my arduino uno borad, m'y 2 motors have built-in encoder so i need to read 4 interrupts and the simple atmega328 have only 2 external INTerrupts
My question is, do the ATmega168 have more configured external interrupts or it has only 2 just like atmega328, because i found in the datasheet of the atmega168 ("just 2 External interrupt") and ("pin change interrupt request") and can't indursent the difference betwin

Thank you so Much !!


westfw

m168 and m328 have the same interrupt capabilities.

Wahrani31

Ok thank you, so this means that i can only use the 2 external interrupts INT0 INT1 or i still can use the External "pin change interrupts" in my original arduino uC wich is ATmega328, sorry but i'm still getting confused about this point. Thank you so much !!

westfw

Quote
i can only use the 2 external interrupts INT0 INT1 or i still can use the External "pin change interrupts"
Both the m168 and m328 have 2 "external interrupts" that will give you an interrupt on (highly configurable) events on two particular pins.  They both ALSO have "pin change interrupts" which provide one interrupt per PORT when any of the (masked) pins changes in any way.  You would have to do additional work in software to decode pin-change interrupts so you knew exactly which pins changed in which direction (if that were important to know.)

I'm not familiar enough with encoder interfacing to understand what is actually required for that.  It seems unlikely that you would actually need two interrupts per motor; I suspect that was just a lazy but clever implementation...

Wahrani31

Ok now will search for pin change INTs programming in arduino or i will have to write my code in atmel uC language. Thank you so much that helpe me alot, i appreciate it ;)

DrAzzy

PCINTs should work for this. They're a bit less intuitive than normal interrupts, and you'll need to use direct port manipulation, but it's not bad...

I would avoid using PCINT libraries - I think they tend to add more complexity rather than remove it.

The ATmega2560 (used in mega2560 board) has 8 external interrupts - though I think this can be done with PCINTs no problem.
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Wahrani31

#6
Apr 29, 2016, 04:51 pm Last Edit: Apr 29, 2016, 05:02 pm by Wahrani31
Yes that's what i will do, now i have an other problem, my hall encoder has 360 points per round so i should count 360 coder ticks per round but what i count is just 90 ticks/round i dont know if my code is wrong or the arduino is missing some interrupts
Thank's For Help‼


Code: [Select]
//#include <FlexiTimer2.h>
//#include <digitalWriteFast.h>

int ra=0,rb,a=0,b=0;
void setup() {
  // put your setup code here, to run once:
pinMode(8, OUTPUT);     //DR M02
pinMode(9, OUTPUT);     //PWM M01
pinMode(1, OUTPUT);     //DR M01
pinMode(0, OUTPUT);     //DR M02
pinMode(10, OUTPUT);    //PWM M02
pinMode(11, OUTPUT);    //DR M02
pinMode(2, INPUT);
pinMode(3, INPUT);
//analogWrite(9, 165);analogWrite(10, 73);
//analogWrite(9, 20);analogWrite(10, 30);
analogWrite(9,0);analogWrite(10, 75);
digitalWrite(2, 1);digitalWrite(3, 1);
digitalWrite(8, 0);digitalWrite(11, 1);
digitalWrite(1, 1);digitalWrite(0, 0);
attachInterrupt(0, GestIntCodM01, RISING);
attachInterrupt(1, GestIntCodM02, RISING);
Serial.begin(115200);

}
void loop() {
  
  //rb=digitalRead(15);
  // put your main code here, to run repeatedly:
        
//else ra=0;
//if(rb==1) b++;
Serial.print("M01 = ");Serial.print(b);
//Serial.print("\r");
Serial.print("    M02 = ");Serial.println(a);
Serial.print("\r");
//Serial.print("b = ");Serial.println(b);
}
void GestIntCodM01()
{
  a++;
  }
  void GestIntCodM02()
{
  b++;
  }



Whandall

Code: [Select]
int ra=0,rb,a=0,b=0;

What lousy names for global variables.  :'(

Integers for counts that start with zero and only increment?

a and b are accessed by ISRs, they have to be defined volatile and must be accessed guarded by noInterrups/interrupts.

http://gammon.com.au/interrupts
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Wahrani31

#8
Apr 30, 2016, 09:18 pm Last Edit: Apr 30, 2016, 10:02 pm by Wahrani31
Code: [Select]
int ra=0,rb,a=0,b=0;

What lousy names for global variables.  :'(

Integers for counts that start with zero and only increment?

a and b are accessed by ISRs, they have to be defined volatile and must be accessed guarded by noInterrups/interrupts.

http://gammon.com.au/interrupts
Thank's for your remark, i changed the type of variables a,b and delated the useless variables
but i still have the same result which is 90 ticks for one round and if i increase the motor's speed the arduino count less points 70 or 60 for the maximum speed



my new simple code is


Code: [Select]

volatile long a=0;
volatile long b=0;
void setup() {
pinMode(8, OUTPUT);     //L298 Direction pin for M02
pinMode(9, OUTPUT);     //PWM for M01
pinMode(1, OUTPUT);     //Dr for M01
pinMode(0, OUTPUT);     //Dr M02
pinMode(10, OUTPUT);    //PWM M02
pinMode(11, OUTPUT);    //Dr M02
pinMode(2, INPUT);
pinMode(3, INPUT);
analogWrite(9,0);analogWrite(10, 75);    //Motors speed
digitalWrite(2, 1);digitalWrite(3, 1);       //Pull-up resistor dor encoder sensors
digitalWrite(8, 0);digitalWrite(11, 1);
digitalWrite(1, 1);digitalWrite(0, 0);
attachInterrupt(0, GestIntCodM01, RISING);
attachInterrupt(1, GestIntCodM02, RISING);
Serial.begin(115200);

}
void loop() {
Serial.print("M01 = ");Serial.print(b);
Serial.print("    M02 = ");Serial.println(a);
Serial.print("\r");
}
void GestIntCodM01()
{
  a++;
  }
  void GestIntCodM02()
{
  b++;
  }

Sorry if i didn't uderstand very well your idea & thak you for traying to help me  ;)

Whandall

This is quite strange, please explain why you think you need that.

Code: [Select]
pinMode(1, OUTPUT);     //Dr for M01
pinMode(0, OUTPUT);     //Dr M02


Quote
The outputs are open collector and require pull-ups to whatever signal level is required.
This means it should be

Code: [Select]
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Wahrani31

This is quite strange, please explain why you think you need that.

Code: [Select]
pinMode(1, OUTPUT);     //Dr for M01
pinMode(0, OUTPUT);     //Dr M02


This means it should be

Code: [Select]
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);

Code: [Select]
pinMode(1, OUTPUT);     //Dr for M01
pinMode(0, OUTPUT);     //Dr M02

This outputs are to change the direction of the motors using the H bridge "L298"

Code: [Select]
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);

sincerely this is the first time i heard about "INPUT_PULLUP" mode.. i will try it now and i will be back.. Thank's

Wahrani31

The Same results for INPUT_PULLUP mode




Whandall

Code: [Select]
pinMode(1, OUTPUT);     //Dr for M01
pinMode(0, OUTPUT);     //Dr M02

This outputs are to change the direction of the motors using the H bridge "L298"
Attaching hardware to the serial pins (0 and 1) on Mega/Uno/Nano is not the best idea if you want to use serial I/O.

sincerely this is the first time i heard about "INPUT_PULLUP" mode..
Maybe you should read the documentation of the functions you are using (here: pinMode).

How did you connect the motor?

Code: [Select]
Connector

The EMG30 is supplied with a 6 way JST  connector (part no PHR-6) at the end of approx 90mm of cable as standard.
The connections are:
Wire colour Connection
Purple (1) Hall Sensor B Vout
Blue (2) Hall sensor A Vout
Green (3) Hall sensor ground
Brown (4) Hall sensor Vcc
Red (5) + Motor
Black (6) - Motor

Wire colours are from the actual cable.
The hall sensors accept voltages between 3.5v and 20v.
The outputs are open collector and require pull-ups to whatever signal level is required.
On the MD25 they are powered from 12v and pulled up to 5v for the signals.

specification

Rated voltage 12v
Rated torque 1.5kg/cm
Rated speed  170rpm
Rated current 530mA
No load speed 216
No load current 150mA
Stall Current  2.5A
Rated output 4.22W
Encoder counts per output shaft turn 360


Quote
...so i need to read 4 interrupts
Probably you don't supply power to the halls, because you believe there are 4 outputs?
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Wahrani31


no sir i am using a module like this one

and vcc of Hall sensor is driven out from the same module in the 5v output and the tow outputs for the direction are attached to the Enable PINs in the module


Whandall

How do you connect the 6 lines from the motor?



Code: [Select]
D2 Purple (1) Hall Sensor B Vout
D3 Blue (2) Hall sensor A Vout
?? Green (3) Hall sensor ground
?? Brown (4) Hall sensor Vcc
HB Red (5) + Motor
HB Black (6) - Motor

Green and Brown are connected to?

Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Go Up