Comparison of the magic cube robot programming machine two serial communication mode

Label: Serial communicationSinglechipPWMMC9S12XS128
576 people read comment(0) Collection Report
Classification:

Because of the serial communication part and cube PC software restoration part has been completed, the next step is how the specific steps of cube (string form) through the serial port to the MC9S12XS128 microcontroller, the debugging process, using two serial communication mode, one is another query. An interrupt mode. First of all, by the way of inquiry, through the cycle, and constantly determine whether there is no receiving data, if the received data, then the microcontroller to send the received data to the computer immediately. The simple implementation of the data received and sent not to perform other operations, the normal transceiver is no problem. But when the PWM program and the traditional communication program together when problems arise, because the PWM program contains duty assignment statements and delay function, when the host computer sends data, if the machine does not perform to the receiving part of the serial number, according to the data received will be incomplete and not receiving to the data.

/ / contains header files
#include "Includes.h" / / total contains header files

This added / / global variable definition
Msg[14] uint8 = "Hello! World!";
      
The main function.
Main void (void)
{
Variable definition used in //0.1 main program
Uint32 mRuncount=0; / / run counter
I uint8;
Uint8 SerialBuff[1]; / / array receiving data
    
//0.2 off total interrupt
DisableInterrupt ();
       
//0.3 chip initialization
MCUInit (FBUS_32M);
    
//0.4 module initialization
Light_Init (Light_Run_PORT, Light_Run, Light_OFF); //RUN indicator light is initialized to dark
SCIInit (0, FBUS_32M, 9600); / / initialize the serial port 0
SCISendN (0,13, MSG); / / send "Hello World!"

 
/ / the main loop
For (;)
{
//1. main loop count to a certain value, so that the Run lamp light, dark state switch
MRuncount++;
If (mRuncount>=50000)
{
MRuncount=0;
Light_Change (Light_Run_PORT, Light_Run); / / the bright lights, dark state switch
}
      
//--------------------------------------------------------------------
//2. main loop waiting to receive the data sent by the PC machine, when the data received from the PC immediately after the return
I = SCIReN (0,1, SerialBuff); / / wait to receive 1 data
If (I = 0) and SCISendN (0,1, SerialBuff); / / send receive data
//--------------------------------------------------------------------
Delay (100000);
} //for_end (end of the main loop)
} //main_end
The program did not use the serial port interrupt, isr.cpp interrupt vector table are the default value, no modification. No delay function Delay (100000) data transceiver is normal, but after adding delay statements, sending time of PC data will have to pay attention to, to be sent when implementing data received, and not to send in the delay time, otherwise, the received data is not complete. Sent the data for the serial port debugging assistant send the contents of the box, each send only received a letter D, the received string is not complete, as follows:


This certainly can not meet the requirements of real-time serial communication, and can not guarantee the accuracy of the transmission, so the back to the serial port to receive the interrupt communication, the main code is as follows:

Main.c file code:

The header file contains / /
#include "Includes.h" / / total header file
  
This added / / global variable definition
Msg[14] uint8 = "Hello! World!";
         

PWM_Init void (void)
{
PWME_PWME3=0x00; / / Disable PWM ban (channel 3)
PWMPRCLK=0x33; / / A=B=32M/8=4M 00110011 clock prescaler register set
PWMSCLA=200; / / SA=A/2/200=10k clock settings
PWMSCLB=200; / / SB=B/2/200 =10k clock set
PWMCLK_PCLK3=1; / / PWM3-----SB clock source selection
PWMPOL_PPOL3=1; / / Duty=High Time polarity set
PWMCAE_CAE3=0; / / set the Left-aligned alignment
PWMCTL=0x00; / / no concatenation control register set
PWMPER3=200; / / Frequency=SB/200=50HZ cycle register set
PWMDTY3=15; / / 1.5ms corresponding to the duty ratio of the duty cycle register set
PWME_PWME3=1; / / Enable PWM enable
}
Main void ()
{
         
Off / / interrupt -------0.1
DisableInterrupt ();
The chip initialization / / -----0.2
MCUInit (FBUS_32M);
/ / -----0.3 initialization module
SCIInit (0, FBUS_32M, 9600); / / initialize the serial port 0
SCISendN (0,13, MSG); / / send "Hello! World!"
       
          
Open / interrupt ------0.4
EnableSCIReInt0; / / open SCI0 receive interrupt
EnableInterrupt (open); / / total interrupt
 
       
       
PWM_Init (); //PWM initialization 1.5ms corresponding to the duty cycle -----90
Delay (1000000);
For (;)
{
//0.5ms; ------0 corresponds to the duty cycle of PWMDTY3=5
Delay (1000000);
//2.5ms; ----180 corresponds to the duty cycle of PWMDTY3=25
Delay (1000000);
} //for_end (end of the main loop)
} //main_end
Interrupt service subroutine code ():
//--------------------------------------------------------------------------*
File name: isr.c * / /
Description: / / interrupt handling function, the file contains: * *
The interrupt handler * / / isr_default:
//--------------------------------------------------------------------------*

/ / header file contains macros, and district

The header file contains / /
#include "Includes.h" / / total contains header files
    
CODE_SEG __NEAR_SEG NON_BANKED #pragma

/ / interrupt service program area

/ / undefined interrupt handling function, this function cannot delete the default.
Void isr_default __interrupt (void)
{
DisableInterrupt ();
EnableInterrupt ();
}

/ / accept the serial interrupt
Void SCI0_Recv __interrupt (void)
{
I uint8;
Uint8 SerialBuff[1]; / / array receiving data
        
(DisableInterrupt); / / total ban interruption
        
I = SCIReN (0,1, SerialBuff); / / wait to receive 1 data
If (I = 0) and SCISendN (0,1, SerialBuff); / / send receive data
        
EnableInterrupt (open); / / total interrupt
}
    
Copy the default / interrupt handler (isr_default) as a template, and get the appropriate interrupt program
    
CODE_SEG DEFAULT #pragma

/ / interrupt vector table corresponding area

/ / interrupt handling type definition
Void typedef (tIsrFunc *near) (void) ();
    
/ / interrupt vector table, if you need to define other interrupt function, please modify the corresponding item in the table below.
TIsrFunc _InterruptVectorTable[] @0xFF10 const = {
Name No. Address Pri Name / / ISR
Isr_default, 0xFF10 - ivVsi / 0x08
Isr_default, 0xFF12 - ivVsyscall / 0x09
Isr_default / / 0x0A 0xFF14 1 ivVReserved118
Isr_default / / 0x0B 0xFF16 1 ivVReserved117
Isr_default / / 0x0C 0xFF18 1 ivVReserved116
Isr_default / / 0x0D 0xFF1A 1 ivVReserved115
Isr_default / / 0x0E 0xFF1C 1 ivVReserved114
Isr_default / / 0x0F 0xFF1E 1 ivVReserved113
Isr_default / / 0x10 0xFF20 1 ivVReserved112
Isr_default / / 0X11 0xFF22 1 ivVReserved111
Isr_default / / 0x12 0xFF24 1 ivVReserved110
Isr_default / / 0x13 0xFF26 1 ivVReserved109
Isr_default / / 0x14 0xFF28 1 ivVReserved108
Isr_default / / 0x15 0xFF2A 1 ivVReserved107
Isr_default / / 0x16 0xFF2C 1 ivVReserved106
Isr_default / / 0x17 0xFF2E 1 ivVReserved105
Isr_default / / 0x18 0xFF30 1 ivVReserved104
Isr_default / / 0x19 0xFF32 1 ivVReserved103
Isr_default / / 0x1A 0xFF34 1 ivVReserved102
Isr_default / / 0x1B 0xFF36 1 ivVReserved101
Isr_default / / 0x1C 0xFF38 1 ivVReserved100
Isr_default / / 0x1D 0xFF3A 1 ivVReserved99
Isr_default / / 0x1E 0xFF3C 1 ivVReserved98
Isr_default / / 0x1F 0xFF3E 1 ivVatd0compare
Isr_default / / 0x20 0xFF40 1 ivVReserved96
Isr_default / / 0x21 0xFF42 1 ivVReserved95
Isr_default / / 0x22 0xFF44 1 ivVReserved94
Isr_default / / 0x23 0xFF46 1 ivVReserved93
Isr_default / / 0x24 0xFF48 1 ivVReserved92
Isr_default / / 0x25 0xFF4A 1 ivVReserved91
Isr_default / / 0x26 0xFF4C 1 ivVReserved90
Isr_default / / 0x27 0xFF4E 1 ivVReserved89
Isr_default / / 0x28 0xFF50 1 ivVReserved88
Isr_default / / 0x29 0xFF52 1 ivVReserved87
Isr_default / / 0x2A 0xFF54 1 ivVReserved86
Isr_default / / 0x2B 0xFF56 1 ivVReserved85
Isr_default / / 0x2C 0xFF58 1 ivVReserved84
Isr_default / / 0x2D 0xFF5A 1 ivVReserved83
Isr_default / / 0x2E 0xFF5C 1 ivVReserved82
Isr_default / / 0x2F 0xFF5E 1 ivVReserved81
Isr_default / / 0x30 0xFF60 1 ivVReserved79
Isr_default / / 0x31 0xFF62 1 ivVReserved78
Isr_default / / 0x32 0xFF64 1 ivVReserved77
Isr_default / / 0x33 0xFF66 1 ivVReserved76
Isr_default / / 0x34 0xFF68 1 ivVReserved75
Isr_default / / 0x35 0xFF6A 1 ivVReserved74
Isr_default / / 0x36 0xFF6C 1 ivVReserved73
Isr_default / / 0x37 0xFF6E 1 ivVReserved72
Isr_default / / 0x38 0xFF70 1 ivVReserved71
Isr_default / / 0x39 0xFF72 1 ivVReserved70
Isr_default / / 0x3A 0xFF74 1 ivVpit3
Isr_default / / 0x3B 0xFF76 1 ivVpit2
Isr_default / / 0x3C 0xFF78 1 ivVpit1
Isr_default / / 0x3D 0xFF7A 1 ivVpit0
Isr_default / / 0x3E 0xFF7C 1 ivVhti
Isr_default / / 0x3F 0xFF7E 1 ivVapi
Isr_default / / 0x40 0xFF80 1 ivVlvi
Isr_default / / 0x41 0xFF82 1 ivVReserved62
Isr_default / / 0x42 0xFF84 1 ivVReserved61
Isr_default / / 0x43 0xFF86 1 ivVReserved60
Isr_default / / 0x44 0xFF88 1 ivVReserved59
Isr_default / / 0x45 0xFF8A 1 ivVReserved58
Isr_default / / 0x46 0xFF8C 1 ivVpwmesdn
Isr_default / / 0x47 0xFF8E 1 ivVportp
Isr_default / / 0x48 0xFF90 1 ivVReserved55
Isr_default / / 0x49 0xFF92 1 ivVReserved54
Isr_default / / 0x4A 0xFF94 1 ivVReserved53
Isr_default / / 0x4B 0xFF96 1 ivVReserved52
Isr_default / / 0x4C 0xFF98 1 ivVReserved51
Isr_default / / 0x4D 0xFF9A 1 ivVReserved50
Isr_default / / 0x4E 0xFF9C 1 ivVReserved49
Isr_default / / 0x4F 0xFF9E 1 ivVReserved48
Isr_default / / 0x50 0xFFA0 1 ivVReserved47
Isr_default / / 0x51 0xFFA2 1 ivVReserved46
Isr_default / / 0x52 0xFFA4 1 ivVReserved45
Isr_default / / 0x53 0xFFA6 1 ivVReserved44
Isr_default / / 0x54 0xFFA8 1 ivVReserved43
Isr_default / / 0x55 0xFFAA 1 ivVReserved42
Isr_default / / 0x56 0xFFAC 1 ivVReserved41
Isr_default / / 0x57 0xFFAE 1 ivVReserved40
Isr_default / / 0x58 0xFFB0 1 ivVcan0tx
Isr_default / / 0x59 0xFFB2 1 ivVcan0rx
Isr_default / / 0x5A 0xFFB4 1 ivVcan0err
Isr_default / / 0x5B 0xFFB6 1 ivVcan0wkup
Isr_default / / 0x5C 0xFFB8 1 ivVflash
Isr_default / / 0x5D 0xFFBA 1 ivVflashfd
Isr_default / / 0x5E 0xFFBC 1 ivVReserved33
Isr_default / / 0x5F 0xFFBE 1 ivVReserved32
Isr_default / / 0x60 0xFFC0 1 ivVReserved31
Isr_default / / 0x61 0xFFC2 1 ivVReserved30
Isr_default / / 0x62 0xFFC4 1 ivVcrgscm
Isr_default / / 0x63 0xFFC6 1 ivVcrgplllck
Isr_default / / 0x64 0xFFC8 1 ivVReserved27
Isr_default / / 0x65 0xFFCA 1 ivVReserved26
Isr_default / / 0x66 0xFFCC 1 ivVporth
Isr_default / / 0x67 0xFFCE 1 ivVportj
Isr_default / / 0x68 0xFFD0 1 ivVReserved23
Isr_default / / 0x69 0xFFD2 1 ivVatd0
Isr_default / / 0x6A 0xFFD4 1 ivVsci1
SCI0_Recv / / 0x6B 0xFFD6 1 ivVsci0
Isr_default / / 0x6C 0xFFD8 1 ivVspi0
Isr_default / / 0x6D 0xFFDA 1 ivVtimpaie
Isr_default / / 0x6E 0xFFDC 1 ivVtimpaaovf
Isr_default / / 0x6F 0xFFDE 1 ivVtimovf
Isr_default / / 0x70 0xFFE0 1 ivVtimch7
Isr_default / / 0x71 0xFFE2 1 ivVtimch6
Isr_default / / 0x72 0xFFE4 1 ivVtimch5
Isr_default / / 0x73 0xFFE6 1 ivVtimch4
Isr_default / / 0x74 0xFFE8 1 ivVtimch3
Isr_default / / 0x75 0xFFEA 1 ivVtimch2
Isr_default / / 0x76 0xFFEC 1 ivVtimch1
Isr_default / / 0x77 0xFFEE 1 ivVtimch0
Isr_default / / 0x78 0xFFF0 1 ivVrti
Isr_default / / 0x79 0xFFF2 1 ivVirq
Isr_default, 0xFFF4 - ivVxirq / 0x7A
Isr_default, 0xFFF6 - ivVswi / 0x7B
Isr_default / / 0x7C 0xFFF8 - ivVtrap
};

    
Note: the interrupt service routine is implemented in the isr.c file, and the corresponding serial port receive interrupt vector table is modified.

Through the experimental test, it is proved that the PWM program and the serial communication between the two ways can not affect each other, so as to overcome the defects of the serial communication mode. The results are as follows:


top
Zero
step on
Zero

Reference knowledge base

Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit:115165 times
    • Integral:Three thousand five hundred and eighty-nine
    • Grade:
    • Rank:6757th name
    • Original:215
    • Reproduced:214
    • Translation:7
    • Comments:18
    Latest comments