|
|
View previous topic :: View next topic |
Author |
Message |
andreluizeng
Joined: 04 Apr 2006 Posts: 117 Location: Brasil
|
spi and interrupts problem |
Posted: Thu May 17, 2007 8:16 pm |
|
|
Hi there..
im having some problem, im working on 16f876A and using both ccp modules to an encoder application and generating pwm for motor by timer 0 interrupt.
im using spi to comunicate the slave board with main board, but.. im having problem with this comunication. im using 20mhz clock, i´ve tried to slowdown the clock from spi master, but i have no result...
any suggestion ?
thanks in advance. _________________ Andre |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 18, 2007 2:04 pm |
|
|
Quote: | I'm having problem with this comunication. |
1. Describe the problem. Is the slave missing bytes that are sent
by the master ?
2. Does the SPI slave use INT_SSP interrupts to receive the bytes
from the Master ? Are these bytes put into a circular buffer (fifo) ?
3. How much time is spent inside other interrupts (Timer0 and CCP)
in the slave program ?
4. Do you disable interrupts at any place in your slave program ?
5. Have you tried putting a delay (1 ms or more) between sending each
byte, in the Master program ? |
|
|
andreluizeng
Joined: 04 Apr 2006 Posts: 117 Location: Brasil
|
|
Posted: Sat May 19, 2007 10:27 am |
|
|
hi, PCM
Quote: | 1. Describe the problem. Is the slave missing bytes that are sent
by the master ? |
yes... Some bytes are missing or unkown bytes are add, Probably this error ocurs when timer0 interrupt takes place.
Quote: | 2. Does the SPI slave use INT_SSP interrupts to receive the bytes
from the Master ? Are these bytes put into a circular buffer (fifo) ? |
I´m not using int_ssp, I coded my own spi lib, getting bytes directly from SSPBUF, it works fine, when there are no interrupts running.
In communication is used a pin labeled as CS (right.. chip select) and when master wants to start the communication, this pin is set, and when the communication stops, this pin is cleared. i´ve designed a protocol, and when the start byte comes, the communiction just ends when the stop byte is found.
Quote: | 3. How much time is spent inside other interrupts (Timer0 and CCP)
in the slave program ? |
I´m using timer0 to produce 2 pwms for 2 servo motors and a temporization if i need (like futaba ones)
here is the code
(dont worry about the language, it is portuguese)
Code: | // Timer 0
// contagem de tempo -> time counter
// pwm para servo Esquerdo -> pwm for left servo
// pwm para servo Direito -> pwm for right servo
#int_RTCC
void RTCC_isr()
{
if (Tempo1)
{
if (tmp1 == 0)
{
FlagTempo1 = TRUE;
Tempo1 = FALSE;
}
else
{
FlagTempo1 = FALSE;
tmp1--;
}
}
// PWM for left servo motor
// PWM para o servo motor E
if (PWMServoE.PWMServo)
{
if (! PWMServoE.Contador)
{
PWMServoE.Contador = PWMServoE.Periodo;
}
else
{
PWMServoE.Contador--;
if (PWMServoE.Contador >= PWMServoE.TmpAlto)
{
SERVO_E = 0;
}
else
{
SERVO_E = 1;
}
}
}
// pwm for right servo motor
// aciona o PWM para o servo motor D
if (PWMServoD.PWMServo)
{
if (PWMServoD.Contador <= 0)
{
PWMServoD.Contador = PWMServoD.Periodo;
}
else
{
PWMServoD.Contador--;
if (PWMServoD.Contador >= PWMServoD.TmpAlto)
{
SERVO_D = 0;
}
else
{
SERVO_D = 1;
}
}
}
} |
and I am using ccps interrupt too.
Code: |
// encoder Esquerdo // left encoder
#int_CCP1
void CCP1_isr ()
{
ContadorEncoderE++;
}
// encoder Direito // right encoder
#int_CCP2
void CCP2_isr ()
{
ContadorEncoderD++;
}
|
Quote: | 4. Do you disable interrupts at any place in your slave program ? |
I cannot disable the interrupts, I need them for pwm and encoder´s read
Quote: | 5. Have you tried putting a delay (1 ms or more) between sending each
byte, in the Master program ? |
No, I have a modular setup for my robot.
the main module, has a pic16f876A which controls a RTC (ds1305), a battery charger (embedded), rs232 and SPI.
i have also 2 modules, one that controls motors and encoders, that commands are sent by the master (main board) and the other one that receive all data from main module and sent by RF to another module in PC.
well. I think that i explain the necessary. sorry if my english is a little confused.
thanks for the help.
regards. _________________ Andre |
|
|
Ttelmah Guest
|
|
Posted: Sat May 19, 2007 1:49 pm |
|
|
You have basically two choices:
Either you put a delay between each SPI byte transmission, which is as long as the worst case response to reach the handler. This would probably be when the code has just left the handler, and then all the other interrupts trigger.
The second choice, is that if you have one more output line available on the slave, and an interrupt input available on the master, have the slave pulse this line when it has accepted the byte (an acknowledge). In the master, before sending, you clear the interrupt flag, send the byte, and then poll the interrupt flag till it sets. The slave has then accepted the byte, and you can send the next.
SPI, because of it's bi-directional nature, is not double buffered on receive, so you absolutely _must_ handle each character, before another arrives.
Best Wishes |
|
|
andreluizeng
Joined: 04 Apr 2006 Posts: 117 Location: Brasil
|
|
Posted: Sun May 20, 2007 7:57 am |
|
|
Ttelmah,
how I said, I have one pin (CS) for each other modules of my robot, and I can change my function to use that pin just not for the function receive control, but for ever received control, maybe it can works.
I will try.
thanks and Im going to poster the results sooner as I can. _________________ Andre |
|
|
|
|
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
|