ADC采样值转电压后要么是3.3V,要么是0V,这是ADC没有配置好,还是中断没有触发呢?求大佬们解答一下
main:
/**************************************************************************************************
* Filename : main.c
* Author : Liming
* Date : 2016-11-15
* Revised :
* Revision : 1.0
* Description : This file contains the
* Caution :
* License : GPL 开源协议使用GPL协议
************************************************************************************Author:liming*/
#include "common.h"
#include "rtc.h"
#include "uart.h"
#include "gpio.h"
#include "UART_app.h"
#include "sysinit.h"
#include "ics.h" //时钟模块
#include "ledctr.h"
#include "uartmodule.h"
#include "timermodule.h"
#include "etmmodule.h"
#include "adcmodule.h"
#define SEND_BUF_LEN 50
void SystemClockInit(void);
void ShowChipInfo(void);
uint8_t send_buf[SEND_BUF_LEN];
volatile uint8_t u8IsSendDone;
uint32_t SysTickCnt;
uint8_t i;
float Volt;
/*************************************************************************************************
* Function Name : delay
* Description : 简单毫秒延时函数 50MHz
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
void delay(uint16_t t)
{
uint16_t i;
while(t--){
for(i=0;i<3666;i++);
}
}
/*************************************************************************************************
* Function Name : mian
* Description : 主函数
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
int main (void)
{
/* Perform processor initialization */
// sysinit(); //系统时钟配置
SystemClockInit();
//SIM_RemapETM2CH1Pin(); //将ETM2_CH1 输出从默认的PC1映射到PH1
LED0_Init(); //指示灯带上 因为我们把ETM输出映射到这个脚上了,所以LED0不用初始化了
EtmInit();
//LED1_Init();
//LED2_Init();
Uart0Init(40000000,115200); //串口0初始化
Uart1Init(40000000,115200); //串口1配置
TimerInit();
AdcInit();
//ADC_SetChannel(ADC,ADC_CHANNEL_AD23_BANDGAP);
//ADC_SetChannel(ADC,ADC_CHANNEL_AD22_TEMPSENSOR);
//ADC_SetChannel(ADC,ADC_CHANNEL_AD0);
/*
while (1)
{
// LedTask();
delay(500);
UartTask();
if(AdcGetFlag)
{
for(i=0;i<AdcCount;i++)
{
AdcBuff[i] = ADC_ReadResultReg(ADC);
printf("0x%x",AdcBuff[i]);
}
AdcGetFlag =0;
AdcCount =0;
//ADC_SetChannel(ADC,ADC_CHANNEL_AD23_BANDGAP);
//ADC_SetChannel(ADC,ADC_CHANNEL_AD22_TEMPSENSOR);
ADC_SetChannel(ADC,ADC_CHANNEL_AD0);
}
}
}
*/
while(1){
delay(500);
UartTask();
AdcGetFlag = 0;
ADC_SetChannel(ADC,ADC_CHANNEL_AD9);
// 等待转换完成
if(!AdcGetFlag){
printf("ADC conversion result as below:\r\n");
AdcValue = ADC_ReadResultReg(ADC);
Volt = (float)AdcValue/256*3.3;
//printf("the Volt value:%fV\r\n",Volt);
printf("%u\r\n",AdcValue);
AdcGetFlag =0;
AdcCount =0;
}
}
}
/*************************************************************************************************
* Function Name : SystemClockInit
* Description : 系统时钟初始化
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
void SystemClockInit(void)
{
OSC->CR = 0XB6; //打开osc外部晶振 高增益
while(!(OSC->CR & OSC_CR_OSCINIT_MASK));//等稳定
ICS->C1 = 0X1B; //256分频给FLL
ICS->C2 = 0X00; //不分频给系统时钟应该是50M
while(ICS->S != 0X40); //等稳定
}
etm.c:
/**************************************************************************************************
* Filename : EtmModule.H
* Author : Liming
* Date : 2016-11-15
* Revised :
* Revision : 1.0
* Description : This file contains the ETM module definitions.
* Caution :
* License : GPL 开源协议使用GPL协议
************************************************************************************Author:liming*/
#define _ETM_MODULE_
/*
***************************************************************************************************
* INCLUDE FILES
***************************************************************************************************
*/
#include "ETM.h"
#include "system_nv32F100.h"
#include "EtmModule.h"
#include "gpio.h"
/*
***************************************************************************************************
* FUNCTION PROTOTYPES
***************************************************************************************************
*/
uint16_t MOD_VALUE = 10000;
uint16_t PWM1_VALUE = 9999;
/*************************************************************************************************
* Function Name : ETM2_IRQHandler
* Description : 定时器中断处理函数
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
void ETM2_IRQHandler(void)
{
static uint16_t IntCount; //中断计数器
static _Bool Direction=0; //逐渐亮还是逐渐灭
ETM_ClrOverFlowFlag(ETM2);
if(IntCount==100) //100次中断修改一次占空比
{
IntCount=0;
if(Direction)
{
PWM1_VALUE += 100; //占空比 分子+100
if(PWM1_VALUE>10100)
{
Direction =0; //逐渐变暗
}
}
else
{
PWM1_VALUE -= 100;
if(PWM1_VALUE<110)
{
Direction = 1; //逐渐变亮
}
}
ETM_SetChannelValue(ETM2,ETM_CHANNEL_CHANNEL1,PWM1_VALUE);
}
else
{
IntCount++;
}
}
/*************************************************************************************************
* Function Name : EtmInit
* Description : 定时器初始化
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
void EtmInit(void)
{
ETM_PWMInit(ETM2,ETM_PWMMODE_EDGEALLIGNED,ETM_PWM_HIGHTRUEPULSE);// 选择ETM2 选择边沿对齐模式,调节高电平占空比
ETM_SetETMEnhanced(ETM2);
ETM_SetModValue(ETM2,MOD_VALUE);// 设置模数值,是一个uint16 也就是我们占空比的分母
ETM_SetChannelValue(ETM2,ETM_CHANNEL_CHANNEL1,PWM1_VALUE); // ETM2的通道1 写入当前值占空比的分子
ETM_ClockSet(ETM2,ETM_CLOCK_SYSTEMCLOCK,ETM_CLOCK_PS_DIV1);// 定时器时钟配置,选择系统时钟50Mhz
NVIC_EnableIRQ(ETM2_IRQn); // 使能中断
ETM_SetCallback(ETM2,ETM2_IRQHandler);
ETM_EnableOverflowInt(ETM2);
}
adc:
/**************************************************************************************************
* Filename : AdcModule.H
* Author : Liming
* Date : 2016-11-15
* Revised :
* Revision : 1.0
* Description : This file contains the
* Caution :
* License : GPL 开源协议使用GPL协议
************************************************************************************Author:liming*/
#define _ADC_MODULE_
/*
***************************************************************************************************
* INCLUDE FILES
***************************************************************************************************
*/
#include "common.h"
#include "adc.h"
#include "system_nv32F100.h"
#include "AdcModule.h"
#include "gpio.h"
/*
***************************************************************************************************
* FUNCTION PROTOTYPES
***************************************************************************************************
*/
/*************************************************************************************************
* Function Name : AdcInit
* Description : ADC初始化
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
void AdcInit(void)
{
ADC_ConfigType ADC_ConfigStructure={0};
PMC->SPMSC1 |= 0X01; // 开启bandgap
ADC_ConfigStructure.sSetting.bCompareAndEn =0; // 比较模式关闭
ADC_ConfigStructure.sSetting.bCompareEn = 0; // 比较模式关闭
ADC_ConfigStructure.sSetting.bCompareGreaterEn =0; // 比较模式关闭
ADC_ConfigStructure.sSetting.bContinuousEn =0; // 连续转换模式
ADC_ConfigStructure.sSetting.bFiFoScanModeEn =0; // FIFO扫描模式关闭
ADC_ConfigStructure.sSetting.bHardwareTriggerEn =0; // 硬件触发模式关闭
ADC_ConfigStructure.sSetting.bLongSampleEn =0; // 长采样模式
ADC_ConfigStructure.sSetting.bLowPowerEn = 0; // 高速度
ADC_ConfigStructure.sSetting.bIntEn =1; // 中断使能
ADC_ConfigStructure.u16PinControl =GPIO_PTC1; // AD0 端口PA0禁止端口控制器控制
ADC_ConfigStructure.u8ClockSource =CLOCK_SOURCE_BUS_CLOCK; // 总线2分频时钟
ADC_ConfigStructure.u8ClockDiv =ADC_ADIV_DIVIDE_4; // 8分频
ADC_ConfigStructure.u8FiFoLevel =ADC_FIFO_DISABLE; // fifo深度3
ADC_ConfigStructure.u8Mode = ADC_MODE_8BIT; // 8bit模式
ADC_SetCallBack(ADC_IRQHandler); // 设置中断回调函数
ADC_Init(ADC,&ADC_ConfigStructure); //
}
/*************************************************************************************************
* Function Name : ADC_IRQHandler
* Description : ADC中断处理
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
void ADC_IRQHandler(void)
{
AdcValue = ADC_ReadResultReg(ADC);
AdcGetFlag =1; // 数据读到可以发送了
}
tim:
/**************************************************************************************************
* Filename : UartModule.H
* Author : Liming
* Date : 2016-11-15
* Revised :
* Revision : 1.0
* Description : This file contains the uart module definitions.
* Caution :
* License : GPL 开源协议GPL
************************************************************************************Author:liming*/
#define _TIMER_MODULE_
/*
***************************************************************************************************
* INCLUDE FILES
***************************************************************************************************
*/
#include "PIT.h"
#include "system_nv32F100.h"
#include "TimerModule.h"
#include "gpio.h"
#include "adcmodule.h"
/*
***************************************************************************************************
* FUNCTION PROTOTYPES
***************************************************************************************************
*/
/*************************************************************************************************
* Function Name : Timer0_IRQHandler
* Description : 定时器中断处理
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
void Timer0_IRQHandler(void)
{
Timer0_Count++;
if(Timer0_Count>500) //500 *100us =50ms
{
LED1_Toggle();
Timer0_Count = 0;
}
}
void Timer1_IRQHandler(void)
{
Timer1_Count++;
if(Timer1_Count>1000) //1000 *1ms =1s
{
LED2_Toggle();
Timer1_Count=0;
}
}
/*************************************************************************************************
* Function Name : TimerInit
* Description : 定时器初始化
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
void TimerInit(void)
{
PIT_ConfigType PIT_ConfigStructure;
PIT_ConfigStructure.u32LoadValue = 5000; //装载值 50MHZ 20ns *5000 100us
PIT_ConfigStructure.bFreeze = FALSE; //
PIT_ConfigStructure.bModuleDis = FALSE;
PIT_ConfigStructure.bInterruptEn = TRUE;
PIT_ConfigStructure.bChainMode = FALSE;
PIT_ConfigStructure.bETMerEn = TRUE;
PIT_Init(PIT_CHANNEL0,&PIT_ConfigStructure); //配置channel_0
PIT_ConfigStructure.u32LoadValue = 10; // 100us*10 1ms
PIT_ConfigStructure.bChainMode = TRUE;
PIT_Init(PIT_CHANNEL1,&PIT_ConfigStructure); //配置channel_1
PIT_SetCallback(PIT_CHANNEL0,Timer0_IRQHandler);
PIT_SetCallback(PIT_CHANNEL1,Timer1_IRQHandler);
}
|