原创 多功能数字示波表(4)

2010-4-26 19:14 2065 7 7 分类: MCU/ 嵌入式

/************************************
*函数名:void Measuring_current(void)
*功  能:电流测量
*输  入:Noen
*返  回:Noen
*************************************/ 
void Measuring_current(void)
{
 Key_number = 0xff;
 
 Set_CT1;
 Reset_CT2;
 Delay_ms(200);


 ADC_DC();    //初始化AD为直流模式
 Delay_ms(10);


 // ADC1 regular channel12 configuration  配置转换规则  PC2
  ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_1Cycles5);
 Delay_ms(10);


 lcd_init();
 lcd_CN("电流测量",0,0);
 lcd_CN("0:返回",0,3);
 step = 2;    //
 while(1)
 {
  switch(step)
  {
   case 1:  First_step_current(); break;
   case 2:  Second_step_current(); break;
   default: step = 1; 
  } 
  if(Key_number == 0)    //退出
  {
    lcd_init();
    lcd_CN("数字示波表",0,0);
    lcd_CN("1:电阻  2:电压",0,1);
    lcd_CN("3:电流  4:正弦波",0,2);
    break;
  }
 }    
 
}             
/*--------------------------------- 电压测量 ---------------------------*/
//采样
void Sampling_DC(void)
{
  u8 j;
  for(j=0;j<245;j++)
  {
    // 软件触发AD使能 
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
    while(ADC_GetCalibrationStatus(ADC1));  //等待转换完成
    ad_buffer[j] = ADC_GetConversionValue(ADC1);  
  }   
}
//u16  Eraser[245];
//绘制直流电压波形
void Waveform_voltage(void)
{
  u32 Xpos,Ypos;
  for(Ypos=319;Ypos>75;Ypos--)
  {     
    if(st == 0)   //1v/div 档
    {
      if(step == 1)   //一通道
        Xpos = 35*(6000-(ad_buffer[319-Ypos]*5000/4096+30))/1000; 
      else
        Xpos = 35*(6000-(ad_buffer[319-Ypos]*61+1500)/613)/1000;    //Vref = 2.496(V)     
    } 
    else      //0.1v/div 档
    {
      if(step == 1)  //二通道
        Xpos = 35*(600-(ad_buffer[319-Ypos]*5000/4096+30))/100;
      else
        Xpos = 35*(600-(ad_buffer[319-Ypos]*61+1500)/613)/100; 
    }
    LCD_SetCursor(Xpos,Ypos); 
    LCD_WriteRAM_Prepare(); // Prepare to write GRAM 
    LCD_WriteRAM(Yellow); 
  }
}
//385mv ~ 5v
//*修正:在测得的电压上加15mv
void First_channel_voltage(void)
{
 // ADC1 regular channel10 configuration  配置转换规则  PC0
  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_1Cycles5);
 Delay_ms(10);
 lcd_CN("7:切换 1v/div  ",0,2);
 st = 0;
 while(1)
 {
  // 软件触发AD使能 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  while(ADC_GetCalibrationStatus(ADC1));  //等待转换完成
  temp = ADC_GetConversionValue(ADC1);
 
  Sampling_DC();   //AD采样
// temp = ad_buffer[10];
  volt = temp*5000/4096+30;     //修正
  if(Key_number == 0)   //退出
  {
  //  Key_number = 0xff;
    LCD_Clear(Black);  //绘制背景颜色
    Display_grid();        //绘制背景网格
    break;
  }
  else if(Key_number == 7) //0.1v/div、1v/div 量程切换
  {
   Key_number = 0xff;
   st = ~st; 
   if(st == 0)
     lcd_CN("7:切换 1v/div  ",0,2);
   else
     lcd_CN("7:切换 0.1v/div",0,2);  
  }
  else if(volt < 385)  //向下溢出  返回并进入第二档
  {
   step = 2;   
   break;
  }   
  else   //电压值和波形显示
  {
    p = ConvCH(volt);     //
    lcd_CN("      mv",0,1);
    lcd_CN(p,0,1);


    LCD_Clear(Black);  //绘制背景颜色 
    Display_grid();    //绘制背景网格
    if((((volt <= 6000)&&(st == 0))||(volt <= 600)))  //显示范围 0~6v
    { 
      Waveform_voltage();  //直流波形显示
    }
    Delay_ms(500); 
   }
 } 

//20mv ~ 450mv
void Second_channel_voltage(void)
{  
  // ADC1 regular channel11 configuration  配置转换规则  PC1
  ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_1Cycles5);
 Delay_ms(10);
 lcd_CN("7:切换 0.1v/div  ",0,2);
 st = 0xff;
 while(1)
 {
  // 软件触发AD使能 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  while(ADC_GetCalibrationStatus(ADC1));  //等待转换完成
  temp = ADC_GetConversionValue(ADC1);
  if(temp == 0)
    volt = 0;
  else   //校正
    volt = (temp*61+1500)/613+2;    //Vref = 2.496(V)
 
  Sampling_DC();   //AD采样 
  if(Key_number == 0)   //退出
  {
  // Key_number = 0xff;    //不能写
    LCD_Clear(Black);  //绘制背景颜色
    Display_grid();        //绘制背景网格
    break;
  }
  else if(Key_number == 7) //0.1v/div、1v/div 量程切换
  {
   Key_number = 0xff;
   st = ~st; 
   if(st == 0)
     lcd_CN("7:切换 1v/div  ",0,2);
   else
     lcd_CN("7:切换 0.1v/div",0,2);
  }
  else if(temp > 4090)  //向上溢出  返回并进入第一档
  {
   step = 1;
   break;
  }
  else
  {
    p = ConvCH(volt);
   lcd_CN("      mv",0,1);
   lcd_CN(p,0,1);


   LCD_Clear(Black);  //绘制背景颜色
   Display_grid();    //绘制背景网格
   if(volt <= 600)  //显示范围 0~600mv
   {
     Waveform_voltage();  //直流波形显示
   }
   Delay_ms(500); 
  }
 }    
}  
/************************************
*函数名:void Measuring_voltage(void)
*功  能:电压测量
*输  入:Noen
*返  回:Noen
*************************************/
void Measuring_voltage(void)
{
 Key_number = 0xff;


 Set_CT1;
 Set_CT2;
 Delay_ms(200);


 ADC_DC();
 Delay_ms(10);
 // ADC1 regular channel10 configuration  配置转换规则  PC0
  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_1Cycles5);
 Delay_ms(10);
 
 lcd_init();
 lcd_CN("电压测量",0,0);
 lcd_CN("0:返回",0,3); 
 
 step = 1;
 while(1)
 {
  switch(step)
  {
   case 1:  First_channel_voltage(); break;  //350mv ~ 5v
   case 2:  Second_channel_voltage(); break; //20mv ~ 408mv
   default: step = 1; 
  } 
  if(Key_number == 0)    //退出
  {
    lcd_init();
    lcd_CN("数字示波表",0,0);
    lcd_CN("1:电阻  2:电压",0,1);
    lcd_CN("3:电流  4:正弦波",0,2);
    break;
  }
 }      
}
/*--------------------------------------- 正弦波测量 ---------------------------------*/
/************************************
*函数名:void Measuring_sine(void)
*功  能:正弦波测量
*输  入:Noen
*返  回:Noen
*************************************/
u8 auto_freq_set(u32 freq)
{
    freq = freq/1000;
  if( freq <= 50 )   //
    {
        return 0 ;     //200ms
    }
  else if( freq <= 250)  //
  {
   return 1;     //20ms
  }
    else if( freq <= 1100 )
    {
        return 2 ;    //2ms
    }
    else if( freq <= 5000 )
    {
        return 3 ;     //200us
    }
    else if( freq <= 50000)
    {
        return 4 ;//40us
    }
    else
    {
        return 5; //2us
    }
}
//2V~5V
void First_channel_sine(void)
{
 u8 i;
// u16 v;  //电压显示
 Reset_CT3;
 Delay_ms(200); 


 state_sine_speed = 1;      //2ms/div 
 state_sine_vpp = 0;       //1v/div
 Key_number = 0xff;
 while(1)
 { 
  if(ms_200 == 1)        //定时器预分频
   TIM_PrescalerConfig(TIM1, 10, TIM_PSCReloadMode_Update);   //10分频
  else
   TIM_PrescalerConfig(TIM1, 0, TIM_PSCReloadMode_Update);
  if(Key_number != 0xff)     //有键按下
  {
    if(Key_number == 0)     //有键按下
       break;
    else
    {
     switch(Key_number)
     {
       case 8: Key_number = 0xff;      //采样速度调节
           if(state_sine_speed >= 5)   //保证数据在0、1、2范围内
             state_sine_speed = 0;   //采样速度档
           else
             state_sine_speed += 1;
           break;
       case 9: Key_number = 0xff;  //显示峰峰值调节
           if(state_sine_vpp >= 2)   //保证数据在0、1、2范围内
             state_sine_vpp = 0;   //采样速度档
           else
             state_sine_vpp += 1;
           break;
      case 12: Key_number = 0xff;    //自动调节
           state_sine_vpp = 0;    //1v/div
           state_sine_speed = auto_freq_set(Freq);   
           break;
       default: Key_number = 0xff;
           break;
     }
     //采样速度选择
     switch(state_sine_speed)                
     {
       case 0: TIM_SetAutoreload(TIM1, 0x6250);  //设置采样速度0x6B24 175Hz 200ms/div 
        //   Sampling_state = 0 ;    //采样标志为 0有效
           ms_200 = 1;         //计数次数为 0xa6b7*6
           step_2us =0;
           lcd_CN("200ms/div",3,3);
           break;
       case 1: TIM_SetAutoreload(TIM1, 27420);  //设置采样速度0x6B24 1.75kHz 20ms/div 
        //   Sampling_state = 0 ;    //采样标志为 0有效
           ms_200 = 0;         //计数次数为 0xa6b7*6
           step_2us =0;
           lcd_CN("20ms/div ",3,3);
           break;
       case 2: TIM_SetAutoreload(TIM1, 2742);  //设置采样速度 17.5K 2ms/div 
       //    Sampling_state = 0 ;
           ms_200 = 0;
           step_2us =0;
           lcd_CN("2ms/div  ",3,3);
           break;
       case 3: TIM_SetAutoreload(TIM1, 274);  //设置采样速度 175K 200us/div 
       //    Sampling_state = 0 ;
           ms_200 = 0;
           step_2us =0;
           lcd_CN("200us/div",3,3);
           break;
       case 4: TIM_SetAutoreload(TIM1, 27);  //设置采样速度 (1.75/2M 40us/div)  1.5M
       //    Sampling_state = 0 ;
           ms_200 = 0;
           step_2us =0;
           lcd_CN("40us/div ",3,3);
           break;
       case 5: TIM_SetAutoreload(TIM1, 32);  //设置采样速度 (17.5M 2us/div)  1.5M
       //    Sampling_state = 0 ;
           ms_200 = 0;
           step_2us =1;
           lcd_CN("2us/div  ",3,3);
           break;
      default: state_sine_speed = 0;   //防错处理
           break;   
     }
     //显示峰峰值选择
     switch(state_sine_vpp)  
     {
       case 0: Av = 3;    //1v/div  2
           lcd_CN("1v/d  ",5,0);
           break;          
       case 1: Av = 6;   //0.5v/div  4
           lcd_CN("0.5v/d",5,0);
           break;
       case 2: Av = 30;   //0.1v/div  20
           lcd_CN("0.1v/d",5,0);
           break;
      default: state_sine_vpp = 0;  //防错
           break;
     }
    }
  }
  else if(vpp < 450)  //向下溢出  返回并进入第二档   650
  {
   step = 2;
   break;
  }
  else
  {
    for(i=0;i<3;i++) 
    {
      if(DMA1_Channel1_complete == 0)  //DMA通道传输完成
      {  
       Eraser_wave();
        dispaly();
       
      }
      Delay_ms(200);
    }
    Sampling_state = 0;


    lcd_CN("峰值:        ",0,2);
    p = ConvCH(vpp*288/100);
 //   v = vpp*288/100;
 //   p = ConvCH(v/1000);
 //   lcd_CN(p,3,2);
  //   lcd_str(".");
  //   lcd_str(ConvCH(v%1000));
     lcd_CN(p,3,2);


    lcd_CN("mv",6,2);
    if(Freq >= 9670000)
    {
      Freq = Freq/1000;   //去mHz
      freqH = Freq/1000;  //取KHz
      freqL = Freq%1000;  //取低位Hz
      lcd_CN(ConvCH(freqH),3,1);
      lcd_str(".");
      lcd_str(ConvCH(freqL));
      lcd_CN("kHz",6,1);
    }
    else if(Freq>967000)
    {
      p = ConvCH(Freq/1000);
      lcd_CN("频率:        ",0,1);
      lcd_CN(p,3,1);
      lcd_CN("Hz ",6,1);
    }
    else
    {
      freqH = Freq/1000;  //取Hz
      freqL = (Freq%1000);  //取低位mHz
      lcd_CN(ConvCH(freqH),3,1);
      lcd_str(".");
      lcd_str(ConvCH(freqL));
      lcd_CN("Hz",6,1);  
    }
 //   lcd_CN("频率:        ",0,1);   
 //   Delay_ms(500);
  }
 }
}
//100mv~2V
void Second_channel_sine(void)
{
 u8 i;
 Set_CT3;
 Delay_ms(200); 


 Key_number = 0xff;


 Av = 1; 
 state_sine_speed = 1;      //2ms/div 
 state_sine_vpp = 0;       //1v/div
 while(1)
 { 
  if(ms_200 == 1)
   TIM_PrescalerConfig(TIM1, 10, TIM_PSCReloadMode_Update);
  else
   TIM_PrescalerConfig(TIM1, 0, TIM_PSCReloadMode_Update);
  if(Key_number != 0xff)     //有键按下
  {
    if(Key_number == 0)
      break;  
    else
    {
     switch(Key_number)
     { 
       case 8: Key_number = 0xff;      //采样速度调节
           if(state_sine_speed >= 5)   //保证数据在0、1、2范围内
             state_sine_speed = 0;   //采样速度档
           else
             state_sine_speed += 1;
           break;
       case 9: Key_number = 0xff;     //显示峰峰值调节
           if(state_sine_vpp >= 2)   //保证数据在0、1、2范围内
             state_sine_vpp = 0;   //采样速度档
           else
             state_sine_vpp += 1;
           break;
      case 12: Key_number = 0xff;    //自动调节
           if(vpp >= 500 && vpp <= 2100)
             state_sine_vpp = 1;    //0.5v/div
           else
             state_sine_vpp = 2;    //0.1v/div
           state_sine_speed = auto_freq_set(Freq);   
           break;
       default: Key_number = 0xff;
           break;
     }
     //采样速度选择
     switch(state_sine_speed)                
     {
       case 0: TIM_SetAutoreload(TIM1, 0x6250);  //设置采样速度0x6B24 175Hz 200ms/div 
        //   Sampling_state = 0 ;    //采样标志为 0有效
           ms_200 = 1;         //计数次数为 0xa6b7*6
           step_2us =0;
           lcd_CN("200ms/div",3,3);
           break;
       case 1: TIM_SetAutoreload(TIM1, 27420);  //设置采样速度0x6B24 1.75kHz 20ms/div 
        //   Sampling_state = 0 ;    //采样标志为 0有效
           ms_200 = 0;         //计数次数为 0xa6b7*6
           step_2us =0;
           lcd_CN("20ms/div ",3,3);
           break;
       case 2: TIM_SetAutoreload(TIM1, 2742);  //设置采样速度 17.5K 2ms/div 
       //    Sampling_state = 0 ;
           ms_200 = 0;
           step_2us =0;
           lcd_CN("2ms/div  ",3,3);
           break;
       case 3: TIM_SetAutoreload(TIM1, 274);  //设置采样速度 175K 200us/div 
       //    Sampling_state = 0 ;
           ms_200 = 0;
           step_2us =0;
           lcd_CN("200us/div",3,3);
           break;
       case 4: TIM_SetAutoreload(TIM1, 27);  //设置采样速度 (1.75/2M 40us/div)  
       //    Sampling_state = 0 ;
           ms_200 = 0;
           step_2us =0;
           lcd_CN("40us/div ",3,3);
           break;
       case 5: TIM_SetAutoreload(TIM1, 32);  //设置采样速度 (17.5M 2us/div)  1.5M
       //    Sampling_state = 0 ;
           ms_200 = 0;
           step_2us =1;
           lcd_CN("2us/div  ",3,3);
           break;
      default: state_sine_speed = 0;   //防错处理
           break;   
     }
     //显示峰峰值选择
     switch(state_sine_vpp)  
     {
       case 0: Av = 1;    //1v/div
           lcd_CN("1v/d  ",5,0);
           break;
       case 1: Av = 2;   //0.5v/div
           lcd_CN("0.5v/d",5,0);
           break;
       case 2: Av = 10;   //0.1v/div
           lcd_CN("0.1v/d",5,0);
           break;
      default: state_sine_vpp = 0;  //防错
           break;
     }
    }
  }
  else if(vpp > 1550)  //向上溢出  返回并进入第一档   2100
  {
   step = 1;
   break;
  }
  else
  {
   for(i=0;i<3;i++) 
   {
     if(DMA1_Channel1_complete == 0)
     {     
      Eraser_wave();
       dispaly();
      
     }
     Delay_ms(200);
   }
    Sampling_state = 0;


    p = ConvCH(vpp);
    lcd_CN("峰值:        ",0,2);
    lcd_CN(p,3,2);
    lcd_CN("mv",5,2);
 
    if(Freq >= 9670000)
    {
      Freq = Freq/1000;   //去mHz
      freqH = Freq/1000;  //取KHz
      freqL = (Freq%1000);  //取低位Hz
      lcd_CN(ConvCH(freqH),3,1);
      lcd_str(".");
      lcd_str(ConvCH(freqL));
      lcd_CN("kHz",6,1);
    }
    else if(Freq>=967000)
    {
      p = ConvCH(Freq/1000);
      lcd_CN("频率:        ",0,1);
      lcd_CN(p,3,1);
      lcd_CN("Hz ",6,1);
    }
    else
    {
      freqH = Freq/1000;  //取Hz
      freqL = (Freq%1000);  //取低位mHz
      lcd_CN(ConvCH(freqH),3,1);
      lcd_str(".");
      lcd_str(ConvCH(freqL));
      lcd_CN("Hz",6,1);  
    }
 
  //  Delay_ms(500);
  }
 }
}
//AD通道 8
void Measuring_sine(void)
{
// TIM1_Configuration(); //初始化定时器1
 // ADC_Configuration();  //初始化ADC   ADC_buffer
 // ADC_SoftwareStartConvCmd(ADC1, ENABLE);  //开启ADC
 // DMA_Cmd(DMA1_Channel1, ENABLE);   //使能DMA1
// Delay_ms(10);
 Key_number = 0xff;
 lcd_init();
 lcd_CN("正弦波测量",0,0);
 lcd_CN("频率:",0,1);
 lcd_CN("峰值:",0,2);
 lcd_CN("0:返回",0,3);  
 step = 1;
 while(1)
 { 
  switch(step)
  {
   case 1:  First_channel_sine(); break;  //350mv ~ 5v
   case 2:  Second_channel_sine(); break;  //20mv ~ 408mv
   default: step = 1; 
  } 
  if(Key_number == 0)    //退出
  {
    DMA_Cmd(DMA1_Channel1, DISABLE);   //使能DMA1  ENABLE
    Eraser_wave();
    lcd_init();
    lcd_CN("数字示波表",0,0);
    lcd_CN("1:电阻  2:电压",0,1);
    lcd_CN("3:电流  4:正弦波",0,2);
    break;
  } 
 }
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
7
关闭 站长推荐上一条 /3 下一条