Monday, December 24, 2012

How to make a Simple dsPIC wave Player

I'm sure this is a pretty interesting project for dsPIC lovers. :-)  first of all I would like to tell this is not a my own idea, Just I found a someones project on a site when I was finding a dsPIC based wave player through the Google. I had to do some modifications to get it to work. The site is http://www.uchobby.com and I would like to thanks for this wonderful project creator Mr. Bern. Here I'm going to describe the special points that you have to pay attention when assembling the circuit and compiling the code.

My modifications : 

1. Used dsPIC30F6014A and dsPIC30F4011 ( I have attached two separate codes for both MCs below)
2. I have cleaned the unnecessary codes from the project.
3. I have removed the code for Serial port interfacing part. ( In this project play a predefined wave file from the Micro SD memory card. ) Don't worry I'll explain how to select a file to play by using buttons. :-)

Lets look at the schematics diagram.
 This dsPIC30F6014A MCU is a surface mount type. If you don't have the SMD facilities to assemble the circuit just buy a MCU card here >> dsPIC30F6014A MCU 
Actually I also bought a dsPIC30F6014A MCU card there. The cost is $24.90 USD and DHL Express charges was $25.00 USD.

dsPIC30F6014A MCU cards.
You can use few dual line IC base for make pin connections like below.
(I'll post a video related to this ASAP) :-(

you can use a bread board to assembling the circuit. My circuit look like below.
(I'll post a video related to this ASAP) :-(

Don't forget to use a good SD card (I used sandisk 1GB microSD card and kingston 1GB microSD card, Both works nicely :-) ) and format it with FAT32 file system.

wave file preparation.
You should use following wave file configuration to play with this player.

File type : wave(microsoft)
Wave sample rate : 22,050
Audio bit depth : 8bit
Channels : 1 (Mono)

Create a wave file with these setting and save it to formatted Micro SD card.  You can use a audio editing software to create a wave file. Anyway I have uploaded a sample wave clip (soundclip.wav) you can download it here.

Compiling the code
You should have to download and install MPLAB IDE and C30 compiler(MPLAB® C Compiler for dsPIC DSCs). Special free versions for academic use are available here.You can download it for free.

Click below to download.

Download the project files here >> Simple dsPIC wave Player   password : simpletechtrick

Then extract project files to a folder and open the project by clicking on the "SDcard.mcw" file.  see the images below.


Then click the "Build all" button to compile the code.

If there is no errors, you can see the message "BUILD SUCCEEDED"  in the Output window. Now go to project folder and you can see a Hex file in the project folder.


Hex file in project folder.

 Now time to write this SDcard.hex file to Microcontroller. Use your programmer to programming the hex.

Here i'm using 10MHz crystal in my system with "PLL4"  so my system clock runs at 40MHz. All sampling rates and PWM frequency calculations were done according to this clock frequency. If you need to run in different frequency you can change the codes according to your needs.

for a example if you like to use 4Mhz crystal with PLL8, you have to change this line in main.c file 2nd line. (_FOSC(CSW_FSCM_OFF & XT_PLL4);) like below.

_FOSC(CSW_FSCM_OFF & XT_PLL8);    // 4X8 = 32MHz

and you have to calculate and change the PR1 value (Timer Period register), see the Line 87 in the main.c file

     PR1 =  (10000000/WavSRate)-10;

change this line as following.

     PR1 =  (8000000/WavSRate)-10;   // (FOSC/4)/WavSRate-10

and you have to change the Line 72 in hardwareProfile.h file.

// Sample clock speed for a 16-bit processor
#elif defined (__C30__)

    #define GetSystemClock()        40000000 //64000000

change like below.

// Sample clock speed for a 16-bit processor
#elif defined (__C30__)

    #define GetSystemClock()        32000000 //64000000

and finally change PR2  value in PWM.c file Line28. It look like this PR2    = 300;

You can calculate it like below.
PR2 = fosc(Hz)/(4*PWM frequency(Hz))

in my case it was 300, that mean I set the PWM frequency to 33KHz

PR2 = 40,000,000/4/33000 = 300 (approximately)

for a example if you like to use 4Mhz crystal with PLL8 and set PWM frequency to 33KHz you can calculate your PR2 value like below.

PR2 = 32,000,000/4/33000 = 242(approximately)

There for your code should look like below ( in PWM.c file 28th line)
PR2    = 242;

(I'll upload a youtube video with above figures and complete working project as soon as possible)

Good luck..! :-)

Tuesday, December 11, 2012

Sending SMS using Huawei 3G modem | MATLAB code (Working and Tested)

This is the method for sending SMS to any Mobile phone or a GSM Modem using MATLAB.
Here I'm using a Huawei  E156G 3G modem/dongle to send the SMS. Don't worry first study the method and then You can try with your model. Most probably this will work with any Huawei 3G modem. lets start the coding..

 First of all you have to get a brief idea about the command. AT Commands (Attention Commands) are used by a mobile application to control a wireless modem. The AT command set consists of a series of short text strings which combine together to produce complete commands for operations such as dialing, hanging up, and changing the parameters of the connection. The command set for GSM modems is specified in 3GPP specifications, TS 27.007 and TS 27.005 (for SMS-related commands). The standardized commands include some commands that are optional. Therefore, most wireless modem makers support most, but not all standardized commands. In addition, most modem makers include additional vendor-specific AT commands.

If I says in simple tums, AT commands are predefined  short  text strings for operating a 3G modem(in our case)

we can send those AT commands as string to 3G modem/dongle to do our task, that mean Sending SMS.

you can use any programming language to do that, Its simple..! but remember this is a MATLAB tutorial, so lets start the work with MATLAB. For better understanding I'll explain with screenshots and photos.

This is the working and tested MATLAB code for sending SMS with a Huawei E156G 3G modem.

%%%%%%%%%%%%%%%%Matlab code for sending SMS%%%%%%%%%%%%
%%%%%% %%%%%%%%Author :  http://simpletechtrick.blogspot.com// %%%%%%%%%%
%%%%%%%%%%%%%%Tested Model Huawei E156G%%%%%%%%%%%%%%
clc;
clear all;
global BytesAvail;
global A;
global B;
tx ='ATI';
tx1=char(13);
tx2=char(26);
tx3='AT+CMGS="+12345678901"';  % You have to replace this with the Receiver's Phone number
tx4= ' This is a test msg ';     %This is the msg body
tx5='AT+CMGF=1';

s = serial('COM4'); % You have to replace this with your 3G modem's COMport number
                             
s.baudrate=9600;
fopen(s);
s.Terminator = 'CR';

fprintf(s,'%s', tx);
fprintf(s,'%s', tx1);

BytesAvail=s.BytesAvailable;
    if(BytesAvail > 0), A=fread(s,BytesAvail,'char'); end
A;
sprintf('%c', A)

%%%%%%%%%%%%%%%Send SMS%%%%%%%%%%%%
fprintf(s,'%s', tx5);
fprintf(s,'%s', tx1);
fprintf(s,'%s', tx3);
fprintf(s,'%s', tx1);
fprintf(s,'%s', tx4);
fprintf(s,'%s', tx2);
BytesAvail=s.BytesAvailable;
    if(BytesAvail > 0), B=fread(s,BytesAvail,'char'); end
B;

fclose(s)
%%%%%%%%%%%%%%%%%%%%End%%%%%%%%%%%


Note :  You have to close Mobile Partner Software completely to work this code. (For more information see the video tutorial below)



This is the MATLAB Output.


If you have any questions, just comment below, I'll answer as soon as possible.. Have a nice day... :-)