实验程序很简单,就如同上面大神说的一样,用AD采样电位器的电压值,来作为PWM的脉冲宽度值(占空比值)。
光盘里的程序是现成的PWM控制LED亮度,只不过少了给AD采样,将之前的程序加进去,稍作修改就可以了:
void tpmInit(void)
{
SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTD_MASK; /* 打开GPIO时钟 */
SIM_SCGC6 |= SIM_SCGC6_TPM0_MASK | SIM_SCGC6_TPM2_MASK; /* 打开TPM时钟 */
SIM_SOPT2 |= SIM_SOPT2_PLLFLLSEL_MASK | SIM_SOPT2_TPMSRC(1); /* 打开时钟门 */
PORTB_PCR18 = ( 0 | PORT_PCR_MUX(3)); /* TPM2_CH0 enable on PTB18 */
PORTB_PCR19 = ( 0 | PORT_PCR_MUX(3)); /* TPM2_CH1 enable on PTB19 */
PORTD_PCR1 = ( 0 | PORT_PCR_MUX(4)); /* TPM0_CH1 enable on PTD1 */
TPM0_MOD = TPM_MODULE; /* 1999 / 50MHz = 40uS PWM */
TPM0_C1SC = TPM_Cn_MODE; /* No Interrupts; High True
pulses on Edge Aligned PWM */
TPM0_C1V = TPM_INIT_VAL; /* 90% pulse width */
TPM2_MOD = TPM_MODULE; /* 1999 / 50MHz = 40uS PWM */
TPM2_C0SC = TPM_Cn_MODE; /* No Interrupts; Low True
pulses on Edge Aligned PWM */
TPM2_C0V = TPM_INIT_VAL; /* 90% pulse width */
TPM2_C1SC = TPM_Cn_MODE; /* No Interrupts; Low True
pulses on Edge Aligned PWM */
TPM2_C1V = TPM_INIT_VAL; /* 90% pulse width */
TPM2_SC = TPM_SC_CMOD(1) | TPM_SC_PS(0); /* Edge Aligned PWM running from
BUSCLK / 1 */
TPM0_SC = TPM_SC_CMOD(1) | TPM_SC_PS(0); /* Edge Aligned PWM running from
BUSCLK / 1 */
}
TMP初始化:
|