CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Stepper motor control with using timer1 and rda interrupts

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
isoment01



Joined: 20 May 2017
Posts: 7
Location: turkey

View user's profile Send private message

Stepper motor control with using timer1 and rda interrupts
PostPosted: Wed Jun 21, 2017 4:49 am     Reply with quote

Hello everyone, this is my first topic here. Let me introduce what will i try to do. I want to control stepper motor by using serial comm. interface rs232. Firstly I set a Timer1 interrupt to produce pulses that works to the my stepper motor but i want to start and stop the motor pulses by sending character via rs232 from pc. I have problem at rda interrupt. I don't know how to call Timer1 interrupt when the program is in rda interrupt. Actually maybe there is no need to call Timer1, maybe there should be another way to do this. I am waiting your advice at this point. I send my program plz help me guys ! Smile
Code:
#device PIC16F877A
#include <16f877a.h>
#fuses xt,nowdt,noprotect, nobrownout, nolvp, noput, nowrt, nocpd
#use delay (clock=4000000)
#use fast_io(b)

unsigned int16 sayac = 0, hedef_ileri = 0, hedef_geri=0, yon=1;
char klavye=0;

#use rs232 (baud=9600, xmit=pin_C6, rcv=pin_C7, parity=N, stop=1, Bits=8,BRGH1OK)

#int_rda
#int_timer1
#priority rda, timer1
void serihaberlesme_kesmesi ()
{
 disable_interrupts(int_rda); // int_rda kesmesini pasif yap
 output_high(pin_b0); // RC5 çıkışı lojik-1
 klavye=getchar();// String ifadeyi al ve "klavye" adlı dizi değişkenine aktar.
 putc(klavye);
 printf("\n\rYazdiginiz harf> %s\n",klavye); // Satır atla, yeni satır,RS232 üzerinden klavye string ifadesini gönder,satır atla
 printf("\f%s",klavye); //LCD'yi temizle ve klavye string ifadesini LCD'de göster
 output_low(pin_b0); // RC5 çıkışı lojik-0
 printf("\n\rKlavyeden bir metin giriniz ve enter tusuna basiniz>"); // Satır atla, yeni satır,RS232 üzerinden belirtilen metni gönder
}

void timer1_interrupts()
{
set_timer1(64923);

sayac++;

if(sayac==hedef_ileri)
  {
   yon=2;
   output_low(pin_c1);// motor geri gidiyor
  }

if(sayac<3200 || sayac>4000)
  {
   output_high(pin_c2);
   delay_us(20);
   output_low(pin_c2);
  }

if(sayac==hedef_geri)
  {
   sayac=0;
   yon=1;
   output_high(pin_c1); // motor yönü belirlendi
   //disable_interrupts(int_timer1); // motor calısıyor fakat pulse uretmıyor
  }
}

void main()
{
setup_psp(PSP_DISABLED);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);      //262 ms overflow      //65,5 ms overflow
setup_timer_2 (T2_DISABLED,0,1);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);

set_tris_b(0x00);
output_b(0x00);
set_tris_c(0x00);
output_c(0x00);

enable_interrupts(GLOBAL);
enable_interrupts(INT_timer1);
enable_interrupts(int_rda);

printf("\n\r System initializing....\n\r");
output_high(pin_c3); // motor aktif
delay_ms(100);
output_high(pin_c1); // motor yönü belirlendi
yon=1; //ileri yon
delay_us(50);
hedef_ileri=3200; //400*8
hedef_geri=6400;
sayac=0;
set_timer1(64923);

while(TRUE)
  {
   enable_interrupts(INT_timer1);
   enable_interrupts(int_rda);  //TODO: User Code
  }

}





 

_________________
hello everyone !
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

Re: Stepper motor control with using timer1 and rda interrup
PostPosted: Wed Jun 21, 2017 6:54 am     Reply with quote

isoment01 wrote:
I don't know how to call Timer1 interrupt when the program is in rda interrupt.


Simple answer, You don't.

Spend as little time as possible in the interrupts (i.e. set flags etc then get out).
Do everything else in main.
Has been discussed on this forum countless times, do a search.

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 21, 2017 6:57 am     Reply with quote

The location of your interrupt directives is wrong:
Quote:

#int_rda
#int_timer1
#priority rda, timer1

void serihaberlesme_kesmesi ()
{
klavye=getchar();
.
.
.
}

void timer1_interrupts()
{
set_timer1(64923);
.
.
.
}

Each interrupt directive should be placed immediately above its own
interrupt routine. Do it as shown in bold below:
Quote:

#priority rda, timer1

#int_rda
void serihaberlesme_kesmesi ()
{
klavye=getchar();
printf("\n\rYazdiginiz harf> %s\n",klavye);
.
.
.
}

#int_timer1
void timer1_interrupts()
{
set_timer1(64923);
.
.
.
}



You declared 'klayve' as a 'char', but then you are telling printf that it is
a string when you use "%s". This won't work. If you want to print
a char, then you must use "%c" with printf:
Quote:

char klavye=0;

void serihaberlesme_kesmesi ()
{
disable_interrupts(int_rda);
output_high(pin_b0);
klavye=getchar();
putc(klavye);
printf("\n\rYazdiginiz harf> %s\n",klavye);
printf("\f%s",klavye);
output_low(pin_b0);
printf("\n\rKlavyeden bir metin giriniz ve enter tusuna basiniz>");
}

Also, putting printf() statements into the #int_rda routine is not
considered to be a good programming method. For example, one of
your printf statements is printing at least 20 characters. Each character
takes 1 ms to transmit. So that's 20 ms. During that time, you may
get more characters coming into the UART. But, you will miss them
and may get an "overrun" error in the UART hardware. It is best to
not put printf statements inside the #int_rda routine.
isoment01



Joined: 20 May 2017
Posts: 7
Location: turkey

View user's profile Send private message

PostPosted: Wed Jun 21, 2017 7:13 am     Reply with quote

PCM programmer wrote:
The location of your interrupt directives is wrong:
Quote:

#int_rda
#int_timer1
#priority rda, timer1

void serihaberlesme_kesmesi ()
{
klavye=getchar();
.
.
.
}

void timer1_interrupts()
{
set_timer1(64923);
.
.
.
}

Each interrupt directive should be placed immediately above its own
interrupt routine. Do it as shown in bold below:
Quote:

#priority rda, timer1

#int_rda
void serihaberlesme_kesmesi ()
{
klavye=getchar();
printf("\n\rYazdiginiz harf> %s\n",klavye);
.
.
.
}

#int_timer1
void timer1_interrupts()
{
set_timer1(64923);
.
.
.
}



You declared 'klayve' as a 'char', but then you are telling printf that it is
a string when you use "%s". This won't work. If you want to print
a char, then you must use "%c" with printf:
Quote:

char klavye=0;

void serihaberlesme_kesmesi ()
{
disable_interrupts(int_rda);
output_high(pin_b0);
klavye=getchar();
putc(klavye);
printf("\n\rYazdiginiz harf> %s\n",klavye);
printf("\f%s",klavye);
output_low(pin_b0);
printf("\n\rKlavyeden bir metin giriniz ve enter tusuna basiniz>");
}

Also, putting printf() statements into the #int_rda routine is not
considered to be a good programming method. For example, one of
your printf statements is printing at least 20 characters. Each character
takes 1 ms to transmit. So that's 20 ms. During that time, you may
get more characters coming into the UART. But, you will miss them
and may get an "overrun" error in the UART hardware. It is best to
not put printf statements inside the #int_rda routine.

Sir thank you for your feedback. Actually i noticed after i posted it Smile I fixed those and there is also one thing: I used these (outputs, printf) because i want to check if rda works or not. I try to handle these problems in main loop. But how? Smile
_________________
hello everyone !
isoment01



Joined: 20 May 2017
Posts: 7
Location: turkey

View user's profile Send private message

Re: Stepper motor control with using timer1 and rda interrup
PostPosted: Wed Jun 21, 2017 7:15 am     Reply with quote

Mike Walne wrote:
isoment01 wrote:
I don't know how to call Timer1 interrupt when the program is in rda interrupt.


Simple answer, You don't.

Spend as little time as possible in the interrupts (i.e. set flags etc then get out).
Do everything else in main.
Has been discussed on this forum countless times, do a search.

Mike

ok sir I'll search now.
_________________
hello everyone !
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 21, 2017 8:20 am     Reply with quote

isoment01 wrote:

I want to check if rda works or not.

If you want a simple test to see if #int_rda is working, then make a
keyboard echo test as shown below. Make sure you turn off "local echo"
in the setup options for your terminal program. Then the program
below will receive a typed character and immediately send it back to
your PC, and you can see it in the terminal window.
Code:
#include <16F877A.h>
#fuses XT, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

#int_rda
void rda_isr(void)
{
char c;

c = getc();
putc(c);
}

//==============================
void main()
{

enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

while(TRUE);   
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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