Преобразование напряжения канала ADCINA0 в управление скоростью загорания светодиодов

Страницы работы

Фрагмент текста работы

Практическая часть

//#####################################################################

//         Имя файла:    _lab6А_.c

//

//         Описание:     Преобразование напряжения канала ADCINA0

//                     в управление скоростью загорания светодиодов

//                     (от 50 мс до 1 с).       Диапазон напряжений потенциометров,

//                     соединенных с каналами АЦП, от 0 до 3 В.

//                     ЗапускАЦПот EVA

//#####################################################################

#include "DSP281x_Device.h"

void Gpio_select(void);

void SpeedUpRevA(void);

void InitSystem(void);    

void show_ADC(int result);

interrupt void adc_isr(void);        // Prototype for ADC result ISR

int Voltage_A0;

int Voltage_B0;   

void main(void)

{

unsigned long i;

InitSystem();                      // Initialize the DSP's core Registers       

Gpio_select();                   // Setup the GPIO Multiplex Registers

InitPieCtrl();                       // Function Call to init PIE-unit ( code : DSP281x_PieCtrl.c)

InitPieVectTable(); // Function call to init PIE vector table ( code : DSP281x_PieVect.c )

InitAdc();                                           // Function call for basic ADC initialisation 

EALLOW;  // This is needed to write to EALLOW protected registers

PieVectTable.ADCINT = &adc_isr;

EDIS;    // This is needed to disable write to EALLOW protected registers

PieCtrlRegs.PIEIER1.bit.INTx6 = 1;                               

IER = 1;

EINT;   // Enable Global interrupt INTM

ERTM;   // Enable Global realtime interrupt DBGM

AdcRegs.ADCTRL1.bit.SEQ_CASC = 0;         // Dual Sequencer Mode

AdcRegs.ADCTRL1.bit.CONT_RUN = 0;       // No Continuous run

AdcRegs.ADCTRL1.bit.CPS = 0;                     // prescaler = 1            

AdcRegs.ADCMAXCONV.all = 0x0001;       // Setup 2 conv's on SEQ1 

AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0; // Setup ADCINA0 as 1st SEQ1 conv.// Assumes EVA Clock is already enabled in InitSysCtrl();

AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 0x8; // Setup ADCINB0 as 2nd SEQ1 conv.// Drive T1PWM / T2PWM by T1/T2 - logic

AdcRegs.ADCTRL2.bit.EVA_SOC_SEQ1 = 1;  // Enable EVASOC to start SEQ1// Polarity of GP Timer 1 Compare = Active low

AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1 = 1;// Enable SEQ1 interrupt (every EOEvaRegs.GPTCONA.bit.T1PIN = 1;

AdcRegs.ADCTRL3.bit.ADCCLKPS = 2;     // Divide HSPCLK by 4

EvaRegs.GPTCONA.bit.TCMPOE = 0;

EvaRegs.GPTCONA.bit.T1PIN = 0;

EvaRegs.GPTCONA.bit.T1TOADC = 2;       // Enable EVASOC in EVA

EvaRegs.T1CON.bit.FREE = 0;                                   // Stop on emulation suspend

EvaRegs.T1CON.bit.SOFT = 0;                                             // Stop on emulation suspend

EvaRegs.T1CON.bit.TMODE = 2;                                        // Continuous up count mode

EvaRegs.T1CON.bit.TPS = 7;                                                // prescaler = 128

EvaRegs.T1CON.bit.TENABLE = 1;                                      // enable GP Timer 1

EvaRegs.T1CON.bit.TCLKS10 = 0;                            // internal clock

EvaRegs.T1CON.bit.TCLD10 = 0;                              // Compare Reload when zero

EvaRegs.T1CON.bit.TECMPR = 0;                            // Disable Compare operation                              

EvaRegs.T1PR = 58594;    

while(1)

{   

for(i=0;i<1500000;i++)

{

EALLOW;

SysCtrlRegs.WDKEY = 0xAA;                       // and serve watchdog #2                         

EDIS;

show_ADC(Voltage_A0>>8);        /* displays the latest result on LED's */

}

for(i=0;i<1500000;i++)

{

EALLOW;

SysCtrlRegs.WDKEY = 0xAA;                       // and serve watchdog #2                         

EDIS;

show_ADC(Voltage_B0>>8); 

}

}

}                             

void Gpio_select(void)

{

EALLOW;

GpioMuxRegs.GPAMUX.all = 0x0;            // all GPIO port Pin's to I/O  

GpioMuxRegs.GPBMUX.all = 0x0;  

GpioMuxRegs.GPDMUX.all = 0x0;

GpioMuxRegs.GPFMUX.all = 0x0;                      

GpioMuxRegs.GPEMUX.all = 0x0;

GpioMuxRegs.GPGMUX.all = 0x0;                                                                                                              

GpioMuxRegs.GPADIR.all = 0x0;           // GPIO PORT  as input

GpioMuxRegs.GPBDIR.all = 0x00FF;// GPIO Port B15-B8 input , B7-B0 output

GpioMuxRegs.GPDDIR.all = 0x0;           // GPIO PORT  as input

GpioMuxRegs.GPEDIR.all = 0x0;           // GPIO PORT  as input

GpioMuxRegs.GPFDIR.all = 0x0;           // GPIO PORT  as input

GpioMuxRegs.GPGDIR.all = 0x0;          // GPIO PORT  as input

GpioMuxRegs.GPAQUAL.all = 0x0;       // Set GPIO input qualifier values to zero

GpioMuxRegs.GPBQUAL.all = 0x0;

GpioMuxRegs.GPDQUAL.all = 0x0;

GpioMuxRegs.GPEQUAL.all = 0x0;

EDIS;

}    

void InitSystem(void)

{

EALLOW;

SysCtrlRegs.WDCR= 0x00AF;                      // Setup the watchdog

// 0x00E8  to disable the Watchdog , Prescaler = 1

// 0x00AF  to NOT disable the Watchdog, Prescaler = 64

SysCtrlRegs.SCSR = 0;                                  // Watchdog generates a RESET

SysCtrlRegs.PLLCR.bit.DIV = 10; // Setup the Clock PLL to multiply by 5

SysCtrlRegs.HISPCP.all = 0x1; // Setup Highspeed Clock Prescaler to divide by 2

SysCtrlRegs.LOSPCP.all = 0x2; // Setup Lowspeed CLock Prescaler to divide by 4              

SysCtrlRegs.PCLKCR.bit.EVAENCLK=1;

SysCtrlRegs.PCLKCR.bit.EVBENCLK=0;

SysCtrlRegs.PCLKCR.bit.SCIAENCLK=0;

SysCtrlRegs.PCLKCR.bit.SCIBENCLK=0;

SysCtrlRegs.PCLKCR.bit.MCBSPENCLK=0;

SysCtrlRegs.PCLKCR.bit.SPIENCLK=0;

SysCtrlRegs.PCLKCR.bit.ECANENCLK=0;

SysCtrlRegs.PCLKCR.bit.ADCENCLK=1;

EDIS;

}  

void show_ADC(int result)

{

switch(result) {

case 0 : GpioDataRegs.GPBDAT.all=0x0000;break;

case 1 : GpioDataRegs.GPBDAT.all=0x0001;break;

}

result>>=1;

switch(result) {

case 1 : GpioDataRegs.GPBDAT.all=0x0003;break;

case 2 : GpioDataRegs.GPBDAT.all=0x0007;break;

case 3 : GpioDataRegs.GPBDAT.all=0x000F;break;

case 4 : GpioDataRegs.GPBDAT.all=0x001F;break;

case 5 : GpioDataRegs.GPBDAT.all=0x003F;break;

case 6 : GpioDataRegs.GPBDAT.all=0x007F;break;

case 7 : GpioDataRegs.GPBDAT.all=0x00FF;break;

}

}                

interrupt void adc_isr(void) 

{

// Serve the watchdog every Timer 0 interrupt

EALLOW;

SysCtrlRegs.WDKEY = 0x55;                       // Serve watchdog #1

EDIS; 

Voltage_A0 = AdcRegs.ADCRESULT0>>4;

Voltage_B0 = AdcRegs.ADCRESULT1>>4;

AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1;         // Reset SEQ1

AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;                             // Clear INT SEQ1 bit

PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;   // Acknowledge interrupt to PIE

}         

#include "DSP281x_Device.h"

void Gpio_select(void);

void SpeedUpRevA(void);

void InitSystem(void);     

void delay_loop(long);

interrupt void adc_isr(void);        // Prototype for ADC result ISR

int Voltage_A0;

int Voltage_B0;

unsigned int i;

unsigned int LED[8]= {0x0001,0x0002,0x0004,0x0008,

0x0010,0x0020,0x0040,0x0080};  

void main(void)

{

InitSystem();                      // Initialize the DSP's core Registers       

Gpio_select();                   // Setup the GPIO Multiplex Registers

InitPieCtrl();                       // Function Call to init PIE-unit ( code : DSP281x_PieCtrl.c)

InitPieVectTable(); // Function call to init PIE vector table ( code : DSP281x_PieVect.c )

InitAdc();                                           // Function call for basic ADC initialisation 

EALLOW;  // This is needed to write to EALLOW protected registers

PieVectTable.ADCINT = &adc_isr;

EDIS;    // This is needed to disable write to EALLOW protected registers

PieCtrlRegs.PIEIER1.bit.INTx6 = 1;                               

IER = 1;

EINT;   // Enable Global interrupt INTM

ERTM;   // Enable Global realtime interrupt DBGM

AdcRegs.ADCTRL1.bit.SEQ_CASC = 0;         // Dual Sequencer Mode

AdcRegs.ADCTRL1.bit.CONT_RUN = 0;       // No Continuous run

AdcRegs.ADCTRL1.bit.CPS = 0;                     // prescaler = 1            

AdcRegs.ADCMAXCONV.all = 0x0001;       // Setup 2 conv's on SEQ1 

AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0; // Setup ADCINA0 as 1st SEQ1 conv.// Assumes EVA Clock is already enabled in InitSysCtrl();

AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 0x8; // Setup ADCINB0 as 2nd SEQ1 conv.// Drive T1PWM / T2PWM by T1/T2 - logic

AdcRegs.ADCTRL2.bit.EVA_SOC_SEQ1 = 1;  // Enable EVASOC to start SEQ1// Polarity of GP Timer 1 Compare = Active low

AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1 = 1;// Enable SEQ1 interrupt (every EOEvaRegs.GPTCONA.bit.T1PIN = 1;

AdcRegs.ADCTRL3.bit.ADCCLKPS = 2;     // Divide HSPCLK by 4

EvaRegs.GPTCONA.bit.TCMPOE = 0;

EvaRegs.GPTCONA.bit.T1PIN = 0;

EvaRegs.GPTCONA.bit.T1TOADC = 2;       // Enable EVASOC in EVA

EvaRegs.T1CON.bit.FREE = 0;                                   // Stop on emulation suspend

EvaRegs.T1CON.bit.SOFT = 0;                                             // Stop on emulation suspend

EvaRegs.T1CON.bit.TMODE = 2;                                        // Continuous up count mode

EvaRegs.T1CON.bit.TPS = 7;                                                // prescaler = 128

EvaRegs.T1CON.bit.TENABLE = 1;                                      // enable GP Timer 1

EvaRegs.T1CON.bit.TCLKS10 = 0;                            // internal clock

EvaRegs.T1CON.bit.TCLD10 = 0;                              // Compare Reload when zero

EvaRegs.T1CON.bit.TECMPR = 0;                            // Disable Compare operation                              

EvaRegs.T1PR = 58594;    

while(1)

{   

for(i=0;i<14;i++)

{

if(i<7) GpioDataRegs.GPBDAT.all = LED[i];

else        GpioDataRegs.GPBDAT.all = LED[14-i];

delay_loop(1000000*Voltage_A0);

while(CpuTimer0.InterruptCount < (1+Voltage_A0/215))

{

EALLOW;

SysCtrlRegs.WDKEY = 0xAA;

SysCtrlRegs.WDKEY = 0x55;                                     

EDIS;

}

CpuTimer0.InterruptCount = 0;

}

}

}                             

void Gpio_select(void)

{

EALLOW;

GpioMuxRegs.GPAMUX.all = 0x0;            // all GPIO port Pin's to I/O  

GpioMuxRegs.GPBMUX.all = 0x0;  

GpioMuxRegs.GPDMUX.all = 0x0;

GpioMuxRegs.GPFMUX.all = 0x0;                      

GpioMuxRegs.GPEMUX.all = 0x0;

GpioMuxRegs.GPGMUX.all = 0x0;                                                                                                              

GpioMuxRegs.GPADIR.all = 0x0;           // GPIO PORT  as input

GpioMuxRegs.GPBDIR.all = 0x00FF;// GPIO Port B15-B8 input , B7-B0 output

GpioMuxRegs.GPDDIR.all = 0x0;           // GPIO PORT  as input

GpioMuxRegs.GPEDIR.all = 0x0;           // GPIO PORT  as input

GpioMuxRegs.GPFDIR.all = 0x0;           // GPIO PORT  as input

GpioMuxRegs.GPGDIR.all = 0x0;          // GPIO PORT  as input

GpioMuxRegs.GPAQUAL.all = 0x0;       // Set GPIO input qualifier values to zero

GpioMuxRegs.GPBQUAL.all = 0x0;

GpioMuxRegs.GPDQUAL.all = 0x0;

GpioMuxRegs.GPEQUAL.all = 0x0;

EDIS;

}    

void InitSystem(void)

{

EALLOW;

SysCtrlRegs.WDCR= 0x00AF;                      // Setup the watchdog

// 0x00E8  to disable the Watchdog , Prescaler = 1

// 0x00AF  to NOT disable the Watchdog, Prescaler = 64

SysCtrlRegs.SCSR = 0;                                  // Watchdog generates a RESET

SysCtrlRegs.PLLCR.bit.DIV = 10; // Setup the Clock PLL to multiply by 5

SysCtrlRegs.HISPCP.all = 0x1; // Setup Highspeed Clock Prescaler to divide by 2

SysCtrlRegs.LOSPCP.all = 0x2; // Setup Lowspeed CLock Prescaler to divide by 4              

SysCtrlRegs.PCLKCR.bit.EVAENCLK=1;

SysCtrlRegs.PCLKCR.bit.EVBENCLK=0;

SysCtrlRegs.PCLKCR.bit.SCIAENCLK=0;

SysCtrlRegs.PCLKCR.bit.SCIBENCLK=0;

SysCtrlRegs.PCLKCR.bit.MCBSPENCLK=0;

SysCtrlRegs.PCLKCR.bit.SPIENCLK=0;

SysCtrlRegs.PCLKCR.bit.ECANENCLK=0;

SysCtrlRegs.PCLKCR.bit.ADCENCLK=1;

EDIS;

}  

void delay_loop(long end)

{

long i;

for (i = 0; i < end; i++);

EALLOW;                                                                                      // Сброс сторожевого таймера

SysCtrlRegs.WDKEY = 0x55;

SysCtrlRegs.WDKEY = 0xAA;

EDIS;

}

interrupt void adc_isr(void) 

{

// Serve the watchdog every Timer 0 interrupt

EALLOW;

SysCtrlRegs.WDKEY = 0x55;                       // Serve watchdog #1

EDIS; 

Voltage_A0 = AdcRegs.ADCRESULT0>>4;

Voltage_B0 = AdcRegs.ADCRESULT1>>4;

AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1;         // Reset SEQ1

AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;                             // Clear INT SEQ1 bit

PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;   // Acknowledge interrupt to PIE

}         

Вывод:изучиди структуру встроенного АЦП, режимы его работы. Научились создавать простейшие программы с использованием

Похожие материалы

Информация о работе