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

📄 shiyan.c

📁 基于dsPIC30F的 智能型变压器有载分接开关参数综合测试仪 C语言
💻 C
📖 第 1 页 / 共 4 页
字号:
  // Holds the information about SPI Enable/Disable 
  unsigned int SPISTATValue;
  //Timeout value during which timer1 is ON 
  int timeout;
  // Turn off SPI modules 
  CloseSPI1();
  CloseSPI2();
  TMR1 = 0 ;
  timeout = 0;
  TRISDbits.TRISD0 = 0;
  // Configure SPI2 interrupt 
  ConfigIntSPI2(SPI_INT_EN & SPI_INT_PRI_6);
  // Configure SPI1 module to transmit 16 bit timer1 value in master mode 
  SPICONValue = FRAME_ENABLE_OFF & FRAME_SYNC_OUTPUT &
  ENABLE_SDO_PIN & SPI_MODE16_ON &
  SPI_SMP_ON & SPI_CKE_OFF &
  SLAVE_SELECT_ENABLE_OFF &
  CLK_POL_ACTIVE_HIGH &
  MASTER_ENABLE_ON &
  SEC_PRESCAL_7_1 &
  PRI_PRESCAL_64_1;
  SPISTATValue = SPI_ENABLE & SPI_IDLE_CON &
  SPI_RX_OVFLOW_CLR;
  OpenSPI1(SPICONValue,SPISTATValue );
  dsPIC Peripheral Libraries
  // Configure SPI2 module to receive 16 bit timer value in slave mode 
  SPICONValue = FRAME_ENABLE_OFF & FRAME_SYNC_OUTPUT &
  ENABLE_SDO_PIN & SPI_MODE16_ON &
  SPI_SMP_OFF & SPI_CKE_OFF &
  SLAVE_SELECT_ENABLE_OFF &
  CLK_POL_ACTIVE_HIGH &
  MASTER_ENABLE_OFF &
  SEC_PRESCAL_7_1 &
  PRI_PRESCAL_64_1;
  SPISTATValue = SPI_ENABLE & SPI_IDLE_CON &
  PI_RX_OVFLOW_CLR;
  OpenSPI2(SPICONValue,SPISTATValue );
  T1CON = 0X8000;
  while(timeout< 100 )
  { 
   timeout = timeout+2 ;
  }
  T1CON = 0;
  WriteSPI1(TMR1); 
  while(SPI1STATbits.SPITBF);
  while(!DataRdySPI2()); //If ‘1’  indicates that the data has been received in the receive buffer and is to be read.
  datard = ReadSPI2();
  if(datard <= 600)
  {
    PORTDbits.RD0 = 1;
  }
  // Turn off SPI module and clear IF bit 
  CloseSPI1();
  CloseSPI2();
  return 0;
}
//定时器1
unsigned int timer_value;
void __attribute__((__interrupt__)) _T1Interrupt(void)
{
  PORTDbits.RD1 = 1; // turn off LED on RD1 
  WriteTimer1(0);
  IFS0bits.T1IF = 0; // Clear Timer interrupt flag 
}
void timer(void)
{
unsigned int match_value;
TRISDbits.TRISD1 = 0;
PORTDbits.RD1 = 1; // turn off LED on RD1 
// Enable Timer1 Interrupt and Priority to "1" 
ConfigIntTimer1(T1_INT_PRIOR_1 & T1_INT_ON);
WriteTimer1(0);
match_value = 0xFFF;
OpenTimer1(T1_ON & T1_GATE_OFF & T1_IDLE_STOP &
T1_PS_1_1 & T1_SYNC_EXT_OFF &
T1_SOURCE_INT, match_value);
// Wait till the timer matches with the period value 
while(1)
{
  timer_value = ReadTimer1();
  if(timer_value >= 0x7FF)
  {
    PORTDbits.RD1 = 0; // turn on LED on RD1 
  }
}
CloseTimer1();
}
//A/D转换
void adrh(void)
(
  bit XWYB;               //相移标志位
  unsigned int g,i;
  TRISCbits.TRISC1 = 0;   
  TRISCbits.TRISC13 = 0;
  TRISB=0xFFFF;
  TRISBbits.TRISB12 = 1;
  PORTCbits.RC13= 1;     //禁止读AD
  OpenTimer1();           //启动定时器1
  PORTCbits.RC1 = 0;      //启动A/D
  g=0;
  while(XWYB==0)
  {
    while(PORTBbits.RB12==0); //A/D转换完
    if(g>=1000) break;
    getu();
    u[g]=e;
    g++;
  }
  CloseTimer1();
  for(i=0;i<1000;i++)
  {
    while(PORTBbits.RB12==0); //A/D转换完
    if(g<1000)
    {
      getu();
      u[g]=e;
    }
    getv();
    v[i]=f;
    g++;
  }
  PORTCbits.RC1 = 1;      //停止A/D 
}
//标准回路数据
void getu(void)
{
  unsigned int e,i,a,b;
  PORTCbits.RC13=0;      //读A/D
  a=PORTB;
  PORTCbits.RC13=1; 
  b=a&0x0FFC;
  PORTCbits.RC13=0;      //读A/D
  i=PORTB;
  PORTCbits.RC13=1; 
  i=0;
  e=b|(a>>14);
}
//正接或反接回路数据
void getv(void)
{
  unsigned int f,i,a,b;
  PORTCbits.RC13=0;      //读A/D
  i=PORTB;
  PORTCbits.RC13=1; 
  i=0;
  PORTCbits.RC13=0;      //读A/D
  a=PORTB;
  PORTCbits.RC13=1; 
  b=a&0x0FFC;
  f=b|(a>>14);
}
//算正切角
float suntg(char i)
{
  unsigned char a;
  float p=0;
  float t;
  for(a=0;a<250;a++)
  {
    p=((float)((*(w+a)-*(w+500+a)+i*(*(w+500-a)-*(w+1000-a))))*(*(f+a))+p;
  }
  if(i>0)
  {
    t=p+*(w+250)-*(w+750);
  }
  if(i<0)
  {
    t=p+*(w+0)-*(w+500);
  }
  return(t);  
} 
//计算介质损耗角
float jztg(void)
{
  char i;
  float A,B,C,D,J;
  float *w,*f;
  i=-1;
  w=&u[0];
  f=&c[0];
  A=suntg(i);
  i=1
  w=&u[0];
  f=&s[0];
  B=suntg(i);
  i=-1;
  w=&v[0];
  f=&c[0];
  C=suntg(i);
  i=1
  w=&v[0];
  f=&s[0];
  D=suntg(i);
  J=(A*D-C*B)/(B*D+A*C);
} 
//十六进制转十进制,将00-FF转化成S1S2S3个十百位
void slzs(unsigned char s)
{
  unsigned mid,y;
  mid=s;
  y=s;
  s1=(unsigned char)(y%10);
  y=mid;
  s2=(unsigned char)((y-s2)%100/10);
  y=mid;
  s3=(unsigned char)((y-(y-s2)%100)/100);
}
//延时函数延明250ms
void delaydms(unsigned int d)
{
    while (--d);
}
//十进制浮点数向规格化浮点数转换
void FloatToChar(double f, unsigned char *ch)
{
    unsigned char i;
    void * pf = &f;
    for( i = 0; i < sizeof(double); ++i)
    {
         *(ch + i) = *((unsigned char*)pf + i);
    }
}
//浮点数转换十进制浮点数
double CtoF(unsigned char *px)
{ 
   double a;
   unsigned char i;
   void *ph;
   ph=&a;
   for(i=0;i<4;i++)
   { 
     *((unsigned char*)ph+i)=*(px+i);
   }
   return(a);
}
//直接从程序存储器里读数据
   EnablePSV();
   counter1=s[3];
   PSVPAG = __builtin_psvpage(&c);
   counter2=c[2];
  TRISD = 0;        // configure PORTD for output
  while(1)          // do forever
  {
    PORTDbits.RD9= counter;  // send value of ‘counter’ out PORTB
    delaydms(1000);
    counter=!counter;  
  }
//使能PVS
void EnablePSV(void)
{
  CORCONbits.PSV = 1;
}
//不使能PVS
void DisablePSV(void)
{
  CORCONbits.PSV = 0;
}
//串口发送单字节数据              
   unsigned int baudvalue;                                 // Holds the value of baud register
   unsigned int U2MODEvalue;                               // Holds the value of uart config reg 
   unsigned int U2STAvalue;                                // Holds the information regarding uart TX & RX interrupt modes
   CloseUART2();                                           // Turn off UART1module 
   ConfigIntUART2(UART_RX_INT_EN & UART_RX_INT_PR6 &
                  UART_TX_INT_EN & UART_TX_INT_PR2);       // Configure uart2 receive and transmit interrupt    
   baudvalue = 64;    //Baud 2400                          // Configure UART2 module to transmit 8 bit data with one stopbit.Also DISable loopback mode 
   U2MODEvalue = UART_EN & UART_IDLE_CON &
                 UART_EN_WAKE & UART_DIS_LOOPBACK &
                 UART_DIS_ABAUD & UART_NO_PAR_8BIT &
                 UART_1STOPBIT;
   U2STAvalue = UART_INT_TX_BUF_EMPTY &
                UART_TX_PIN_NORMAL &
                UART_TX_ENABLE & UART_INT_RX_CHAR &
                UART_ADR_DETECT_DIS &
                UART_RX_OVERRUN_CLEAR;
   OpenUART2(U2MODEvalue, U2STAvalue, baudvalue);      // Load transmit buffer and transmit the same till null character is encountered 
   WriteUART2 (0x55);                                  // send to a data 
   while(BusyUART2());                                 // If ‘1’ is returned, it indicates that UART is busy in transmission   
   while(!IFS1bits.U2RXIF);                        // Waiting for produce interrupt
   IFS1bits.U2RXIF=0;                              // clear interrupt flag
   while(DataRdyUART2())                               //If ‘1’ is returned, it indicates that the receive buffer has a data to be read.
   { 
     (*(Receiveddata)++)=ReadUART2() ;                  // Read all the data remaining in receive buffer which are unread 
      ;
    a=ReadUART2();           
   }
   b=a;
     //delaydms(1000);
     //b=*Receiveddata;
     //CloseUART2();                              // Turn off UART2 module
   c=b;
   e=c;
   a=Buf[0];
   while(1)
   {
    if(b==0x55)  break;
   }                    
   while(1) 
   (
     ;
   } 
//中断函数   
void __attribute__((__interrupt__)) _U2TXInterrupt(void)
{
  IFS1bits.U2TXIF = 0;
}
void __attribute__((__interrupt__)) _U2RXInterrupt(void)   //This is UART2 receive ISR 
{
  //char a;
  IFS1bits.U2RXIF = 0;  // Read the receive buffer till at least one or more character can be read
  while( DataRdyUART2())
  {
     a=ReadUART2();
    //*Receiveddata=ReadUART2();
  }
  b=a;
}                        
******************************************************************************************************/
/*****************************************************************************************************
//ADzh
int a,b,e,g,i;
  g=0;
  CloseUART2();                 
  TRISCbits.TRISC1 = 0;         // set portc one is output
  TRISCbits.TRISC13 = 0;        // set portc thirteen is output
  TRISB=0xFFFF;                 // set portb is input
  TRISBbits.TRISB12 = 1;
  PORTCbits.RC13= 1;            // no read AD
  PORTCbits.RC1 = 0;            // set out A/D
  while(1)
  {
    while(PORTBbits.RB12==0);     // A/D convert end 
    PORTCbits.RC13=0;             // read A/D
    a=PORTB;
    PORTCbits.RC13=1; 
    b=a&0x0FFC;
    PORTCbits.RC13=0;             // read A/D 
    i=PORTB;
    PORTCbits.RC13=1; 
    i=0;
    e=b|((a>>14)&0x2);
    g++;
***************************************************************************************************/
/**************************************************************************************************
 putsUART1(Buf);     //发一个字符数据   
  while(BusyUART1());   // Wait for transmission to complete If ‘1’ is returned, it indicates that UART is busy in transmission 
  while(1)
  {                                 
    while(!IFS0bits.U1RXIF ); // Waiting for produce interrupt
    IFS0bits.U1RXIF =0;    
    while(DataRdyUART1())     // Read all the data remaining in receive buffer which are unread 
    {
       a=ReadUART1();
    }
    if(a==0xaa)
    {
      delaydms(5000);
      PORTBbits.RB14=!PORTBbits.RB14;
    }
    else
    {
      delaydms(5000);
      PORTBbits.RB15=!PORTBbits.RB15; 
    }
*************************************************************************************************/
/*************************************************************************************************
//定时器1的定时程序 
void timer(void)
{
  unsigned int match_value;
  TRISDbits.TRISD1 = 0;
  PORTDbits.RD1 = 1;                               // turn off LED on RD1
  ConfigIntTimer1(T1_INT_PRIOR_1 & T1_INT_ON);     // Enable Timer1 Interrupt and Priority to "1" 
  WriteTimer1(0);
  match_value = 0xFFF;
  OpenTimer1(T1_ON & T1_GATE_OFF & T1_IDLE_STOP &
             T1_PS_1_1 & T1_SYNC_EXT_OFF &T1_SOURCE_INT, match_value);
  while(1)
  {
    timer_value = ReadTimer1();
    if(timer_value >= 0x7FF)
    {
      PORTDbits.RD1 = 0; // turn on LED on RD1 
    }
  }
  CloseTimer1();
}
**************************************************************************************************/
/*
ZDWR2=!ZDWR2;
    //CONTROLZF=1;
    CONTROLAD=1;
    nop();
    nop();
    nop();
    delaydms(50000);
    delaydms(50000);
    nop();
    nop();
    nop();
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    nop();
    nop();
    nop();
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    nop();
    nop();
    nop();
    //CONTROLZF= 0;
    CONTROLAD= 0;
    //ZDWR2=!ZDWR2;
    ZDWR20=!ZDWR20; 
    nop();
    nop();
    nop();
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    ZDWR20=!ZDWR20;
    ZDWR200=!ZDWR200;
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    ZDWR200=!ZDWR200; 
    BDWR200=!BDWR200;
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    BDWR200=!BDWR200;
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
    delaydms(50000);
*/
/**********************************************************
//输出20kHz方波
void highpower(void)
{
  timerint(225);
  HIGHPOWER=1;
}
void __attribute__((__interrupt__)) _T1Interrupt(void)
{
  IFS0bits.T1IF = 0; // Clear Timer interrupt flag 
  WriteTimer1(0);
  HIGHPOWER=!HIGHPOWER;
}
**********************************************************/

⌨️ 快捷键说明

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