|
|
View previous topic :: View next topic |
Author |
Message |
faffou
Joined: 12 May 2009 Posts: 2
|
command of a DC motor |
Posted: Sun May 17, 2009 5:44 am |
|
|
Hi,
I want to send an angle using RS232 to my motor to attempt a specific position. But my motor still stopped even when I send the angle and Idon't know where is my mistake if any one can see it please help me.Thanks a lot.
Code: | #include<16F877.h>
#fuses HS,XT,NOWDT,NOPROTECT
#use delay(clock=4000000)
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#use RS232 (baud=9600, Xmit=PIN_C6, Rcv=PIN_C7) //includes the rs232 libraries
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
long teta;
float angle;
long teta1=30;
int diff;
int value=0;
void rotation (long teta)
{
input(PIN_A4);
setup_counters(RTCC_EXT_L_TO_H,WDT_18MS); //utiliser timer0 en mode compteur
set_timer0(0);
diff=teta - teta1; // comparer la position actuelle à celle désirée
if (diff>0)
output_high(PIN_B1); //rotation dans le 1er sens
if (diff<0)
output_low(PIN_B1); //rotation dans le 2ème sens
if (diff=0)
Set_pwm1_DUTY(0); //arrêt donc maintenir la positionn actuelle
do{
set_pwm1_duty(255); // moteur en marche
value=get_timer0(); //nombre d'impulsions
angle=value*3.6; // position atteinte
teta1 = ceil (angle);
}while(teta1<=diff);
write_eeprom(1,teta); //teta1=teta; //mémorisation de la position
}
#INT_RDA
void serial_interrupt ( )
{
int check;
do
{
buffer[next_in]=getc();
check = buffer[next_in];
next_in=(next_in+1) % BUFFER_SIZE;
}
while(check!=13 & check!=0);//13 = end of transmission
//00 = timeout
teta = atol(buffer);//convertir la chaine de caractère en un entier de 16 bits
rotation(teta);
next_in = 0;
}
main(){
//int duty= 220;
teta1= read_EEPROM (1);
enable_interrupts(GLOBAL); //activation de sinterruption
enable_interrupts(INT_RDA); //interrupt fires when the receive data is available, RS232 ON
setup_timer_2(T2_DIV_BY_1,255,1); //génération du PWM
setup_ccp1(CCP_PWM);
Set_pwm1_DUTY(0);
}//fin du programme
|
_________________ GOOD LUCK |
|
|
Ttelmah Guest
|
|
Posted: Sun May 17, 2009 8:06 am |
|
|
Add:
while(TRUE);
at the end of the main code.
The code is falling off the end of the main. Has nowhere to go. When this happens, you hit a hidden 'sleep' instruction put there to stop the code running through garbage, and the chip just goes to sleep. Nothing will then work....
It is a key difference between coding for the PIC, and codng on something like a PC. On the PC, there is an operating system running 'outside' your code, and if your code runs off the end, you return to this, and the processor keeps running. On the PIC, _your program_, is 'everything', and without an OS, you must therefore ensure your code keeps running....
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|