Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Controlling PWM module through serial port

Status
Not open for further replies.

ravimd

Newbie level 4
Joined
Mar 2, 2012
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,345
Hi all,

I am doing an exciting project and now I really need a help. I am trying to control the Pulse width and the Period of a PWM (PIC24FJ128GA010) module through serial port.
Here I am having the following configuration

Explorer 16 Development Board- PC.
PC is connected through serial port of micro-controller. I have a python interface in the computer. This python interface can take input from user and can send data through serial port to PIC and it can read back from micro-controller.

So i need to control the pulse width and period of PWM by giving inputs through python. I am giving the pulse width in nano seconds and period in microseconds. I have a minimum pulse width from micro-controller as 12.5ns while am giving oxooo1 in to the OC1R and OC1RS registers. So my pulse width is a function of 62.5ns (Clock frequency of micro-controller is 32MHz using the PLL). So my equation for the PWM pulse width is
OC1RS value= raound off(Input value/62.5)
I am sending this value through the serial port as string

Python program given below

import serial
import sys
import struct
import os

ser = serial.Serial(
port='COM19',
baudrate=115200,timeout=1,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS)

ser.close()
ser.open()
###########################################################################
data=input("Enter pulse width:")
pulse_width=data/62.5
pulse_width1=int(pulse_width)
send_string=str(pulse_width1)
print "Sending Data:"
print send_string
ser.write(send_string)
ser.write('A')
data=ser.read(4)
print "data received:\n"
print data
##########################################################################
data=input("Enter PWM Period:")
period=data/0.0625
period_intiger=int(period)
send_string=str(period_intiger)
print "Sending Data:"
print period_intiger
ser.write(period_intiger)
ser.write('A')
data=ser.read(4)
print "data Received:\n"
print data
##########################################################################


Here I will make the data from the equation and make it as string and send through serial port. For terminating the data waiting I sens a character 'A' as termination character.

Now move to the function written to receive the data through serial port

***********************Pulse width **********************************

#include<p24FJ128GA010.h>

#include<stdlib.h>

unsigned int Receive_pulse_width(void)

{
int loop_control=1;
unsigned char data[5];
unsigned char final_string_1[5];
int j,data_rt_2,count=0;


while(loop_control!=0)
{
data[j]=UART2GetChar();

UART2PutChar(data[j]);
if(data[j]=='A')
{loop_control=0;}

}
count=j-1;
for(j=0;j<=count;j++)
{
final_string_1[j]=data[j];
}

data_rt_2=atoi(final_string_1);

return(data_rt_2);

}

Here this function will receive each character by character and will save that data in to a string called data[].
To exit from this while loop if the program will compare each character and if it is 'A'
So the received character without 'A' saved in the final_string_1[].

By using the atoi function i can get the integer value from that ascii string.

This is returned from the function.

Similar logic for period of the PWM.

***********************PERIOD**************************************************

#include<p24FJ128GA010.h>
#include<stdlib.h>


unsigned int Receive_period(void)

{
int loop_control_2=1;
unsigned char data_2[5];
unsigned char final_string[5];
int i,data_rt_1,count=0;


while(loop_control_2!=0)
{
data_2=UART2GetChar();

UART2PutChar(data_2);

if(data_2=='A')
{loop_control_2=0;}

}
count=i-1;
for(i=0;i<=count;i++)
{
final_string=data_2;
}

data_rt_1=atoi(final_string);

return(data_rt_1);

}
******************************************************************************************************

But my issue is that , the data recieved is not the ocrrect and it showing that 0x0000 every time from the function.

can any one help me in this? Is there any other way to do the same thing ?

Ravi M.D.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top