C PROGRAMMING CODEVISION AVR

758 views Asked by At

i cant understand some lines of this code please describe me what theise code do: this code is witten in atmelstudio and is a sample code of AVR443 application note of ATMEL

unsigned char *pTemp;
fastTemp.word = ((PIN_HALL & hallMask)>>1);
pTemp = pDrvPattern + fastTemp.word;
PORT_MC = *(pTemp);

as *pDrvPattern is defined as follow:

__regvar __no_init unsigned char *pDrvPattern 

pDrvPattern = drvPatternsCW;
unsigned char drvPatternsCW[] = {
0,    //Stop
// MC_PORT drive config
PDP2_CW, //Phase2
PDP6_CW, //Phase6
PDP1_CW, //Phase1
PDP4_CW, //Phase4
PDP3_CW, //Phase3
PDP5_CW,  //Phase5
// Configuration of Output Compare operation for timer 0
COM0P2_CW, //Phase2
COM0P6_CW, //Phase6
COM0P1_CW, //Phase1
COM0P4_CW, //Phase4
COM0P3_CW, //Phase3
COM0P5_CW,  //Phase5
// Configuration of Output Compare operation for timer 2
   COM2P2_CW, //Phase2
COM2P6_CW, //Phase6
COM2P1_CW, //Phase1
COM2P4_CW, //Phase4
COM2P3_CW, //Phase3
COM2P5_CW  //Phase5
};
__regvar __no_init union _fastTemp{
unsigned int word;
struct{
unsigned char LByte;
unsigned char HByte;  //Hbyte = Zero
};
} fastTemp @12;
#define PDP1_CW  ((0<<UL)|(0<<VL)|(1<<WL)|(0<<UH)|(1<<VH)|(0<<WH))
#define PDP2_CW  ((0<<UL)|(0<<VL)|(1<<WL)|(1<<UH)|(0<<VH)|(0<<WH))
#define PDP3_CW  ((0<<UL)|(1<<VL)|(0<<WL)|(1<<UH)|(0<<VH)|(0<<WH))
#define PDP4_CW  ((0<<UL)|(1<<VL)|(0<<WL)|(0<<UH)|(0<<VH)|(1<<WH))
#define PDP5_CW  ((1<<UL)|(0<<VL)|(0<<WL)|(0<<UH)|(0<<VH)|(1<<WH))
#define PDP6_CW  ((1<<UL)|(0<<VL)|(0<<WL)|(0<<UH)|(1<<VH)|(0<<WH))
#define HALL1 PB1
#define HALL2 PB2
#define HALL3 PB3
#define HALL_MASK  ((1<<HALL1)|(1<<HALL2)|(1<<HALL3))
#define PORT_MC PORTD
1

There are 1 answers

1
Mikel F On BEST ANSWER

The code you have is pulling a data field from a memory buffer. Rather than defining a structure with meaningful fields, the code created a character array, drvPatternsCW[], and then created a pointer to that array with pDrvPattern. Setting pTemp to pDrvPattern + fastTemp.word is the equivalent of pTemp = &pDrvPattern[fastTemp.word]. PORT_MC = *(pTemp); ends up being the same as PORT_MC = pDrvPattern[fastTemp.word].