Reply by microcodefun March 24, 20062006-03-24
You can download the Driver2Go demo. It contains a fully functional
CAN driver that is preconfigured for 500kbps on channel 0:
  - http://www.feaser.com/store/getfile.php?file=an1004.zip

Message transmission
--------------------
tCanMsg       MyMsg;
unsigned char MyData[2];

MyMsg.id   = 0x100; 
MyMsg.dlc  = 2;
MyMsg.type = kStdId;

MyData[0] = 0xaa;
MyData[1] = 0x55;

CanTransmit(kCan0, &MyMsg, MyData);

Message reception
-----------------
The following function gets called automatically by the CAN driver
when CAN related events occur:

void AppCanEvent(tCanChannel channel, tCanEvent *pEvent)
{
  if (pEvent->eventType == kCanIndication) 
  {
    /* handle received message.. */
  }
}

-Frank

----
Feaser LLC
http://www.feaser.com
----
	
Reply by Steve Letkeman March 23, 20062006-03-23
Hello, one thing you need to know about CAN is that it is different than
other methods of communicating (like RS232) in that there has to be
at least two working nodes on your network in order for the transmitting
node to send a successful CAN message.  The second node actually inserts
an ack bit into the senders data stream, as it is being transmitted.  If you
try to transmit with a second node you will see a continous stream of
data coming from your tx pin.
Here's a sample CAN program that might help you out,
Steve

Steven D. Letkeman BSc.
President - Zanthic Technologies Inc.
403-526-8318
www.zanthic.com Embedded micro-controllers and CAN interfaces
www.brightan.com Automated lighting systems
	// This file may be freely distributed as long as this header remains

// intact in order to encourage you to visit our web site

// at www.zanthic.com home of the CAN-4-USB interface

// This simple program was written as a quick and easy

// example of initializing and testing the msCAN port (V2.14)

// on an MC9S12 processor. No warranty implied or given.

// This program was written as a single file for

// simplicity and only some of the registers are included.

// Written by Steve Letkeman Feb 2004

// compiled using the ICC12 compiler from imagecraft (www.imagecraft.com)

// Vector file not included but only contains two entries, start at 0xFFFE

// and the CAN0 receive interrupt vector pointing to CAN_Receive()

// sends CAN ID 1 (29 bit) at power up and receives ID 2 (29 bit) and

// echos back to ID 1 when received
	#define CAN0CTL0 *(unsigned char volatile *)(0x0140)

#define CAN0CTL1 *(unsigned char volatile *)(0x0141)

#define CAN0BTR0 *(unsigned char volatile *)(0x0142)

#define CAN0BTR1 *(unsigned char volatile *)(0x0143)

#define CAN0RFLG *(unsigned char volatile *)(0x0144)

#define CAN0RIER *(unsigned char volatile *)(0x0145)

#define CAN0TFLG *(unsigned char volatile *)(0x0146)

//#define CAN0TIER *(unsigned char volatile *)(0x0147)

//#define CAN0TARQ *(unsigned char volatile *)(0x0148)

//#define CAN0TAAK *(unsigned char volatile *)(0x0149)

#define CAN0TBSEL *(unsigned char volatile *)(0x014A)

#define CAN0IDAC *(unsigned char volatile *)(0x014B)

//#define CAN0RXERR *(unsigned char volatile *)(0x014E)

//#define CAN0TXERR *(unsigned char volatile *)(0x014F)

#define CAN0IDAR0 *(unsigned char volatile *)(0x0150)

#define CAN0IDAR1 *(unsigned char volatile *)(0x0151)

#define CAN0IDAR2 *(unsigned char volatile *)(0x0152)

#define CAN0IDAR3 *(unsigned char volatile *)(0x0153)

#define CAN0IDMR0 *(unsigned char volatile *)(0x0154)

#define CAN0IDMR1 *(unsigned char volatile *)(0x0155)

#define CAN0IDMR2 *(unsigned char volatile *)(0x0156)

#define CAN0IDMR3 *(unsigned char volatile *)(0x0157)

#define CAN0IDAR4 *(unsigned char volatile *)(0x0158)

#define CAN0IDAR5 *(unsigned char volatile *)(0x0159)

#define CAN0IDAR6 *(unsigned char volatile *)(0x015A)

#define CAN0IDAR7 *(unsigned char volatile *)(0x015B)

#define CAN0IDMR4 *(unsigned char volatile *)(0x015C)

#define CAN0IDMR5 *(unsigned char volatile *)(0x015D)

#define CAN0IDMR6 *(unsigned char volatile *)(0x015E)

#define CAN0IDMR7 *(unsigned char volatile *)(0x015F)

#define CAN0RXFG (unsigned char volatile *)(0x0160)

#define CAN0TXFG (unsigned char volatile *)(0x0170)

#define CANE 0x80

#define INITAK 1

#define INITRQ 1

#define SLPRQ 2

#define SLPAK 2

void SendCANMessage(unsigned char NumBytes, unsigned char *BufPntr);

void CANInit(void);

void main(void)

{

unsigned char BufPntr[8];

CANInit();

asm("cli");

BufPntr[0]=0x11;

BufPntr[1]=0x22;

BufPntr[2]=0x33;

BufPntr[3]=0x44;

BufPntr[4]=0x55;

BufPntr[5]=0x66;

BufPntr[6]=0x77;

BufPntr[7]=0x88;

SendCANMessage(8, BufPntr); // send a CAN packet with 8 bytes of data

while(1); // wait here for receive interupt

}

// 
#############################################################################

// Incoming CAN messages will be caught here

#pragma interrupt_handler CAN_Receive

void CAN_Receive(void)

{

unsigned char Temp[8];

Temp[0]=*(CAN0RXFG+4); // Grab the first data byte

// currently no checking for number of bytes etc

SendCANMessage(1, Temp); // send it back
	CAN0RFLG |=1; // clear rec flag

}

// 
#############################################################################

void SendCANMessage(unsigned char NumBytes, unsigned char *BufPntr)

{

unsigned char NACAN,C;

while(!CAN0TFLG); // wait for available buffer

NACAN0TFLG; // get the next available CAN buffer

CAN0TBSEL=NACAN;

*(CAN0TXFG+0)=0x00; // set the transmit ID

*(CAN0TXFG+1)=0x08; // extended ID (29 bit)

*(CAN0TXFG+2)=0x00;

*(CAN0TXFG+3)=0x02; // ID-00-00-01

if (NumBytes>8) NumBytes=8;

for (C=0;C<NumBytes;C++)

*(CAN0TXFG+4+C)=*BufPntr++; // store the data

*(CAN0TXFG+0x0C)=NumBytes; // set number of bytes to send

NACAN0TBSEL;

CAN0TFLG =NACAN; // transmit

}

// 
#############################################################################

void CANInit(void)

{

CAN0CTL0 |= INITRQ; // set INITRQ, this will also set INITAK

while (!(CAN0CTL1 & INITAK)); // wait for init mode to occur

CAN0CTL1 = CANE; // Set CANE just in case this is the first time after reset

CAN0BTR0=0x03;

CAN0BTR1=0x32; // for a CAN Baud of 500kbps at 16Mhz crystal

CAN0IDMR0=0x00;

CAN0IDMR1=0x08; //IDE=1

CAN0IDMR2=0x00;

CAN0IDMR3=0x00;

CAN0IDAR0=0x00;

CAN0IDAR1=0x18; // SRR&IDE=1

CAN0IDAR2=0x00;

CAN0IDAR3=0x04; // ID=2, remote frame (bit 0)=0

// set the second filter to must match 0xFFFFFFFF

CAN0IDMR4=0;

CAN0IDMR5=8; //IDE=1

CAN0IDMR6=0;

CAN0IDMR7=0;

CAN0IDAR4=0xFF;

CAN0IDAR5=0xFF;

CAN0IDAR6=0xFF;

CAN0IDAR7=0xFF;

CAN0IDAC=0; // set filters to 2 32 bit acceptance filters

CAN0CTL0 &= ~INITRQ; // clear INITRQ

while (CAN0CTL1 & INITAK);

CAN0CTL1 |= CANE; // Set CANE just in case this is the first time after 
reset

CAN0RIER |= 1; // enable receive interrupt

}
	----- Original Message ----- 
From: "Ashwin" <m_ashwin_81@m_as...>
To: <68HC12@68HC...>
Sent: Thursday, March 23, 2006 1:48 PM
Subject: [68HC12] Help with M68EVB912DP256
	> Hello,
>
> I am a newbie and am not too well versed with microcontrollers. So I
> will appreciate any help.
>
> I am using a M68EVB912DP256 development board (it has the
> MC9S12DP256B chip). I want to install a CAN interface so that it can
> communicate with another microcontroller (XC3S-1500). I was able to
> write a simple CAN transmit code and I think it works fine. NOw, I
> want to test the CAN receive. These are my steps:
>
> - I am using the other microncontroller to generate the CAN signals.
> (That board works at a clock frequency of 40MHz and is scaled by 4/5.)
>
> - I am physically wiring that board's RX & TX pins to this
board's RX
> & TX pin. I figured out the pins from the data sheet of the CAN
> controller (MC33388D).
>
> - I don't know if this is working or not, because the source board
> seems to be effortlessly and continuously transmitting the data. I
> thought if I could see some indication of any sort on this board, I
> can proceed. Hence I thought if I could make the D0 LED glow
> (PORTB_BIT0), each time the code enters the receive ISR, at least I
> know it is working till that part.
>
> - To test that, I first wrote an even simpler code to play with the
> PORTB_BIT0. Here is the code:
>
> #include <hidef.h>      /* common defines and macros */
> #include <mc9s12dp256.h>     /* derivative information */
>
>
> #pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
>
> void turnlight() {
>  int i;
>
>  DDRB_BIT0 = 0;
>  for (i =0;i<10;i++);
>  PORTB_BIT0 = 1;
> }
>
> void main(void) {
>
>  int i;
>
>  for(;;) {
>
>    for (i=0;i<10000;i++);
>    turnlight();
>  }
> }
>
> I expected to see the LED blink on and off. It does not do so.
> However, when I manually trace the code step by step, it does so. Am
> I missing out on some jumper settings or something? When I hit run,
> the LED does not glow. If someone could help me with, both, this and
> the CAN receive, I will be grateful. Thanks.
>
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
	
Reply by Doron Fael March 23, 20062006-03-23
Rob already got on your real problem:

The delay you have in the 10 iterations of the "for" loop is too short
to 
see the LED blinks.
When you single-step you insert the delay manually using the 
single-stepping, so then you are able to see the LED turns on and off.

Increase the delay significantly (for example to repeat 1 million times), 
and you should be able to observe the LED blinking.

Hope this helps,
Doron
Nohau
HC12 In-Circuit Emulators
www.nohau.com/emul12pc.html

At 22:08 23/03/2006 +0000, you wrote:
>I am sorry, I forgot to post the edited code:
>
>    DDRB_BIT0 = 1;
>
>    PORTB_BIT0 = 0;
>    for (i =0;i<10;i++);
>    PORTB_BIT0 = 1;
>
>
>As I said, tracing this code, I can see the LED go on and off, but it
>doesn't do so when I hit Run.
	
	
Reply by Ashwin March 23, 20062006-03-23
I am sorry, I forgot to post the edited code:

   DDRB_BIT0 = 1;

   PORTB_BIT0 = 0;
   for (i =0;i<10;i++);
   PORTB_BIT0 = 1;
	As I said, tracing this code, I can see the LED go on and off, but it
doesn't do so when I hit Run.
	
Reply by Doron Fael March 23, 20062006-03-23
Regarding your LED test:

1) To use PORTB_BIT0 as an output pin you need to set the DDRB_BIT0 bit, 
and not clear it.

2) In your posted code I can see where you set PORTB_BIT0 to a 1, but I 
don't see you clear it anywhere, so how can it turn off and on?

Regarding your CAN test I am not an expert - but maybe something very basic:
You need to have a CAN level translator IC to convert from the CMOS levels 
in the MCU outputs to the CAN voltage levels.

Hope this helps,
Doron
Nohau
HC12 In-Circuit Emulators
www.nohau.com/emul12pc.html

At 20:48 23/03/2006 +0000, you wrote:
>Hence I thought if I could make the D0 LED glow
>(PORTB_BIT0), each time the code enters the receive ISR, at least I
>know it is working till that part.
>
>- To test that, I first wrote an even simpler code to play with the
>PORTB_BIT0. Here is the code:
>
>#include <hidef.h>      /* common defines and macros */
>#include <mc9s12dp256.h>     /* derivative information */
>
>
>#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
>
>void turnlight() {
>   int i;
>
>   DDRB_BIT0 = 0;
>   for (i =0;i<10;i++);
>   PORTB_BIT0 = 1;
>}
>
>void main(void) {
>
>   int i;
>
>   for(;;) {
>
>     for (i=0;i<10000;i++);
>     turnlight();
>   }
>}
>
>I expected to see the LED blink on and off. It does not do so.
>However, when I manually trace the code step by step, it does so. Am
>I missing out on some jumper settings or something? When I hit run,
>the LED does not glow. If someone could help me with, both, this and
>the CAN receive, I will be grateful. Thanks.
	
	
Reply by Robert Milne March 23, 20062006-03-23
for (i =0;i<10;i++);

is only about .4 microseconds if you are running your pll at the 
standard 24MHz.  It's impossible to see such a short burst.  Better to 
use a scope.
	The CAN dev work that I have done was without the benefit of a CAN 
analyser or even a debugger.  A scope and print statements over the uart 
did the job.  I could not find a full featured CAN driver in my 
searches. The app notes on the Freescale site contain rudimentary 
examples only so I had to spend a fair bit of time carefully digesting 
the MSCAN module pdf.  In the end, your application will probably 
determine how complete your driver will be since the MSCAN module has 
more error checking than you may need.  I put the complete data link 
layer into the driver such as buffered I/O, so that the app is 
abstracted from low level services.  I wouldn't dream of using CAN 
without interrupts unless the app is very very simple.

rob

Ashwin wrote:

> Hello,
>
> I am a newbie and am not too well versed with microcontrollers. So I
> will appreciate any help.
>
> I am using a M68EVB912DP256 development board (it has the
> MC9S12DP256B chip). I want to install a CAN interface so that it can
> communicate with another microcontroller (XC3S-1500). I was able to
> write a simple CAN transmit code and I think it works fine. NOw, I
> want to test the CAN receive. These are my steps:
>
> - I am using the other microncontroller to generate the CAN signals.
> (That board works at a clock frequency of 40MHz and is scaled by 4/5.)
>
> - I am physically wiring that board's RX & TX pins to this
board's RX
> & TX pin. I figured out the pins from the data sheet of the CAN
> controller (MC33388D).
>
> - I don't know if this is working or not, because the source board
> seems to be effortlessly and continuously transmitting the data. I
> thought if I could see some indication of any sort on this board, I
> can proceed. Hence I thought if I could make the D0 LED glow
> (PORTB_BIT0), each time the code enters the receive ISR, at least I
> know it is working till that part.
>
> - To test that, I first wrote an even simpler code to play with the
> PORTB_BIT0. Here is the code:
>
> #include <hidef.h>      /* common defines and macros */
> #include <mc9s12dp256.h>     /* derivative information */
>
>
> #pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
>
> void turnlight() { 
>   int i;
>  
>   DDRB_BIT0 = 0;
>   for (i =0;i<10;i++);  
>   PORTB_BIT0 = 1;
> }
>  
> void main(void) {
>
>   int i;
>  
>   for(;;) {
>    
>     for (i=0;i<10000;i++);
>     turnlight();
>   }    
> }
>
> I expected to see the LED blink on and off. It does not do so.
> However, when I manually trace the code step by step, it does so. Am
> I missing out on some jumper settings or something? When I hit run,
> the LED does not glow. If someone could help me with, both, this and
> the CAN receive, I will be grateful. Thanks.
>
>
>
>
> >.
>
>
>
	
Reply by Ashwin March 23, 20062006-03-23
Hello,

I am a newbie and am not too well versed with microcontrollers. So I 
will appreciate any help.

I am using a M68EVB912DP256 development board (it has the 
MC9S12DP256B chip). I want to install a CAN interface so that it can 
communicate with another microcontroller (XC3S-1500). I was able to 
write a simple CAN transmit code and I think it works fine. NOw, I 
want to test the CAN receive. These are my steps:

- I am using the other microncontroller to generate the CAN signals. 
(That board works at a clock frequency of 40MHz and is scaled by 4/5.)

- I am physically wiring that board's RX & TX pins to this board's
RX 
& TX pin. I figured out the pins from the data sheet of the CAN 
controller (MC33388D).

- I don't know if this is working or not, because the source board 
seems to be effortlessly and continuously transmitting the data. I 
thought if I could see some indication of any sort on this board, I 
can proceed. Hence I thought if I could make the D0 LED glow 
(PORTB_BIT0), each time the code enters the receive ISR, at least I 
know it is working till that part.

- To test that, I first wrote an even simpler code to play with the 
PORTB_BIT0. Here is the code:

#include <hidef.h>      /* common defines and macros */
#include <mc9s12dp256.h>     /* derivative information */
	#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"

void turnlight() {  
  int i;
  
  DDRB_BIT0 = 0;
  for (i =0;i<10;i++);   
  PORTB_BIT0 = 1; 
}
  
void main(void) {

  int i;
  
  for(;;) {
    
    for (i=0;i<10000;i++);
    turnlight();
  }     
}

I expected to see the LED blink on and off. It does not do so. 
However, when I manually trace the code step by step, it does so. Am 
I missing out on some jumper settings or something? When I hit run, 
the LED does not glow. If someone could help me with, both, this and 
the CAN receive, I will be grateful. Thanks.