Help me implement CAN code on LPC2129 ARM microcontroller

Status
Not open for further replies.

ramasamy

Member level 1
Joined
Mar 15, 2005
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,524
Hi
I need C code for CAN interface of LPC2129, please any one help me.
Best regard
Ramasamy
 

#include<LPC21xx.h>
#include<stdio.h>


#define DWORD unsigned int

#define CAN_REG_BASE (0xE0000000)
#define ACCEPTANCE_FILTER_RAM_BASE (CAN_REG_BASE + 0x00038000)

void delay(unsigned int count)
{
/* if this flag is set than execute all other function */

while(count--); /*decrementing value of count untill it becomes zero*/
}




int Can_Init(void)
{

//DWORD candata;
DWORD *pAddr;
PINSEL1 |= (DWORD) 0x00054000;


C1MOD = 1; // Enter Reset Mode
C1GSR = 0; // Clear status register
C1BTR = 0x001C001D; // Set bit timing

AFMR = 0x00000001; // Disable acceptance filter

// Disable All Interrupts
C1IER = 0;

// Enter Normal Operating Mode
C1MOD = 0; // Operating Mode

// Init CAN interface 2
C2MOD = 1; // Enter Reset Mode
C2GSR = 0; // Clear status register
C2BTR = 0x001C001D; // Set bit timing

// Disable All Interrupt
C2IER = 0;

// Enter Normal Operating Mode
C2MOD = 0; // Operating Mode

AFMR = 0x00000001;
SFF_sa = 0;

pAddr = (DWORD *) ACCEPTANCE_FILTER_RAM_BASE;
//candata = 0x20002000 + (ID_lower << 16) + ID_upper;
*pAddr = 0x211f20cf;
pAddr++;
*pAddr = 0x206B2064;

SFF_GRP_sa = 0x00000008;
EFF_sa = 0x00000008;
EFF_GRP_sa = 0x00000008;
ENDofTable = 0x00000008;

AFMR = 0x00000000;

return 0;
}

void SendMessage(unsigned int x)
{
unsigned int status;
//IOSET1 = 0X00800000;
while((C2SR & 0x00000004) != 0x00000004);


C2TFI1 = 0x00010000;
C2TDA1 = 0x0000000F;
C2TDB1 = 0x00000000;
C2TID1 = x;

//C1MOD = 0x1;
C2CMR = 0x21;
//C1MOD = 0x04;
status = C2GSR;
while((status & 0x00000008) != 0x00000008)
status = C2GSR;


// if((CANTxSR & 0x00010000)!=0);


}

int ReceiveMessage (void)
{
unsigned int data,len,id;

len = C1RFS;
id = C1RID;
data = C1RDA;
if(id == 0x000000CF) {
IOSET1 = 0X00010000;
delay(500000);
//delay(500000);
IOCLR1 = 0x00010000;
}
else if (id == 0x0000011F) {
IOSET1 = 0X00020000;
delay(500000);
//delay(500000);
IOCLR1 = 0x00020000;
}
else if (id == 0x0000006B) {
IOSET1 = 0X00040000;
delay(500000);
//delay(500000);
IOCLR1 = 0x00040000;
}
else if (id == 0x00000064) {
IOSET1 = 0X00080000;
delay(500000);
//delay(500000);
IOCLR1 = 0x00080000;
}
else
return 1;

C1CMR = 0x04;

return 0;

}

int Adc_conversion(void)
{
int a;

ADCR=0x01200001;

while(!(ADDR & 0x80000000));

a=ADDR;

a=a & 0x0000ffc0;

a=a>>6;

return a;
}



int main(void)
{
int value;
IODIR1 = 0X00FF0000;
IOCLR1 = 0x00FF0000;

Can_Init();

while(1) {
value = Adc_conversion();
if(value<= 256){
SendMessage(0x000000CF);
ReceiveMessage();
}
else if(value > 257 & value <=512){
SendMessage(0x0000011F);
ReceiveMessage();
}
else if(value > 513 & value <=768){
SendMessage(0x0000006B);
ReceiveMessage();
}
else if(value > 769 & value <=1024){
SendMessage(0x00000064);
ReceiveMessage();
}
else
return 1;

}

return 0;
}

---------- Post added at 21:57 ---------- Previous post was at 21:42 ----------

when you install keil the examples are loaded in the installation folder check that toooo........

You can search for LPC21xx codebundle.. you will get lot of examples also
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…