int main(int argc, char **argv) {
	setvbuf(stdout, NULL, _IONBF, 0); // needed to print to the command line

	int x;
	int a;

	// initialise the servo pi on I2C address 0x40 with the Output Enable pin enabled.
	// Check the returned value to ensure the Servo Pi initialised correctly

	a = servopi_init(0x40, 1);
	if (a != 0) {
		if (a == 1) {
			printf("Error enabling GPIO Pin");
		}
		if (a == 2) {
			printf("Error setting GPIO Pin direction");
		}
		return (0);
	}

	//Set PWM frequency to 60 Hz and enable the output
	set_pwm_freq(60, 0x40);
	output_enable();

	while (1) {
		set_pwm(1, 0, servoMin, 0x40); // set the pwm width to servoMin
		usleep(500000); // sleep 0.5 seconds
		set_pwm(1, 0, servoMed, 0x40); // set the pwm width to servoMed
		usleep(500000); // sleep 0.5 seconds
		set_pwm(1, 0, servoMax, 0x40); // set the pwm width to servoMax
		usleep(500000); // sleep 0.5 seconds
	}

	return (0);
}
int main(int argc, char **argv) {
	setvbuf(stdout, NULL, _IONBF, 0); // needed to print to the command line

	int x;
	int a;

	// initialise the servo pi on I2C address 0x40 with the Output Enable pin enabled.
	// Check the returned value to ensure the Servo Pi initialised correctly

	a = servopi_init(0x40, 1);
	if (a != 0){
		if (a == 1){
			printf("Error enabling GPIO Pin");
		}
		if (a == 2){
			printf("Error setting GPIO Pin direction");
		}
		return (0);
	}

	//Set PWM frequency to 1 Khz and enable the output
	set_pwm_freq(1000, 0x40);
	output_enable();



	while (1) {
		for (x = 1; x <= 4095; x = x + 5) {
			set_pwm(1, 0, x, 0x40); // set the pwm width to x
		}

		for (x = 4095; x >= 0; x = x - 5) {
			set_pwm(1, 0, x, 0x40); // set the pwm width to x
		}

	}

	return (0);
}
示例#3
0
static int __init pwm_start(void) {

	int ret = 0;
  	struct clk *timer_fclk;
	uint32_t gt_rate;


	printk(KERN_INFO "Loading PWM Module... \n");

	// request any timer
	timer_ptr = omap_dm_timer_request();
	if(timer_ptr == NULL){
		// no timers available
		printk("pwm module: No more gp timers available, bailing out\n");
		return -1;
	}

	// set the clock source to the system clock
	ret = omap_dm_timer_set_source(timer_ptr, OMAP_TIMER_SRC_SYS_CLK);
	if(ret) {
		printk("pwm module: could not set source\n");
		return -1;
	}

	// set prescalar to 1:1
	omap_dm_timer_set_prescaler(timer_ptr, 0);

	// figure out what IRQ our timer triggers
	timer_irq = omap_dm_timer_get_irq(timer_ptr);

	// install our IRQ handler for our timer
	ret = request_irq(timer_irq, timer_irq_handler, IRQF_DISABLED | IRQF_TIMER , "pwm", timer_irq_handler);
	if(ret){
		printk("pwm module: request_irq failed (on irq %d), bailing out\n", timer_irq);
		return ret;
	}

	// get clock rate in Hz and add it to struct
	timer_fclk = omap_dm_timer_get_fclk(timer_ptr);
	gt_rate = clk_get_rate(timer_fclk);
	pwm_data_ptr.timer_rate = gt_rate;

	// set preload, and autoreload
	// we set it to a default of 1kHz
	set_pwm_freq(1000);

	// setup timer to trigger IRQ on the overflow
	omap_dm_timer_set_int_enable(timer_ptr, OMAP_TIMER_INT_OVERFLOW);
	
	// start the timer
	omap_dm_timer_start(timer_ptr);

	// done!
	printk("pwm module: GP Timer initialized and started (%lu Hz, IRQ %d)\n", (long unsigned)gt_rate, timer_irq);


	
	// setup a GPIO
	pwm_setup_pin(38);
	
	pwm_data_ptr.pin = 38;

	set_pwm_dutycycle(1,150);
	

	// return success
	return 0;
}