Vous êtes sur la page 1sur 10

SISTEMAS DIGITALES BASADOS EN MICROPROCESADORES

DEPARTAMENTO DE TECNOLOGA ELECTRNICA

Sistemas Digitales Basados en Microprocesadores Grado en Ingeniera de Sistemas de Comunicaciones 2 Curso, 2 Cuatrimestre

Prctica 4
Manejo del DAC

Grupo / Puesto de laboratorio

Apellidos

Nombre

NOTA: ENTREGAR ESTA HOJA RELLENA COMO PORTADA DE LA MEMORIA

27

Las partes de cdigo que vienen por defecto en la plantilla del proyecto han sido suprimidas. Solamente se muestran aqu las partes donde se ha modificado o aadido cdigo. uint16_t Sine12bit[50] = {1500,1593,1687,1875,2062,2156,2250,2343,2437,2625,2812,2906,3000,2906,28 12,2718,2625,2437,2343,2250,2156,2062,1875,1687,1593,1500,1406,1312,1125, 937,843,750,656,562,375,187,93,0,93,187,375,562,656,750,843,937,1125,1312 ,1406,1500}; uint16_t Sine12bit2[50] = {750,796,843,937,1031,1078,1125,1171,1218,1312,1406,1453,1500,1453,1406,1 359,1312,1218,1171,1125,1078,1031,937,843,796,750,703,656,562,468,421,375 ,328,281,187,93,46,0,46,93,187,281,328,375,421,468,562,656,703,750}; /*variable para recorrer el array de las tensiones de salida*/ uint16_t posicion=0; uint16_t pulsacion=0; uint16_t flag=0; uint8_t unidades; uint8_t decimas; void Config_Systick(){ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); } int main(void){ RCC_Configuration(); /* Init I/O ports */ Init_GPIOs (); /* Init Systick */ Config_Systick(); EXTI0_Config(); TIM2_Config(); Init_NVIC (); Config_DAC(); LCD_GLASS_Init(); LCD_GLASS_ScrollSentence((uint8_t*)"

PRACTICA DAC ",1,100);

while (1){ if(flag==1){ if(posicion<50){ if (pulsacion==0){ /*si no se ha pulsado nada, la salida del PA4 ser la del array 1 (Sine12bit)*/ DAC_SetChannel1Data(DAC_Align_12b_R, Sine12bit[posicion]); unidades = 3; decimas= 0; Display(); /*se mostrar por pantalla la amplitud de la seal de salida*/ posicion++; }else{ /*en caso de que se pulse el botn user, la salida del PA4 ser la del array 2 (Sine12bit2)*/ DAC_SetChannel1Data(DAC_Align_12b_R, Sine12bit2[posicion]); unidades = 1; decimas= 5;

Display(); /*se mostrar por pantalla la amplitud de la seal de salida*/ posicion++; } }else{ posicion=0; } flag=0; } } return 0; } void Config_DAC(void){ DAC_InitTypeDef DAC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIOA clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Configure PA.04 (DAC_OUT1) in analog mode --------------------*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Enable DAC clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO ; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; /* DAC Channel1 Init */ DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* Enable DAC Channel1 */ DAC_Cmd(DAC_Channel_1, ENABLE); } void Display(void){ uint8_t ch[1] = "V"; unidades = unidades + 0x30; // convierto a hexadecimal decimas = decimas + 0x30; // convierto a hexadecimal LCD_GLASS_WriteChar(&unidades, POINT_ON, COLUMN_OFF, 1);//Escribe las unidades LCD_GLASS_WriteChar(&decimas, POINT_OFF, COLUMN_OFF, 2);//Escribe las decimas LCD_GLASS_WriteChar(ch, POINT_OFF, COLUMN_OFF, 3);//Escribe el carcter V } void TIM2_Config(void){ NVIC_InitTypeDef NVIC_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); TIM_TimeBaseInitStruct.TIM_Prescaler=(uint16_t)(16000000/5000)-1;// cada tic esta cada 0.2 ms TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInitStruct.TIM_Period=50; TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct); TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update);

/* TIM enable counter */ TIM_Cmd(TIM2, ENABLE); TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Disable; TIM_OCInitStructure.TIM_Pulse=1; TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; TIM_OC2Init(TIM2,&TIM_OCInitStructure); TIM_ITConfig(TIM2,TIM_IT_CC2,ENABLE); TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Disable); TIM_ARRPreloadConfig(TIM2,DISABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void TIM2_IRQHandler(void){ if(TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET){ flag=1; TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); } } void Init_NVIC (void) { NVIC_InitTypeDef NVIC_InitStructure; /* Enable the TIM2 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void EXTI0_IRQHandler(void){ if(EXTI_GetITStatus(EXTI_Line0) != RESET){ if(pulsacion==0){ pulsacion=1; }else{ pulsacion=0; } EXTI_ClearFlag(EXTI_Line0); } EXTI_ClearITPendingBit(EXTI_Line0); /* Clear the EXTI line 0 pending bit */ } void EXTI0_Config(void){ EXTI_InitTypeDef EXTI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable GPIOA clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Configure PA0 pin as input floating */

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Enable SYSCFG clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /* Connect EXTI0 Line to PA0 pin */ SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0); /* Configure EXTI0 line */ EXTI_InitStructure.EXTI_Line = EXTI_Line0; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Enable and set EXTI0 Interrupt to the lowest priority */ NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }

SISTEMAS DIGITALES BASADOS EN MICROPROCESADORES


DEPARTAMENTO DE TECNOLOGA ELECTRNICA

Sistemas Digitales Basados en Microprocesadores Grado en Ingeniera de Sistemas de Comunicaciones 2 Curso, 2 Cuatrimestre

Prctica 4
Manejo del DAC

Grupo / Puesto de laboratorio

Apellidos

Nombre

NOTA: ENTREGAR ESTA HOJA RELLENA COMO PORTADA DE LA MEMORIA

27

/* Includes */ #include <stddef.h> #include "stm32l1xx.h" #include "discover_board.h" #include "stm32l_discovery_lcd.h" USART_InitTypeDef USART_InitStructure; void Init_GPIOs (void); void RCC_Configuration(void); void Config_DAC(void); void Init_NVIC (void) ; void Display(void); void TIM2_Config(void); void EXTI0_Config(void); void TIM2_IRQHandler(void); void EXTI0_IRQHandler(void); static volatile uint32_t TimingDelay; RCC_ClocksTypeDef RCC_Clocks; const uint16_t Sine12bit[50] = { 3000, 2906, 2812, 2718, 2625, 2437, 2343, 2250, 2156, 2062, 1875, 1687, 1593, 1500, 1406, 1312, 1125, 937, 843, 750, 652, 562, 375, 187, 94, 0, 93, 187, 375, 562, 656, 750, 843, 937, 1125, 1312, 1500, 1593, 1687, 1875, 2062, 2156, 2250, 2343, 2437, 2625, 2718, 2812, 2906, 3000}; uint16_t muestras=0; uint16_t aux=0; uint16_t flag=0; uint8_t unidades; uint8_t decimas;

void Config_Systick(){ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); } int main(void){ RCC_Configuration(); /* Init I/O ports */ Init_GPIOs (); /* Init Systick */ Config_Systick(); EXTI0_Config(); TIM2_Config(); Init_NVIC ();

Config_DAC(); /* Initializes the LCD glass */ LCD_GLASS_Init(); LCD_GLASS_ScrollSentence((uint8_t*)" ** PRACTICA DAC ** ",1,150); LCD_GLASS_Clear(); /* Infinite loop */ while (1){ if(flag==1){ if(muestras<50){ if (aux==0){ DAC_SetChannel1Data(DAC_Align_12b_R, Sine12bit[muestras]); unidades=3; decimas=0; Display(); muestras++; } else{ DAC_SetChannel1Data(DAC_Align_12b_R, Sine12bit[muestras]/2); unidades=1; decimas=5; Display(); muestras++; } }else{ muestras=0; } flag=0; } } return 0; } void __assert_func(const char *file, int line, const char *func, const char *failedexpr){ while(1) {} } void __assert(const char *file, int line, const char *failedexpr){ __assert_func (file, line, NULL, failedexpr); } void RCC_Configuration(void){ /* Enable the GPIOs Clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC|

RCC_AHBPeriph_GPIOD| RCC_AHBPeriph_GPIOE| RCC_AHBPeriph_GPIOH, ENABLE); /* Enable comparator clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP | RCC_APB1Periph_LCD | RCC_APB1Periph_PWR,ENABLE); /* Enable SYSCFG */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG , ENABLE); /* Allow access to the RTC */ PWR_RTCAccessCmd(ENABLE); /* Reset Backup Domain */ RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(DISABLE); /*!< LSE Enable */ RCC_LSEConfig(RCC_LSE_ON); /*!< Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) /*!< LCD Clock Source Selection */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); } void Init_GPIOs (void){ //MTODO EN LA PLANTILLA } void Delay(uint32_t nTime){ TimingDelay = nTime; while(TimingDelay != 0); } void TimingDelay_Decrement(void){ if (TimingDelay != 0x00){ TimingDelay--; } } void Config_DAC(void){ //MTODO EN LA PLANTILLA } void Display(void){ uint8_t ch[1] = "V"; unidades = unidades + 0x30; decimas= decimas + 0x30; LCD_GLASS_WriteChar(&unidades, POINT_ON, COLUMN_OFF, 1);//unidades LCD_GLASS_WriteChar(&decimas, POINT_OFF, COLUMN_OFF, 2);//decimas LCD_GLASS_WriteChar(ch, POINT_OFF, COLUMN_OFF, 3);//unidad de voltaje

} void TIM2_Config(void){ //MTODO EN LA PLANTILLA } void TIM2_IRQHandler(void){ if(TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET){ flag=1; TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); } } void Init_NVIC (void) { //MTODO EN LA PLANTILLA } void EXTI0_IRQHandler(void){ if(EXTI_GetITStatus(EXTI_Line0) != RESET){ if(aux==0){ aux=1; }else{ aux=0; } EXTI_ClearFlag(EXTI_Line0); } EXTI_ClearITPendingBit(EXTI_Line0); /* Clear the EXTI line 0 pending bit */ } void EXTI0_Config(void){ //MTODO EN LA PLANTILLA }