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

PIC18F2431 #int_RDA BUG?

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







PIC18F2431 #int_RDA BUG?
PostPosted: Sat Jan 13, 2007 2:49 am     Reply with quote

I'm beginner of PICMicros.
I've developed 128-microstepping driver board using LMD18201 + 16F873 device.
I'm about to communicate another MCU board(18f2431) between my PC and the microstepping driver board.

<<pc>> - rs232 - <<MCU>>master - i2c - slave<<motor>>

Control of the motor board is accomplished via IO pins, but a message for
the over-current/over-heating condition or other status from the motor board(i2c slave) is need to be read in another way by the i2c master host.

Using a trigger to INT0 external interrupt to the MCU board(i2c master), I think, MCU boad can read the messages-on-wait.

I guess i2c communication will have no problem.

The Problem is.....

I cannot use #int_RDA isr routine and #int_EXT isr routine simultaneousy.
Device can run with each isr routine but not work with the both isr routines.

Because 18F2431 has QEI modue I need, I hope to use it, but I cannot
run with the both interrupt isr routines.

Why?

Please let me know what problem is?

Source file as follows:

<<18201i2chost.h>>====================

#include "c:\pic\khkdriver\sfr18.h" // I made it for Resisters.

#define TRISA_VALUE 0b11111111
#define LATA_VALUE 0b00000000

#bit DCM_RUN =LATB.0
#bit DCM_BRAKE =LATB.1
#bit DCINT =LATB.2
#bit SDO2 =LATB.3
#bit STM_BRAKE =LATB.4
#bit STM_DIR =LATB.5
#bit STEP =LATB.6
#bit SDO1 =LATB.7

#define TRISB_VALUE 0b10001000
#define LATB_VALUE 0b00110010

#bit DCM_TRIG =PORTC.0
#bit DCM_ERR_REL =LATC.1
#bit STM_ERR_REL =LATC.2
#bit STM_TRIG =PORTC.3 //I2C trigger from Stepper
#bit SDA =PORTC.4
#bit SCL =PORTC.5
#bit TX =LATC.6
#bit RX =PORTC.7
#define TRISC_VALUE 0b10111001
#define LATC_VALUE 0b00000000

// Step Mode Value
#define STEP_FULL 0
#define STEP_HALF 1
#define STEP_4 2
#define STEP_8 3
#define STEP_16 4
#define STEP_32 5
#define STEP_64 6
#define STEP_128 7

// I2C Slave address definition
#define STM_ADDR 0xB0
#define DCM_ADDR 0xB2

// Define Macros
#define MASK(x) (~x)
#define TEST(x,y) (x&y)
#define SET(x,y) (x|=y)
#define CLR(x,y) (x&=MASK(y))
#define ON 1
#define OFF 0

#define PSTM_INIT 'A'
#define PSTM_SPEED 'B'
#define PSTM_LL_HOST 'C'
#define PSTM_UL_HOST 'D'
#define PSTM_LL_DEVICE 'E'
#define PSTM_UL_DEVICE 'F'
#define PSTM_CALIBRATED 'G'
#define PSTM_POWER_ON 'H'
#define PSTM_POWER_OFF 'I'
#define PSTM_CW 'J'
#define PSTM_CCW 'K'
#define PSTM_STEP 'L'
#define PSTM_RUN 'M'
#define PSTM_STOP 'N'
#define PSTM_POS 'O'
#define PSTM_OVER_CUR 'P'
#define PSTM_OVER_HEAT 'Q'
#define PSTM_STEPMODE 'R'

#define PDCM_SPEED 'a'
#define PDCM_POWER_ON 'b'
#define PDCM_POWER_OFF 'c'
#define PDCM_RUN 'd'
#define PDCM_STOP 'e'
#define PDCM_OVER_CUR 'f'
#define PDCM_OVER_HEAT 'g'

#define is_dcm_msg (uart_ch>'Z')
#define UART_FAILURE 0xFF

#define ISTM_ZERO 0x10
#define ISTM_SEND_POS 0x11
#define ISTM_STEP_MODE 0x12
#define ISTM_OVER_CUR 0x13
#define ISTM_OVER_HEAT 0x14
#define ISTM_I2C_SUCC 0x1E
#define ISTM_I2C_FAIL 0x1F

#define IDCM_SPEED 0x20
#define IDCM_OVER_CUR 0x21
#define IDCM_OVER_HEAT 0x22
#define IDCM_I2C_SUCC 0x2E
#define IDCM_I2C_FAIL 0x2F


// Structure definition
union INTVAL
{
unsigned int ui;
int i;
unsigned char ub[2];
char b[2];
};

union LONGVAL
{
float f;
unsigned long ul;
long l;
unsigned int ui[2];
int i[2];
unsigned char ub[4];
char b[4];
};

union COMM_STATUS
{
unsigned char value;
struct
{
unsigned pc_msg:1;
unsigned stm_msg:1;
unsigned dcm_msg:1;
unsigned txing:1;
unsigned txrdy:1;
unsigned fill:3;
} items;
};

union STM_STATUS
{
unsigned char value;
struct
{
unsigned init:1;
unsigned power:1;
unsigned in_step:1;
unsigned in_run:1;
unsigned dir:1;
unsigned l_lim_set:1;
unsigned u_lim_set:1;
unsigned calibrated:1;
} items;
};

union DCM_STATUS
{
unsigned char value;
struct
{
unsigned power:1;
unsigned in_run:1;
unsigned fill:6;
} items;
};


#define STM_ON STM_BRAKE=OFF,stm_stat.power=ON,delay_ms(10)
#define STM_OFF STM_BRAKE=ON, stm_stat.items.power=OFF,stm_stat.items. in_run=OFF
#define DCM_ON DCM_BRAKE=OFF, dcm_stat.items.power=ON, delay_ms(10)
#define DCM_OFF DCM_BRAKE=ON, DCM_RUN=0, dcm_stat.value=0

union COMM_STATUS comm_stat;
union STM_STATUS stm_stat;
union DCM_STATUS dcm_stat;

char stm_i2c_cnt;
char dcm_i2c_cnt;

char stm_stepmode;
char read_stepmode;
union INTVAL stm_stepcnt;
union INTVAL stm_enccnt;
union LONGVAL stm_position;
int stm_velcnt;
union INTVAL stm_speed;

union LONGVAL stm_ulimit;
union INTVAL stm_svolt;
union INTVAL dcm_speed;
union INTVAL dcm_svolt;

char uart_ch;
union LONGVAL uart_buf;
char uart_rxbuf[13];
char uart_rxptr;
char uart_rxcnt;
char uart_txbuf[14];
char uart_valbuf[9];


char i2c_rxch;
union LONGVAL i2c_rxbuf;
char *i2c_txbuf;
int timer2_cnt;

void do_dcm(void);
void do_stm_inrun(void);
void do_stm_instop(void);
void do_i2c(char);
void stm_pulse(void);
void send_msg(char,char *);
void send_daughter_msg(char);
void reset_limit(void);
void i2c_get_msg(char);
void i2c_send_msg(char,char,char);
void setup(void);
void setup_variables(void);
void init_qei(void);


<<18201i2chost.c>>====================


#include <18f2431.h>
#device adc=10

#fuses HS,NOWDT,NOPUT,NOLVP

#use delay(clock=20000000)
#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7,parity=N,bits=8)
#use i2c(master,sda=PIN_C4,scl=PIN_C5)

#type int=16, long=32

#include "c:\pic\ccs-c\18201host_ccsm.h"
#include "c:\pic\khkdriver\util.h" // itohex(),ltohex(),hextoi(),hextol(),toupper() defined.
#include "c:\pic\khkdriver\qei.h" // for QEI module

#int_RDA
RDA_isr()
{
char c;
c=getc();

if(c=='<')
uart_rxptr=0;
uart_rxbuf[uart_rxptr++]=c;

if(c=='>')
{
uart_rxbuf[uart_rxptr-1]='\0';
uart_rxptr=0;
uart_ch=uart_rxbuf[1];
uart_buf.l=hextol(uart_rxbuf+3);
comm_stat.items.pc_msg=ON;
}
}

#int_EXT
EXT_isr()
{
stm_i2c_cnt++;
INT0IF=0;
}


void main(void)
{
setup();

while(1)
{
if(comm_stat.items.pc_msg)
{
/*if(TEST_PDCM_MSG)
//do_dcm();
else if(TEST_STM_RUN)
do_stm_inrun();
else*/
do_stm_instop();
comm_stat.items.pc_msg=OFF;
}

if(stm_i2c_cnt)
{
i2c_get_msg(STM_ADDR);
stm_i2c_cnt--;
}

if(dcm_i2c_cnt)
{
i2c_get_msg(DCM_ADDR);
dcm_i2c_cnt--;
}

if(comm_stat.value&0x06);
{
do_i2c(i2c_rxch);
comm_stat.value&=(~0x06);
}

if(comm_stat.items.txrdy)
{
send_daughter_msg(i2c_rxch);
comm_stat.items.txrdy=OFF;
}
}
}

void do_stm_instop(void)
{
switch(uart_ch)
{
case PSTM_INIT:
stm_enccnt.ui=uart_buf.ui[1];
stm_stepcnt.ui=uart_buf.ui[0];
stm_stepcnt.ui<<4>>4);
stm_stepmode=(uart_buf.ui[0]&0x0F);
stm_stat.items.init=ON;
send_msg(PSTM_INIT,0);
i2c_send_msg(STM_ADDR,ISTM_STEP_MODE,0);
break;
default:
send_msg(UART_FAILURE,0);
break;
}
if(~stm_stat.items.calibrated & stm_stat.items.l_lim_set & stm_stat.items.u_lim_set)
{
stm_stat.items.calibrated=ON;
send_msg(PSTM_CALIBRATED,0);
}
}

void do_i2c(char msg)
{
switch(msg)
{
case ISTM_STEP_MODE:
read_stepmode=i2c_rxbuf.ub[3];
comm_stat.items.txrdy=ON;
break;
case ISTM_OVER_CUR:
case ISTM_OVER_HEAT:
case IDCM_OVER_CUR:
case IDCM_OVER_HEAT:
comm_stat.items.txrdy=ON;
break;
default:
break;
}
}

void send_msg(char msg,char *data)
{
char *s;

while(comm_stat.items.txing);

comm_stat.items.txing=ON;

uart_txbuf[1]=msg;

if(data==0)
{
uart_txbuf[3]='0';
uart_txbuf[4]='>';
uart_txbuf[5]='\0';
}
else
{
s=uart_txbuf+3;
do
{
*s++=*data;
} while (*data++);
*--s='>';
*++s='\0';
}
puts(uart_txbuf);

comm_stat.items.txing=OFF;
}

void send_daughter_msg(char msg)
{
char c;

switch(msg)
{
case ISTM_STEP_MODE:
ltohex((long)read_stepmode,uart_valbuf);
c=PSTM_STEPMODE;
break;
case ISTM_OVER_CUR:
c=PSTM_OVER_CUR;
break;
case ISTM_OVER_HEAT:
c=PSTM_OVER_HEAT;
break;
case IDCM_OVER_CUR:
c=PDCM_OVER_CUR;
break;
case IDCM_OVER_HEAT:
c=PDCM_OVER_HEAT;
break;
default:
c=UART_FAILURE;
break;
}

if(msg==ISTM_STEP_MODE)
send_msg(c,uart_valbuf);
else
send_msg(c,0);
}

void reset_limit(void)
{
long lval;

if(stm_ulimit.l<stm_llimit.l)
{
lval=stm_llimit.l;
stm_llimit.l=stm_ulimit.l;
stm_ulimit.l=lval;
}
}

void i2c_get_msg(char addr)
{
char n;

i2c_start();
i2c_write(addr+1);
for(n=0;n<4;n++)
{
if(n==3)
i2c_rxbuf.ub[n]=i2c_read(0);
else
i2c_rxbuf.ub[n]=i2c_read(1);
}
i2c_stop();
i2c_rxch=i2c_rxbuf.ub[0];
if(i2c_rxch<0x20)
comm_stat.items.stm_msg=ON;
else
comm_stat.items.dcm_msg=ON;
}

void i2c_send_msg(char addr,char msg,char num)
{
char i;
i=0;

i2c_start();
i2c_write(addr);
i2c_write(msg);
while(i<num)
i2c_write(*(i2c_txbuf+(i++)));
i2c_stop();
}

void setup(void)
{
comm_stat.value=0;
setup_variables();

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0|ADC_CONT_A|ADC_WHEN_INT0|ADC_INT_EVERY_OTHER);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_OFF);
setup_timer_1(T1_EXTERNAL_SYNC|T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_1,249,1);
setup_oscillator(FALSE);

set_timer1(0xFFFF);

LATA=LATA_VALUE;
LATB=LATB_VALUE;
LATC=LATC_VALUE;
TRISA=TRISA_VALUE;
TRISB=TRISB_VALUE;
TRISC=TRISC_VALUE;

enable_interrupts(INT_RDA);
enable_interrupts(INT_EXT); // if this line is deleted, rs232 comm is enabled!!!!!!!!
enable_interrupts(GLOBAL);

STM_OFF;
DCM_OFF;
}

void setup_variables(void)
{
stm_stat.value=0;
dcm_stat.value=0;

stm_i2c_cnt=0;
dcm_i2c_cnt=0;

stm_stepmode=-1;
stm_stepcnt.i=0;
stm_enccnt.i=0;
stm_position.l=0;
stm_velcnt=0;

stm_step_pos.l=0;

stm_speed.i=10;
stm_ulimit.l=0;
stm_llimit.l=0;
stm_svolt.i=0;

dcm_speed.i=0;
dcm_svolt.i=0;

uart_ch=0;
uart_buf.l=0;
uart_rxptr=0;
uart_rxcnt=0;
memset(uart_rxbuf,0,13);
memset(uart_txbuf,0,14);
uart_txbuf[0]='<';
uart_txbuf[2]='|';
memset(uart_valbuf,0,9);

i2c_rxch=0;
i2c_rxbuf.l=0;

timer2_cnt=0;
}


Thanks for reading. Sad
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sun Jan 14, 2007 6:59 pm     Reply with quote

You did�t get any answer for one or more reason described here:

http://www.ccsinfo.com/forum/viewtopic.php?t=29483


Humberto
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