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

📄 comm.c

📁 MSP430F1610 generates a sine wave from a look up table using timer interrupt.
💻 C
字号:
#include  <msp430x16x.h>
#include  <defines.h>
#include  <comm.h>
#include  <comm_vars.h>

//---ADC convertion---
unsigned char Rampa = 0;
unsigned int byte1, byte2, byte3, byte4, channel;    // Used in adc_convert().
unsigned long int byte0;
unsigned int result=10000;

unsigned int adc_data[4];                   // Storage for converted data.
unsigned int ADC_Buffer_index = 0;

unsigned char paquete[PACKETSIZE];              // Holds each 8 byte packet
//unsigned char AcquireBuffer[BIG_BUFFER_SIZE]; // Holds samples acquired until reach 512 samples.
//unsigned char BigPaquete[BIG_BUFFER_SIZE];      // Holds buffer of 512 byte samples used by DMA.
//unsigned char BigPaquete2[BIG_BUFFER_SIZE];     // Holds buffer of 512 byte samples used by DMA.

__no_init unsigned char   DataFromPC[PC_to_MSP_Nr_SAMPLES * 4 ];       //2048 bytes used by DMA0 to get 512 PC samples.
extern unsigned int    DAC12_Buffer_1[DAC_DMA_BLOCK_SIZE];    //1024 bytes to store 512 samples from PC
extern unsigned int    DAC12_Buffer_2[DAC_DMA_BLOCK_SIZE];    //1024 bytes to store 512 samples from PC


//unsigned int SampleNumber;                // Used by make_packet()
int packet_INDEX = START_PACKET_INDEX;    // Used by make_packet()
unsigned char orden;


//static char string1[] = { "Hello World\r\n" };

//-- SynchSamples( )
    unsigned int loopcounter = 0;
    unsigned int total_loopcounter = 0;
    unsigned int previous_sample=0;
    int storeBufferIndex = 0;
    unsigned char lost1_sample = 0;
    int synch_i=0, j_loop=0, synch_k=0;
    unsigned int crc_result = 0; unsigned char crc_valid = 0;
    unsigned int temp_goodsamples = 0;
    unsigned char temp_synch_Buff[8];

//==================== config_DMA0 () ===================================
void config_DMA(
                unsigned char what_to_config,
                unsigned int bytes_DMA_RX,
                unsigned int bytes_DMA_TX,
                unsigned char * RXAddress,
                unsigned char * TXAddress)
{
  switch(what_to_config)
  { 
      case DMA_RX:                 //(0)
           {
              //--------DMA1 CONFIGURATION
              DMA1SZ = bytes_DMA_RX;             // Size in bytes
              break;
           }
      case DMA_TX:                 //(1)
           {
              //--------DMA0 CONFIGURATION
              DMA0SZ = bytes_DMA_TX;             // Size in bytes
              break;
           }
      case DMA_RX_TX:              //(2)
           {
//              //--------DMA1 CONFIGURATION
              DMA1SA = U0RXBUF_;                      // Src address = UART RX Buffer
              DMA1DA = (unsigned int)DataFromPC;       // Dst address = RAM memory
              DMA1SZ = PC_to_MSP_Nr_SAMPLES * 4;                  // Size in bytes
              DMA1CTL = DMADSTINCR_3 + DMASBDB  +  DMAEN  + DMADT_4 + DMAIE  ;
                      //Inc.Dest.Add + 1B to 1B +         + Repeat single
              
              //-------- FOR BOTH DMA CHANNELS:
              DMACTL0 = DMA1TSEL_3;//DMA1TSEL_9;// + DMA0TSEL_10;     // DMA1=URXIFG1, DMA0=UTXIFG1
              break;
           }
      case DMA_RX_TX_DAC12:                 //(3)
           {

               break;
           }           
           
           
  }//End of switch-case

}

//================== delay_time (int cycles) ============================
void delay_time_comm(int cycles)
{ int i_comm,j_comm;  i_comm=j_comm=0;
  for (i_comm = cycles ; i_comm > 0 ; i_comm--) 
    {   for (j_comm = 5; j_comm > 0; j_comm--); }
}
//==================== RX_complete ( ) ==================================
void RX_complete(void)
{   do { IFG1 &=~ URXIFG0;
       } while (URXIFG0 & IFG1);      
}
//==================== adc_convert (int chann) ============================
//  Description :  This prototype does the adc conversion
unsigned int adc_convert()
{ 

  return result;
  //return 10000;
}
//==================== addToBuffer ( ) ============================
void addToBuffer(unsigned int sample__value)
{
  unsigned int tempo_result1 = 0, tempo_result2 = 0, i_addBuffer;
}
//==================== CRC16 ( ) ===================================
unsigned int addCRC( unsigned int CRC, unsigned char b )
{
    return ( CRC >> 8 ) ^ CRCtbl[ ( CRC & 0xFF ) ^ b ];
}
//==============================================================================
/////////////////////////// CheckARMPacket ( ) ///////////////////////////
//==============================================================================
unsigned char CheckARMPacket(unsigned char* ARM_vector)
{
  unsigned int temp1_checkARM = 0, temp2_checkARM = 0, temp3_checkARM = 0, i_checkARM;
  unsigned char resutl_checkARM = 0;

  return resutl_checkARM;
}
//==============================================================================
/////////////////////////// make_PACKET_advanced ( ) ///////////////////////////
//==============================================================================
void make_PACKET_advanced(unsigned int OrderOfSample_adv,unsigned int sample_value_adv)
{ 
}
//==================== make_PACKET_2 ( ) ==================================
void make_PACKET_2(unsigned char task_command, unsigned int * data_to_pack, unsigned int nr_samples_packed)
{ 
}
//==================== copyBuffer( ) ================================

void copyBuffer(unsigned int FillLoopCounter)
{
}
//==================== send_UART1( ) ================================
void send_UART1(unsigned char dataToUart)
{
  int k;

  while ((IFG2 & UTXIFG1) != UTXIFG1);      //USART0 TX buffer ready?. 
  TXBUF1 = dataToUart;
}
//==================== send_UART1( ) ================================
void send_UART0(unsigned char dataToUart)
{
  int k;

  while ((IFG1 & UTXIFG0) != UTXIFG0);      //USART0 TX buffer ready?.
  TXBUF0 = dataToUart;
}
//==============================================================================
/////////////////////// SynchSamples ( ) ///////////////////////////////////////
//==============================================================================

unsigned int SynchSamples(unsigned char buffer_nr)
{
    /******************************
    USED ONLY FOR RECEIVING BINARY 
    FILES FROM PC FOR DAC output
    ******************************/    

    unsigned int  make_packet_index = 0, value;
    unsigned char tempo_value = 0;
    
    loopcounter = 0;temp_goodsamples = 0;total_loopcounter=0;
  
    synch_i=0; j_loop=0; synch_k=0;lost1_sample = 0;
  
    //loopcounter = PDA_TO_IOC_BLOCK_SIZE 8;// There are 8 bytes per packet 
    loopcounter = PC_to_MSP_Nr_PACKETS;
    
    for( synch_i=1 ; synch_i <= loopcounter ; synch_i++ )
    {
        crc_valid = 0;
    
        for( j_loop=0 ; j_loop < 8 ; j_loop++ )
        {   // Try to sync with a moving window of up to 8 bytes.
            temp_synch_Buff[0] = DataFromPC[synch_i * 8 + j_loop - 8];
            temp_synch_Buff[1] = DataFromPC[synch_i * 8 + j_loop - 7];
            temp_synch_Buff[2] = DataFromPC[synch_i * 8 + j_loop - 6];
            temp_synch_Buff[3] = DataFromPC[synch_i * 8 + j_loop - 5];
            temp_synch_Buff[4] = DataFromPC[synch_i * 8 + j_loop - 4];
            temp_synch_Buff[5] = DataFromPC[synch_i * 8 + j_loop - 3];      
    
            //-- CRC validation:
            crc_result = 0; 
            for( synch_k=0 ; synch_k < 6  ; synch_k++ )
            {  crc_result = addCRC( crc_result , temp_synch_Buff[synch_k] );  }
          
            if(( (256*DataFromPC[synch_i*8+j_loop-2])+DataFromPC[synch_i*8+j_loop-1]) == crc_result)
            {
                // Load index value
            
                // Load control value:
            
                //Mark as valid CRC
                crc_valid = 1;
                //Increment good samples counter:
                temp_goodsamples = temp_goodsamples +2;
            }
            if((synch_i == loopcounter)||(crc_valid==1))  {  j_loop = 7;   }
        }//End of for j_loop

        if(crc_valid==1)
        {
            //--Save raw data when a valid CRC is found.
            if(buffer_nr == 1)
            {
#ifdef BITS12
                value = (temp_synch_Buff[2] + 256 * temp_synch_Buff[3]) & 0x00FFFF;
                value = ((value >> 4) & 0x000FFF);
                if(value > DC_REMOVAL)    value = value - DC_REMOVAL;
                else    value = 0;
                DAC12_Buffer_1[storeBufferIndex]   = value;

                value = (temp_synch_Buff[4] + 256 * temp_synch_Buff[5]) & 0x00FFFF;
                value = ((value >> 4) & 0x000FFF);
                if(value > DC_REMOVAL)    value = value - DC_REMOVAL;
                else    value = 0;                
                DAC12_Buffer_1[1+storeBufferIndex] = value;

//                DAC12_Buffer_1[storeBufferIndex]   = (temp_synch_Buff[2] + 256 * temp_synch_Buff[3]) & 0x00FFFF;
//                DAC12_Buffer_1[storeBufferIndex]   = ((DAC12_Buffer_1[storeBufferIndex] >> 4) & 0x000FFF)-DC_REMOVAL;
//                DAC12_Buffer_1[1+storeBufferIndex] = (temp_synch_Buff[4] + 256 * temp_synch_Buff[5]) & 0x00FFFF;            
//                DAC12_Buffer_1[1+storeBufferIndex] = ((DAC12_Buffer_1[1+storeBufferIndex] >> 4) & 0x000FFF)-DC_REMOVAL;
#endif                
#ifdef BITS8
                DAC12_Buffer_1[storeBufferIndex]   = (temp_synch_Buff[3]) & 0x0000FF;
                DAC12_Buffer_1[1+storeBufferIndex] = (temp_synch_Buff[5]) & 0x0000FF;            
#endif      
                previous_sample = DAC12_Buffer_1[1 + storeBufferIndex];
            }
            else
            {
#ifdef BITS12 
                
                value = (temp_synch_Buff[2] + 256 * temp_synch_Buff[3]) & 0x00FFFF;
                value = ((value >> 4) & 0x000FFF);
                if(value > DC_REMOVAL)    value = value - DC_REMOVAL;
                else    value = 0;
                DAC12_Buffer_2[storeBufferIndex]   = value;

                value = (temp_synch_Buff[4] + 256 * temp_synch_Buff[5]) & 0x00FFFF;
                value = ((value >> 4) & 0x000FFF);
                if(value > DC_REMOVAL)    value = value - DC_REMOVAL;
                else    value = 0;                
                DAC12_Buffer_2[1+storeBufferIndex] = value;                 
                
//                DAC12_Buffer_2[storeBufferIndex]   = (temp_synch_Buff[2] + 256 * temp_synch_Buff[3]) & 0x00FFFF;
//                DAC12_Buffer_2[storeBufferIndex]   = ((DAC12_Buffer_2[storeBufferIndex] >> 4) & 0x000FFF)-DC_REMOVAL;
//                DAC12_Buffer_2[1+storeBufferIndex] = (temp_synch_Buff[4] + 256 * temp_synch_Buff[5]) & 0x00FFFF;            
//                DAC12_Buffer_2[1+storeBufferIndex] = ((DAC12_Buffer_2[1+storeBufferIndex] >> 4) & 0x000FFF)-DC_REMOVAL;
#endif                
#ifdef BITS8
                DAC12_Buffer_2[storeBufferIndex]   = (temp_synch_Buff[3]) & 0x0000FF;
                DAC12_Buffer_2[1+storeBufferIndex] = (temp_synch_Buff[5]) & 0x0000FF;            
#endif      
                previous_sample = DAC12_Buffer_2[1 + storeBufferIndex];
            }
            storeBufferIndex = storeBufferIndex + 2;
            lost1_sample = 1;    
        }
        else //--When CRC is not found copy previous sample in received buffer.
        {
            if(lost1_sample == 1)
            {
                if(buffer_nr == 1)
                {
                    DAC12_Buffer_1[storeBufferIndex]       = previous_sample;
                    DAC12_Buffer_1[1 + storeBufferIndex]   = previous_sample;
                }
                else
                {
                    DAC12_Buffer_2[storeBufferIndex]       = previous_sample;
                    DAC12_Buffer_2[1 + storeBufferIndex]   = previous_sample;
                }
                storeBufferIndex = storeBufferIndex + 2;
            }
            else //--CRC not valid in consecutives samples.
            {
                if(buffer_nr == 1)
                {
                    DAC12_Buffer_1[storeBufferIndex]       = previous_sample;
                    DAC12_Buffer_1[1 + storeBufferIndex]   = previous_sample;
                }
                else
                {
                    DAC12_Buffer_2[storeBufferIndex]       = previous_sample;
                    DAC12_Buffer_2[1 + storeBufferIndex]   = previous_sample;
                }
                storeBufferIndex = storeBufferIndex + 2;
            }
            lost1_sample = 0;    
        }// End of else when CRC not valid.
 
    }//End of for synch_i
    //-----------------------------------------------------
    if(storeBufferIndex >= PC_to_MSP_Nr_SAMPLES)// 
    {  
        storeBufferIndex = 0; 
    }
    return temp_goodsamples;
}



⌨️ 快捷键说明

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