Home > other >  Arduino CAN test - shields PWM output and send it a message
Arduino CAN test - shields PWM output and send it a message

Time:09-18

Recent EPS test equipment in use, a torque sensor output signal is 5 KHZ ~ 15 KHZ, the corresponding torque is - 100 nm to 100 nm, plans to use the Arduino gathering this signal, because the Vector of the CANoe, hope to send the to signal to the CAN bus, and then displayed on the CANoe,
To test and bought a CAN - shields board, compatible UNO, tests first not even the torque sensor, with UNO analog output PWM, first because the arduino anlogWrite () sent by the PWM frequency CAN not change, so the Internet under the method of manual configuration, with the fast PWM mode is to modify the limit,

Hardware: the Arduino UNO R3. CAN - shields V2.0;

1, modify the ceiling fast PWM output
Official reference Aduino PWM files and ATMEL328P datasheet
Modify limit PWM quickly if use TIMER2 timer, the corresponding output pin is 3,11,3 corresponding output is OCB, OCA output corresponding to the second, the mode of OCB as output, OCA only as output PWM duty ratio fixed at 50%, with 3 when the output, with 12 when input,
 
Int pin1=3;//the output; 11 feet OCCRA control, OCCRB control 3 feet
Int pin2=12;//input

And then the four registers TCCR2A TCCR2B, OCCR2A, OCCR2B set, preassigned frequency choose 1/8, is set in the TCCR2B CS21, so the output frequency of the just in 5 KHZ ~ 15 KHZ, other specific setting methods copy in the code comments,

2, use shields V2.0 send CAN analog torque message bus
CAN shields corresponding library files, MS - CAN, it is good to call directly, in order to facilitate subsequent CAN analysis equipment (PCAN, Vector CANoe, etc.) read a newspaper article, the torque signal into 0 ~ 10000 number, resolution, 0.01 points in two byte, direction with a direction signal, and then is 0 ~ 255 counter and a checksum,

3, the test effect
Use PCAN read CAN shields hair message, use the serial port monitor reading frequency and torque signal, compare, the output is normal,




/* 
Simulation of torque sensor output frequency signal and the signal into a suitable data sent to the CAN BUS

Definition - stitch: 3 feet output frequency pulse, 12 feet acquisition frequency pulse: 10 feet SPI_CS
- signal conversion: torque sensor 5 KHZ ~ 15 hz corresponding - 100 nm to 100 nm; The torque signal
Converted to 0.01 resolution, unsigned types, namely torque CAN signal 0 ~ 10000 (0 x0 ~ 0 x2710),
Direction with the direction signal, & gt;=0 to 1, <0 to 0,
- frequency signal simulation method: fast PWM, modify the clock count limit
B (3 feet) output duty cycle control signal, A fixed duty cycle (50%) and the output frequency is half that of B

*/


# include & lt; Df_can. H>
# include & lt; SPI. H>
Const int SPI_CS_PIN=10;//Set the CS pin
MCPCAN CAN (SPI_CS_PIN);

Int pin1=3;//the output; 11 feet OCCRA control, OCCRB control 3 feet
Int pin2=12;//input

Double duration;//high level duration us
Double lowlevel;//low level duration us
Double OutputDuty;//output duty cycle
Double period;//pulse cycle
Double freq.//pulse frequency
Double torque;//signed torque values
Int torque_send;//torque take absolute value
Int torque_direction;//torque direction
Char data_to_PC;
Byte count=0;//the message counter
Byte checksum=0;//the message checksum
Byte torque_send_high=0;//high 8 bytes
Byte torque_send_low=0;//torque low 8 bytes

Void setup ()
{
//put your setup code here, to run once:
PinMode (pin1, the OUTPUT);//3 feet output
PinMode (pin2, INPUT);//12 feet enter
//the following Settings for Timer2 see ATMEL328P datasheet
TCCR2A=_BV (COM2A0) | _BV (COM2B1) | _BV (WGM21) | _BV (WGM20);//COM2A0 output in A to TOP output reverse phase; B no anti COM2B1 outputs; WGM is set to 111, namely fast PWM control count limit
TCCR2B=_BV (WGM22) | _BV (CS21);//the preassigned frequency of 010:8 points frequency
/*
TCCR2A
Bit 7 6 5 4 3 2 1 0
COM2A1 COM2A0 COM2B1 COM2B0 - WGM21 WGM20

Compare the Output Mode, Fast PWM Mode:
COM2A1 COM2A0
0 0 Nomal port operation, OC2A disconnected
WGM22=0 0 1: Normal port operation, OC0A disconnected; WGM22=1: Toggle OC2A on compare match
1 0 the Clear OC2A on compare the match, the set OC2A at BOTTOM (non - inverting mode)
1 1 Set OC2A on compare the match, the clear OC2A at BOTTOM (inverting mode)
COM2B1 COM2B0
0 0 Normal port operation, OC2B disconnected
0 1 Reserved
1 0 the Clear OC2B on compare the match, the set OC2B at BOTTOM (non - inverting mode)
1 1 Set OC2B on compare the match, the clear OC2B at BOTTOM (inverting mode)

Mode WGM22 WGM21 WGM20 | | the Timer/Counter Mode of Operation | | TOP | | Update of OCRx at | | TOV Flag Set on
Normal 0 0 0 0 0 XFF Immediate MAX
1 0 0 1 PWM, phase correct 0 XFF TOP BOTTOM
2 0 0 1 CTC OCRA Immediate MAX
3 0 1 1 Fast PWM 0 XFF BOTTOM MAX
4 1 0 0 Reserved -- -- --
5 1 0 1 PWM, phase correct OCRA TOP BOTTOM
6 1 1 0 Reserved -- -- --
7 1 1 1 Fast PWM OCRA BOTTOM TOP

TCCR2B
Bit 7 6 5 4 3 2 1 0
FOC2A FOC2B - WGM22 CS22 CS21 CS20
Clock Select Bit Description
CS22 CS21 CS20 Description
0 0 0 No clock source (stopped) the Timer/Counter
0 0 1 clkT2s/(no prescaling)
1 0 0 clkT2s/8 (from prescaler)
0 1 1 clkT2s/32 (from prescaler)
1 0 0 clkT2s/64 (from prescaler)
1 0 1 clkT2s/128 (from prescaler)
1 1 0 clkT2s/256 (from prescaler)
1 1 1 clkT2s/1024 (from prescaler)

*/

OCR2A=249;//modify the clock count limit mode, control the maximum count
OCR2B=50;//control the match point of count

/*
Frequency calculation:
B: 16 m/eight/(249 + 1)=8000 hz
A: 16 m/eight/(249 + 1)/2=4000 hz
Duty ratio calculation:
B: (50 + 1)/(249 + 1)=20.4%
A: 50% of the fixed
*/
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related