This blog is written in order to save notes about the stuffs I´m doing on technical issues.
Friday, October 1, 2010
Accelerometer LIS3LV02DL controlled by a dsPIC, I2C communication
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.
Wednesday, March 17, 2010
dsPIC30F3012 hello world program
In other words:
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.5kbpsvoid wait() //Subroutine to wait for 2ms or until a character is received{int countdown; //Define a variable to coutndowncountdown=200;//Charge value to 200 times 10uswhile((--countdown!=0)&&!kbhit()) //kbhit returns true when rs232 receives a characterdelay_us(10);//do a delay of 10us}void main() //Main program{char pos;//Define the variable to receive the characterset_tris_b(0);//Define PORTB as outputpos='0';//Charge an initial value to the variablewhile(TRUE) //Do a loop{if((pos=='A'))//If the received character is an "A"output_high(PIN_B0);//Turn on the whole PORTBelse if ((pos=='X'))output_low(PIN_B0);//Turn on the whole PORTBelse//otherwiseoutput_high(PIN_B1);//Turn off the more significant bitswait();//Wait for visualization or another characterif(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.
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.
Monday, March 1, 2010
Inserting code in blogger
WLWSourceCodePluginSetup.msiWhich 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 librariesvoid main() {unsigned int8 i, value, min, max;setup_adc_ports(AN0);setup_adc(ADC_CLOCK_INTERNAL); // Built-in A/D setup functionset_adc_channel(0); // Built-in A/D setup functiondo {printf("Sampling:"); // Printf function included in RS232 librarydelay_ms(1000);min=255;max=0;for(i=0; i<=30; ++i) {delay_us(50); // Built-in delay functionvalue = read_adc(); // Built-in A/D read functionif(value<min)min=value;if(value>max)max=value;}printf("\r\nMin: %2X Max: %2X\n\r",min,max);} while (TRUE);}