⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e0cc0277e720001e137f9d34181c3283

📁 自己编写的PWM模块
💻
字号:

//Includes
#include "altera_avalon_pwm_regs.h"
#include "altera_avalon_pwm_routines.h"
#include "system.h"
#include <stdio.h>
//Function Protypes
void print_error(unsigned int address, int return_code);
void check_return_code(unsigned int address, int return_code);

int main(void)

{
 
 unsigned int duty_cycle;
 int return_code = ALTERA_AVALON_PWM_OK;
 
 printf("Hello from the PWM test program.\n");
 printf("The starting values in the PWM registers are:\n");
 printf("Period = %u\n", IORD_ALTERA_AVALON_PWM_CLOCK_DIVIDER(Z_PWM_0_BASE) );
 printf("Duty cycle = %u\n", IORD_ALTERA_AVALON_PWM_DUTY_CYCLE(Z_PWM_0_BASE) );

 printf("\nNotice the pulsing LED on the development board.\n");
 
 //Initialize PWM and Check Return Code
 return_code = altera_avalon_pwm_init(Z_PWM_0_BASE, 500000, 1);  
 check_return_code(Z_PWM_0_BASE, return_code);
 
 //Enable PWM and Check Return Code
 return_code = altera_avalon_pwm_enable(Z_PWM_0_BASE); 
 check_return_code(Z_PWM_0_BASE, return_code);

//init duty_cycle with the value written to duty_cycle register during initialization
 duty_cycle = IORD_ALTERA_AVALON_PWM_DUTY_CYCLE(Z_PWM_0_BASE);
 
 
 while(1)
   {
     while(duty_cycle++ < IORD_ALTERA_AVALON_PWM_CLOCK_DIVIDER(Z_PWM_0_BASE))
     {
      	return_code = altera_avalon_pwm_change_duty_cycle(Z_PWM_0_BASE, duty_cycle);
      	check_return_code(Z_PWM_0_BASE, return_code);
     }          
     while(--duty_cycle > 1)
      	altera_avalon_pwm_change_duty_cycle(Z_PWM_0_BASE, duty_cycle);
        check_return_code(Z_PWM_0_BASE, return_code);
   }
return 0;
}

void check_return_code(unsigned int address, int return_code)
{
	if(return_code != ALTERA_AVALON_PWM_OK)
      		print_error(address, return_code);	
}

void print_error(unsigned int address, int return_code)
{
	printf("Program Terminated Due to an error with Avalon PWM located at 0x%x:\n", address);
	switch(return_code)
	{
	case ALTERA_AVALON_PWM_DUTY_CYCLE_GREATER_THAN_CLOCK_CYCLE_ERROR:
		printf("The value in the clock cycle register must be greater than the value in the duty cycle register\n");
		printf("Value in the Clock Divide Register: 0x%x\n", IORD_ALTERA_AVALON_PWM_CLOCK_DIVIDER(address)); 
		printf("Value in the Duty Cycle Register: 0x%x\n", IORD_ALTERA_AVALON_PWM_DUTY_CYCLE(address));
		break;
        case ALTERA_AVALON_PWM_ENABLED_CONFIRMATION_ERROR:
                printf("Unable to confirm that the PWM is enabled\n");
		printf("Value in the Enable Register: 0x%x\n", IORD_ALTERA_AVALON_PWM_ENABLE(address)); 
        	break;
        case ALTERA_AVALON_PWM_DISABLED_CONFIRMATION_ERROR:
        	printf("Unable to confirm that the PWM is disabled\n");
		printf("Value in the Enable Register: 0x%x\n", IORD_ALTERA_AVALON_PWM_ENABLE(address)); 
        	break;
        default:
        	break; 		     
	}

while(1);
}
// end of file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -