#include "stm32f10x.h"
volatile uint32_t Timer=0;
int16_t vout;
/* Init Structure definition */
DAC_InitTypeDef DAC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
void Delay(volatile uint32_t ms) {
volatile uint16_t i,j;
for(i=0;i<ms;i++)
for(j=0; j<3442; j++) ;
}
void GPIO_setup()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOC, ENABLE);
// Configure the GPIO Switch B1 pin (PA0)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure the GPIO Switch SW2(PC13)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void GPIO_DAC1_setup()
{
// Configure PA4 (DAC_OUT1) to Analog in
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void DAC1_setup()
{
// DAC Periph clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
// DAC channel1 Configuration
DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is
automatically connected to the DAC converter. */
DAC_Cmd(DAC_Channel_1, ENABLE);
}
/*--------------------------------------------------------------
MAIN function
*------------------------------------------------------------*/
int main (void)
{
uint8_t reading1, previous1=0;
vout=1000;
GPIO_setup();
GPIO_DAC1_setup();
DAC1_setup();
while(1)
{
reading1 = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);
if((reading1 ==1) && (previous1 !=1) )
{
Delay (10); // delay 10 msec for debounce
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)
{
vout=vout+1000;
if(vout>=4095)
vout=4095;
DAC_SetChannel1Data(DAC_Align_12b_R, vout);
DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);
}
}
Delay(100);
previous1=reading1;
}
}