标题:
STM32学习-Adc显示电位器的内码,模拟看门狗
[打印本页]
作者:
xiongxiao
时间:
2015-5-27 16:50
标题:
STM32学习-Adc显示电位器的内码,模拟看门狗
完整代码下载:
Adc显示电位器的内码,模拟看门狗.rar
(142.71 KB, 下载次数: 29)
2015-5-27 16:49 上传
点击文件名下载附件
下载积分: 黑币 -5
/*******************************************************************************
STM32学习日志(9)----ADC 测试程序,显示电位器或者内部参考电压的内码,模拟看门狗监控
编译环境: EWARM V5.30
硬件环境: 南京万利 EK-STM32F
主芯片 : STM32F103VBT6
STM32 FW: V3.0.0
作者 : szlihongtao
时间 : 2010-07-01
说明 : 1. 在 SysTick 定时中断程序中刷新LCD的显示
2. 要设定 CH_DISP 的值
3. 模拟看门狗监控的功能
*******************************************************************************/
#include "stm32f10x.h"
#include "stm32_m.h"
#include "lcd.h"
//******************************************************************************
#define ADC1_DR_Address ((u32)0x4001244C) // ADC1->DR
#define CH_DISP 1 // 0显示电位器的内码
// 1显示内部参考电压的内码
//******************************************************************************
bit f_tb; // 基本定时标志
static INT16U tmr_1sec,cnt_test;
extern bit f_adc;
extern INT16U code_adc;
//******************************************************************************
static void delayms(INT16U cnt)
{
INT16U i;
while(cnt--)
for (i=0; i<7333; i++);
}
//******************************************************************************
// 延时50ms
//******************************************************************************
static void delay(void)
{
INT32U i;
static INT32U jjj=5240*50;
for (i=0; i<jjj; i++);
}
//******************************************************************************
// 时钟设置初始化
//******************************************************************************
static void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
/*
RCC_AdjustHSICalibrationValue 调整内部高速晶振(HSI)校准值
RCC_ITConfig 使能或者失能指定的RCC中断
RCC_ClearFlag 清除RCC的复位标志位
RCC_GetITStatus 检查指定的RCC中断发生与否
RCC_ClearITPendingBit 清除RCC的中断待处理位
*/
/* RCC system reset(for debug purpose) */
// 时钟系统复位
RCC_DeInit();
// 使能外部的8M晶振
// 设置外部高速晶振(HSE)
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
// 使能或者失能内部高速晶振(HSI)
RCC_HSICmd(DISABLE);
// 等待HSE起振
// 该函数将等待直到HSE就绪,或者在超时的情况下退出
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
// 设置AHB时钟(HCLK)
RCC_HCLKConfig(RCC_SYSCLK_Div1); // 72 MHz
/* PCLK1 = HCLK/2 */
// 设置低速AHB时钟(PCLK1)
RCC_PCLK1Config(RCC_HCLK_Div2); // 36 MHz
/* PCLK2 = HCLK */
// 设置高速AHB时钟(PCLK2)
RCC_PCLK2Config(RCC_HCLK_Div1); // 72 MHz
/* ADCCLK = PCLK2/8 */
// 设置ADC时钟(ADCCLK)
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
// 设置USB时钟(USBCLK)
// USB时钟 = PLL时钟除以1.5
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
// 设置外部低速晶振(LSE)
RCC_LSEConfig(RCC_LSE_OFF);
// 使能或者失能内部低速晶振(LSI)
// LSE晶振OFF
RCC_LSICmd(DISABLE);
// 设置RTC时钟(RTCCLK)
// 选择HSE时钟频率除以128作为RTC时钟
RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
// 使能或者失能RTC时钟
// RTC时钟的新状态
RCC_RTCCLKCmd(DISABLE);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz */
// 设置PLL时钟源及倍频系数
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
// 使能或者失能PLL
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
// 检查指定的RCC标志位设置与否
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
// 设置系统时钟(SYSCLK)
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
// 返回用作系统时钟的时钟源
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
// 使能或者失能AHB外设时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
|RCC_AHBPeriph_DMA2
|RCC_AHBPeriph_SRAM
|RCC_AHBPeriph_FLITF
|RCC_AHBPeriph_CRC
|RCC_AHBPeriph_FSMC
|RCC_AHBPeriph_SDIO,DISABLE);
// 使能或者失能APB1外设时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);
// 强制或者释放高速APB(APB2)外设复位
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
// 退出复位状态
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); // adc时钟使能
// 强制或者释放低速APB(APB1)外设复位
RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);
// 强制或者释放后备域复位
RCC_BackupResetCmd(ENABLE);
// 使能或者失能时钟安全系统
RCC_ClockSecuritySystemCmd(DISABLE);
}
//******************************************************************************
// NVIC设置
//******************************************************************************
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* enabling interrupt */
NVIC_InitStructure.NVIC_IRQChannel=ADC1_2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//******************************************************************************
// SysTick设置初始化
//******************************************************************************
static void SysTick_Config1(void)
{
#define SystemFreq 72000000.0 // 单位为Hz
#define TB_SysTick 2000.0 // 单位为uS
INT32U ticks;
ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
ticks=ticks;
SysTick_Config(ticks);
}
//******************************************************************************
// GPIO设置
//******************************************************************************
static void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 使能或者失能APB2外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4; // key
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_Init(GPIOC, &GPIO_InitStructure); // lcd_comm
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; // lcd_seg
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Configure PC.00 (ADC Channel10) as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
//******************************************************************************
static void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_DeInit(ADC1); // ADC参数复位
ADC_StructInit(&ADC_InitStructure); // 默认的参数
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;// ADC1与ADC2工作在独立模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE; // 单通道
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // 连续转换模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;// 软件触发启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;// ADC数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 1; // 进行规则转换的ADC通道的数目
ADC_Init(ADC1, &ADC_InitStructure);
#if (CH_DISP==0)
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_28Cycles5);
#else
ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 1, ADC_SampleTime_28Cycles5);
ADC_TempSensorVrefintCmd(ENABLE); // 打开温度传感器以及参考电压通道
/*
Table 12. Embedded internal reference voltage
Symbol Parameter Conditions Min Typ Max Unit
VREFINT Internal reference voltage 1.16 1.20 1.24 V
典型值为1.20V
实测,VDD=3.310V,ADC_Channel_17 = VREFINT 对应的内码为1509
理论上,VREFINT的计算值为 1509*3.310/4096= 1.219V
*/
#endif
ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE); // 开中断
ADC_ClearFlag(ADC1,ADC_FLAG_EOC); // 清除中断标志位
ADC_DMACmd(ADC1, DISABLE);
#if 1
/* Configure high and low analog watchdog thresholds */
ADC_AnalogWatchdogThresholdsConfig(ADC1, 3000, 1000);
/* Configure channel10 as the single analog watchdog guarded channel */
ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_Channel_10);
/* Enable analog watchdog on one regular channel */
// 模拟看门狗监控单个的常规通道
ADC_AnalogWatchdogCmd(ADC1, ADC_AnalogWatchdog_SingleRegEnable);
/* Enable AWD interupt 开模拟看门狗监控功能 */
ADC_ITConfig(ADC1, ADC_IT_AWD, ENABLE);
#endif
ADC_Cmd(ADC1, ENABLE); // 使能ADC
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}//******************************************************************************
// LED5亮
//******************************************************************************
void Led_RW_ON(void)
{
GPIO_SetBits(GPIOC,GPIO_Pin_4);
}
//******************************************************************************
// LED5灭
//******************************************************************************
void Led_RW_OFF(void)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_4);
}
//******************************************************************************
// pos=0-3
//******************************************************************************
static void myWrite_char(u8 pos,char car,u8 f_dot)
{
write_char(&car,f_dot,pos);
}
//******************************************************************************
static void disp_adc(INT16U code_adc1)
{
INT8U i,ch;
for(i=0;i<4;++i)
{
ch=code_adc1%10;
code_adc1/=10;
ch+=0x30;
myWrite_char(3-i,ch,0);
}
}//******************************************************************************
// 主程序
//******************************************************************************
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
SysTick_Config1();
Led_RW_ON();
delay();
Led_RW_OFF();
//------------------------------------------------------------------------------
write_string("STM ");
delayms(333);
write_string("ADC ");
delayms(333);
write_string("TEST");
delayms(333);
ADC_Configuration();
//------------------------------------------------------------------------------
for(;;)
{
if (f_adc)
{
f_adc=0;
GPIOC->ODR ^= GPIO_Pin_4; // led5 toogle
++cnt_test;
disp_adc(code_adc);
}
if (f_tb) // 每隔2ms设置为1
{
f_tb=0;
if (++tmr_1sec>=(1000/2))
{
tmr_1sec=0;
GPIOC->ODR ^= GPIO_Pin_6; // led3 toogle
}
}
}
}
//******************************************************************************
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
//******************************************************************************
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
//******************************************************************************
/*
LED2---------PC7
LED3---------PC6
LED4---------PC5
LED5---------PC4
KEY2---------PD3
KEY3---------PD4
*/
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1