1kHZ基频 1毫秒定时器 IO口 模拟pwm 呼吸灯

获取占空比排序数组 c在线编译

#define NUM 12
#define SIZE ((NUM-1)*NUM/2)

typedef struct rate_struct {
    float val;
    short ind;
}_def_rate_str;

uint8_t buff[SIZE][2];
_def_rate_str rate_ind[SIZE];

/**@brief : 排序
 */
void Sort_val(_def_rate_str *array,const short N){
    for(short i = 0; i < N; i++){
        for(short j = i+1; j < N; j++){
            if(array[i].val > array[j].val){
                _def_rate_str temp;
                memcpy((uint8_t *)&temp, (uint8_t *)&array[i], sizeof(array[i]));
                memcpy((uint8_t *)&array[i], (uint8_t *)&array[j], sizeof(array[i]));
                memcpy((uint8_t *)&array[j], (uint8_t *)&temp, sizeof(array[i]));
            }
        }
    }
}

int main(void){
    short k = 0;
    for(char i = 1; i < NUM; i++){
        for(char j = i+1; j <= NUM; j++){
            buff[k][0] = i;
            buff[k][1] = j;
            rate_ind[k].val = float(i)/float(j);
            rate_ind[k].ind = k;
            printf(" [%02d,%02d]=%.2f ",i,j,rate_ind[k].val);
            k++;
        }
        printf("\n");
    }
    k = 0;
    Sort_val(rate_ind, SIZE);
    for(short i = 0; i < SIZE-1; i++){
        float diff = rate_ind[i+1].val - rate_ind[i].val;
        printf("{%2d,%2d}, /*%.2f%%*/ \t", buff[rate_ind[i].ind][0],buff[rate_ind[i].ind][1],100*(diff));
        if(diff > 0)
            k++;
        if((i & 0x7) == 0x7)
            printf("\n");
    }
    printf("\n There are %d points with different values.\n",k);
}

在线工具获取到 的数组
在这里插入图片描述

void GREEN_OFF(void) { /* 灭灯 */ }
void GREEN_ON(void)  { /* 点灯 */ }
#define DUTY_RATE_NUM  48
#define DURATION_TIME  27 // 每个占空比持续 >= 27ms

static uint8_t count = 0;  // 小周期计时
static uint8_t count2 = 0; // 小周期计次
/* {compare, period} */
static const uint8_t s_LedDutyBuff[DUTY_RATE_NUM][2] = 
{
    { 1,18}, { 1,17}, { 1,16}, { 1,15}, { 1,14}, { 1,13}, { 1,12}, { 1,11},
    { 1,10}, { 1, 9}, { 1, 8}, { 1, 7}, { 1, 6}, { 2,11}, { 1, 5}, { 2, 9},
    { 1, 4}, { 3,11}, { 2, 7}, { 3,10}, { 1, 3}, { 3, 8}, { 2, 5}, { 3, 7},
    { 4, 9}, { 1, 2}, { 5, 9}, { 4, 7}, { 3, 5}, { 5, 8}, { 2, 3}, { 7,10},
 
    { 5, 7}, { 3, 4}, { 7, 9}, { 4, 5}, { 5, 6}, { 6, 7}, { 7, 8}, { 8, 9},
    { 9,10}, {10,11}, {11,12}, {12,13}, {13,14}, {14,15}, {15,16}, { 1, 1}
};
/**@brief: 放在 1ms 定时器中断执行
 */
void BreathLed2(void)
{
	static uint8_t steppingDir = 0; // 变化方向控制
	static uint8_t s_dutyIndex = 0; // 占空比数组索引
	static uint8_t compare = 1;
	static uint8_t period = 18;
 //   如果是其他灯模式,这里返回
 //   if(LED_MODE_BREATH != s_ledNow.mode)
 //       return ;
    ++count;
    if((period < 2) || (compare < 2)){
        if(count <= compare)
            GREEN_ON();
        else
            GREEN_OFF();
    }else{
        if(count <= (period>>1)){
            if(count <= (compare>>1))
                GREEN_ON();
            else
                GREEN_OFF();
        }else{
            if(count <= ( ( (compare+1)>>1) + (period>>1) ) )
                GREEN_ON();
            else
                GREEN_OFF();
        }
    }

    if(count < period)
        return;
    count = 0;
    
    if(++count2*period < DURATION_TIME) // each rate keep about 30ms
        return;
    count2 = 0;
    
    switch(steppingDir)
    {
    case 0:    // duty up
        if(s_dutyIndex < (DUTY_RATE_NUM - 1))
            s_dutyIndex ++;
        else
            steppingDir = 1;
        break;
    default:    // duty down
        if(s_dutyIndex)
            s_dutyIndex --;
        else
            steppingDir = 0;
        break;
    }
    compare = s_LedDutyBuff[s_dutyIndex][0];
    period  = s_LedDutyBuff[s_dutyIndex][1];
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值