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 CCS Technical Support

I can't get the interrupts to work
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Matro
Guest







PostPosted: Fri Mar 21, 2008 1:36 pm     Reply with quote

set_tris_b(0x0F) means RB7 to RB4 are outputs and RB3 to RB0 are outputs.
Is this really the case in your design?

Just clean your code and post it again. ;-)
And the schematic could help.

Matro
Ttelmah
Guest







PostPosted: Fri Mar 21, 2008 3:14 pm     Reply with quote

Well, if it is, then it'd explain it not working!.....
The RB change interrupt, only works on RB4 to RB7, which _must_ be set as inputs to be significant.
So the code needs:
set_tris_b(0xF0);

To work....

Best Wishes
Guest








PostPosted: Sat Mar 22, 2008 4:47 am     Reply with quote

thanks for the replies. sorry for taking so long to post back but I didn't realise that there was a second page Embarassed

At the moment I've got two versions of the program. the one I'm going to post doesn't have the motor control. I'll post the other one later because it's a mess at the moment.
I've got one encoder connected to ports b4, b5, b6 and b7. the encoder (b4 and b6 are connected to eachover so the always read the same value and the same happens with b5 and b7. the reason I'm doing that is because on monday I'll have a second encoder which will connect to ports b6 and b7 while the current encoder will only connect to ports b4 and b5, but since I'm not at my uni at the moment, I can't get my hands on the second encoder. the reason why is because I need to control simultaneaously two motors) here's the code at the moment. I've translated the comments to english.

Code:


#include <16F876A.h>
#device ICD=TRUE
#device adc=8

#use fast_io(b) //_inputs=PIN_b4, PIN_b5, PIN_b6, PIN_b7)

#use delay(clock=20000000)
#fuses NOWDT,HS, NOPUT, NOPROTECT, DEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT

//to use serial port
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

//to start the timer
#define high_start 1
 
 CHAR selection;

// Global vars

#byte Portb = 6
int16  counter_A=0;   //counts impulses from the encoder


unsigned long  velang=0; //angular velocity of the encoder
unsigned long  velang2=0;//rounded angular velocity of the encoder not needed in final version
//used to compare current value from the encoders with previous value
int value, valueanterior;

byte segundos, high_count;

//needed for rounding function
#include <math.h>

VOID Le_ADC1()                         // Função A
{

   putc(counter_A);//sends to serial port the char corresponding to the number of impulses read from the encoder. not needed in final version. just for tests
}

VOID Le_ADC2()                         // Função B
{

   putc(velang2); //sends to the serial port the char corresponding to the rounded value of the angular velocity. not needed in the final version.

}


void le_portb ()                       // Função C
{
   value=input_b()&0x0f;               //reads the current state of the encoders
   printf("%d", value);
 

}


void escreve_portb()                   // Função H
{

//not really sure what this is for, ask mates at uni
   value=getc();
   if (value < 0x10 )
      output_b(swap(value)&0xf0);


}

#int_rda
void serial_isr()
{



//MY VERSION. READS FROM SERIAL AND CHOOSES THE RIGHT FUNCTION

selection=getc();//reads serial port


         SWITCH (selection)
         {
         case 'A' : prINTf ("\r\nA");// Leitura entrada Analogica 1
         le_ADC1 ();
         BREAK;
         case 'B' : prINTf ("\r\nB");// Leitura entrada Analogica 2
         le_ADC2 ();
         BREAK;
        case 'C' : prINTf ("\r\nC");// Leitura entrada Digital
         le_portb ();
         BREAK;
        case 'H' : prINTf ("\r\nH");// Escrita saida Digital
         escreve_portb();
         counter_A=0;
         counter_B=0;

        BREAK;
        }
}
//timer code. every 1/76 of a second, calculate the angular velocity
#INT_RTCC
void clock_isr()
{
//timer interrupt, subtracts 1 from high_count
high_count -=1;

//checks if high_count is 0. if it is 0, then calculates the angular velocity
 if (high_count == 0)
 {
//++segundos;

high_count=high_start; //restarts the timer

velang = ((counter_A*3.141592*38/200); //calculates the ang velocity

//rounds down the value of the angular velocity so that there is a corresponding char to send. just for tests, not in final version
velang2=floor(velang);


counter_A=0; //restarts the counter

 }
}


#int_RB
void RB_isr(void)      //Interrupt handler for line change in port B
{
int8 c;



//checks if the value read from the encoder has changed or not
if (value != valueanterior)
{
//increments the counter
++counter_A;

}


 valueanterior=value;


//this should work without the next three lines but doesn't
set_tris_b (0x0f);
port_b_pullups(TRUE);
delay_us(10);
c=Portb;


}



VOID main()
{
char c;

//defines the ports
set_tris_b (0x0f);
port_b_pullups(TRUE);
delay_us(10);
c=Portb;



//timer setup
high_count = high_start;
setup_timer_0( RTCC_INTERNAL | RTCC_DIV_256 );
set_timer0(0);
enable_interrupts(INT_RTCC);

counter_A=0;
enable_interrupts(INT_RB);     
enable_interrupts(GLOBAL);
enable_interrupts(int_rda);

//Setup Tmr2
 setup_timer_2 (T2_DIV_BY_4, 255, 1);

SET_TRIS_C (0xF0);

//USB/Bluetooth SELECtion
output_bit( PIN_C4, 0);      //Inicializa Reset
output_bit( PIN_C4, 1);      //Inicio do impulso
Delay_ms (10);               //Duração do impulso
output_bit( PIN_C4, 0);      //Fim do Reset

WHILE (TRUE)      //endless loop
      {
//reads portb
   value=input_b()&0x0f;
      }
}

Matro
Guest







PostPosted: Sat Mar 22, 2008 6:23 am     Reply with quote

Read again the beginnig of this page.
Your tris is inverted.
I see there is a mistake in my previous post so I correct it :
set_tris_b(0x0F) means RB7 to RB4 are outputs and RB3 to RB0 are INputs.

Just put set_tris_b(0xF0) instead of the current one. ;-)

Matro
Guest








PostPosted: Sat Mar 22, 2008 6:42 am     Reply with quote

Quote:
Read again the beginnig of this page.
Your tris is inverted.
I see there is a mistake in my previous post so I correct it :
set_tris_b(0x0F) means RB7 to RB4 are outputs and RB3 to RB0 are INputs.

Just put set_tris_b(0xF0) instead of the current one. ;-)

Matro


Morning Matro.
I've just tried you sugestion and the PIC can no longer read the encoder. :( I've been reading the datasheet and I think it's connected to ports 0 to 3, although I had been told it was 4 to 7 by the guy that did the wireing.
Matro
Guest







PostPosted: Sat Mar 22, 2008 7:02 am     Reply with quote

But INT_RB can only be used with pins from RB7 to RB4.
RB3 to RB0 are not connected to the port B interrupt.
If you can post a simple schematic of your board it could be easier. ;-)

Matro.
ej



Joined: 19 Mar 2008
Posts: 3

View user's profile Send private message

PostPosted: Sat Mar 22, 2008 8:51 am     Reply with quote

Here's a very simple drawing of the board. I don't know much about it because I didn't buy it or anything. It was given to me at my uni for this project but no extra info or documentation was given.

[img=http://img108.imageshack.us/img108/1556/schematicpn6.th.jpg]
Matro
Guest







PostPosted: Sat Mar 22, 2008 9:05 am     Reply with quote

I don't know if we can really call it a schematic... :-)
Just a simple question : On which pins of the PIC are the encoders connected?

Matro
Ttelmah
Guest







PostPosted: Sat Mar 22, 2008 10:01 am     Reply with quote

You need to move the encoder to the right hand four pins on the diagram. If it works at all on the left hand four pins, it is just because the other signals are floating, and triggering often enough to give some operation.

Best Wishes
ej



Joined: 19 Mar 2008
Posts: 3

View user's profile Send private message

PostPosted: Sat Mar 22, 2008 10:02 am     Reply with quote

according to the shematic in the datasheet, they are connected to ports 20 to 24, which are ports RB0 to RB3/PGM.
I didn't know that it only worked on the other pins Embarassed. though it used to seem to work fine. at least when I turned the encoder 1 time I recieved aproximatly the right amount of impulses.
ej



Joined: 19 Mar 2008
Posts: 3

View user's profile Send private message

PostPosted: Sat Mar 22, 2008 10:34 am     Reply with quote

I've seems to be working great now Smile. I deleted the value=input_b()&0x0f; command that was inside the while true. it seemed useless now the interrupts are working properly Smile Now all I've got to do is to get the motor control working properly.
thanks for all the help Smile
Guest








PostPosted: Fri Mar 28, 2008 8:23 am     Reply with quote

hey everybody. I've got another question. I now am able to read the encoders, and use a PID controller to speed up(or slow down) the motors to a certan angular velocity. the problem I'm having now is that the serial port comunication stops working after a while. my serial port comunication is the following.

Quote:


#int_rda
void serial_isr()
{

selection=getc();


SWITCH (selection)
{
case 'A' : prINTf ("\r\nA");// Leitura entrada Analogica 1
le_ADC1 ();
BREAK;
case 'B' : prINTf ("\r\nB");// Leitura entrada Analogica 2
le_ADC2 ();
BREAK;
case 'C' : prINTf ("\r\nC");// Leitura entrada Digital
le_portb ();
BREAK;
case 'D' : prINTf ("\r\nD");// Escrita PWM2 e Frente.
// output_bit( PIN_C0, 0);
escreve_motor1 ();
BREAK;
case 'E' : prINTf ("\r\nE");// Escrita PWM2 e Traz.

escreve_motor1 ();
BREAK;
case 'F' : prINTf ("\r\nF");// Escrita PWM1 e Frente.

escreve_motor2 ();
BREAK;
case 'G' : prINTf ("\r\nG");// Escrita PWM1 e Traz.

escreve_motor2 ();
BREAK;
case 'H' : prINTf ("\r\nH");// Escrita saida Digital
escreve_portb();
BREAK;

}
}


to generate the interrupts I used the following commands in the main

Quote:


enable_interrupts(int_rda);



thanks in advance
Matro
Guest







PostPosted: Fri Mar 28, 2008 8:49 am     Reply with quote

It seems that "selection" variable is a global one. Let it as it is.
In your ISR, just store the character in this variable and exit.
Then in your main (I guess it's an endless loop) insert the code that is currently in the ISR with an "if" statement.
Code:

int8 selection

#int_rda
void serial_isr()
{
  if(selection == 0x00)
  {
    selection=getc();
  }
  else
  {
     printf("Not yet ready : %c",getc());
  }
}

void main()
{
  selection = 0x00; //init
  enable_interrupts(int_rda);
  enable_interrupts(global);
  while(1)
  {
     if(selection != 0x00) //if a character has been received
     printf("\n\r%c",selection); //echo
     {
        //Here your "switch" statement
     }
     selection = 0x00; //reset
  }
}


Matro.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Mar 28, 2008 9:05 am     Reply with quote

As a general rule: never use printf in the serial receive interrupt handler!

Explanation: The hardware UART can only buffer up to three characters. With sending and receiving data occurring at the same baudrate you will get a receive buffer overflow when sending 3 or more characters. Depending on the rest of your program it is possible to get a receive buffer overflow when sending fewer characters (other interrupts, etc.).

Exception: printf can be used when sending to another, much faster, stream. For example to an LCD.

The given code of Matro is an improvement and shows how you can use flags to work around the printf limitation. Too bad Matro added a new huge printf to the interrupt routine, this should have been solved by a flag as well.
Guest








PostPosted: Fri Mar 28, 2008 9:07 am     Reply with quote

It didn't work :( matro. the program is still not working :S

here's the entire code that I have at the moment. It might help to understand the problem.



Quote:

#include <16F876A.h>
#device ICD=TRUE
#device adc=8
#use fast_io(b) //_inputs=PIN_b4, PIN_b5, PIN_b6, PIN_b7)
#use delay(clock=20000000)
#fuses NOWDT,HS, NOPUT, NOPROTECT, DEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
//para temporizador
#define high_start 1


// Global vars

#byte Portb = 6
signed long erroRodaAnt=0;
signed long erroRodaAnt2=0;
signed long errototalRoda2=0;
signed long errototalRoda=0;
signed long DerivErro2=0;
signed long DerivErro=0;
unsigned long counter_A=0; //Contador dos impulsos encoder_A
unsigned long counter_B=0; //Contador dos impulsos encoder_B
unsigned long counter_C=0; //Contador dos impulsos encoder_A
unsigned long counter_D=0; //Contador dos impulsos encoder_B
unsigned long velang=0;
unsigned long velang2=0;
unsigned long Kp=15;
unsigned long Td=0.15;
unsigned long TI=1.5;
unsigned long Kp2=15;
unsigned long Td2=0.15;
unsigned long TI2=1.5;
int VelRoda1=15;
int VelRoda2=15;
int8 selection;
signed long velangEsq=0;
signed long velang2Esq=0;
signed long erroRoda2=0;
signed long voltagemDeSaidaRoda2;
signed long erroRoda1=0;
signed long erroRoda=0;
signed long erroRodaAnt1=0;
signed long voltagemDeSaida;
int value, valueanterior, port, type, valueRoda2, valueanteriorRoda2, valor;
byte high_count;
#include <math.h>



VOID Le_ADC1() // Função A
{
set_adc_channel (0);
DELAY_us (50);
value = read_adc ();
// putc (value);

putc(velangEsq);
}

VOID Le_ADC2() // Função B
{
set_adc_channel (1);
DELAY_us (50);
value = read_adc ();
//putc (value);
//putc(counter_B);
putc(velang);

}


void le_portb () // Função C
{
value=input_b()&0xf0; // Lé parte baixa do portb
printf("%d", value);
// putc (value); // Escreve no RS232

}

void escreve_motor1 () // Função D,E
{

VelRoda1 = getc();

// set_pwm2_duty(VelRoda1); // Actualiza duty ns saida DO PWM2

}

void escreve_motor2 () // Função F,G
{

VelRoda2 = getc();

//set_pwm1_duty(VelRoda2); // Actualiza duty ns saida DO PWM1

}

void escreve_portb() // Função H
{

value=getc();
if (value < 0x10 )
output_b(swap(value)&0xf0);


}

#int_rda
void serial_isr()
{


if(selection == 0x00)
{
selection=getc();
}
else
{
printf("Not yet ready : %c",getc());
}
}
//codigo para o temporizador
#INT_RTCC
void clock_isr()
{

high_count -=1;
if (high_count == 0)
{


velang = ((counter_A*3.141592*38/200)+(counter_B*3.141592*38));
velang2=floor(velang);
erroRoda1 = (VelRoda1-velang);
DerivErro=(erroRoda1-erroRodaAnt1)*76;
errototalRoda= errototalRoda+erroRoda1*(1/76);
voltagemDeSaida=51*Kp*(erroRoda1+Td*DerivErro+TI*errototalRoda);

if(voltagemDeSaida<30)
{
voltagemDeSaida=30;
}
//if(voltagemDeSaida>255)
//{
//voltagemDeSaida=255;
//}

output_bit( PIN_C0, 0);
set_pwm1_duty(voltagemDeSaida);
counter_A=0;
counter_B=0;
erroRodaAnt=erroRoda;


velangEsq = ((counter_C*3.141592*38/200)+(counter_D*3.141592*38));
velang2Esq=floor(velangEsq);
erroRoda2 = (VelRoda2-velangEsq);
DerivErro2=(erroRoda2-erroRodaAnt2)*76;
errototalRoda2= errototalRoda2+erroRoda2*(1/76);
voltagemDeSaidaRoda2=51*(Kp2*(erroRoda2+Td2*DerivErro2+TI2*errototalRoda2));

if(voltagemDeSaidaRoda2<30)
{
voltagemDeSaidaRoda2=30;
}
//if(voltagemDeSaidaRoda2>255)
//{
//voltagemDeSaidaRoda2=255;
//}

output_bit( PIN_C3, 0);
set_pwm2_duty(voltagemDeSaidaRoda2);
counter_C=0;
counter_D=0;
erroRodaAnt2=erroRoda2;
high_count=high_start; //reinicializa o temporizador
}
}


#int_RB
void RB_isr(void) //Interrupt handler for line change in port B
{
int8 c;

// ler primeiro encoder. primeiras 2 portasB
valor=input_b()&0xC0;
if (valor != valueanterior)
{
++counter_A;
if (counter_A>200)
{
counter_A=0;
++counter_B;
}
}

valueanterior=valor;


//ler segundo encoder terceira e quarta portaB
valueRoda2=input_b()&0x30;
if (valueRoda2 != valueanteriorRoda2)
{
++counter_C;
if (counter_C>200)
{
counter_C=0;
++counter_D;
}
}
valueanteriorRoda2=valueRoda2;

c=Portb;
}


VOID main()
{
selection = 0x00; //init
setup_adc_ports (AN0_AN1_AN3);
setup_adc (ADC_CLOCK_INTERNAL);

setup_timer_2 (T2_DIV_BY_4, 255, 1);


setup_ccp1 (CCP_PWM);
setup_ccp2 (CCP_PWM);


SET_TRIS_B (0x0F);
SET_TRIS_C (0xF0);
port = 0;
value = 0;
type = 0;

output_bit( PIN_C3, 0);

set_pwm2_duty (150); //Inicializa a 0 os motores 1 e 2

output_bit( PIN_C0, 0);
set_pwm1_duty (150);



set_tris_b (0xF0);

port_b_pullups(TRUE);
delay_us(10);




//codigo para o temporizador adicionado agora

high_count = high_start;
setup_timer_0( RTCC_INTERNAL | RTCC_DIV_256 );
set_timer0(0);
enable_interrupts(INT_RTCC);





enable_interrupts(INT_RB); // turn on port b interrupt on change
enable_interrupts(int_rda);
enable_interrupts(GLOBAL);



//Setup Tmr0
// setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_1);

//Setup Tmr2
setup_timer_2 (T2_DIV_BY_4, 255, 1);

//SET_TRIS_C (0xF0);

//USB/Bluetooth SELECCIONADA
//output_bit( PIN_C4, 0); //Inicializa Reset
//output_bit( PIN_C4, 1); //Inicio do impulso
//Delay_ms (10); //Duração do impulso
//output_bit( PIN_C4, 0); //Fim do Reset

WHILE (TRUE) //Loop sem fim
{

if(selection != 0x00) //if a character has been received
printf("\n\r%c",selection); //echo
{
//Here your "switch" statement

SWITCH (selection)
{
case 'A' : prINTf ("\r\nA");// Leitura entrada Analogica 1
le_ADC1 ();
BREAK;
case 'B' : prINTf ("\r\nB");// Leitura entrada Analogica 2
le_ADC2 ();
BREAK;
case 'C' : prINTf ("\r\nC");// Leitura entrada Digital
le_portb ();
BREAK;
case 'D' : prINTf ("\r\nD");// Escrita PWM2 e Frente.
// output_bit( PIN_C0, 0);
escreve_motor1 ();
BREAK;
case 'E' : prINTf ("\r\nE");// Escrita PWM2 e Traz.
// output_bit( PIN_C0, 1);
escreve_motor1 ();
BREAK;
case 'F' : prINTf ("\r\nF");// Escrita PWM1 e Frente.
// output_bit( PIN_C3, 0);
escreve_motor2 ();
BREAK;
case 'G' : prINTf ("\r\nG");// Escrita PWM1 e Traz.
// output_bit( PIN_C3, 1);
escreve_motor2 ();
BREAK;
case 'H' : prINTf ("\r\nH");// Escrita saida Digital
escreve_portb();
BREAK;

}

}
selection = 0x00; //reset

}
}


As I said before, I'm trying to control simultaneously two motors. through the serial port I'll send 2 reference velocity (preceded by a letter which indicates what motor and in what direction it´s supposed to work in.). the PID controller will then feed voltage to the motores until they stabalise at the correct speed.
I'm also trying to read information from two infrared sensors and send that data to the computer.
the problem that I'm getting is that I can send information for a few seconds but after that, the PIC keeps the last value and seems to stop listening to the information from the serial port.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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