获取占空比排序数组 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];
}