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

📄 lh79520_pwm_driver.c

📁 Sharp Lh79520 PWM驱动源码.
💻 C
📖 第 1 页 / 共 2 页
字号:
                duty_cycle = ((arg * period) + 50) / 100;
                PWM0->dc = duty_cycle;
                break;
                
            case PWM1_SET_FREQ:
                /* Set PWM1 frequency, arg = is the frequency in Hz */
                pclk = (CLOCK_MAINOSC * 21)/(RCPC->hclkprescale * 2);
                prescale = RCPC->pwm1prescale;
                if (prescale > 0)
                {
                    pwm1clk = pclk / (prescale * 2);
                }
                else
                {
                    //this should not happen
                    status = _ERROR;
                    break;
                }
                div = pwm1clk / arg;
                while (div > 32768 && prescale <= 32768)
                {
                    prescale += 1;
                    RCPC->pwm1prescale = prescale;
                    pwm1clk = pclk / (prescale * 2);
                    div = pwm1clk / arg;
                }
                PWM1->tc = div;
                break;
                
            case PWM1_SET_DUTY_CYCLE:
                /* Set PWM1 duty cycle, arg = 1 to 100 for 1% to 100 % */
                period = (UNS_16) PWM1->tc;
                duty_cycle = ((arg * period) + 50) / 100;
                PWM1->dc = duty_cycle;
                break;
                
            case PWM0_SET_OUTPUT_INVERT:
                /* Set the PWM0 output signal to be normal or invert output, 
                arg = 1 for invert singla, arg = 0 for normal signal */
                if (arg == 1)
                {
                    PWM0->inv = PWM_INV_INVERT;
                }
                else if (arg == 0)
                {
                    PWM0->inv = ~PWM_INV_INVERT;
                }
                else
                {
                    status = _ERROR;
                }
                break;
                
            case PWM1_SET_OUTPUT_INVERT:
                /* Set the PWM1 output signal to be normal or invert output, 
                arg = 1 for invert singla, arg = 0 for normal signal */
                if (arg == 1)
                {
                    PWM1->inv = PWM_INV_INVERT;
                }
                else if (arg == 0)
                {
                    PWM1->inv = ~PWM_INV_INVERT;
                }
                else
                {
                    status = _ERROR;
                }
                break;
                
            case PWM0_GET_STATUS:
                switch (arg)
                {
                    case PWM_GET_START:
                        /* Returns 1 for PWM is started, 0 for stop */
                        if (PWM0->en == PWM_EN_ENABLE)
                        {
                            status = 1;
                        }
                        else
                        {
                            status = 0;
                        }
                        break;
                        
                    case PWM_GET_MODE:
                        /* Returns 1 for synchronous mode, 0 for normal mode */
                        if (PWM0->sync == PWM_SYNC_SYNC)
                        {
                            status = 1;
                        }
                        else
                        {
                            status = 0;
                        }
                        break;
                        
                    case PWM_GET_FREQ:
                        /* Returns frequency of PWM in Hz */
                        pclk = (CLOCK_MAINOSC * 21)/(RCPC->hclkprescale * 2);
                        prescale = RCPC->pwm0prescale;
                        if (prescale > 0)
                        {
                            pwm0clk = pclk / (prescale * 2);
                        }
                        else
                        {
                            //this should not happen
                            status = _ERROR;
                            break;
                        }
                        div = PWM0->tc;
                        status = pwm0clk/div;
                
                        break;
                        
                    case PWM_GET_DUTY_CYCLE:
                        /* Returns duty cycle of PWM, 1-100 for 1% to 100 % */
                        period = (UNS_16) PWM0->tc;
                        prev_dc = PWM0->dc;
                        if (period > 0)
                        {
                            prev_dc_percent = ((prev_dc * 100) + (period >> 1)) / period;
                        }
                        else
                        {
                            prev_dc_percent = 100;
                        }
                        status = prev_dc_percent;
                        break;
                        
                    case PWM_GET_OUTPUT_INVERT:
                        /* Return 1 for output signal to be invert signal, 0 for
                        output singal to be normal signal */
                        if (PWM0->inv == PWM_INV_INVERT)
                        {
                            status = 1;
                        }
                        else
                        {
                            status = 0;
                        }
                        break;
                        
                    default:
                        status = _ERROR;
                        break;
                }
                break;
                
            case PWM1_GET_STATUS:
                switch (arg)
                {
                    case PWM_GET_START:
                        /* Returns 1 for PWM is started, 0 for stop */
                        if (PWM1->en == PWM_EN_ENABLE)
                        {
                            status = 1;
                        }
                        else
                        {
                            status = 0;
                        }
                        break;
                        
                    case PWM_GET_FREQ:
                        /* Returns frequency of PWM in Hz */
                        pclk = (CLOCK_MAINOSC * 21)/(RCPC->hclkprescale * 2);
                        prescale = RCPC->pwm1prescale;
                        if (prescale > 0)
                        {
                            pwm1clk = pclk / (prescale * 2);
                        }
                        else
                        {
                            //this should not happen
                            status = _ERROR;
                            break;
                        }
                        div = PWM1->tc;
                        status = pwm1clk/div;
                        break;
                        
                    case PWM_GET_DUTY_CYCLE:
                        /* Returns duty cycle of PWM, 1-100 for 1% to 100 % */
                        period = (UNS_16) PWM1->tc;
                        prev_dc = PWM1->dc;
                        if (period > 0)
                        {
                            prev_dc_percent = ((prev_dc * 100) + (period >> 1)) / period;
                        }
                        else
                        {
                            prev_dc_percent = 100;
                        }
                        status = prev_dc_percent;
                        break;
                        
                    case PWM_GET_OUTPUT_INVERT:
                        /* Return 1 for output signal to be invert signal, 0 for
                        output singal to be normal signal */
                        if (PWM1->inv == PWM_INV_INVERT)
                        {
                            status = 1;
                        }
                        else
                        {
                            status = 0;
                        }
                        break;
                        
                    default:
                        status = _ERROR;
                        break;
                }
                break;

            default:
                /* Unsupported parameter */
                status = _ERROR;
                break;
        }
    }

    return status;
}

/***********************************************************************
 *
 * Function: pwm_read
 *
 * Purpose: PWM read function (stub only)
 *
 * Processing:
 *     Return 0 to the caller.
 *
 * Parameters:
 *     devid:     Pointer to PWM config structure
 *     buffer:    Pointer to data buffer to copy to
 *     max_bytes: Number of bytes to read
 *
 * Outputs: None
 *
 * Returns: Number of bytes actually read (always 0)
 *
 * Notes: None
 *
 **********************************************************************/
INT_32 pwm_read(INT_32 devid,
                void *buffer,
                INT_32 max_bytes)
{
    return 0;
}

/***********************************************************************
 *
 * Function: pwm_write
 *
 * Purpose: PWM write function (stub only)
 *
 * Processing:
 *     Return 0 to the caller.
 *
 * Parameters:
 *     devid: Pointer to PWM config structure
 *     buffer:  Pointer to data buffer to copy from
 *     n_bytes: Number of bytes to write
 *
 * Outputs: None
 *
 * Returns: Number of bytes actually written (always 0)
 *
 * Notes: None
 *
 **********************************************************************/
INT_32 pwm_write(INT_32 devid,
                 void *buffer,
                 INT_32 n_bytes)
{
    return 0;
}

⌨️ 快捷键说明

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