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

📄 gene_sinus.c

📁 这是ARM的一个例子
💻 C
字号:
//*----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : dac_sine.c
//* Object              : Sinus wave generation for the AT91EB55
//*
//* 1.0 00/00/00 JCZ    : Creation
//*----------------------------------------------------------------------------

#ifndef AT91_DEBUG_NONE
#include <stdio.h>
#endif

#include <math.h>
#define _pi_       3.14159265358979323846

#include "parts/m55800/lib_m55800.h"
#include "targets/eb55/eb55.h"
#include "drivers/analog/analog.h"
#include "lib_tools.h"

extern AnalogDACTimerIrqDesc it_timer;
extern AnalogDescDac AnalogDac;
//* assembly Handler for software PDC
extern TypeAICHandler at91_asm_irq_timer_loop;

extern TypeAICHandler at91_asm_irq_check_pdc;


extern u_int mcki;

//* Local Variable
extern u_short sinus_table[4096*2];
const u_short sinus[]= {
    0x0200,    0x020C,    0x0218,    0x0224,    0x0231,    0x023D,    0x0249,    0x0255,
    0x0261,    0x026D,    0x0279,    0x0285,    0x0291,    0x029D,    0x02A9,    0x02B4,
    0x02C0,    0x02CB,    0x02D6,    0x02E1,    0x02EC,    0x02F7,    0x0302,    0x030C,
    0x0316,    0x0321,    0x032B,    0x0334,    0x033E,    0x0347,    0x0351,    0x035A,
    0x0362,    0x036B,    0x0373,    0x037C,    0x0384,    0x038B,    0x0393,    0x039A,
    0x03A1,    0x03A8,    0x03AE,    0x03B4,    0x03BA,    0x03C0,    0x03C5,    0x03CA,
    0x03CF,    0x03D4,    0x03D8,    0x03DC,    0x03E0,    0x03E3,    0x03E6,    0x03E9,
    0x03EC,    0x03EE,    0x03F0,    0x03F2,    0x03F3,    0x03F4,    0x03F5,    0x03F5,
    0x03F6,    0x03F5,    0x03F5,    0x03F4,    0x03F3,    0x03F2,    0x03F0,    0x03EE,
    0x03EC,    0x03E9,    0x03E6,    0x03E3,    0x03E0,    0x03DC,    0x03D8,    0x03D4,
    0x03CF,    0x03CA,    0x03C5,    0x03C0,    0x03BA,    0x03B4,    0x03AE,    0x03A8,
    0x03A1,    0x039A,    0x0393,    0x038B,    0x0384,    0x037C,    0x0373,    0x036B,
    0x0362,    0x035A,    0x0351,    0x0347,    0x033E,    0x0334,    0x032B,    0x0321,
    0x0316,    0x030C,    0x0302,    0x02F7,    0x02EC,    0x02E1,    0x02D6,    0x02CB,
    0x02C0,    0x02B4,    0x02A9,    0x029D,    0x0291,    0x0285,    0x0279,    0x026D,
    0x0261,    0x0255,    0x0249,    0x023D,    0x0231,    0x0224,    0x0218,    0x020C,
    0x01FF,    0x01F3,    0x01E7,    0x01DB,    0x01CE,    0x01C2,    0x01B6,    0x01AA,
    0x019E,    0x0192,    0x0186,    0x017A,    0x016E,    0x0162,    0x0156,    0x014B,
    0x013F,    0x0134,    0x0129,    0x011E,    0x0113,    0x0108,    0x00FD,    0x00F3,
    0x00E9,    0x00DE,    0x00D4,    0x00CB,    0x00C1,    0x00B8,    0x00AE,    0x00A5,
    0x009D,    0x0094,    0x008C,    0x0083,    0x007B,    0x0074,    0x006C,    0x0065,
    0x005E,    0x0057,    0x0051,    0x004B,    0x0045,    0x003F,    0x003A,    0x0035,
    0x0030,    0x002B,    0x0027,    0x0023,    0x001F,    0x001C,    0x0019,    0x0016,
    0x0013,    0x0011,    0x000F,    0x000D,    0x000C,    0x000B,    0x000A,    0x000A,
    0x000A,    0x000A,    0x000A,    0x000B,    0x000C,    0x000D,    0x000F,    0x0011,
    0x0013,    0x0016,    0x0019,    0x001C,    0x001F,    0x0023,    0x0027,    0x002B,
    0x0030,    0x0035,    0x003A,    0x003F,    0x0045,    0x004B,    0x0051,    0x0057,
    0x005E,    0x0065,    0x006C,    0x0074,    0x007B,    0x0083,    0x008C,    0x0094,
    0x009D,    0x00A5,    0x00AE,    0x00B8,    0x00C1,    0x00CB,    0x00D4,    0x00DE,
    0x00E9,    0x00F3,    0x00FD,    0x0108,    0x0113,    0x011E,    0x0129,    0x0134,
    0x013F,    0x014B,    0x0156,    0x0162,    0x016E,    0x017A,    0x0186,    0x0192,
    0x019E,    0x01AA,    0x01B6,    0x01C2,    0x01CE,    0x01DB,    0x01E7,    0x01F3
} ;

#define SIZE_BUFFER (sizeof(sinus)/sizeof(u_short))
// * Local variable
static const char main_menu[]=
{
"\n\r  COMMAND MENU:Sinus Gene\n\r"
"  0)  Info \n\r"
"  1)  Get timer value \n\r"
"  2)  Get sinus val \n\r"
"  3)  Rampping \n\r"
"  4)  get 256 pt sinus val \n\r"
"  5)  Print \n\r"
"  6)  Stop DAC 0\n\r"
"  7)  Stop DAC 1\n\r"
"  8)  Run DAC 0 in loop mode not guarantee\n\r"
"  9)  Run DAC 1 in loop mode not guarantee\n\r"
" 10)  Run DAC 0 with Software PDC \n\r"
" 11)  Run DAC 0 with Software PDC \n\r"
" 12)  Set echo \n\r"
" 99)  Quit program\n\r"
"Enter command to execute: "
};

//*----------------------------------------------------------------------------
//* Function Name       : Get_Command_sinus
//* Object              : Get command val
//* Input Parameters    : None
//* Output Parameters   : int : Command num
//*----------------------------------------------------------------------------
int Get_Command_sinus(void)
{
  int command;
  at91_print_frame(&COM,(char *)main_menu,sizeof(main_menu));
  at91_scanf(&COM,"%d", &command);
  at91_print_crlf(&COM);
  return command;
}

//*----------------------------------------------------------------------------
//* Function Name       : menu
//* Object              : check general menu
//* Input Parameters    : None
//* Output Parameters   : None
//*----------------------------------------------------------------------------
void menu_sinus(void)
{
    int command = 0;
    int tioc;
    int tc_div = TC_CLKS_MCK32;
    int begin_val,end_val,nb_point,cmpt,min,max;
    int step;
    int div;
    u_short *sinus_gene = &sinus_table[1];

    double sin_val;
    sinus_table[0]=0x03FF;
    //*int val
    nb_point = 25;
    tioc = 100;
    begin_val=0x100;
    end_val=0x3A0;
         for (cmpt = 0,max = 0 ,min =-1; cmpt < nb_point;cmpt++)
         {
            //* compute sinus  by point
                sin_val = sin((2*_pi_)*((double)cmpt/(double)nb_point));
            //* scale
            sin_val= sin_val * ((end_val-begin_val)/2);
            sin_val += ((end_val -begin_val)/2);
            sin_val += begin_val;
            sinus_gene[cmpt]= (u_short )sin_val ;
            if ( max > sinus_gene[cmpt]) max =sinus_gene[cmpt];
                if ( min < sinus_gene[cmpt]) min = sinus_gene[cmpt];
        }
     timer_interrup.echo =0;

while(command!=99)
  {
    command = Get_Command_sinus();
    switch(command)
    {
     case 0:          /*  Info */
        sprintf(message," output min 0x%04X max 0x%04X nb point %d 0x%04X \n\r",min,max,nb_point,nb_point);
        at91_print(&COM,message);
        print_timer_mode(tioc,tc_div);


    // * calculate the frequnecy
        if (tc_div == TC_CLKS_MCK1024) div = 1024;
        if (tc_div == TC_CLKS_MCK128) div = 128;
        if (tc_div == TC_CLKS_MCK32) div = 32;
        if (tc_div == TC_CLKS_MCK8) div = 8;
        if (tc_div == TC_CLKS_MCK2) div = 2;
        div = (tioc*nb_point*div);
        if (div > mcki)
        sprintf(message," frequency generation = %d mhZ\n\r",(mcki*1000)/div);
        else
        sprintf(message," frequency generation = %d Hz\n\r", mcki/div );
        at91_print(&COM,message);

     break;
     case 1:         /* Set Timer*/
        get_timer_mode(&tioc,&tc_div);

     break;

     case 2:        /* Get Sinus Val*/
     //* Get Value
        at91_print(&COM,"Low DC Value (Hex):");
        at91_scanf(&COM,"%X", &begin_val);
        sprintf(message,"\n\r%d Hex:%04X\n\r",begin_val,begin_val);
        at91_print(&COM,message);
        at91_print(&COM,"MAX DC Value (Hex):");
        at91_scanf(&COM,"%X", &end_val);
        sprintf(message,"\n\r%d Hex:%04X\n\r",end_val,end_val);
        at91_print(&COM,message);
        at91_print(&COM," nb_point (dec):");
        at91_scanf(&COM,"%d", &nb_point);
        sprintf(message,"\n\r%d Hex:%04X\n\r",nb_point,nb_point);
        at91_print(&COM,message);
     //* Compute sinus
         for (cmpt = 0,max = 0 ,min =-1; cmpt < nb_point;cmpt++)
         {
            //* compute sinus  by point
                sin_val = sin((2*_pi_)*((double)cmpt/(double)nb_point));
            //* scale
            sin_val= sin_val * ((end_val-begin_val)/2);
            sin_val += ((end_val -begin_val)/2);
            sin_val += begin_val;
            sinus_gene[cmpt]= (u_short )sin_val ;
            if ( max > sinus_gene[cmpt]) max =sinus_gene[cmpt];
                if ( min < sinus_gene[cmpt]) min = sinus_gene[cmpt];
        }
    //* print result
        sprintf(message," min 0x%04X max 0x%04X nb val %d \n\r",max,min,cmpt);
        at91_print(&COM,message);
        break;
     case 3:         /* Set DC Value */
        at91_print(&COM," begin DC Value (Hex):");
        at91_scanf(&COM,"%X", &begin_val);
        sprintf(message," \n\r%d Hex:%04X \n\r",begin_val,begin_val);at91_print(&COM,message);
        at91_print(&COM," step (dec):");
        at91_scanf(&COM,"%d", &step);
        sprintf(message," \n\r%d Hex:%04X \n\r",step,step);at91_print(&COM,message);
        at91_print(&COM," nb (HEX):");
        at91_scanf(&COM,"%X", &nb_point);
        sprintf(message," \n\r%d Hex:%04X \n\r",nb_point,nb_point);at91_print(&COM,message);
        sinus_gene[0]= begin_val;
        for (cmpt = 1,max = 0 ,min =-1; cmpt <= nb_point;cmpt++)
        {
            sinus_gene[cmpt]= sinus_gene[cmpt-1]+ step;
            if ( max > sinus_gene[cmpt]) max =sinus_gene[cmpt];
            if ( min < sinus_gene[cmpt]) min =sinus_gene[cmpt];
        }
        for (; cmpt <= 2* nb_point ;cmpt++)
        {
            sinus_gene[cmpt]= sinus_gene[cmpt-1]- step;
            if ( max > sinus_gene[cmpt]) max =sinus_gene[cmpt];
            if ( min < sinus_gene[cmpt]) min =sinus_gene[cmpt];
        }
         sprintf(message," min %04X max %04X nb val %d \n\r",max,min,cmpt);at91_print(&COM,message);
        nb_point=cmpt-1;
        break;

     case 4:
        for ( max = 0 ,min =-1 ,cmpt = 0,nb_point=0; cmpt <SIZE_BUFFER ;cmpt++)
        {
            sinus_gene[cmpt] = sinus[cmpt];
            if ( max > sinus_gene[cmpt]) max =sinus_gene[cmpt];
            if ( min < sinus_gene[cmpt]) min =sinus_gene[cmpt];
            nb_point++;
        }
        break;
     case 5:
        for (cmpt = 0; cmpt < nb_point;cmpt++)
        {
            if ( !(cmpt %8)) at91_print_crlf(&COM);
            sprintf(message,"%04X ",sinus_gene[cmpt]);at91_print(&COM,message);
        }
        break;
     case 6:      /* Command Reset */
        AnalogDac.dac=&DAC0_DESC;
        AnalogDac.timer=timer_base.TC_dac0;
        at91_analog_close_dac( &AnalogDac);
     break;
     case 7:      /* Command Reset */
        AnalogDac.dac=&DAC1_DESC;
        AnalogDac.timer=timer_base.TC_dac1;
        at91_analog_close_dac(&AnalogDac);
     break;

     case 8:       /*  DAC0 PDC hard  */
        AnalogDac.dac=&DAC0_DESC;
        AnalogDac.timer=timer_base.TC_dac0;
//      at91_analog_open_dac_loop(&AnalogDac,timer_base.trig_selection_dac0 ,tioc,tc_div,sinus_table,nb_point);
        //* Open the interrupt on the AIC
         at91_irq_open ( AnalogDac.timer->periph_id, 7, AIC_SRCTYPE_INT_EDGE_TRIGGERED, &at91_asm_irq_check_pdc ) ;
         //* Enable the RC Compare interrupt
        AnalogDac.timer->tc_base->TC_IER = TC_CPCS ;
        break;
    case 9:      /*  DAC1 PDC hard  */
        AnalogDac.dac=&DAC1_DESC;
        AnalogDac.timer=timer_base.TC_dac1;
//      at91_analog_open_dac_loop(&AnalogDac,timer_base.trig_selection_dac1,tioc,tc_div,sinus_table,nb_point);
        //* Open the interrupt on the AIC
         at91_irq_open ( AnalogDac.timer->periph_id, 7, AIC_SRCTYPE_INT_EDGE_TRIGGERED, &at91_asm_irq_check_pdc ) ;
         //* Enable the RC Compare interrupt
        AnalogDac.timer->tc_base->TC_IER = TC_CPCS ;
        break;

    case 10:         /*  DAC0 PDC soft  */
        it_timer.dac=&DAC0_DESC;
        it_timer.timer=timer_base.TC_dac0;
        it_timer.AsmDacHandler=&at91_asm_irq_timer_loop;
        timer_interrup.cmpt=0;
        at91_analog_open_dac_loop_soft(&it_timer,timer_base.trig_selection_dac0 ,tioc,tc_div,sinus_gene,nb_point);
        break;
    case 11:         /*  DAC1 PDC soft  */
        it_timer.dac=&DAC1_DESC;
        it_timer.timer=timer_base.TC_dac1;
        it_timer.AsmDacHandler=&at91_asm_irq_timer_loop;
        timer_interrup.cmpt=0;
        at91_analog_open_dac_loop_soft(&it_timer,timer_base.trig_selection_dac1,tioc,tc_div,sinus_gene,nb_point);
         break;
    case 12:         /*  set echo  */
        at91_print(&COM,"\n\r set the echo :");
        timer_interrup.echo = at91_get_val(&COM,timer_interrup.echo );
        break;
    case 99:         /*  quit  */
        break;

    default:         /*  invalid command  */
        at91_print(&COM,"Invalid command entered. Please enter again!\n\r");
    }
  }
}

⌨️ 快捷键说明

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