// put a voltage v on a 3 Phase motor at specified commutation angle
void Write3PH_DACs(CHAN *ch, float v, double angle_in_cycles)
{
	float theta,sin1f;

	if (angle_in_cycles<0)
		theta = 1.0f-fractionf(-angle_in_cycles);
	else
		theta = fractionf(angle_in_cycles);

	DAC(ch->OutputChan0, (int)(v * Sine3PH(theta,&sin1f)));
	DAC(ch->OutputChan1, (int)(v * sin1f));
}
Example #2
0
int main(void)
{
    int value, step, idx;

    Init_port();
    Init();
    Init_timers();
    Init_ADC_sine();
    Init_DAC_sine();

    for (idx = 0; idx < BUFSIZE; idx++)
    {
        buf[idx] = (int) (double) (sin(2 * PI * idx / (BUFSIZE))*(double) MAX_VAL);
    }
    idx = 0;

    while (1)
    {
        // Wait for timeout of Timer4
        while (!IFS1bits.T4IF)
        {
            DAC(value, value);
        }
        IFS1bits.T4IF = 0;

        // Define stepwidth
        step = 20 + (ADC1BUF0 + 512);
        // Define index in buffer
        idx = idx + step;
        if (idx >= BUFSIZE) idx -= BUFSIZE;
        value = buf[idx];
        // New value to ADC converters
        DAC(value, value);
    }
    return (0);
}
Example #3
0
void main()
{
 uchar i=0;
 uchar vot[4];

interrupt_init();
 lcdInitial();

//DAC(0);
 while(1)
 {
    if(ADFlag==1)
    {
       for(i=0; i<4; i++) vot[i]=read(i);
    DAC(200);//DA转换函数,0-255渐暗
  }
    dis(vot);
 }
}
Example #4
0
static void fill_audio(void *udata, Uint8 *stream, int len)
{
    sample_t *samples = (sample_t*)stream;
	audio_t *audio = (audio_t*)udata;
    int num_samples = len / (sizeof(sample_t));
    for(int i = 0; i < num_samples; i++)
    {
        float t = audio->buffer_pos / (float)FREQUENCY;
        int16_t val_l = 0;
        int16_t val_r = 0;
        uint8_t channels[4];
#if 1
        if(audio->sq1.en)
        {
            channels[0] = square1(t, audio);
        }

        if(audio->sq2.en)
        {
            channels[1] = square2(t, audio);
        }

        if(audio->wave.en && audio->wave.power)
        {
            channels[2] = wave(t, audio);
        }
#endif

#if 1
		if (audio->noise.en)
		{
			channels[3] = noise(t, audio);
		}
#endif

        for(int i = 0; i < 4; i++)
        {
            int16_t v = DAC(channels[i]) / 4;
            if(BIT_N(audio->control.r_en, i))
            {
                val_r += v;
            }
            if(BIT_N(audio->control.l_en, i))
            {
                val_l += v;
            }
        }

		samples[i] = (sample_t) { .left = val_l / 4, .right = val_r / 4 };
		audio->buffer_pos = (audio->buffer_pos + 1) % FREQUENCY;
    }
}

static void init_wave_table(audio_t *audio)
{
	const uint8_t wave_default_table[32] =
	{
		0x4, 0x8, 0x0, 0x4, 0x3, 0x4,0xA, 0xA,
		0xD, 0x2, 0x8, 0x7, 0x2, 0x9,0xC, 0x3,
		0x0, 0x6, 0x9, 0x5, 0x9, 0x5,0x0, 0xB,
		0x4, 0x3, 0x8, 0xB, 0xE, 0x2,0xA, 0xD,
	};
	memcpy(audio->wave_table, wave_default_table, sizeof(audio->wave_table));
}

audio_t *audio_init(cpu_state_t *state)
{
	audio_t *out = CALLOC(1, sizeof(audio_t));
	out->state   = state;

	init_wave_table(out);

#if AUDIO
	SDL_AudioSpec wanted;
	/* Set the audio format */
	wanted.freq     = FREQUENCY;
	wanted.format   = AUDIO_S16;
	wanted.channels = NUM_CHANNELS;
	wanted.samples  = NUM_SAMPLES;
	wanted.callback = fill_audio;
	wanted.userdata = out;

	/* Open the audio device, forcing the desired format */
	if ( SDL_OpenAudio(&wanted, NULL) < 0 )
	{
        log_error("Couldn't open audio: %s\n", SDL_GetError());
	}
	SDL_PauseAudio(0);
#endif
	return out;
}