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 support@ccsinfo.com

CAN and RS485 don't work in one file

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



Joined: 07 Aug 2008
Posts: 11
Location: saigon,vietnam

View user's profile Send private message Yahoo Messenger

CAN and RS485 don't work in one file
PostPosted: Wed Dec 31, 2008 6:03 am     Reply with quote

I have a problem with CAN bus and RS485. When CAN bus in single file it work well, RS485 work also. But when I implement those in same file they don't work. Please help me!
Here is my code.
Code:
#include <18f4680.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=PC)

#define  RS485_RX_BUFFER_SIZE 64
#define  RS485_USE_EXT_INT    FALSE

int8 OUR_RS485_ID = 0;
#define RS485_ID OUR_RS485_ID

#include <rs485.c>
#include <stdlib.h>
#include "my_can.c"

int1 flag;
int flag1;
int flag2;
int16 count;
int cmp[4];
int8 msg[8];
int i;


void delay_seconds(int n) {

  for (;n!=0; n--)
  delay_ms( 1000 );
}


void main()
{
   cmp[0]=0;
   cmp[1]=1;
   cmp[2]=2;
   cmp[3]=3;
   set_tris_b(0x00);
   set_tris_c(0x00);
   flag=0;
   flag1=0;
   flag2=0;
   count=0;
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4); // rs485 ko chay
   enable_interrupts(int_timer0);
   enable_interrupts(GLOBAL);
   my_can_init();
   my_can_set_id(111);
   rs485_init();
   
   while(1){
         if(rs485_get_message(msg, TRUE))
         {
            /*printf("\n\r%d: ", msg[0]);
            for(i=0; i < msg[1]; ++i)
               putc(msg[i+2]);
            printf("\n\r");*/
            flag1=4;
         }
         //restart_wdt();
         if ( can_kbhit() )   //if data is waiting in buffer...
         {
            if(can_getd(rx_id, &in_data[0], rx_len, rxstat)) { //...then get data from buffer
               if(rx_len<=MY_CAN_BUFFER_SIZE){
                  if(rx_id==my_can_id){
                     if(memcmp(in_data,cmp,4)==0)
                     
                        flag2=4;
                     //output_toggle(PIN_B4);
                     output_bit(PIN_B5,in_data[0]);
                     output_bit(PIN_B4,in_data[1]);
                  }
                  else{
                       printf("\r\n ID=%Lu Data not for me !\r\n",rx_id);
                  }
               }
               else{
                  printf("\r\nFAIL on rx_len\r\n");
               }
            }
            else {
               printf("\r\nFAIL on GETD\r\n");
            }
      }
      //out put to check for receive data
     if(flag1==4){
          output_bit(PIN_D0,1);
          delay_seconds(3);
          output_bit(PIN_D0,0);
          flag1=0;
      }if(flag2==4){
          output_bit(PIN_D0,1);
          delay_seconds(1);
          output_bit(PIN_D0,0);
          flag2=0;
      }
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 31, 2008 12:09 pm     Reply with quote

Quote:

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4); // rs485 ko chay
enable_interrupts(int_timer0);
enable_interrupts(GLOBAL);

You have enabled Timer0 interrupts, but you don't have an #int_timer0
interrupt service routine.
t90



Joined: 07 Aug 2008
Posts: 11
Location: saigon,vietnam

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Dec 31, 2008 7:31 pm     Reply with quote

I use #int_timer0 to handle the data receiving from RS485. But it don't
work, so that I delete #int_timer0 but I forget to delete setup_timer0.
Now I delete setup_timer0 but it still don't work. Please help me
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 31, 2008 7:52 pm     Reply with quote

Quote:
Now I delete setup_timer0 but it still don't work.

Delete these two lines:
Quote:
enable_interrupts(int_timer0);
enable_interrupts(GLOBAL);
t90



Joined: 07 Aug 2008
Posts: 11
Location: saigon,vietnam

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Dec 31, 2008 8:23 pm     Reply with quote

I delete it, but it don't work.
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