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
Non-Working code but similar and same as above:
#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 can1_init() { LPC_SC->PCONP |= (1<<13); LPC_SC->PCLKSEL0 |= (0<<26)|(0<<27)|(0<<30)|(0<<31); LPC_PINCON->PINSEL0 |= (1<<0)|(1<<2); LPC_CAN1->MOD = 1; LPC_CAN1->BTR = 0x7000E; LPC_CAN1->MOD = 0; } void uart0_init() { LPC_SC->PCONP |= (1<<3); LPC_SC->PCLKSEL0 |= (0<<6)|(0<<7); 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 uart0_tx(unsigned char x) { while((LPC_UART0->LSR & (1<<5))!= (1<<5)); LPC_UART0->THR = x; } void can_afmr() { NVIC_EnableIRQ(CAN_IRQn); LPC_CAN1->IER = (1<<0); LPC_CANAF->AFMR = 1; 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; uart0_tx(data); uart0_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(); can1_init(); can_afmr(); can_tx(1, 'A', 1, 0); can_tx(4, 'B', 1, 0); while(1); }
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
View all questions in Keil forum