PaulStoffregen
Well-known member
Arduino 1.6.4 is likely to release very soon too...
Arduino 1.6.4 is likely to release very soon too...
#define IN_BDM (bool)((LPC_GPIO2->FIOPIN) & (1 << 0)) // FREEZE is p26 P2.0
PinMode(FREEZE, INPUT);
if (digitalRead(FREEZE) == HIGH){ IN_BDM }
// For Teensy
#define PIN_PWR 2 // power supply
#define PIN_NC 14 // connection signal
#define PIN_BERR 7 // double bus fault input - will be an input when it is working properly
#define PIN_BKPT 8 // breakpoint/serial clock
#define PIN_RESET 6 // reset signal
#define PIN_DSI 20 // data input (to ECU) signal
#define PIN_DSO 21 // data output (from ECU) signal
#define PIN_FREEZE 5 // freeze signal
GPIOD_PDIR &= ~(1 << 2); // Set pin D2 as input.
...
GPIOD_PDIR |= (1 << 2); // Set pin D2 as output.
#define RST_COUNT 0
#define OE_COUNT 1
#define SEL1 3
#define SEL2 4
byte pinTable[] = {2,14,7,8,6,20,21,5}; //5 is D7 while 2 is D0
byte firstByte;
byte secondByte;
byte thirdByte;
byte fourthByte;
long fastcount = 0;
void setup() {
Serial.begin(115200);
for (int i=0; i<8; i++) { pinMode(pinTable[i],INPUT); } //I changed it from INPUT_PULLUP to INPUT
pinMode(SEL1, OUTPUT);
pinMode(SEL2, OUTPUT);
pinMode(RST_COUNT, OUTPUT);
pinMode(OE_COUNT, OUTPUT);
digitalWrite(RST_COUNT, HIGH);
digitalWrite(OE_COUNT, HIGH);
}
void loop() {
digitalWrite(OE_COUNT,LOW);
digitalWrite(SEL1,1);
digitalWrite(SEL2,0);
firstByte = GPIOD_PDIR & 0xFF;
fastcount = (long)firstByte;
digitalWrite(SEL1,0);
digitalWrite(SEL2,0);
secondByte = GPIOD_PDIR & 0xFF;
fastcount |= (long)(secondByte<<8);
digitalWrite(SEL1,1);
digitalWrite(SEL2,1);
thirdByte = GPIOD_PDIR & 0xFF;
fastcount |= (long)(thirdByte<<16);
digitalWrite(SEL1,0); //SEL1
digitalWrite(SEL2,1); //SEL2
fourthByte = GPIOD_PDIR & 0xFF;
fastcount |= (long)(fourthByte<<24);
digitalWrite(OE_COUNT,HIGH);
timer = micros();
Serial.print(timer);
Serial.print("\t");
Serial.print(fastcount);
Serial.println(" ");
}
#define RST_COUNT 0
#define OE_COUNT 1
#define SEL1 3
#define SEL2 4
byte pinTable[] = {2,14,7,8,6,20,21,5}; //5 is D7 while 2 is D0
byte firstByte;
byte secondByte;
byte thirdByte;
byte fourthByte;
int encoder_array[32];
long count = 0;
long fastcount = 0;
void setup() {
Serial.begin(115200);
for (int i=0; i<8; i++) { pinMode(pinTable[i],INPUT); } //I changed it from INPUT_PULLUP to INPUT
pinMode(SEL1, OUTPUT);
pinMode(SEL2, OUTPUT);
pinMode(RST_COUNT, OUTPUT);
pinMode(OE_COUNT, OUTPUT);
digitalWrite(RST_COUNT, HIGH);
digitalWrite(OE_COUNT, HIGH);
}
void loop() {
digitalWrite(OE_COUNT,LOW);
digitalWrite(SEL1,1);
digitalWrite(SEL2,0);
encoder_array[0] = digitalRead(2); //I was trying to use digitalReadFast with the argument as a constant
encoder_array[1] = digitalRead(14);
encoder_array[2] = digitalRead(7);
encoder_array[3] = digitalRead(8);
encoder_array[4] = digitalRead(6);
encoder_array[5] = digitalRead(20);
encoder_array[6] = digitalRead(21);
encoder_array[7] = digitalRead(5);
firstByte = GPIOD_PDIR & 0xFF;
fastcount = (long)firstByte;
digitalWrite(SEL1,0);
digitalWrite(SEL2,0);
encoder_array[8] = digitalRead(2);
encoder_array[9] = digitalRead(14);
encoder_array[10] = digitalRead(7);
encoder_array[11] = digitalRead(8);
encoder_array[12] = digitalRead(6);
encoder_array[13] = digitalRead(20);
encoder_array[14] = digitalRead(21);
encoder_array[15] = digitalRead(5);
secondByte = GPIOD_PDIR & 0xFF;
fastcount |= (long)(secondByte<<8);
digitalWrite(SEL1,1);
digitalWrite(SEL2,1);
encoder_array[16] = digitalRead(2);
encoder_array[17] = digitalRead(14);
encoder_array[18] = digitalRead(7);
encoder_array[19] = digitalRead(8);
encoder_array[20] = digitalRead(6);
encoder_array[21] = digitalRead(20);
encoder_array[22] = digitalRead(21);
encoder_array[23] = digitalRead(5);
thirdByte = GPIOD_PDIR & 0xFF;
fastcount |= (long)(thirdByte<<16);
digitalWrite(SEL1,0); //SEL1
digitalWrite(SEL2,1); //SEL2
encoder_array[24] = digitalRead(2);
encoder_array[25] = digitalRead(14);
encoder_array[26] = digitalRead(7);
encoder_array[27] = digitalRead(8);
encoder_array[28] = digitalRead(6);
encoder_array[29] = digitalRead(20);
encoder_array[30] = digitalRead(21);
encoder_array[31] = digitalRead(5);
fourthByte = GPIOD_PDIR & 0xFF;
fastcount |= (long)(fourthByte<<24);
digitalWrite(OE_COUNT,HIGH);
long count = 0;
for (int i=0; i<32; i++)
{
count = count + (encoder_array[i] * (int)pow(2,i));
}
timer = micros();
Serial.print(timer);
Serial.print("\t");
Serial.print(fastcount);
Serial.print("\t");
Serial.print(count);
Serial.println(" ");
}
Does it work with Teensy @ 24 MHZ ? If yes, perhaps the new code is too fast
byte pinTable[] = {2,14,7,8,6,20,21,5}; //5 is D7 while 2 is D0
byte firstByte;
byte secondByte;
byte thirdByte;
byte fourthByte;
#define RST_COUNT 0
#define OE_COUNT 1
#define SEL1 3
#define SEL2 4
int encoder_array[32];
long fastcount = 0;
unsigned long timer;
void setup() {
Serial.begin(115200);
for (int i=0; i<8; i++) { pinMode(pinTable[i],INPUT); }
pinMode(SEL1,OUTPUT); //SEL1
pinMode(SEL2,OUTPUT); //SEL2
pinMode(RST_COUNT,OUTPUT); //RST_COUNT
pinMode(OE_COUNT,OUTPUT); //OE_COUNT
digitalWrite(0,HIGH); //RST COUNT
digitalWrite(1,HIGH); //OE_COUNT
}
void loop() {
long fastcount = 0;
digitalWrite(1,LOW); //OE_COUNT
digitalWrite(3,1);
digitalWrite(4,0);
encoder_array[0] = digitalRead(2); //{2,14,7,8,6,20,21,5} **leaving this random read to refresh the 8 bits
// encoder_array[1] = digitalRead(14);
// encoder_array[2] = digitalRead(7);
// encoder_array[3] = digitalRead(8);
// encoder_array[4] = digitalRead(6);
// encoder_array[5] = digitalRead(20);
// encoder_array[6] = digitalRead(21);
// encoder_array[7] = digitalRead(5);
firstByte = GPIOD_PDIR & 0xFF;
fastcount = (long)firstByte;
digitalWrite(3,0);
digitalWrite(4,0);
encoder_array[8] = digitalRead(2); //**leaving this random read to refresh the 8 bits
// encoder_array[9] = digitalRead(14);
// encoder_array[10] = digitalRead(7);
// encoder_array[11] = digitalRead(8);
// encoder_array[12] = digitalRead(6);
// encoder_array[13] = digitalRead(20);
// encoder_array[14] = digitalRead(21);
// encoder_array[15] = digitalRead(5);
secondByte = GPIOD_PDIR & 0xFF;
fastcount |= (long)(secondByte<<8);
digitalWrite(3,1);
digitalWrite(4,1);
encoder_array[16] = digitalRead(2); //**leaving this random read to refresh the 8 bits
// encoder_array[17] = digitalRead(14);
// encoder_array[18] = digitalRead(7);
// encoder_array[19] = digitalRead(8);
// encoder_array[20] = digitalRead(6);
// encoder_array[21] = digitalRead(20);
// encoder_array[22] = digitalRead(21);
// encoder_array[23] = digitalRead(5);
thirdByte = GPIOD_PDIR & 0xFF;
fastcount |= (long)(thirdByte<<16);
digitalWrite(3,0); //SEL1
digitalWrite(4,1); //SEL2
encoder_array[24] = digitalRead(2); //**leaving this random read to refresh the 8 bits
// encoder_array[25] = digitalRead(14);
// encoder_array[26] = digitalRead(7);
// encoder_array[27] = digitalRead(8);
// encoder_array[28] = digitalRead(6);
// encoder_array[29] = digitalRead(20);
// encoder_array[30] = digitalRead(21);
// encoder_array[31] = digitalRead(5);
fourthByte = GPIOD_PDIR & 0xFF;
fastcount |= (long)(fourthByte<<24);
digitalWrite(1,HIGH); //OE_COUNT
timer = micros();
Serial.print(timer);
Serial.print("\t");
Serial.print(fastcount);
}
I know, but this test would help to know wether it is a speed issue or not. It takes some seconds only....Hmm, it kind of defeats the purpose of running at 24MHz, I was intending to have it as fast as possible.
I know, but this test would help to know wether it is a speed issue or not. It takes some seconds only....
If it is a speed-issue, a delayMicroseconds(1) would help, too.... then, you could try shorter delays, and insert some asm volatile ("nop").
I made an account just to say thanks.
It would have been so annoying going through the datasheet until that WTF moment.
I discovered that the PINx registers were emulated because you can't assign as you would in AVR likeso DDRA=0x0F|1<<6. It throws an error. I hope the guys on PJRC put together a better pin register access tutorial, because I think that pin register emulation is pointless, and direct access to pins is critical.
Hey! thanks for the answer. Indeed I understand. Now I have two questionsHope that helps clarify.
https://forum.pjrc.com/threads/17532-Tutorial-on-digital-I-O-ATMega-PIN-PORT-DDR-D-B-registers-vs-ARM-GPIO_PDIR-_PDOR?p=21228&viewfull=1#post21228A lot of planning went into maximizing Arduino compatibility. A lesson learned from earlier versions of Teensy was how entrenched the Arduino Uno pinout is, even years after Arduino has released other boards with signals on different pins. For example, a LOT of tutorials on many websites, particularly Adafruit and Sparkfun, are written for the SPI signals to be pins 10, 11, 12 and 13. A tremendous effort went into studying nearly all libraries and examples that existed 2 years ago (when Teensy 3.0 was designed) and crafting a pinout that would maximize compatibility.
Even with a 4 layer PCB, routing all those pins was incredibly difficult. In the end, I believe it was worthwhile. For most advanced users, the pin arrangement doesn't matter much. But for a lot of novices, having the signals on pins that correspond to many tutorials published on other websites really makes things simpler.
About pins changing states at the exact same moment, why don't you try using transparent latches like HEF40373 or 74HCT573 (D-type Flip-flops with Output latches)? You input your parallel data, and latch it, and unlatch it, on and on, and you get simultaneous bus bits state changes. I think the downside of this is more on about speed issues.
time response is different on PIN 13 (LED) (PTC5) than on any other Pin.