This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Setting a PWM in a seperat library

I try to execute the PWM settings via the library in an extra .h/.c file to keep my main method as small as possible. Unfortunately the program sticks in the ErrCheck and does not execute the PWM. Unfortunately I can not find the error and there is no error displayed when building and transferring.

Here you can see the funktionen_pwm.c file:

This is the associated funktionen_pwm.h file:

And this is the main.c :

I hope someone can help me with this error.

Greetings

Parents
  • the program sticks in the ErrCheck

    So use the debugger to put a breakpoint there, and see what the error is.

    not as an image!

  • Thank you very much for your quick reply.
    When I run the program with the debugger, it writes at the position
    err_code = app_pwm_init(&motor_pwm, &pwm_config, status_pwm_handler);  
    0x000000 in the ErrCode. If I continue with the debugger it jumps back to the main.c file. If I go further it jumps back to the functions_pwm.c file and writes 0x00000004 to the ErrCode, then it jumps to the app_error_wak.c file and stays at the position
    NRF_BREAKPOINT_COND;
    Also with several further switching the program remains now at this place.

    In the Stack this Code appears:

    Here my C Code again:

    main.c

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "app_error.h"
    #include "bsp.h"
    #include "nrf_delay.h"
    #include "app_pwm.h"
    
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #include "funktionen_pwm.h"
    
    uint32_t an = 99;
    uint32_t aus = 1;
    
    
    
    int main(void)
    {
        #ifdef PIN
         pins_konfigurieren();
         nrf_gpio_pin_set(15);
          nrf_gpio_pin_clear(14);
        #endif
    
        initialisierung_pwm();
    
        while (true)
        {
            wert_setzen_pwm(an);
            nrf_delay_ms(3);
            wert_setzen_pwm(aus);
            nrf_delay_ms(2);
    
        }
    
    }
    
    
    /** @} */

    funktionen_pwm.c

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "app_error.h"
    #include "app_pwm.h"
    
    const app_pwm_t motor_pwm;
    
    void initialisierung_pwm(void)
    { 
        APP_PWM_INSTANCE(motor_pwm, 1);
    
        #define M_CONTR_PWM 16
        #define frequenz_teiler  1000
    
        static volatile bool status_pwm;
        void status_pwm_handler(uint32_t pwm_id)
        {
            status_pwm = true; 
        }
        ret_code_t err_code;
        app_pwm_config_t pwm_config = APP_PWM_DEFAULT_CONFIG_1CH(frequenz_teiler, M_CONTR_PWM);
        pwm_config.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
        err_code = app_pwm_init(&motor_pwm, &pwm_config, status_pwm_handler);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&motor_pwm);
        while(app_pwm_channel_duty_set(&motor_pwm, 0, 99) == NRF_ERROR_BUSY);
    }
    
    
    void wert_setzen_pwm(uint16_t Wert_PWM)
    {
         while(app_pwm_channel_duty_set(&motor_pwm, 0, Wert_PWM) == NRF_ERROR_BUSY);
    }

    funktionen_pwm.h

    #ifndef FUNKTIONEN_PWM_H_
    #define FUNKTIONEN_PWM_H_
    
    
    
    void initialisierung_pwm(void);
    void wert_setzen_pwm(uint16_t);
    
    
    #endif

Reply
  • Thank you very much for your quick reply.
    When I run the program with the debugger, it writes at the position
    err_code = app_pwm_init(&motor_pwm, &pwm_config, status_pwm_handler);  
    0x000000 in the ErrCode. If I continue with the debugger it jumps back to the main.c file. If I go further it jumps back to the functions_pwm.c file and writes 0x00000004 to the ErrCode, then it jumps to the app_error_wak.c file and stays at the position
    NRF_BREAKPOINT_COND;
    Also with several further switching the program remains now at this place.

    In the Stack this Code appears:

    Here my C Code again:

    main.c

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "app_error.h"
    #include "bsp.h"
    #include "nrf_delay.h"
    #include "app_pwm.h"
    
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #include "funktionen_pwm.h"
    
    uint32_t an = 99;
    uint32_t aus = 1;
    
    
    
    int main(void)
    {
        #ifdef PIN
         pins_konfigurieren();
         nrf_gpio_pin_set(15);
          nrf_gpio_pin_clear(14);
        #endif
    
        initialisierung_pwm();
    
        while (true)
        {
            wert_setzen_pwm(an);
            nrf_delay_ms(3);
            wert_setzen_pwm(aus);
            nrf_delay_ms(2);
    
        }
    
    }
    
    
    /** @} */

    funktionen_pwm.c

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "app_error.h"
    #include "app_pwm.h"
    
    const app_pwm_t motor_pwm;
    
    void initialisierung_pwm(void)
    { 
        APP_PWM_INSTANCE(motor_pwm, 1);
    
        #define M_CONTR_PWM 16
        #define frequenz_teiler  1000
    
        static volatile bool status_pwm;
        void status_pwm_handler(uint32_t pwm_id)
        {
            status_pwm = true; 
        }
        ret_code_t err_code;
        app_pwm_config_t pwm_config = APP_PWM_DEFAULT_CONFIG_1CH(frequenz_teiler, M_CONTR_PWM);
        pwm_config.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
        err_code = app_pwm_init(&motor_pwm, &pwm_config, status_pwm_handler);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&motor_pwm);
        while(app_pwm_channel_duty_set(&motor_pwm, 0, 99) == NRF_ERROR_BUSY);
    }
    
    
    void wert_setzen_pwm(uint16_t Wert_PWM)
    {
         while(app_pwm_channel_duty_set(&motor_pwm, 0, Wert_PWM) == NRF_ERROR_BUSY);
    }

    funktionen_pwm.h

    #ifndef FUNKTIONEN_PWM_H_
    #define FUNKTIONEN_PWM_H_
    
    
    
    void initialisierung_pwm(void);
    void wert_setzen_pwm(uint16_t);
    
    
    #endif

Children
Related