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...