View previous topic :: View next topic |
Author |
Message |
longmilk
Joined: 24 May 2010 Posts: 7
|
Please help me, please |
Posted: Mon May 24, 2010 6:04 am |
|
|
Hey all, I'm a student and a very very new in microcontroller.
But my teacher gave me a hard project: 'Control DC Motor (Servo motor) using PIC 16F877A , control and receive data from computer'.
I've written a simple program ( in Delphi) to communicate with COM1 via RS232 using Max232. But now, the big problem is, I don't know the way PIC 16F877A interact with the data it received from PC and how it transfer data to PC ( maybe via TX and RX).
Please help me a sample CCS program for this project because the deadline for my project is about 1 week. Help me please, and I promise you I'll learn hard to program PIC like all of you but now please help me pass through this misson.
Thanks you in advanced.
My motor is 24V and has an encoder 400 pulses.
Mail: nguyenlongvp@gmail.com
Last edited by longmilk on Mon May 24, 2010 9:31 pm; edited 1 time in total |
|
|
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Mon May 24, 2010 8:46 am |
|
|
Quote: | Help me please, and I promise you I'll learn hard to program PIC like all oF you but now please help me pass through this misson. | What good is it to us if you 'learn hard to program the PIC like all of us'?
Anyway, no one is going to write code for you unless you pay them. You'll be better off writing some code on your own. Post the code. People on this forum will definitely help you, but you need to make a start.
I have no idea what sort of control you're looking for. Is it simple open loop control? Or closed loop speed and position? Either way, you'll need to use PWM. Search on the forum for these terms - you'll find several useful links.
Rohit |
|
|
longmilk
Joined: 24 May 2010 Posts: 7
|
|
Posted: Mon May 24, 2010 9:33 pm |
|
|
Rohit de Sa wrote: | Quote: | Help me please, and I promise you I'll learn hard to program PIC like all oF you but now please help me pass through this misson. | What good is it to us if you 'learn hard to program the PIC like all of us'?
Anyway, no one is going to write code for you unless you pay them. You'll be better off writing some code on your own. Post the code. People on this forum will definitely help you, but you need to make a start.
I have no idea what sort of control you're looking for. Is it simple open loop control? Or closed loop speed and position? Either way, you'll need to use PWM. Search on the forum for these terms - you'll find several useful links.
Rohit |
I use PWM, i really need help. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 25, 2010 12:23 am |
|
|
We are not going to do your project, but if you post what you've got now we can help you by giving tips.
This is how I would start:
1) Create a tiny program only blinking a LED once a second. This proves you can program the hardware and it is running at the correct clock speed.
2) Use the CCS provided example program ex_sisr.c as a basis for receiving serial data.
Now comes the harder part:
- You have to write a routine to interpret the received data. Hint: make things easier to yourself by designing a protocol with a unique start character.
- Act on the received command. |
|
|
longmilk
Joined: 24 May 2010 Posts: 7
|
|
Posted: Tue May 25, 2010 7:21 am |
|
|
ckielstra wrote: | We are not going to do your project, but if you post what you've got now we can help you by giving tips.
This is how I would start:
1) Create a tiny program only blinking a LED once a second. This proves you can program the hardware and it is running at the correct clock speed.
2) Use the CCS provided example program ex_sisr.c as a basis for receiving serial data.
Now comes the harder part:
- You have to write a routine to interpret the received data. Hint: make things easier to yourself by designing a protocol with a unique start character.
- Act on the received command. |
I have:
1. MAX232 for RS232 connection
2. LMD18200 as motor Driver.
3. An encoder 400 pulses, 5V.
4. Motor 24V, 43rpm.
5. PIC16F877A.
I have this code for program to control a simple DC motor.
Someone can help me to modify it for my project???
My project requires:
Sending the rotation angle on pc to PIC
then PIC feedback the real rotation angle via motor's encoder
Code: |
#include <16f877a.h>
#fuses HS,NOPROTECT,NOWDT,NOBROWNOUT
#use delay (clock = 20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include<ctype.h>
#byte PR2 = 0x92
int16 e2;
int16 e1;
///////////////////////////////////////////////////////
int16 v_set = 350;
/////////////////////////////////////////////////////////
int16 v_cur;
int16 e_sum;
int16 e_del;
int16 flag_timer0;
unsigned long kp =2.5;
unsigned long ki = 0.0092;
unsigned long kd = 0;
////////////////////////////////////////////////////////////
int16 i = 1 ;
int16 pw_duty;
int16 counted_round_value;
int16 temp_timer0;
int16 temp_timer1;
int16 update_counted_round_value;
//#byte TMR0 = 0x01 //timer0 REGISTER
//#byte TMR1_L = 0X0E
//#byte TMR1_H = 0X0F
#define START_VALUE_TIMER0 5
#define START_VALUE_TIMER1 5535
#INT_TIMER0
void TIMER0_int()
{
set_timer0(START_VALUE_TIMER0);
i++;
if(i==60 ){
temp_timer1 = get_timer1();
set_timer1(START_VALUE_TIMER1);
i=0;
v_cur = 2*(temp_timer1 - START_VALUE_TIMER1);
flag_timer0 = 1;
}
}
#INT_TIMER1
void timer1_int(){
set_timer1(START_VALUE_TIMER1);
}
void init_timer0()
{
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_16);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_2);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER1);
}
void init_PWM(int16 frequency){
setup_ccp1(CCP_PWM); // initiate PWM
PR2 = 20000000/4/frequency - 1; // set PWM period
setup_timer_2(T2_DIV_BY_1,255,1); // initiate time 2 The cycle time will be (1/clock)*4*t2div*(period+1)
// (1/20000000)*4*1*(255+1) = 51.2 us( will over flow every 51.2 us, will intrup every 51.2 uS or 19.5 khz;
}
void cal_pid(){
long temp_kp;
long temp_ki;
long temp_kd;
e2 = v_set - v_cur;
e_sum += e2;
e_del = e2 - e1;
e1 = e2;
temp_kp = kp*e2;
temp_ki = ki*e_sum;
temp_kd = kd*e_del;
pw_duty +=temp_kp;
if (pw_duty <1000)
pw_duty += temp_ki;
if (pw_duty <1000)
pw_duty += temp_kd;
if (pw_duty <256)
pw_duty = 256;
if (pw_duty >1000)
printf(" \t%ld", v_cur);
}
void main()
{
int8 timer0_value;
init_timer0();
init_PWM(19500);
//set_pwm1_duty(500);
while(true){
if (flag_timer0 ==1){
flag_timer0 = 0;
cal_pid();
set_pwm1_duty(pw_duty);
}
}
}
|
Someone can help me to modify it for my project???
My project requires:
Sending the rotation angle on pc to PIC
then PIC feedback the real rotation angle via motor's encoder. I don't know
how to write CCS code for this |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 25, 2010 4:14 pm |
|
|
I do feel pessimistic about helping you as you have so little time left and so little knowledge. It probably is a waste of my time but I feel sorry for you.
It would help if you show you tried to put in some effort in designing your own program. The shown code is copied directly from another forum in 2006.
The original code was better as it contained some comments (in Vietnamese). Why did you delete it?
It is a difficult to understand program and contains a few obvious errors (assigning float values to integer). So why did you choose this program as a starting point?
Even if you have little knowledge of programming the PIC in C, you can describe the flowchart of your design in plain English. We can then help you to convert this flowchart into a program. |
|
|
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
Help |
Posted: Tue May 25, 2010 11:28 pm |
|
|
You could talk to your teacher and ask him for a another simpler project, just think of some simple projects that could be completed in a weeks time. Most teachers are reasonable enough.
What about other students, maybe they are also facing the same problem as you ?
Is this a weekend project or a long pending project that you neglected ??
In any case I don't think you can complete this project in a week if you have very little experience,
What compiler are you using ??
thanks
a |
|
|
longmilk
Joined: 24 May 2010 Posts: 7
|
|
Posted: Wed May 26, 2010 5:02 am |
|
|
ckielstra wrote: | I do feel pessimistic about helping you as you have so little time left and so little knowledge. It probably is a waste of my time but I feel sorry for you.
It would help if you show you tried to put in some effort in designing your own program. The shown code is copied directly from another forum in 2006.
The original code was better as it contained some comments (in Vietnamese). Why did you delete it?
It is a difficult to understand program and contains a few obvious errors (assigning float values to integer). So why did you choose this program as a starting point?
Even if you have little knowledge of programming the PIC in C, you can describe the flowchart of your design in plain English. We can then help you to convert this flowchart into a program. |
I don't choose but i find it. I have do a lot of search and find this code. So I want all of you to help me ( because you know that I have little time and lack of knowledge about CCS for PIC). But, the purpose of my topic is finding your help. I really need your help.
My Program requires:
Sending the Velocity and the Rotation Angle to PIC via RS232 and then motor's encoder will feedback the real velocity and real Angle to PIC and then to PC. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed May 26, 2010 6:52 am |
|
|
For three days now you have been posting the same cry for help but you haven't shown to do anything constructive. I suggested you several actions to start with and you have done nothing. Three precious days wasted...
I do understand you need help, but you need to understand I'm not going to do your work for you.
Most people in this forum here feel the same. We want to help you with technical problems but first you have to show us you have tried to solve the problem yourself. We want you to learn, not just give you the solution.
If you don't want to learn but just get the solution: pay someone to make the program for you.
As a side note, writing in bold is considered shouting. Use it with care, people might take offence.
I think there is not enough time left to help you solve the problem. As Arun suggested, talk to your teacher. Now he might be willing to help you somehow. Maybe he gives you more time or changes the assignment. If you talk to him next week it will be too late. |
|
|
longmilk
Joined: 24 May 2010 Posts: 7
|
|
Posted: Wed May 26, 2010 7:39 am |
|
|
First. Sorry for the Bold.
I've learnt from ex_sisr.c the basic sending and receiving between PC and PIC. I ask you for how we send data ( in bytes, char, string,..) to PIC and PIC knows this byte or that byte is for specific task??
P/S: and defined(__PCM__) : what is it??
And the good news is: I've compromised with my teacher to lengthen the deadline to end of next week. |
|
|
longmilk
Joined: 24 May 2010 Posts: 7
|
|
Posted: Wed May 26, 2010 8:32 pm |
|
|
Many thanks all of you
Last edited by longmilk on Wed May 26, 2010 9:19 pm; edited 2 times in total |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed May 26, 2010 8:35 pm |
|
|
Quote: | P/S: and defined(__PCM__) : what is it?? | CCS is selling several compilers for the different processor families. The PCB compiler for PIC10/12, PCM for the PIC16, PCH for the PIC18, etc.
With the mentioned test CCS creates a single example file that can be used for all compilers. In C it is a convention that compiler defined values always start with two '_' characters.
For your PIC 16 the code inside the __PCM__ section is important and you can forget the parts inside __PCH__ and __PCD__.
Quote: | I ask you for how we send data ( in bytes, char, string,..) to PIC and PIC knows this byte or that byte is for specific task?? | This is partly up to you. As far as the PIC is concerned you are always sending bytes, it is only in the processing of this data that you decide what type it is (byte, char, string, ...).
For this you have to create a protocol; a contract between the sending PC and receiving PIC about the data format.
Quote: | I've done a lot of google and find this code, can you explain it for me ( line by line) ??? | As with the other program: Why did you choose this one?
Many programs can be found on the internet, but if you don't know what you are looking for you will find a lot of garbage. It seems to me you are still looking for the quick and easy answer instead of doing a serious job yourself (as you should have several months ago).
I'm wasting my time here. |
|
|
longmilk
Joined: 24 May 2010 Posts: 7
|
|
Posted: Wed May 26, 2010 8:57 pm |
|
|
Code: |
#priority EXT,TIMER1,RDA : what is it?
#define break PIN_C5 // To DC servo, LMD18200
#define pwm PIN_C2
#define dir PIN_C0
|
I've known that pwm is PIN_C2 but about the break and dir PIN, may I defined them to another PIN like:
Code: |
#define break PIN_C1
#define dir PIN_B1
|
( Because I've a PCB which define C1: break and B1: dir) |
|
|
|