Escolar Documentos
Profissional Documentos
Cultura Documentos
In the task, all the data except one are to be sent every 10ms.
The one is to be sent for every 20ms. In order to achieve this,
we declare a count variable that ensures that that this is done.
/ / C o u n t e r f o r t i m e r 20ms
int counter timer = 0;
I. E NUNCIATION
This task aims to develop a C program that transmits and
receives data in the CAN module of a Texas TMS320F28335
microcontroller. A list is provided with data from a SEAT
car that needs to be transmitted (Fig. 1). The list defines the
identifier of the data, data length,units and cycle time. The list
also defines the structure of the CAN data frame. The speed of
data transmission is defined to be 500kbps. Additionally, the
received data must be sent via the serial port and the result
displayed in a terminal in the PC.
B. Initialization
In this part, all the registers needed for the functioning of
the CAN module - clock,input/output pins, interrupts and the
ADC - are initialised.
InitSysCtrl () ;
Gpio select () ;
InitECan ( ) ;
InitPieCtrl () ;
InitPieVectTable () ;
InitAdc () ;
Fig. 1.
For the first task, the CAN module is set for Self-test mode
(STM). This means that the data is sent from one mailbox to
the other and the acknowledge signal to the first mailbox is
generated internally. In the STM, mailboxes are programmed
with the respective identifiers of each data that needs to be
transmitted. The data to be transmitted is taken from the ADC
of the peripheral explorer kit.
For the second task, the data is received from the CANoe
module directly to the mailbox and it is sent to the PC via
serial port.
II. TASK 1 - S ELF T EST M ODE
A. Variable declaration
To demonstrate the task, we need to send and receive several
data. They are stored in the mailboxes. One of them is the
Wheel based vehicle speed. To receive this data, an integer
variable is being used. After transmission, some pre-defined
operation is performed on this data, and the results are stored
in a floating variable, to be transmitted serially. An example
for the Mailbox 1 is:
/ / MAILBOX 1 :
i n t wbv speed ;
f l o a t wbv speed RS232 = 0 ;
/ / Interrupt Vectors
P i e V e c t T a b l e . SCIRXINTA = &SCIA RX isr ;
P i e V e c t T a b l e . TINT0 = &c p u t i m e r 0 i s r ;
P i e V e c t T a b l e . ECAN0INTA = &e c a n 0 i n t A i s r ;
The CPU timer and CAN interrupts are enabled and the
global interrupt is also assigned.
/ / E n a b l e CPUT i m e r 0 ISR :
P i e C t r l R e g s . PIEIER1 . b i t . INTx7 = 1 ;
/ / E n a b l e ECAN0INTA ISR :
P i e C t r l R e g s . PIEIER9 . b i t . INTx5 = 1 ;
IER | = 0 x0101 ;
/ / e n a b l e INT1 & INT9 i n IER
C. Infinite loop
Here, the DSP is waiting in a sleep mode. When a CAN
interrupt is produced then the program enters the internal loop
and sends the data serially.
while ( 1 )
{
/ / I f a new CAN Frame was r e c e i v e d :
i f ( CANreceived ==TRUE)
{
/ / The new v a l u e s a r e SCI t r a n s m i t t e d :
CANreceived =FALSE ;
s p r i n t f ( m e s s a g e t x s c i , \ r %2.1 fkm / h %c %4.1 f d e g %2.1
fkm / h %2.1 fkm / h %2.1 fkm / h %2.1 fkm / h %3.1 f%c ,
wbv speed RS232 , s t w a n g d i r , stw ang RS232 ,
rlw speed RS232 , flw speed RS232 ,
frw speed RS232 , rrw speed RS232 , app RS232 , %
);
do
{
/ / Send s i n g l e c h a r a c t e r :
S c i a R e g s . SCITXBUF= m e s s a g e t x s c i [ i n d e x + + ] ;
/ / W a i t f o r TX e m p t y :
w h i l e ( S c i a R e g s . SCICTL2 . b i t . TXEMPTY == 0 ) ;
}
/ / W a i t f o r TX e m p t y :
w h i l e ( m e s s a g e t x s c i [ i n d e x ] ! = \0 ) ;
index =0;
} / / i f ( C A N r e c e i v e d==TRUE )
i f ( TXSCI pending ==TRUE)
{
do
{
/ / Send s i n g l e c h a r a c t e r :
S c i a R e g s . SCITXBUF= m e s s a g e t x s c i [ i n d e x + + ] ;
/ / W a i t f o r TX e m p t y :
w h i l e ( S c i a R e g s . SCICTL2 . b i t . TXEMPTY == 0 ) ;
}
/ / W a i t f o r TX e m p t y :
w h i l e ( m e s s a g e t x s c i [ i n d e x ] ! = \0 ) ;
index =0;
TXSCI pending =FALSE ;
} / / i f ( T XS C I pe n d i ng==TRUE )
} / / while (1)
D. Fuction expansions
The functions expanded below are all declared in the initial
part of the program.
First, the serial interrupt function is defined. The bit baud
rates are defined as required (115200 bps).
void SCIA init ( )
{
/ / 1 s t o p b i t , No l o o p b a c k No p a r i t y , 8 c h a r b i t s ,
/ / a s y n c mode , i d l e l i n e p r o t o c o l :
S c i a R e g s . SCICCR . a l l =0 x0007 ;
/ / E n a b l e TX , RX , i n t e r n a l SCICLK ,
/ / D i s a b l e RX ERR , SLEEP , TXWAKE:
S c i a R e g s . SCICTL1 . a l l =0 x0003 ;
S c i a R e g s . SCIHBAUD
= 40 >> 8 ;
/ / Highbyte
S c i a R e g s . SCILBAUD
= 40 & 0 x00FF ;
/ / Lowbyte
/ / E n a b l e SCI A RxISR :
S c i a R e g s . SCICTL2 . b i t . RXBKINTENA = 1 ;
/ / R e l i n q u i s h SCI f r o m R e s e t :
S c i a R e g s . SCICTL1 . a l l = 0 x0023 ;
}
/ * Timer0 I n t e r r u p t F u n c t i o n * /
i n t e r r u p t void c p u t i m e r 0 i s r ( void )
{
/ / Receiving the required data i n t o the v a r i a b l e
declared :
app = A d c M i r r o r . ADCRESULT0 ;
ECanaMboxes .MBOX8.MDH. b y t e . BYTE5 = ( app & 0 x00FF ) ;
/ / I f prev . transmission completed :
i f ( CanMbox8Quit == TRUE && c o u n t e r t i m e r == 1 )
{
/ / t r a n s m i t a new one
CanMbox8Quit=FALSE ;
ECanaShadow . CANTRS . a l l = 0 ;
/ / S e t TRS f o r m a i l b o x u n d e r t e s t :
ECanaShadow . CANTRS . b i t . TRS8 = 1 ;
ECanaRegs . CANTRS . a l l = ECanaShadow . CANTRS . a l l ;
/ / R e s e t c o u n t e r o f 20 ms :
counter timer = 0;
}
/ / A c k n o w l e d g e t h e PIE g r o u p 1 :
P i e C t r l R e g s . PIEACK . b i t . ACK1 = 1 ;
/ / Increment of counter :
c o u n t e r t i m e r ++;
}
Here, the program checks for the number of the mailbox that
is raising the CAN interrupt and makes a transmission/reception based on its number. In this example we use the mailbox
2 (for reception) and mailbox 6 (for transmission). In mailbox
6, transmission is done by charging the values of the ADC to
the ECAN mailbox register, making sure that the number of
bits are the same as that in the given data. This message is
placed in the CAN bus and is read by mailbox 2. Once the
message is read by the intended mailbox, it saves the value
to an array. We then perform an equation to this array with
the correct bits and save the result in a new variable which is
used for serial transmission.
A similar structure is used for transmitting and receiving
data of the other mailboxes.
i n t e r r u p t void e c a n 0 i n t A i s r ( void )
{
unsigned i n t mailbox nr ;
/ / M a i l b o x t h a t s e t GMIF0I0EN :
m a i l b o x n r = ECanaRegs . CANGIF0 . b i t . MIV0 ;
/ / Reception
i f ( m a i l b o x n r == 2 )
{
m e s s a g e [ 0 ] = ECanaMboxes .MBOX2.MDL.
read message
m e s s a g e [ 1 ] = ECanaMboxes .MBOX2.MDL.
m e s s a g e [ 2 ] = ECanaMboxes .MBOX2.MDL.
m e s s a g e [ 3 ] = ECanaMboxes .MBOX2.MDL.
m e s s a g e [ 4 ] = ECanaMboxes .MBOX2.MDH.
m e s s a g e [ 5 ] = ECanaMboxes .MBOX2.MDH.
m e s s a g e [ 6 ] = ECanaMboxes .MBOX2.MDH.
m e s s a g e [ 7 ] = ECanaMboxes .MBOX2.MDH.
Fig. 2.
b y t e . BYTE0 ;
byte
byte
byte
byte
byte
byte
byte
//
. BYTE1 ;
. BYTE2 ;
. BYTE3 ;
. BYTE4 ;
. BYTE5 ;
. BYTE6 ;
. BYTE7 ;
/ / C l e a r t h e s t a t u s f l a g RMP1
/ / and p r e p a r e MBX1 f o r n e x t r e c e i v e
ECanaRegs .CANRMP. b i t . RMP2 = 1 ;
stw ang RS232 = ( ( ( u n s i g n e d i n t ) m e s s a g e [ 1 ] & 0 x7F )
* 256 + ( u n s i g n e d i n t ) m e s s a g e [ 0 ] ) * 0 . 0 4 3 7 5 ;
i f ( ( m e s s a g e [ 1 ] & 0 x80 ) == 1 2 8 )
{
s t w a n g d i r = ;
}
i f ( ( m e s s a g e [ 1 ] & 0 x80 ) == 0 )
{
/ / C o n f i g u r e CAN