Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lab_4 / МПУ_ЛАБ_4

.txt
Скачиваний:
34
Добавлен:
18.05.2014
Размер:
1.89 Кб
Скачать
#include "Board.h"
#include "math.h"

static const int ledMask[2]= {LED1, LED2};
static AT91PS_PIO pioa = AT91C_BASE_PIOA;
static AT91PS_PMC pmc = AT91C_BASE_PMC;
static AT91PS_ADC adc = AT91C_BASE_ADC;

void setUpLeds (void)
	{
	pioa->PIO_PER = LED_MASK;
	pioa->PIO_OER = LED_MASK;
	pioa->PIO_SODR = LED_MASK;
	}

void setupADC (void)
	{
	adc->ADC_MR = AT91C_ADC_LOWRES_10_BIT | AT91C_ADC_SLEEP_NORMAL_MODE | (0x07 <<8) | (0x02 << 24);
	adc->ADC_CHER = AT91C_ADC_CH4;
	}

void setUpButtons (void)
	{
 	pioa->PIO_PER = SW_MASK;
 	pmc->PMC_PCER = 1 << AT91C_ID_PIOA;
	}

unsigned int getADCvalue (void)
	{
	adc->ADC_CR = AT91C_ADC_START;
	while (! (adc->ADC_SR & AT91C_ADC_EOC4)) {};
	const unsigned int adcCode = adc->ADC_CDR4;
	return adcCode;
	}


void indicate (int index)
	{
	pioa->PIO_CODR = ledMask[index];
                                                volatile unsigned int pauseCounter = 1000000;
                                                while (--pauseCounter > 0) {};
	pioa->PIO_SODR = (~ledMask[index]) & LED_MASK;
	}


int pressedButton (void)
	{
	unsigned int pinsStatus = pioa->PIO_PDSR;
	if (((~pinsStatus) & SW1)||((~pinsStatus) & SW2))
	return 1;
	return 0;
	}

void init(void)
	{
	setUpLeds();
	setUpButtons();
	setupADC();

	}

double newdisp(double oldDisp, double newValue, int n)
	{
	double disp;
	disp=oldDisp*(n-1)/n+(newValue-15)*(newValue-15)/n;
	return (sqrt(disp));
	}

int main(void)
	{
	static const double K=0.04;
	static const int X=15;
	init();
	while (1)
	{
	int buttonIndex = pressedButton();
	if (buttonIndex ==1)
	{
	static double D=0;
	static double Ux=0;
 	static int Value=0;
	pioa->PIO_SODR = LED_MASK;
	for(int n=1;n<=20000;n++)
	{
		Value=getADCvalue();
		Ux=Value*K;
		D=newdisp(D, Ux, n);
	}
    if (Ux>=20)
      indicate(1);
    }
  }
}
Соседние файлы в папке Lab_4
  • #
    18.05.20144.09 Кб29MT-Link.launch
  • #
    18.05.2014171 б29olimex.gdb
  • #
    18.05.20144.09 Кб29Olimex.launch
  • #
    18.05.201445 б29simulator.gdb
  • #
    18.05.20143.16 Кб29Simulator.launch
  • #
    18.05.20141.89 Кб34МПУ_ЛАБ_4.txt