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

📄 power.c

📁 用DSPIC306011完成的数控逆变电源
💻 C
字号:
#include "p30fxxxx.h"
/*;------------------------------------------------------------------------------
; UART-PC display routines for Lab 4 and 5
;------------------------------------------------------------------------------
*/
        #include "p30f6011.h"

//;Initialize UART port to communicate with the PC for display

void UART_Display_Setup(void){
        U1MODE = 0xA000;     // Enable UART, 8 bits, 1 stop, no parity
        U1STA = 0x8400;     // Enable transmitter, receiver, normal modes
        U1BRG = 0x0019;

/* ;------------------------------------------------------------------------------
;Write array contents to PC through UART
*/

void UART_Display(unsigned int num_bytes,int *array_ptr[]){
        unsigned int i = 0, k;
        unsigned char j;
//        UART_PutChar(0);                    // Send out start of packet delimiter
//        UART_PutChar(num_bytes);            // Send out the number of bytes in the array
//        UART_PutChar(num_bytes>>8);
        while(i < num_bytes){
            j = (unsigned char)array_ptr[i++];
            if(j == 0)
                j = 1;
            UART_PutChar(j);
            }
        k = 12500;
        while(k--);                    // Delay for 50 mS
        }

void UART_PutChar(unsigned char i){
    while(U1STAbits.UTXBF);        // Spin lock waiting for buffer to be empty
    U1TXREG = i;                // Send character
    }
;*************************************************************8
/*;------------------------------------------------------------------------------
; LCD display routines for Lab 5/6
;------------------------------------------------------------------------------
*/
        #include "p30f6014a2.h"
        #include "LCD_Display.h"

//;Initialize SPI port to communicate with the LCD display

void LCD_Display_Setup(void){
        LATGbits.LATG9 = 1;     // Set SPI slave select pin high
        TRISGbits.TRISG9 = 0;    // and set as output
        SPI2CON = 0x003c;        // Load SPI control register with Master mode, Clk = Fosc/64, Input data sampled at middle of output time, Data clocked out on rising edge, Clk idle is low
        SPI2STAT = 0x8000;         // Enable SPI port
        }

/* ;------------------------------------------------------------------------------
;Clear a column at X=W10 of the display
*/

void LCD_Display_ClrCol(unsigned char x){
        unsigned char counter;
        counter = 32;            // Load counter to clear pixel 32 times
        while(counter > 0){
        LCD_Display_Byte(0xd9);        // Load command to clear a pixel, Call routine to send the command to the display
        LCD_Display_Byte(x);         // Load X location of pixel, Call routine to send the command to the display
        LCD_Display_Byte(counter--);     // Load Y location of pixel, Call routine to send the command to the display
        }
}
        
/* ;------------------------------------------------------------------------------
;Display a pixel at X=W10 and Y=W11
*/

void LCD_Display_Pixel(unsigned char x,unsigned char y){
        LCD_Display_Byte(0xd8);        // Load command to set a pixel, Call routine to send the command to the display
        LCD_Display_Byte(x);        // Load X location of pixel, Call routine to send the command to the display
        LCD_Display_Byte(y);        // Load Y location of pixel, Call routine to send the command to the display
        }

/* ;------------------------------------------------------------------------------
;Send W0 byte to LCD
*/

void LCD_Display_Byte(unsigned char value){
        unsigned int junk;
        LATGbits.LATG9 = 1;        // Set slave select high
        LATGbits.LATG9 = 0;        // Set slave select low for new transmission
        junk = SPI2BUF;            // Read buffer to avoid overflow
        SPI2BUF = value;        // Write the data to the output buffer
        while(!SPI2STATbits.SPIRBF); // Check if transmission complete
        }

void LCD_Display_array(int *array_ptr[]){
    unsigned char j,k;
    for (k=0; k<122; k++)
               {
               j = (unsigned char)array_ptr[k]/8;
            LCD_Display_ClrCol(k);        // Clear the next whole column of the display 
              LCD_Display_Pixel(k,j);        // Set the desired pixel (k is X position, y is Y position)
            }
    }

/*;-------------------------------------
/*;------------------------------------------------------------------------------
; Read ADC and plot voltage on the LCD display
;------------------------------------------------------------------------------
*/
    #include <p30fXXXX.h>
    #include <dsp.h>
    #include <math.h>
    #include "LCD_Display.h"

    #define NUM_SAMPLES       256
    #define FilteredButton        PORTAbits.RA13
    #define UnfilteredButton     PORTAbits.RA12

/* externs... from Filter Source File generated from dsPIC Filter Design */
/* NOTE:  your filter structure names will differ */
extern FIRStruct fir_test1Filter;
extern FIRStruct f_solutionFilter;

_FOSC(CSW_FSCM_OFF & XT_PLL4);
_FWDT(WDT_OFF);
_FBORPOR(PBOR_ON & BORV_42 & PWRT_OFF & MCLR_EN);
_FGS(CODE_PROT_OFF);

unsigned int fsample, freqa, freqb, freqc;
int array[NUM_SAMPLES];
int array_plot[NUM_SAMPLES];
unsigned int max_magnitude;

fractional filter_output_array[NUM_SAMPLES];
float fINA[NUM_SAMPLES]; 
float fINB[NUM_SAMPLES]; 
float fINC[NUM_SAMPLES];

void makewaves(void)
{
        unsigned int n;

        fsample = 10000;        // 10 Khz sampling example
        freqa   =     847;        // 847 Hz sine wave
        freqb    =    367;        // 367 Hz sine wave
        freqc     =     123;        // 123 Hz sine wave
            
            for (n=0; n < NUM_SAMPLES; n++)
            {
                    fINA[n] = sin((2*PI*freqa*n)/fsample);                // Create sine wave of Frequency freqa
                    fINB[n] = sin((2*PI*freqb*n)/fsample);                // Create sine wave of Frequency freqb
                    fINC[n] = sin((2*PI*freqc*n)/fsample);                // Create sine wave of Frequency freqc
                    array[n] = ((fINA[n]+fINB[n]+fINC[n])/3)*0x8000;    // Scaled as fractional integer values
                    array_plot[n] = ((array[n]+0x8000)/256);             // Scaled for UART Charting Utility
            }
                
}


void Filter (void)
{
   int average;
   unsigned u;

    /* Call FIR filter initialization here */
    FIRDelayInit (&fir_test1Filter); // Call FIR filter initialization

    // Extra Credit:    Clear timer 1
    // Extra Credit:    Set up Timer 1 (T1CON) with Timer ON, Gate Disabled, 1:1 Prescaller, Internal Clock

    /* Call FIR filter here */
     FIR (NUM_SAMPLES, &filter_output_array[0], &array[0], &fir_test1Filter); // Call FIR filter routine

   // Extra Credit:    Turn Timer OFF

   /* compute the average of output[] */
   /* this helps tune the level shifter */
   average = 0;
   for (u=0; u<NUM_SAMPLES; u++)
   {
      average += filter_output_array[u];
   }
   average = average / NUM_SAMPLES;

   /* remove the average from the filter output */
   /* this has the effect of removing a DC offset... */
   for (u=0; u<NUM_SAMPLES; u++)
   {
      filter_output_array[u] -= average;
      filter_output_array[u] = (filter_output_array[u] + 0x8000)/256;
   }

}  /* end Filter() */

int main(void)
{
    UART_Display_Setup();    // Set up UART1 for dsPIC->PC display communications for SimpleChart waveform plotting
    LCD_Display_Setup();    // Set up LCD display to display waveform outputs
    makewaves();            // Generate multiple tone waveform        
    Filter();                // Filter out unwanted frequency components

        while(1)            
        {
        if(!UnfilteredButton){                            // Update display with Input data if Unfiltered button is pressed
            UART_Display(NUM_SAMPLES,array_plot);        // Display filter input multiple tone waveform
            LCD_Display_array(array_plot);
            while(!UnfilteredButton);                    // Wait for Unfiltered Button to be released
            }
        if(!FilteredButton){                                // Update display with Filter output data if Filtered button is pressed
            UART_Display(NUM_SAMPLES,filter_output_array);    // Display output of filter
            LCD_Display_array(filter_output_array);
            while(!FilteredButton);                        // Wait for Filtered button to be released
            }
        }
}

⌨️ 快捷键说明

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