This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

CAN protocol not working

I have written CAN driver program for LPC1768 microcontroller. One is working code and based on that another driver program i have written. But the problem is first driver program is working fine but second code is not working even though its similar to the first program. Also note that both codes were tested on controller hardware. I am unable to find whats wrong with the second driver code. I am pasting working and non-working code for your reference.

Working CAN driver:


        #include<LPC17xx.h>
        unsigned int data, id, frame, data1;
        void pll()
        {
                LPC_SC->SCS = (1<<5);
                while((LPC_SC->SCS & (1<<6)) ==  0);
                LPC_SC->CLKSRCSEL = (1<<0);
                LPC_SC->PLL0CON = (1<<0);
                LPC_SC->PLL0CFG = (14<<0);
                LPC_SC->PLL0FEED = (0xAA);
                LPC_SC->PLL0FEED = (0x55);
                LPC_SC->CCLKCFG = (5<<0);
                while((LPC_SC->PLL0STAT & (1<<26)) == 0);
                LPC_SC->PLL0CON |= (1<<1);
                LPC_SC->PLL0FEED = (0xAA);
                LPC_SC->PLL0FEED = (0x55);
        }

        void can_init()
        {
                LPC_SC->PCONP |= (1<<13);
                LPC_PINCON->PINSEL0 |= (1<<0)|(1<<2);
                LPC_SC->PCLKSEL0 |= (0<<26)|(0<<27);
                LPC_CAN1->MOD = 1;
                LPC_CAN1->BTR = 0x7000E;
                LPC_CAN1->MOD = 0;
        }

        void uart0_init()
        {
                LPC_SC->PCONP |= (1<<3);
                LPC_PINCON->PINSEL0 |= (1<<4)|(1<<6);
                LPC_UART0->LCR = (0x03<<0)|(1<<7);
                LPC_UART0->DLL = 97;
                LPC_UART0->DLM = 0;
                LPC_UART0->LCR &= ~(1<<7);
        }

        void tx(unsigned char a)
        {
                while((LPC_UART0->LSR & (1<<5)) != (1<<5));
                LPC_UART0->THR = a;
        }

        void can_filter()
        {
                NVIC_EnableIRQ(CAN_IRQn);
                LPC_CAN1->IER = (1<<0);
                LPC_CANAF->AFMR = (1<<0);
                LPC_CANAF_RAM->mask[0] = (1<<16)|(4<<0);
                LPC_CANAF->SFF_sa = 0;
                LPC_CANAF->SFF_GRP_sa = 4;
                LPC_CANAF->EFF_sa = 4;
                LPC_CANAF->EFF_GRP_sa = 4;
                LPC_CANAF->ENDofTable = 4;
                LPC_CANAF->AFMR = (0<<0);
        }

        void CAN_IRQHandler()
        {
                id = LPC_CAN1->RID;
                frame = LPC_CAN1->RFS;
                data = LPC_CAN1->RDA;
                data1 = LPC_CAN1->RDB;
                tx(data);
                tx(data1);
                LPC_CAN1->CMR |= (1<<2);
        }

        void can_tx(int id, char data, char dlc, char rtr)
        {
                while((LPC_CAN1->SR & (1<<2)) == 0);
                LPC_CAN1->TID1 = id;
                LPC_CAN1->TDA1 = data;
                LPC_CAN1->TFI1 = (dlc<<16)|(rtr<<30);
                LPC_CAN1->CMR = 1;
                while((LPC_CAN1->SR & (1<<3)) == 0);
                LPC_CAN1->CMR = 0;
        }

        int main()
        {
                pll();
                uart0_init();
                can_init();
                can_filter();
                can_tx(1, 'A', 1, 0);
                can_tx(4, 'B', 1, 0);
                while(1);
        }

Thanks
Bhaskar

Parents
  • Hi Andrew,

    Its completely my mistake and the function name has been modified from CAN_IRQHandler() to CAN_IRQhandler() (i.e. capital H in IRQHandler to h). I have struggled a lot to figure out this mistake and how to resolve these kind of logical errors which are not very easy to trace.

    And also I want to build some protocol kind of project which has to include multiple protocols like UART, I2C and CAN combined and requesting small kind of guidance on this topic from embedded geeks.

    Thanks
    Bhaskar

Reply
  • Hi Andrew,

    Its completely my mistake and the function name has been modified from CAN_IRQHandler() to CAN_IRQhandler() (i.e. capital H in IRQHandler to h). I have struggled a lot to figure out this mistake and how to resolve these kind of logical errors which are not very easy to trace.

    And also I want to build some protocol kind of project which has to include multiple protocols like UART, I2C and CAN combined and requesting small kind of guidance on this topic from embedded geeks.

    Thanks
    Bhaskar

Children