We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
So what have you tried?
And what have you learned from that?
Hi Andrew,
I have copied each function definition at a time from working code to non working code (cumulative all function definitions we're copied at finally) and tested on hardware. And with that also I didn't get any results. Finally I have tried with copying complete code and that is working fine. whether any logical error or something else I am unable figure it out.
Use diff or merge? Pin code looks different bits/order Compare/contrast registers
your code doesn't just exist on your editor screen - it has to run in the microcontroller, and get down with the hardware.
So what debugging have you done on the hardware ?
Have you stepped through the code to see what's happening?
Have you looked at the CAN lines with an oscilloscope to see what's actually happening?
In both cases, compare & contrast the working & non-working code.
If you had working code to start with, why did you change it?
Starting with your working code, make only small changes, and test after each one - then you will only have a small problem space to search.
You have a uart0_init() function, which you call - but I don't see you making any use of the UART for debug or diagnostic output.
Your code is full of "Magic Numbers" - it would make a lot more sense if you used symbolic definitions.
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.
" I have struggled a lot to figure out this mistake"
As already suggested, text comparison utility - or "diff" - would immediately highlight such mistakes.
There are many available; here's just one example:
http://winmerge.org/
Another suggestion made earlier was that you use the debugger to step through your code & see what happens.
This would have shown you immediately that your mis-named interrupt handler was not being called ...
Thanks for your support and I have asked the following for mini project on ARM protocols. 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.