主程序:
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* NVIC configuration ------------------------------------------------------*/
NVIC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
LcdShow_Init();
/* TIM1 configuration -------------------TIM1配置------------------------------*/
TIM1_DeInit(); TIM1设置为缺省值
/* Time Base configuration
typedef struct
{
u16 TIM1_Period;
u16 TIM1_Prescaler;
u8 TIM1_ClockDivision;
u16 TIM1_CounterMode;
u8 TIM1_RepetitionCounter;
} TIM1_TIM1BaseInitTypeDef;
*/
TIM1_TimeBaseStructure.TIM1_Prescaler = 0x4;
//TIM1_Prescaler设置了用来作为TIM1时钟频率除数的预分频值。
TIM1_TimeBaseStructure.TIM1_CounterMode = TIM1_CounterMode_Up;
//TIM1向上计数模式。
TIM1_TimeBaseStructure.TIM1_Period = 0xFF;
//TIM1_Period设置了在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM1_TimeBaseStructure.TIM1_ClockDivision = 0x0; //TIM1_ClockDivision设置了时钟分割
TIM1_TimeBaseStructure.TIM1_RepetitionCounter = 0x0;
//TIM1_RepetitionCounter设置了周期计数器值
TIM1_TimeBaseInit(&TIM1_TimeBaseStructure);
//根据TIM1_TIM1BaseInitStruct中指定的参数初始化TIM1的时间基数单位
/* Channel1 Configuration in PWM mode
{
u16 TIM1_OCMode;
u16 TIM1_OutputState;
u16 TIM1_OutputNState;
u16 TIM1_Pulse;
u16 TIM1_OCPolarity;
u16 TIM1_OCNPolarity;
u16 TIM1_OCIdleState;
u16 TIM1_OCNIdleState;
} TIM1_OCInitTypeDef;
*/
TIM1_OCInitStructure.TIM1_OCMode = TIM1_OCMode_PWM1; //TIM1脉冲宽度调制模式1
TIM1_OCInitStructure.TIM1_OutputState = TIM1_OutputState_Enable; // 使能输出比较状态
TIM1_OCInitStructure.TIM1_Pulse = 0x7F; //设置待装入捕获比较寄存器的脉冲值
TIM1_OCInitStructure.TIM1_OCPolarity = TIM1_OCPolarity_Low; // TIM1输出比较极性低
TIM1_OC1Init(&TIM1_OCInitStructure);//根据TIM1_OCInitStruct中指定的参数初始化TIM1通道1
/* DMA Channel1 Configuration ----------------------------------------------*/
DMA_DeInit(DMA_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_RegularConvertedValueTab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 32;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA_Channel1, &DMA_InitStructure);
/* Enable DMA channel1 */
DMA_Cmd(DMA_Channel1, ENABLE);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
//ADC_ExternalTrigConv_T1_CC1 选择定时器1的捕获比较1作为转换外部触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel10configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_13Cycles5);
/* Set injected sequencer length */
ADC_InjectedSequencerLengthConfig(ADC1, 1);//设置注入组通道的转换序列长度
/* ADC1 injected channel Configuration */
ADC_InjectedChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_71Cycles5);
/* ADC1 injected external trigger configuration */
ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_None);
//配置ADC1的外部触发启动注入组转换功能
//注入转换由软件而不是外部触发启动
/* Enable automatic injected conversion start after regular one */
ADC_AutoInjectedConvCmd(ADC1, ENABLE);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 external trigger */
ADC_ExternalTrigConvCmd(ADC1, ENABLE);
/* Enable JEOC interupt */
ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* TIM1 counter enable */
TIM1_Cmd(ENABLE);
/* TIM1 main Output Enable */
TIM1_CtrlPWMOutputs(ENABLE);
/* Test on channel1 transfer complete flag */
while(!DMA_GetFlagStatus(DMA_FLAG_TC1));
/* Clear channel1 transfer complete flag */
DMA_ClearFlag(DMA_FLAG_TC1);
/* TIM1 counter disable */
TIM1_Cmd(DISABLE);
while (1)
{
displayconvert(ADC_RegularConvertedValueTab[15]);
// DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_RegularConvertedValueTab;
write_string(display);
delay();
delay();
displayconvert(ADC_InjectedConvertedValueTab[15]);
write_string(display);
delay();
delay();
}
}
void displayconvert(vu16 value)
{
u8 num1 = value&0xf;
u8 num2 = (value&0xff)>>4;
u8 num3= (value&0xfff)>>8;
u8 num4 = value>>12;
if (num1>9)
display[3]=num1+(65-10);
else
display[3]=num1+(48-0);
if (num2>9)
display[2]=num2+(65-10);
else
display[2]=num2+(48-0);
if (num3>9)
display[1]=num3+(65-10);
else
display[1]=num3+(48-0);
if (num4>9)
display[0]=num4+(65-10);
else
display[0]=num4+(48-0);
}
GPIO配置
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure TIM1_CH1 (PA8) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure PC.06 as output push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure PC.0 and PC.01 (ADC Channel10 and Channel11) as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// for LCD display
/* Configure PE.00 -- PE.15 as Output push-pull : SEG0~15 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Configure PC.08 -- PC.11 as Output push-pull : COM1~4 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
NVIC配置
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Enable ADC IRQChannel */
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQChannel; //ADC全局中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* enabling interrupt */ // For LCD enable IT from TIM2
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
在中断函数文件stm32f10x_it.c中
void ADC_IRQHandler(void)
{
/* Set PC.06 pin */
GPIO_WriteBit(GPIOC, GPIO_Pin_6, Bit_SET);
/* Get injected channel11 converted value */
ADC_InjectedConvertedValueTab[Index++] = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_1); //选择注入通道1
/* Clear ADC1 JEOC pending interrupt bit */
ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC);
/* Reset PC.06 pin */
GPIO_WriteBit(GPIOC, GPIO_Pin_6, Bit_RESET);
}
void TIM2_IRQHandler(void) (略)。