本帖最后由 wang0225 于 2011-7-30 12:29 编辑
int main (void)
{
uint8_t test = 250;
uint8_t PWM0_tmp =PWM0;
uint8_t PWM0_Duty_Cycle_tmp = 50;
uint16_t PWM_CNR0_tmp = 0;
uint16_t tmp;
uint32_t i32ConversionData;
uint32_t i32ConversionData_Sum;
uint8_t tmp1;
S_DRVPWM_TIME_DATA_T sPt;
ADC_Status = BUSY;
ADC_Cnt = 2;
Init_System();
Init_Uart();
Init_PWM0();
DrvGPIO_Open(E_GPA,Run_Led, E_IO_OUTPUT); //程序运行指示
DrvGPIO_ClrBit(E_GPA,Run_Led);
printf("\n");
printf("/*==========================\n");
printf("=====助学板test%d 计划======\n",test);
printf("========NUC120========\n");
printf("======程序参考新唐BSP库======\n");
printf("=======2010年04月14日=======\n");
printf("=========PWM实验=========\n");
printf("WM基本配置如下:\n");
printf("WMA 预分频为:0x%x\n",PWMA->R.CP01);
printf("WMA 时钟选择寄存器:0x%x\n",PWMA->CSR.CSR0);
PWM_CNR0_tmp = PWMA->CNR0;
printf("WM 计数器寄存器CNR0: 0x%x\n",PWM_CNR0_tmp);
printf("WM 比较寄存器 CMR0: 0x%x\n", PWMA->CMR0);
printf("请输入1~9调整频率开始测试!\n");
printf("==========================*/\n");
while(1)
{
DrvGPIO_InitFunction(E_FUNC_ADC1); //注:使能 ADC功能前,模拟输入引脚输入类型. 助学板上与电位器相连
DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_OP, 0x02, EXTERNAL_12MHZ, 3);
// 单端输入 单一转换 通道1 外部12M 分频3
tmp1 = ADC_Cnt;
i32ConversionData_Sum = 0;
for (;tmp1>0;tmp1--)
{
DrvADC_StartConvert(); //Set the ADST bit of ADCR to start the A/D conversion action
while(DrvADC_IsConversionDone()==FALSE); // Check whether the conversion action is finished or not
i32ConversionData = DrvADC_GetConversionData(1); //Get the conversion result of the specified ADC channel */
i32ConversionData_Sum += i32ConversionData;
}
i32ConversionData = i32ConversionData_Sum/ADC_Cnt;
i32ConversionData =(i32ConversionData*5000/4096);
// printf("AD采集电压值为%dmV.\n",i32ConversionData);
PWM0_Duty_Cycle= i32ConversionData/50;
// printf("占空比值为%d:100.\n",PWM0_Duty_Cycle);
if (PWM0_tmp!=PWM0)
{
sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE; /*自动重载模式 DRVPWM_ONE_SHOT_MODE为单触发模式*/
sPt.u32Frequency = PWM0; /*PWM 频率 为40KHZ*/
sPt.u8HighPulseRatio = PWM0_Duty_Cycle; /* 高脉冲宽度 : 总脉冲宽度 = 50 : 100 */
sPt.i32Inverter = 0; /*反向关闭*/
DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);
DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1);
DrvPWM_Enable(DRVPWM_TIMER0, 1);
}
if (PWM0_Duty_Cycle_tmp!=PWM0_Duty_Cycle)
{
//更改CMR0 调整PWM0输出占空比
PWM0_Duty_Cycle_tmp = PWM0_Duty_Cycle;
if (0==PWM0_Duty_Cycle)
{
PWMA->CMR0 = 0;
}
else
{
tmp = (PWM_CNR0_tmp+1)*PWM0_Duty_Cycle/100-1;
PWMA->CMR0 = tmp;
}
}
}
}
|