View previous topic :: View next topic |
Author |
Message |
bharatwalia
Joined: 04 May 2009 Posts: 35 Location: India
|
S/W hangs.... |
Posted: Sun Jul 18, 2010 2:30 am |
|
|
Hi,
Guys again I'm here to take your help for my latest project (i.e.Servo Stabilizer). This S/W is written to read the output voltage of Servo and correct the voltage to 220V(+/- 1V).
Now every things works fine except when I am sending data from my master PIC to slave PIC, the master PIC's gets hang after sometime. As soon I comment "write_spi16()", function all works perfect. I don't know whats the problem but I am not using any watch dog timer and I don't know how to and where to use WDT in my program.
The Master's PIC works perfect when there is not too many variations in the data to be send to slave PIC, but as soon as the data varies the master's PIC hangs.
The master PIC and Slave PIC both are very well protected from any kind of interference on MCLR pin.
Hereby I am posting a little piece of my master PIC code.
Please look at it.
Thanks
Code: |
#include <16f876A.h>
#device adc=10
#include <timers.h>
#USE DELAY( CLOCK=4000000 ) /* Using a 4 Mhz clock */
#FUSES XT,NOWDT,NOPROTECT,NOPUT
void led(void);
void cpuSetup(void);
void readVoltage(void);
void case1_h(void);
void case1_l(void);
void case2_h(void);
void case2_l(void);
void spi_write16(int16 data);
//void case3(void);
unsigned int32 result,setpoint=751,error_h=0,error_l=0,low_limit=0,high_limit=0;
unsigned int8 data=0;
boolean flag=0;
//----------------------------------------------------
//External interrupt routine to set flag on occcurence of "zero-crossing"
#INT_EXT
void setFlag(void)
{
flag=1;
}
//----------------------------------------------------
//----------------------------------------------------
void main()
{
gc_led_timer = LED_TIMER_TICKS;
cpuSetup();
while(1){
if(flag){
flag=0;
readVoltage();
// spi_write16(result); ----->AS SOON AS I UNCOMMENT THIS THE S/W HANGS
error_h=(result-setpoint);
error_l=(setpoint-result);
if(error_h>17){
case1_h();
}
if(error_h>7){
case2_h();
}
if(error_l>17){
case1_l();
}
if(error_l>7){
case2_l();
}
}
}
}
//----------------------------------------------------
//----------------------------------------------------
void cpuSetup(void)
{
// setup_counters(RTCC_INTERNAL,RTCC_DIV_256);
// enable_interrupts(INT_RTCC);
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_DIV_64);
set_tris_a(0xDB);
set_tris_b(0x01);
set_tris_c(0);
output_low(PIN_A2);
output_low(PIN_A5);
ext_int_edge(H_TO_L);
enable_interrupts(INT_EXT);
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4 );
enable_interrupts(GLOBAL);
delay_ms(2000);
}
//----------------------------------------------------
//----------------------------------------------------
//Read Voltage from ADC channel 0.
void readVoltage(void)
{
result=0;
set_adc_channel(0);
//delay_us(150);
result=read_adc();
}
void spi_write16(int16 data) //sending 10-bit data in two parts.
{
spi_write(make8(data, 1)); // Send MSB
delay_us(150);
spi_write(make8(data, 0)); // Send LSB
}
|
|
|
|
jbmiller
Joined: 07 Oct 2006 Posts: 73 Location: Greensville,Ontario
|
|
Posted: Sun Jul 18, 2010 6:05 am |
|
|
Your spi_write_16 function only has 3 lines and I really doubt the delay is the problem line.
You don't show the called spi_write fuction but it's probably the area to look at.
If you add LEDS/ messages to show you the steps inside the s_w-16 function you may see what is happening. |
|
|
bharatwalia
Joined: 04 May 2009 Posts: 35 Location: India
|
|
Posted: Sun Jul 18, 2010 6:23 am |
|
|
Hi,
spi_write() is the library function provided by CCS, so there is nothing to look into it.Perhaps there is a propblem in spi_write16() but dont knwo what it is. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Jul 18, 2010 6:26 am |
|
|
I don't see anything strange in the code to create hanging software.
bharatwalia wrote: | Perhaps there is a propblem in spi_write16() but dont knwo what it is. | It is posted and looks OK.
Post your compiler version number. Some versions are known to have problems.
How do you know the master is hanging and it is not the slave?
A strange thing in your design is that you are not using a Slave Select hardware line. Besides selecting the desired slave on the SPI bus this signal is also an easy method for message synchronization. Just assume your slave has missed one byte, then how is it ever to know which is the MSB and which is the LSB byte?
If you have only one slave and don't want to use a Slave Select line, then add some synchronization method to your data stream. For example have each message start with a known fixed value, a so called Start Of Message character. |
|
|
bharatwalia
Joined: 04 May 2009 Posts: 35 Location: India
|
|
Posted: Sun Jul 18, 2010 8:18 am |
|
|
Hi,
Compiler version is 4.084
And yes master is hanging because it is also doing some other task ,they also gets hanged with other operations.
i am only using one slave, i agree that it is the synchronization problem but dont know how to fix it i tried working with SS line but it didn't seems to work.
if you want i can post the complete code along with slave PIC code.
Thanks |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Sun Jul 18, 2010 10:07 am |
|
|
As pointed by ckielstra I'm agree that your SPI is running on the fly, without any synchronization
method and probably it fall in a lack of synchronism condition.
Regarding your code and this is not related with your problem I want to point
Code: |
................................
if( error_h>17)
{ case1_h();}
if( error_h>7)
{ case2_h();}
if( error_l>17)
{ case1_l();}
if( error_l>7)
{ case2_l();}
..................................
|
For example
if error_h = 18, first is called
case_1h() function and then
case2_h(),
whats the reason to call case1_h() previously?
Humberto |
|
|
bharatwalia
Joined: 04 May 2009 Posts: 35 Location: India
|
|
Posted: Sun Jul 18, 2010 12:01 pm |
|
|
yes, my mistake i forgot to comment those statements. |
|
|
bharatwalia
Joined: 04 May 2009 Posts: 35 Location: India
|
|
Posted: Sun Jul 18, 2010 11:34 pm |
|
|
Hi,
I figured out the problem. I implemented the watchdog timer and it works great.
Thanks. |
|
|
|