JaSoN
Joined: 05 Jul 2006 Posts: 15
|
timer 1 interrupt does not work after read by I2C master PIC |
Posted: Fri Jul 07, 2006 4:12 am |
|
|
Hi, I am programming to control a sevo motor which using software PWM generated by the interrupt of timer 1.
The PWM duty cycle can be changed or read by the master PIC through I2C communication. It works fine when duty cycle is changed by the master PIC. But, once the duty cycle has been read, the timer 1 interrupt does not work any more.
Can you tell me why do the timer 1 interrupt does not work once Master to slave I2C read is processed.
here is my code
Code: | #include <16F877>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use I2C(slave,sda=PIN_C4,scl=PIN_C3,address = 0x1E,FAST,FORCE_HW)
// I2C Bus Line Definitions
#define SCL PIN_C3
#define SDA PIN_C4
// I2C data variable
char I2C_send;
char I2C_receive[4];
int send_buffer;
int16 PWMvalue; //STEERING PWM
int THRESHOLD; //SENSOR THRESHOLD
int SET_STEERING; //STEERING STRATEGY
int TRACK; //TRACK STATUS
int READ_DATA;
int i=0;
//LOOK UP TABLE FOR ANGLE COMMAND FROM -38 TO +22 DEGREES, EACH VALUE STORED IN THE ARRAY REPRESENTS
//THE REQUIRED STEERING PWM DUTY CYCLE FOR EACH DEGREE.
int ANGLE_LOW[27]={ 170,175,180,183,185,188,190,193,195,198,
200,203,205,208,210,213,215,218,220,223,
227,230,233,237,240,243,247 };
int ANGLE_HIGH[34]={ 0,3,7,10,15,20,25,30,35,40,
45,50,55,60,64,66,72,80,85,90,
95,100,105,110,117,124,130,137,144,150
155,160,175,210 };
int16 instant_steering;
int steeringhi;
int steeringlo;
int x;
int U;
//------------------------------------------------------------------------------------------
//I2C INTERRUPT: READ/WRITE DATA FROM/TO COMMUNICATION MODULE
//------------------------------------------------------------------------------------------
#INT_SSP //I2C interrupt
void I2C_TxRx()
{
if(i2c_poll()== FALSE) // Poll I2C to check for Received Data Byte
{
if(U==0)
{
steeringhi=0;
instant_steering=PWMvalue;
while(instant_steering>=256)
{
instant_steering=instant_steering-256;
steeringhi++;
}
steeringlo=instant_steering;
}
if(U==0)
{ I2C_send=steeringhi; }
else if(U==2)
{ I2C_send=steeringlo; }
else if(U==4)
{ I2C_send=THRESHOLD; }
else if(U==6)
{ I2C_send=SET_STEERING; }
else if(U==8)
{ I2C_send=READ_DATA; }
if(U==9)
{ U=0; }
else
{ U++; }
i2c_write(I2C_send); // Transmit Data to the Master Processor
}
else // Receive I2C Data Byte and Send ACK
{ I2C_receive[i]=i2c_read(1); //Store received data
i++;
//------------------------------------------------------------------------------------------
//I2C BUS: WRITE DATA TO VARIABLES
//------------------------------------------------------------------------------------------
if (i==3)
{
switch(I2C_receive[1])
{
case 0x41: if(I2C_receive[2]!=0)
{ PWMvalue=I2C_receive[2]; } break;
case 0x4F: if(I2C_receive[2]!=0)
{ THRESHOLD=I2C_receive[2]; } break;
case 0x53: SET_STEERING=I2C_receive[2]; break;
}
//SHIFT UP PWMvalue TO SUITABLE STEERING ANGLE PWM
if(I2C_receive[1]==0x41&&I2C_receive[2]!=0)
{
if(PWMvalue<27>=THRESHOLD)
READ_DATA=READ_DATA+1;
set_adc_channel(1);
delay_us(10);
SENSOR[1] = Read_ADC();
if (SENSOR[1]>=THRESHOLD)
READ_DATA=READ_DATA+2;
set_adc_channel(2);
delay_us(10);
SENSOR[2] = Read_ADC();
if (SENSOR[2]>=THRESHOLD)
READ_DATA=READ_DATA+4;
set_adc_channel(3);
delay_us(10);
SENSOR[3] = Read_ADC();
if (SENSOR[3]>=THRESHOLD)
READ_DATA=READ_DATA+8;
set_adc_channel(4);
delay_us(10);
SENSOR[4] = Read_ADC();
if (SENSOR[4]>=THRESHOLD)
READ_DATA=READ_DATA+16;
//------------------------------------------------------------------------------------------
//STEERING STRATEGY: FAST STEERING
//------------------------------------------------------------------------------------------
if(SET_STEERING==2)
{
if (READ_DATA==0) //CASE FOR TOTALLY OFF-TRACK
{
if(STORED_DATA==0)
{
if(STORED_DATA2==8||STORED_DATA2>=16)
{ STEERING=750; }
if(STORED_DATA2<3>0)
{ STEERING=380; }
if(STORED_DATA2==0)
{ STEERING=STORED_STEERING; }
}
if(STORED_DATA2==8||STORED_DATA2>=16)
{ STEERING=650; }
if(STORED_DATA2<3>0)
{ STEERING=450; }
}
if (READ_DATA>0)
{
switch(READ_DATA)
{
case 16: STEERING=600; break;
case 24: STEERING=570; break;
case 8: STEERING=550; break;
case 2: STEERING=540; break;
case 3: STEERING=530; break;
case 1: STEERING=500; break;
default: STEERING=550; break;
}
}
}
//------------------------------------------------------------------------------------------
//STEERING STRATEGY: FAST STEERING
//------------------------------------------------------------------------------------------
if(SET_STEERING==3)
{
if (READ_DATA==0) //CASE FOR TOTALLY OFF-TRACK
{
if(STORED_DATA==0)
{
if(STORED_DATA2==8||STORED_DATA2>=16)
{ STEERING=750; }
if(STORED_DATA2<3>0)
{ STEERING=380; }
if(STORED_DATA2==0)
{ STEERING=STORED_STEERING; }
}
if(STORED_DATA2==8||STORED_DATA2>=16)
{ STEERING=650; }
if(STORED_DATA2<3>0)
{ STEERING=450; }
}
if (READ_DATA>0)
{
switch(READ_DATA)
{
case 16: STEERING=580; break;
case 24: STEERING=570; break;
case 8: STEERING=550; break;
case 2: STEERING=540; break;
case 3: STEERING=530; break;
case 1: STEERING=520; break;
default: STEERING=550; break;
}
}
}
//------------------------------------------------------------------------------------------
//STEERING STRATEGY: NORMAL STEERING
//------------------------------------------------------------------------------------------
if(SET_STEERING==1)
{
if (READ_DATA==0) //CASE FOR TOTALLY OFF-TRACK
{
if(STORED_DATA==8||STORED_DATA>=16)
{ STEERING=750; }
if(STORED_DATA<3>0)
{ STEERING=380; }
}
if (READ_DATA>0)
{
switch(READ_DATA)
{
case 16: STEERING=670; break;
case 24: STEERING=630; break;
case 8: STEERING=600; break;
case 2: STEERING=520; break;
case 3: STEERING=490; break;
case 1: STEERING=450; break;
default: STEERING=560; break;
}
}
}
//------------------------------------------------------------------------------------------
//OUTPUT STEERING PWM VALUE
//------------------------------------------------------------------------------------------
if(SET_STEERING!=0)
{ PWMvalue=STEERING; }
STORED_DATA2=STORED_DATA;
STORED_DATA=READ_DATA;
output_b(READ_DATA);
READ_DATA=0;
STORED_STEERING=STEERING;
}
} |
|
|