|
ADC采樣值轉(zhuǎn)電壓后要么是3.3V,要么是0V,這是ADC沒有配置好,還是中斷沒有觸發(fā)呢?求大佬們解答一下
main:
/**************************************************************************************************
* Filename : main.c
* Author : Liming
* Date : 2016-11-15
* Revised :
* Revision : 1.0
* Description : This file contains the
* Caution :
* License : GPL 開源協(xié)議使用GPL協(xié)議
************************************************************************************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 : 簡單毫秒延時函數(shù) 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 : 主函數(shù)
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
int main (void)
{
/* Perform processor initialization */
// sysinit(); //系統(tǒng)時鐘配置
SystemClockInit();
//SIM_RemapETM2CH1Pin(); //將ETM2_CH1 輸出從默認(rèn)的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);
// 等待轉(zhuǎn)換完成
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 : 系統(tǒng)時鐘初始化
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
void SystemClockInit(void)
{
OSC->CR = 0XB6; //打開osc外部晶振 高增益
while(!(OSC->CR & OSC_CR_OSCINIT_MASK));//等穩(wěn)定
ICS->C1 = 0X1B; //256分頻給FLL
ICS->C2 = 0X00; //不分頻給系統(tǒng)時鐘應(yīng)該是50M
while(ICS->S != 0X40); //等穩(wěn)定
}
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 開源協(xié)議使用GPL協(xié)議
************************************************************************************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 : 定時器中斷處理函數(shù)
* Input : None
* Output : None
* Return : None
***********************************************************************************Author:liming*/
void ETM2_IRQHandler(void)
{
static uint16_t IntCount; //中斷計數(shù)器
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 選擇邊沿對齊模式,調(diào)節(jié)高電平占空比
ETM_SetETMEnhanced(ETM2);
ETM_SetModValue(ETM2,MOD_VALUE);// 設(shè)置模數(shù)值,是一個uint16 也就是我們占空比的分母
ETM_SetChannelValue(ETM2,ETM_CHANNEL_CHANNEL1,PWM1_VALUE); // ETM2的通道1 寫入當(dāng)前值占空比的分子
ETM_ClockSet(ETM2,ETM_CLOCK_SYSTEMCLOCK,ETM_CLOCK_PS_DIV1);// 定時器時鐘配置,選擇系統(tǒng)時鐘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 開源協(xié)議使用GPL協(xié)議
************************************************************************************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; // 比較模式關(guān)閉
ADC_ConfigStructure.sSetting.bCompareEn = 0; // 比較模式關(guān)閉
ADC_ConfigStructure.sSetting.bCompareGreaterEn =0; // 比較模式關(guān)閉
ADC_ConfigStructure.sSetting.bContinuousEn =0; // 連續(xù)轉(zhuǎn)換模式
ADC_ConfigStructure.sSetting.bFiFoScanModeEn =0; // FIFO掃描模式關(guān)閉
ADC_ConfigStructure.sSetting.bHardwareTriggerEn =0; // 硬件觸發(fā)模式關(guān)閉
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); // 設(shè)置中斷回調(diào)函數(shù)
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; // 數(shù)據(jù)讀到可以發(fā)送了
}
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 開源協(xié)議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);
}
|
|