Beruflich Dokumente
Kultur Dokumente
f0 2f0 3f0
Embedded Control Applications I MP9-4
average average
Embedded Control Applications I MP9-5
PORT 7
M
RxD
P7.3
TxD
0V
Embedded Control Applications I MP9-8
(…)
/* main */
} void main(void) {
} File: adc.h
/* declare ADC_Read */
extern unsigned int ADC_Read(unsigned int chan);
/* declare ADC_Init */
extern void ADC_Init(void);
Embedded Control Applications I MP9-10
4 source
code files
(modules)
need to
be
defined:
serial.c,
pwm.c,
adc.c and
DCMot.c
(the main
program)
Embedded Control Applications I MP9-11
#endif /* _SERIAL_H_ */
Embedded Control Applications I MP9-12
#ifndef _PWM_H_
#define _PWM_H_
void pwm_init(void);
void pwm_set(unsigned int off_phase);
#endif /* _PWM_H_ */
Macro PWMPERIOD has been moved into the header file to make it
available to the calling modules
Embedded Control Applications I MP9-13
void pwm_init(void) {
} /* pwm_init */
} /* pwm_set */
Embedded Control Applications I MP9-14
#ifndef _ADC_H_
#define _ADC_H_
#endif /* _ADC_H_ */
} /* adc_init */
} /* adc_read */
Embedded Control Applications I MP9-16
/* say hello... */
blinky(10000);
/* display message */
myPrintf("\r\nDC Motor controller \n\r");
(…)
- The call to function blinky reveals whether the target is running or not;
this is particularly important when working on a new microcontroller
and/or when modifying the settings of the startup module
- The program initializes the serial interface ASC0, the ADC and the
PWM unit; it then sends a welcome message to a terminal on ASC0
Embedded Control Applications I MP9-18
pwm_set((unsigned int)(myVoltage/5*PWMPERIOD));
myVoltage_old = myVoltage;
} /* if */
} /* while */
} /* main */
ADC channel 4 is read and the result compared to the previously read
value; if there is a difference of more than 0.02 V, the duty cycle of
the PWM is changed and a log message is sent to the terminal
Embedded Control Applications I MP9-19
Simulation
allows the
program
structure
to be
checked;
however,
its timing
can only
be
checked
Embedded Control Applications I MP9-20
} /* adc_init */
Note that the ADC has been set up for continuous conversion, i. e. the
ADC is immediately restarted following the completion of a
conversion
Embedded Control Applications I MP9-27
pwm_set((unsigned int)(myVoltage/5*PWMPERIOD));
myVoltage_old = myVoltage;
} /* if */
} /* while */
} /* main */
Embedded Control Applications I MP9-28
} /* adc_init */
Simulation
shows
how the
PEC
operates;
notice the
almost
permanent
availability
of ADC
conversio
Embedded Control Applications I MP9-31
} /* adc_init */