int main(int argc, char **argv) { if (!bcm2835_init()) { printf("bcm2835 init failed\n"); return 1; } // Set the output pin to Alt Fun 5, to allow PWM channel 0 to be output there bcm2835_gpio_fsel(PIN0, BCM2835_GPIO_FSEL_ALT5); bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_2); bcm2835_pwm_set_mode(PWM0, 1, 1); bcm2835_pwm_set_range(PWM0, RANGE); bcm2835_gpio_fsel(PIN1, BCM2835_GPIO_FSEL_ALT5); bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_4); bcm2835_pwm_set_mode(PWM1, 1, 1); bcm2835_pwm_set_range(PWM1, RANGE*2); bcm2835_pwm_set_data(PWM0, 1); bcm2835_pwm_set_data(PWM1, 1); while(1) { } bcm2835_close(); return 0; }
int main(){ bcm2835_init(); // Setup PWM bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_16); bcm2835_pwm_set_mode(PWM_CHANNEL_0,1,1); bcm2835_pwm_set_mode(PWM_CHANNEL_1,1,1); bcm2835_pwm_set_range(PWM_CHANNEL_0, PWM_RANGE); bcm2835_pwm_set_range(PWM_CHANNEL_1, PWM_RANGE); setup(); while(1) { loop(); } return (0); }
int main(int argc, char **argv) { if (!bcm2835_init()) return 1; bcm2835_gpio_fsel(PIN0, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(PIN1, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_set_pud(PIN0, BCM2835_GPIO_PUD_UP); bcm2835_gpio_set_pud(PIN1, BCM2835_GPIO_PUD_UP); bcm2835_gpio_fsel(POUT1, BCM2835_GPIO_FSEL_ALT5); bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_256); bcm2835_pwm_set_mode(0, 1, 1); bcm2835_pwm_set_range(0, 1024); int data=0; while (1) { uint8_t in0 = bcm2835_gpio_lev(PIN0); uint8_t in1 = bcm2835_gpio_lev(PIN1); if (in1 == 0) break; data ++; if(data > 1023) data=0; bcm2835_pwm_set_data(0, data); bcm2835_delay(2); } bcm2835_pwm_set_data(0, 0); bcm2835_gpio_fsel(POUT1, BCM2835_GPIO_FSEL_INPT); bcm2835_close(); return 0; }
//============================================================================== // TODO add error messages void PWM_init() { // setting PWM_PIN as pwm from channel 0 in markspace mode with range = RANGE bcm2835_gpio_fsel(PWM_PIN, BCM2835_GPIO_FSEL_ALT5); // ALT5 is pwm mode bcm2835_pwm_set_clock( BCM2835_PWM_CLOCK_DIVIDER_16); // pwm freq = 19.2 / 16 MHz bcm2835_pwm_set_mode(PWM_CHANNEL, 1, 1); // markspace mode bcm2835_pwm_set_range(PWM_CHANNEL, RANGE); bcm2835_gpio_fsel(OE_SHIFTER, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_set_pud( OE_SHIFTER, BCM2835_GPIO_PUD_DOWN); // pull-down for output enable of logic shifters bcm2835_gpio_fsel(MOTOR_D3, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_set_pud(MOTOR_D3, BCM2835_GPIO_PUD_DOWN); // pull-down for motor enable bcm2835_gpio_fsel(PA0, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_set_pud(PA0, BCM2835_GPIO_PUD_UP); bcm2835_gpio_write(PA0, HIGH); bcm2835_gpio_write(OE_SHIFTER, HIGH); bcm2835_gpio_write(MOTOR_D3, LOW); }
int main(int argc, char *argv[]) { bcm2835_init(); if(comparse(argc, argv) == EXIT_FAILURE) return 0; // setting PWM_PIN as pwm from channel 0 in markspace mode with range = RANGE bcm2835_gpio_fsel(PWM_PIN, BCM2835_GPIO_FSEL_ALT5); //ALT5 is pwm mode bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_16); // pwm freq = 19.2 / 16 MHz bcm2835_pwm_set_mode(PWM_CHANNEL, 1, 1); // markspace mode bcm2835_pwm_set_range(PWM_CHANNEL, RANGE); bcm2835_gpio_fsel(OE_SHIFTER, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_set_pud(OE_SHIFTER, BCM2835_GPIO_PUD_DOWN); //pull-down for output enable of logic shifters bcm2835_gpio_fsel(MOTOR_D3, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_set_pud(MOTOR_D3, BCM2835_GPIO_PUD_DOWN); //pull-down for motor enable bcm2835_gpio_fsel(PA0, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_set_pud(PA0, BCM2835_GPIO_PUD_UP); bcm2835_gpio_write(PA0, HIGH); bcm2835_gpio_write(OE_SHIFTER, HIGH); bcm2835_gpio_write(MOTOR_D3, LOW); // creating and running threads pthread_t th1, th2, th3, th4, th5, th6, th7; pthread_create(&th1, NULL, (void*)encoder_time_thread, NULL); pthread_create(&th2, NULL, (void*)magnet_time_thread, NULL); pthread_create(&th3, NULL, (void*)encoder_thread, NULL); pthread_create(&th4, NULL, (void*)magnet_thread, NULL); pthread_create(&th5, NULL, (void*)energy_time_thread, NULL); pthread_create(&th6, NULL, (void*)calculate_energy, NULL); pthread_create(&th7, NULL, (void*)calculate_I_ref, NULL); bcm2835_delay(100); // delay to make sure that all threads are initialised and iC-MU is conofigured printf("\nPress enter to start the motor."); getchar(); bcm2835_gpio_write(MOTOR_D3, HIGH); start = 1; printf("Started.\n"); printf("\nPress enter to stop the motor.\n"); getchar(); bcm2835_gpio_write(MOTOR_D3, LOW); bcm2835_spi_end(); bcm2835_close(); return 0; }
/* * Function: pwm_test * Description: be used to test PWM related functions */ void pwm_test(void) { printf("--------------->Start Test PWM<--------------\n"); bcm2835_gpio_fsel(PWM_PIN, BCM2835_GPIO_FSEL_ALT5); //set Phy12 to pwm function /*pwm clock = 24M/240*/ bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_16); /*pwm channel mode: cycle mode OR pulse mode*/ bcm2835_pwm_set_mode(PWM_CHANNEL, 1, 1); /*the pwm cycle = pwm clock * PWM_CYCLE */ bcm2835_pwm_set_range(PWM_CHANNEL, PWM_CYCLE); //enable pwm //active state: High level //enable clock for PWM1 //set the pwm duty bcm2835_pwm_set_data(PWM_CHANNEL, PWM_DUTY); printf("--------------->Test Over Of PWM<--------------\n"); }
void setupPins() { if (!bcm2835_init()) std::cout<<"initialication error"; bcm2835_gpio_fsel(controlPin, BCM2835_GPIO_FSEL_ALT5); bcm2835_gpio_fsel(in1, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(in2, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(in3, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(in4, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(turnPin, BCM2835_GPIO_FSEL_OUTP); bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_16); bcm2835_pwm_set_mode(pwmChannel, 1, 1); bcm2835_pwm_set_range(pwmChannel, range); //bcm2835_gpio_write(in3, LOW); //bcm2835_gpio_write(in4, LOW); bcm2835_gpio_write(turnPin, LOW); }