Tuesday, November 2, 2010

Finding the area of an amorphous figure in MATLAB

Imagine that you have two series of data (nx1). Imagine that these data come from two sensors measuring the displacement of a particle around a certain point, something like this:image
  The sensors measure the position X-Y 100 times each second and you measure for 30 seconds. At the end you will have two vectors containing 3000 samples each one.
Something like this:
x=[3,6,7,5,3,2,3,7,8,9,7,5,3,2,12,3,5,7,8……...n]
y=[2,4,5,6,7,3,5,3,2,5,7,43,6,8,4,2,4,6,7,……..n]
From these vectors of data you must to calculate the area  drawn by the (x,y) values, the contour of the image.

Sunday, October 31, 2010

Electric blanket

Have you ever wonder how an electric blanket works?

Well, Personally I have not wonder that because I Thought had an idea about the basis of this common device. But some days ago somebody give me a small electric blanket and suddenly it stops working. And, you known me, if something can be striped down, it will be striped down! Guiño

Here the pics and the things I saw.

Friday, October 1, 2010

Accelerometer LIS3LV02DL controlled by a dsPIC, I2C communication

This post describe the basic data acquisition of an accelerometer LIS3LV02DL over I2C bus using a dsPIC30F3012.
This tri-axial accelerometer has a 12 or 16 bit digital output and can be interfaced using SPI or I2C communication.
The circuit employed is a very simple one, The dsPIC is connected to the accelerometer LIS3LV02 via I2C using the B7 and B6 pines. The dsPIC reads the data and send the results in ascii format to the PC using the bluetooth module KC21.

Thursday, August 12, 2010

The internal oscillator in the pic12f629

Recently I’ve been using the small pic12f629. My interest was to use it as a cheap D/A converter using PWM. Unfortunately, I chose this pic at the store without check the characteristics first, yeah, I know, how stupid of me! :-(
I thought it had a PWM module as well as a ADC converter, but not, no PWM and no ADC, but anyway, for my purposes it could works! Also, the price was good! just 1000 yen for 10 SO chips plus one DILP!  :-)
So, I present a program to read three inputs “quasi-digital” coming from a FSR sensor and then, according to the inputs, the pic will generate a PWM signal with width of pulse depending on the combination of the input signals.

Friday, August 6, 2010

Analog Digital converter of dsPIC30F3012 using internal ADC clock

I have been reading about the ADC for the dspic30F3012 or the family of these dsPICs.
As I explained in the anterior post, the data sheet mentions an internal clock exclusive for the ADC. From this clock depends the conversion time and then the sampling rate, so, it’s very important to know that data. Surprisingly I didn’t find that data neither the datasheet nor the family reference manual. Probably it exists there but very very hidden. 

Thursday, August 5, 2010

PWM using the dsPIC30F3012 with internal oscillator

This time let’s generate a PWM signal using the dsPIC30F3012 running with its internal oscillator in order to have the RC15 pin available for other applications.
The internal oscillator of this device runs at 7.37MHz and is possible to multiply it by 4, or 16 using the PLL.
In this example I’m using the PLL16 which combined with the 7.37MHz internal oscillator gives a total clock frequency of 117.92MHz, or 29.48 MIPS.
I’m using the circuit depicted below.

Monday, May 24, 2010

USING A SD CARD WITH FAT16, PIC18F2550 and Bluetooth

imageI bet some day we have needed to or wanted to save a large amount of data using the common SD cards and some microcontroller.
For example to save samples of temperature every hour, or every minute, to read pictures and show them in a full color display, or maybe to make an mp3 player. Well the possibilities are huge.
This time I wanted to save EMG signals (Electromyography) from my active EMG sensors. For this reason I made a circuit and a program for the PIC18F2550 microcontroller.
Basically I have to meet two requirements.

Friday, March 19, 2010

dsPIC30F3012 rs232 115200 bps

Now the program and circuit to connect a dsPIC 30F3012 via serial to a PC. The serial connection is made using a Bluetooth module fro kc wirefree (R). The KC21.
BLUETOOTH characteristics

Wednesday, March 17, 2010

dsPIC30F3012 hello world program

The next program flashes a LED connected in the PB0 (pin 2) of a dsPIC30F3012. The chip is running at 80MHz using a crystal of 20MHz. The PLL multiplies the half of the crystal frequency by 8.
In other words:

Tuesday, March 16, 2010

IBOulet

This is an identifier used before to declare a variable

Use example:

IBOutlet UITextField*txtUserName;

Where:

UITextFiel= Type of variable

txtUserName= Name of the variable

IBOutlet is used in order to be able the interface builder to synchronize the display and connections of outlets with Xcode.

Monday, March 8, 2010

Iphone’s accelerometer

Here the first findings about the iphone’s accelerometer.
According to this source, the iphone has a LSI302DL sensor. Here some specs data about that sensor.

Sunday, March 7, 2010

RS232 and the PIC18F2550

The next code connects a PIC to a computer via RS232.

//Program to receive characters over RS232, if the received character is "A" then set B0 Other character set B1 "X" low B0
#include <18F2550.h>//Selects the microcontroller
#fuses HS,NOWDT,NOPROTECT,PLL5,CPUDIV1//High freq clock, No WDT, No code protection, No low voltage programming
#use delay(clock=20000000)//Xtal frequency = 20MHz
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7)//11.5kbps
void wait() //Subroutine to wait for 2ms or until a character is received
{
  int countdown;        //Define a variable to coutndown 
  countdown=200;//Charge value to 200 times 10us
  while((--countdown!=0)&&!kbhit()) //kbhit returns true when rs232 receives a character
    delay_us(10);//do a delay of 10us
}
void main() //Main program
{
  char pos;//Define the variable to receive the character
  set_tris_b(0);//Define PORTB as output
  pos='0';//Charge an initial value to the variable
  while(TRUE) //Do a loop
{
if((pos=='A'))//If the received character is an "A"
  output_high(PIN_B0);//Turn on the whole PORTB
  else if ((pos=='X'))
  output_low(PIN_B0);//Turn on the whole PORTB
else//otherwise
  output_high(PIN_B1);//Turn off the more significant bits
  wait();//Wait for visualization or another character
  if(kbhit()) //If a character is received
{
   pos=getc();//Save the received character in the 'pos' variable
   }
}
}

Basically the micro turns on a LED connected in the PORTB 0 (pin 21)when the character received is an “A” and turns off the same pin when the character received is “X”. In case of receive any other character turns the B1 (pin 22).

NOTE:

While doing this program I faced several problems.

My connection RS232 is via Bluetooth using a KC-21 module of KC- wirefree. The problem was that, once the connection Bluetooth was achieved, the PIC could send information but it was unable to receive it. It was necessary reinitialize the PIC in order to be able to receive data.

My original firmware had a delay just after configure the PORT B as output for allowing a stabilization of the RS232 (I thought) but when I eliminated this delay, everything worked fine.

image

The Bluetooth module connects to the PC when the PC tries to connect to the port mapped for that module, in this case COM44 for my module and PC. after the connection the PIC is able to receive and sent data smoothly!

Below the pic of my circuit and the terminal RS232 in the PC using the terminal of the mikro C compiler (Please, don’t be confused, the code is written in the CCS compiler)

I hope it works fine after tomorrow also. Now is time to leave the lab.

Wednesday, March 3, 2010

#FUSES for the PICF2550

If you are:

  • Programming using the CCS compiler.
  • Using the PIC18F2550
  • Loading the firmware using the WinPic800
  • Your Xtal is 20MHz

Then, in order for everything works fine when programming the RS232 module, you have to, either, change the parameters as showed in the below figure every time you load the firmware or define the FUSE directive as follows:

#fuses HS,NOWDT,NOPROTECT,PLL5,CPUDIV1

PLL5: defines the oscillator selection to 20MHz

CPUDIV1: Avoid a postscaler of the main clock “CPU system clock”

image

This issue made me waste a lot of time because my RS232 model was not working as expected using the CCS compiler but using the Mikro C compliler everything worked fine. Until I changed these parameters.

Tuesday, March 2, 2010

Using the ADC of the PIC18F2550

 

NOTE: The program was wrote using the CCS C compiler PIC C with a license PCWHD

The next program reads the 10-bit ADC of the PIC18F2550, channel 0(pin 2), and send the  higher 8 bits of the result through the port B

The sampling rate is  1kHz approximately.

  1: #include <18F2550.h>			//Selecting the microcontroller
  2: #fuses HS,NOWDT,NOPROTECT,NOLVP		//Selecting set-up (high freq oscillator, no watch dog timer, et)
  3: #use delay(clock=20000000)		//We gonna use a 20MHz crystal
  4: void main()				//Main function
  5:  {    unsigned int8  value;		//Defines a variable called "value", length 8 bits
  6:    setup_adc_ports(AN0);		//Defines wich port will be used for the ADC module, ANO (pin 2)
  7:    set_tris_b(0xFF);     		//Sets the whole port B as output 
  8:    setup_adc(ADC_CLOCK_DIV_32);         //Selects the TAD=1.6us (Xtal=20MHz) 
  9:    set_adc_channel(0);                  //Selects the channel from were the measures will be taken
 10:    do	 	             	//Initiates a loop
 11: 	{
 12: 		delay_ms(1);		//Delay of 1ms between measures
 13: 		value = read_adc();        //Starts the ADC module which return a value and this value is saved.
 14: 		output_b(value);    	//The ADC returned value is sent to the port B
 15: 	} 	
 16: 	while (TRUE);			//The loop never ends	
 17: }

Important issues:

The ADC module takes 11TAD to gat a 10-bit conversion. The TAD time is defined by the instruction

setup_adc(ADC_CLOCK_DIV_32);

In this case we divide the clock (20MHz) into 32 to get a time of 1.6us. This is the minimum time of adquisition for the ADC module.  As the module takes 11 TAD to performs the conversion in 10-bit, then the total time of conversion is:

Total conversion time=11*TAD=11*(1.6uS)=17.6uS

If we wish a more accurate sampling rate we should take this value into account and subtract it from our delay routine. In this case, for a 1kHz sampling rate we should put a delay of:

Delay=1ms-17.6uS=982.4uS

This value without take into account the time that each instruction take to be executed by the micro.

image

The image above shows the results of the program in a simulation made using the PIC18 simulator.

Note that for a 468 in decimal, the output in binary is 0111010100 That is the value showed by the LEDS without the two bits least significant.

image

The circuit in breadboard working.

References:

Monday, March 1, 2010

Inserting code in blogger

In order to be able to insert source code in a post in blogger i´m using the below application:
WLWSourceCodePluginSetup.msi
Which is used by windows live writer.
It has several options like:
Type of source code (C, C++, phyton, basic, etc etc)
The result is something like this:
#include <18F2550.h>                            //Selecting the PIC
#fuses HS,NOWDT,NOPROTECT,NOLVP                 //Defining the fuses
#use delay(clock=20000000)                      //Clock speed
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7)  //Defining the RS232 pins and including the libraries
void main() {
unsigned int8 i, value, min, max;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);            // Built-in A/D setup function
set_adc_channel(0);                       // Built-in A/D setup function
do {
printf("Sampling:");                      // Printf function included in RS232 library
delay_ms(1000);
min=255;
max=0;
for(i=0; i<=30; ++i) {
delay_us(50);                      // Built-in delay function
value = read_adc();                 // Built-in A/D read function
if(value<min)
min=value;
if(value>max)
max=value;
}
printf("\r\nMin: %2X  Max: %2X\n\r",min,max);    
} while (TRUE);
}
Related Posts Plugin for WordPress, Blogger...