Tuesday, 19 June 2012

32BITS , 80Mhz Pinguino with PIC32MX440F256H

Yes, the ready-to-use version arrived, and its from one of my favourite developers of boards out there-OLIMEX !! They have got us used to top quality stuff, no doubt !!
So THIS BOARD , with a PIC32 MX440F256H, uses the PINGUINO IDE , with the same kind of language that Arduino got us used to !
I am also doing a PINGUINO DIY version of it with the PIC32MX250F128B DIP, that takes advantage of the USB bootloader for the PIC32.














Saturday, 16 June 2012

INTRO to port manipulation

Someone asked me about what sometimes comes in more direct code in Arduino, when we use port manipulation to make it a bit faster or for convenience, etc...
So a likkle explanation here !


As we know, Arduino made it much easier for beginners to create code in sketches by doing away with the complexity seen before in uC's ( port registers, header files, linker files, etc etc)
With just one button , most people are blinking a led and really understanding the basics of it in 10 minutes( most of it will be installing and downloading the IDE anyway).

But time comes that we will get to hit the limits of such simplicity, as their own functions, etc do take more time. For that, AVR way of doing things is the way to go.
So, by doing it directly improves the speed quite considerably.
So in this case we will do away with digitalWrite()/digitalRead(), by doing some port manipulation directly..


ATMega328 has three port registers that  can be altered to set the digital and analogue I/O pins. A port register is a kind of byte variable that we can change on the microcontroller, in order to control the state of various I/O ports. We have three port registers to work with:

D – digital pins 7 to zero (Port D)
B – digital pins 13 to eight (Port B)
C – analogue pins 5 to zero (Port C!)



In void setup(), we will use

DDRy = Bxxxxxxxx
where y is the register type (B/C/D) and  xxxxxxxx  are 8 bits that determine if a pin is to be an input or output. Use 0 for input, and 1 for output. The LSB (least-significant bit [the one on the right!]) is the lowest pin number for that register.
So , then, to control the port pins, we will use

PORT y  = Bxxxxxxxx
where y is the register type (B/C/D) and  xxxxxxxx  are 8 status bits = 1 for HIGH, 0 for LOW. That can be seen in the following example:

// Digital 0 to 7 set to outputs, then on/off using port manipulation

void setup()
{
  DDRD = B11111111; // set PORTD (digital 7~0) to outputs
}

void loop()
{
  PORTD = B11111100; // digital 2~7 HIGH, digital 1~0 LOW
  delay(1000);
  PORTD = B00000011; // digital 2~7 LOW, digital 1~0 HIGH
  delay(1000);
}




So, we have set the digital pins 7~0 to output in void setup().
Then we alternate them on and off , pins 2 to 7 with 0 and 1 always in opposite state to the rest .

So, why would we want to do that ?!?
Well, John Boxall done some measurements and he came up with these results :



The code as a first test:


// Digital 0 t o7 set as outputs, then on/off using digitalWrite()

void setup()
{
  for (int a=0; a<8; a++)
  {
    pinMode(a, OUTPUT);
  }
}

void loop()
{
  for (int a=0; a<8; a++)
  {
    digitalWrite(a, HIGH);
  }
  for (int a=0; a<8; a++)
  {
    digitalWrite(a, LOW);
  }
}

And the frequency was  14.085 kHz.
This would be a way of doing it.
Another way would be:


// Digital 0~7 set to outputs, then on/off using individual digitalWrite() 

void setup()
{
  for (int a=0; a<8; a++)
  {
    pinMode(a, OUTPUT);
  }
}

void loop()
{
  digitalWrite(0, HIGH);
  digitalWrite(1, HIGH);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(0, LOW);
  digitalWrite(1, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
}

A small speed boost, the frequency has increased to 14.983 kHz. 

Then he used same type of port manipulation with:

// John Boxall - October 2011
// Digital 0~7 set to outputs, then on/off using port manipulation

void setup()
{
  DDRD = B11111111; // set PORTD (digital 7~0) to outputs
}

void loop()
{
  PORTD = B11111111;
  PORTD = B00000000;
}


And  the frequency measurements – 1.1432 MHz! 




However, as he notes, there are a few things to take note of:


You can’t control digital pins 0 and 1 (in bank D) and use the serial monitor/port. For example if you set pin zero to output, it can’t receive data!


Also , keep this in mind, if you are not aware of it-


Binary| Hex
0000 |  0
0001 |  1
0010 |  2
0011 |  3
0100 |  4
0101 |  5
0110 |  6
0111 |  7
1000 |  8
1001 |  9
1010 |  A
1011 |  B
1100 |  C
1101 |  D
1110 |  E
1111 |  F

(precede with 0x)


So in those terms, this code

void setup()
{
  DDRD = B11111111; // set PORTD (digital 7~0) to output
}

could be written as:

void setup()
{
  DDRD = 0xFF; // set PORTD (digital 7~0) to output
}


Same way that


void loop()
{
  PORTD = B00000000; //Digital 0~7  to LOW
}
Could be done as

void loop()
{
  PORTD = 0x00; //Digital 0~7  to LOW
}

*PS- Someone asked "...why is digitalWrite() relatively that much slower?"

Well, here is the source code for digitalWrite() – quite a lot of code to convert compared against port manipulation:
void digitalWrite(uint8_t pin, uint8_t val)
{
  uint8_t timer = digitalPinToTimer(pin);
  uint8_t bit = digitalPinToBitMask(pin);
  uint8_t port = digitalPinToPort(pin);
  volatile uint8_t *out;

  if (port == NOT_A_PIN) return;

  // If the pin that support PWM output, we need to turn it off
  // before doing a digital write.
  if (timer != NOT_ON_TIMER) turnOffPWM(timer);

  out = portOutputRegister(port);

  if (val == LOW) {
    uint8_t oldSREG = SREG;
    cli();
    *out &= ~bit;
    SREG = oldSREG;
  } 
  else {
    uint8_t oldSREG = SREG;
    cli();
    *out |= bit;
    SREG = oldSREG;
  }
}



Tuesday, 12 June 2012

Endeavours with Robots and Sonar type App

Sonar type graphical app with Ultra Sonic range measurement module Model: SEN136B5B from seedstudio, Atmel Mega2560 test, to use in a robot.
 Was done using Processing language and the Ultra Sonic range measurement is programmed using Wiring Language/Arduino ( As its for a robot). Implementation will be an autonomous obstacle avoiding robot, as first instance to be upgraded incrementally !






Sonar App with ProcessingUltra Sonic range measurement module Model: SEN136B5B

















Based on work and code, with 2 different sensors  @ http://luckylarry.co.uk/arduino-projects/arduino-processing-make-a-radar-screen-part-3-visualising-the-data-from-sharp-infrared-range-finder/

g7electronica Portugal

If you are around in Portugal ( Country i was born), and into Robotics/Electronics check http://www.g7electronica.com for online shopping and personalized service as well ! !!


Se estiveres em Portugal e gostares de Robotica  e/ou Electronica da um salto a http://www.g7electronica.com para compras online, e servicos personalizados para privados/empresas ( PCBs, desenvolvimento de solucoes por medida em robotica e electronica).

Friday, 8 June 2012

Direct Digital Synthesis

For a while i been exploring the several possible ways of generating sound with the Atmega 328.
So one of the good ways is always to start from the basic things !

Direct digital synthesis (DDS) is a method of producing an analog waveform— Sine wave, Square wave or Triangle are the most used and required— by generating a time-varying signal in digital form and then performing a digital-to-analog conversion.
Square, triangular, and sinusoidal waves from a DDS




There are many possibilities for frequency generation , ranging from phase-locked-loop (PLL)-based techniques for very high-frequency synthesis, to dynamic programming of digital-to-analog converter (DAC) outputs to generate arbitrary waveforms at lower frequencies.
A good example can be a signal generator, an essential tool in the arsenal of any electronics geek !!
It is also widely used in Radio frequencies, and modulation  .


So i came across this Article on Direct Digital Synthesis. http://lionel.cordesses.free.fr/gpages/DDS1.pdf

 Also check these webcasts from Analog Devices...   http://www.analog.com/en/content/WC_FUN_PLL/webcast.html
and
http://www.analog.com/en/content/WC_FUN_DDS/webcast.html

Last but not least, the good ol'Wikipedia
http://en.wikipedia.org/wiki/Direct_digital_synthesizer

Thursday, 7 June 2012

Mobile Processing and Bluetooth app to control my projects...

Wow !!
 I just realized that the language Processing also has a MOBILE environment, as a replacement for J2ME( Java). 
So that means i can finally write the Apps to control whatever i want ( bluettoth, web, messaging, all with a good graphic set of libraries, 3D included...) EH EH EH
 Can i sense a possible interface with a dub siren controlled by the phone and all !?! YES !! Nokia and Android !! I will be forced to discriminate I Phones for obvious reasons( it will soon be dead anyway).
For those interested in the programming side of it, here are some links
http://mobile.processing.org
http://processing.org/
PS- Also check the blog of the person who gave me the hint at http://lusorobotica.com/ 's forum !!
http://redacacia.wordpress.com/2010/09/15/controlling-a-robot-with-bluetooth-enabled-cell-phone/




Saturday, 2 June 2012

temperature sensor TMP36

Basic test, with a temperature sensor TMP36, displaying voltage, Celsius degrees and Fahrenheit degrees.
This will be added to the clock my dad asked me ( see below).