微信:13588713967
#include <stdio.h>
#define BS1_MIN 1
#define BS1_MAX 16
#define BS2_MIN 1
#define BS2_MAX 8
#define PRE_MIN 1
#define PRE_MAX 1024
typedef struct Band_ret{
int BS1;
int BS2;
int PRE;
}Band_ret;
int main(){
Band_ret band_ret[1000];
unsigned int index;
unsigned int bandRate;//kbit/s
unsigned int APB1_FRE;//kHZ
float BS1 = 0;
float BS2 = 0;
float prescaler = 0;
int i,j,k;
float tmp;
float ret;
printf("请输入目标波特率(kbit/s):\n");
scanf("%d",&bandRate);
printf("请输入APB1时钟(MHZ):\n");
scanf("%d",&APB1_FRE);
APB1_FRE*=1000;
tmp = APB1_FRE/(bandRate*1.0);
for(i=BS1_MIN;i<BS1_MAX;i++){
for(j=BS2_MIN;j<BS2_MAX;j++){
for(k=PRE_MIN;k<PRE_MAX;k++){
ret = (1+i+j)*k;
//printf("BS1: %d BS2: %d Prescaler: %d\n",i,j,k);
if(tmp == ret){
if((1000-1) == index){
printf("溢出\n");
return;
}
band_ret[index].BS1 = i;
band_ret[index].BS2 = j;
band_ret[index].PRE = k;
index++;
}
}
}
}
printf("==========================以下是符合要求的参数结果=======================\n");
for(i=0;i<index;i++){
printf(">>>BS1: %d BS2: %d Prescaler: %d\n",band_ret[i].BS1,band_ret[i].BS2,band_ret[i].PRE);
}
printf("=========================================================================\n");
getchar();
getchar();
return 0;
}