who can explain the following example code,pls

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

who can explain the following example code,pls

3,715 次查看
wxx_rui
Contributor I
i learn and read the example program. in 3_bldc_quadrature_encoder.mcp, events.c
the following code, i dont understand.
 
extern const pwm_sChannelControl bldcCommutationTableComp[8] = {
           { MC_PWM_ALL_SIGNALS, PWM_ZERO_MASK },
           { (MC_PWM_SIGNAL_3 | MC_PWM_SIGNAL_2), (PWM_SWAP_1 | PWM_SWAP_0) },
     { (MC_PWM_SIGNAL_1 | MC_PWM_SIGNAL_0), (PWM_SWAP_2 | PWM_SWAP_0) },
     { (MC_PWM_SIGNAL_5 | MC_PWM_SIGNAL_4), (PWM_SWAP_0) },
     { (MC_PWM_SIGNAL_5 | MC_PWM_SIGNAL_4), (PWM_SWAP_2 | PWM_SWAP_1) },
     { (MC_PWM_SIGNAL_1 | MC_PWM_SIGNAL_0), (PWM_SWAP_1) },
     { (MC_PWM_SIGNAL_3 | MC_PWM_SIGNAL_2), (PWM_SWAP_2) },
      { MC_PWM_ALL_SIGNALS, PWM_ZERO_MASK }
      };
who can explain it for me with details. in the up codes , these variables represent what meaning, which one swap which?
Read the codes make me feel faint.
标签 (1)
0 项奖励
回复
2 回复数

1,100 次查看
SteveRussell
Contributor III
Understanding a statement like this is a common difficulty for all C users.  One of the big secrets is that each piece is simple, and it can be understood one piece at a time.
 
Part of the reason it makes you feel faint, is that the way it is layed out on the page isn't very helpful.
 
To make it a little simpler to understand the structure, lets make that clear by formatting:
 
extern  const  pwm_sChannelControl     bldcCommutationTableComp[ 8 ] =
{
  {
     MC_PWM_ALL_SIGNALS,
     PWM_ZERO_MASK
  },
  {
     ( MC_PWM_SIGNAL_3 |  MC_PWM_SIGNAL_2 ),
     ( PWM_SWAP_1 | PWM_SWAP_0 )
  },
  {
      ( MC_PWM_SIGNAL_1 | MC_PWM_SIGNAL_0 ),
     ( PWM_SWAP_2 | PWM_SWAP_0 )
  },
  {
     (MC_PWM_SIGNAL_5 | MC_PWM_SIGNAL_4),
     (PWM_SWAP_0)
  },
  {
     (MC_PWM_SIGNAL_5 | MC_PWM_SIGNAL_4),
     (PWM_SWAP_2 | PWM_SWAP_1)
  },
   {
     (MC_PWM_SIGNAL_1 | MC_PWM_SIGNAL_0),
     (PWM_SWAP_1)
  },
  {
     (MC_PWM_SIGNAL_3 | MC_PWM_SIGNAL_2),
    (PWM_SWAP_2)
  },
  {
     MC_PWM_ALL_SIGNALS,
     PWM_ZERO_MASK
  }
};
Now look at the pieces:
 
extern  const  pwm_sChannelControl     bldcCommutationTableComp[ 8 ] =
{
...
} ;
 
This is a declaration of an externally visible, constant array of 8 elements, with a value.  The array elements are of type "pwm_sChannelControl" which must be defined before this declaration, usually in some header (by convention ".h") file.
 
Each element of the 8 is initialized by the sequence enclosed in curly brackets.  For example element 0  is set up as:
 
   {
     MC_PWM_ALL_SIGNALS,
     PWM_ZERO_MASK
  },
Which suggests that the "pwm_sChannelControl" type is a structure or array with 2 elements. 
 
The symbols "MC_PWM_ALL_SIGNALS" and "PWM_ZERO_MASK" are likely defined in the same header file as the "pwm_sChannelControl" type with preprocessor statements like:
 
#define PWM_ZERO_MASK ( 0x40 )
 
If you look at the documentation for the hardware involved, you will likely find that these defined symbol names are related to the names or functions of the bits and fields in the hardware control registers.
 
Look in the header files involved and search for the uses of the "bldcCommutationTableComp" array for further hints and comments about the contents and usage of this array.
 
This style of dealing with hardware registers in a C program is quite common, and can best be understood with several open editor windows and the hardware documentation.
 
 
MORAL
 
Code is written once, (or maybe 2 or 3 times) but read many more times. 
 
If the formatting helps make the structure clear, it makes understanding the code easier.   Comments that tell the reader useful information that is not clear from the code are also a big help.
 
In this case, it probably would have been helpful to add comments on each element of the array about the purposeof that element in the program structure.
 
Remember this when you write code!
0 项奖励
回复

1,100 次查看
J2MEJediMaster
Specialist I
We'd need a bit more information to be of help. What part are you programing to? What version of Codewarrior are you using? All I can say is that the code is setting or clearing certain bits in the PWM's control registers, and that's about it.

---Tom
0 项奖励
回复