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
image
  • Bluetooth v2.1 + EDR Compatible
  • 2.4GHz Class 2 Radio
  • Range Exceeds 20m
  • High Speed Data Rate Up To 3Mbps
  • 12 Programmable Digital I/O Pins
  • 2 Programmable Analog I/O Pins
  • UART and USB Interfaces
  • Onboard Antenna
  • 8Mbit Flash Memory
  • CSR BlueCore™ 4 Ext Chipset
The program basically turn on a LED connected at the PB0 when receives an A character form the PC and turn off the same LED when the received character is X.
The circuit is:
image
The 3D representation
 dsPIC30F3012BLUETOOTH
The code:
#include <30F3012.h>
#FUSES HS2_PLL8//The xtal freq is divided by 2 and multiplied by 8 (maximum using 20MHz xtal)
#FUSES NOWDT // NO Watch Dog Timer
#FUSES PR_PLL //Primary Oscillator
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#use delay(clock=80000000)// 20MHz/2*8=80MHz=80000000
#use rs232(baud=115200, UART1) //Set UART speed in 115.2kbps using the UART1 
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
}
}
}
Important points:
  • Is important to use the PLL in order to achieve large clock frequency, otherwise, the compiler sends an error if you want baud rate higher than 38400 bps.
  • The Bluetooth module is basically a serial port. The only difference is that you have to make the link to the PC before you can use it. After the connection, the module is view as any other port in the PC and can be accessed by a simple terminal.
For this example a terminal built in the mikroC compliler was used.
The code is written in C for the CCS compiler (NOT the mikroC).

No comments:

Related Posts Plugin for WordPress, Blogger...