在CC3200SDK-1.2.0上测试可用,下面是main函数的代码:
注意:需使用example adc project 进行代码融合
//采集电压以PWM的方式输出,串口接收相关指令,执行相关操作,显示相关信息
#include
#include
#include
#include
#include "utils.h"
#include "hw_memmap.h"
#include "hw_common_reg.h"
#include "hw_types.h"
#include "hw_adc.h"
#include "hw_ints.h"
#include "hw_gprcm.h"
#include "rom.h"
#include "rom_map.h"
#include "interrupt.h"
#include "prcm.h"
#include "uart.h"
#include "pinmux.h"
#include "pin.h"
#include "adc.h"
#include "gpio.h"
#include "adc_userinput.h"
#include "uart_if.h"
#include "gpio_if.h"
#include "hw_apps_rcm.h"
#include "timer.h"
#define USER_INPUT
#define UART_PRINT Report
#define FOREVER 1
#define APP_NAME "ADC Reference"
#define NO_OF_SAMPLES 128
#define CONSOLE UARTA0_BASE
#define UartGetChar() MAP_UARTCharGet(CONSOLE)
#define UartPutChar(c) MAP_UARTCharPut(CONSOLE,c)
unsigned long pulAdcSamples[4096];
#define TIMER_INTERVAL_RELOAD 40035 /* =(255*157) */
#define DUTYCYCLE_GRANULARITY 157
#define MAX_STRING_LENGTH 80
#if defined(ccs)
extern void (* const g_pfnVectors[])(void);
#endif
#if defined(ewarm)
extern uVectorEntry __vector_table;
#endif
static void BoardInit(void);
static void DisplayBanner(char * AppName);
void UpdateDutyCycle(unsigned long ulBase, unsigned long ulTimer,
unsigned char ucLevel)
{
MAP_TimerMatchSet(ulBase,ulTimer,(ucLevel*DUTYCYCLE_GRANULARITY));
}
void SetupTimerPWMMode(unsigned long ulBase, unsigned long ulTimer,
unsigned long ulConfig, unsigned char ucInvert)
{
MAP_TimerConfigure(ulBase,ulConfig);
MAP_TimerPrescaleSet(ulBase,ulTimer,0);
MAP_TimerControlLevel(ulBase,ulTimer,ucInvert);
MAP_TimerLoadSet(ulBase,ulTimer,TIMER_INTERVAL_RELOAD);
MAP_TimerMatchSet(ulBase,ulTimer,TIMER_INTERVAL_RELOAD);
}
void InitPWMModules()
{
MAP_PRCMPeripheralClkEnable(PRCM_TIMERA2, PRCM_RUN_MODE_CLK);
MAP_PRCMPeripheralClkEnable(PRCM_TIMERA3, PRCM_RUN_MODE_CLK);
SetupTimerPWMMode(TIMERA2_BASE, TIMER_B,
(TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_PWM), 1);
SetupTimerPWMMode(TIMERA3_BASE, TIMER_A,
(TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM | TIMER_CFG_B_PWM), 1);
SetupTimerPWMMode(TIMERA3_BASE, TIMER_B,
(TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM | TIMER_CFG_B_PWM), 1);
MAP_TimerEnable(TIMERA2_BASE,TIMER_B);
MAP_TimerEnable(TIMERA3_BASE,TIMER_A);
MAP_TimerEnable(TIMERA3_BASE,TIMER_B);
}
void DeInitPWMModules()
{
MAP_TimerDisable(TIMERA2_BASE, TIMER_B);
MAP_TimerDisable(TIMERA3_BASE, TIMER_A);
MAP_TimerDisable(TIMERA3_BASE, TIMER_B);
MAP_PRCMPeripheralClkDisable(PRCM_TIMERA2, PRCM_RUN_MODE_CLK);
MAP_PRCMPeripheralClkDisable(PRCM_TIMERA3, PRCM_RUN_MODE_CLK);
}
static void
DisplayBanner(char * AppName)
{
Report("\n\n\n\r");
Report("\t\t *************************************************\n\r");
Report("\t\t CC3200 %s Application \n\r", AppName);
Report("\t\t *************************************************\n\r");
Report("\n\n\n\r");
}
static void
BoardInit(void)
{
#ifndef USE_TIRTOS
#if defined(ccs)
MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
#endif
#if defined(ewarm)
MAP_IntVTableBaseSet((unsigned long)&__vector_table);
#endif
#endif
MAP_IntMasterEnable();
MAP_IntEnable(FAULT_SYSTICK);
PRCMCC3200MCUInit();
}
void main()
{
unsigned int uiChannel;
unsigned int uiIndex=0;
unsigned long ulSample;
float Brightness;
char cString[MAX_STRING_LENGTH+1];
char cCharacter;
int iStringLength = 0;
BoardInit();
PinMuxConfig();
InitPWMModules();
InitTerm();
DisplayBanner(APP_NAME);
while(FOREVER)
{
uiIndex=0;
#ifdef CC3200_ES_1_2_1
//
// Enable ADC clocks.###IMPORTANT###Need to be removed for PG 1.32
//
HWREG(GPRCM_BASE + GPRCM_O_ADC_CLK_CONFIG) = 0x00000043;
HWREG(ADC_BASE + ADC_O_ADC_CTRL) = 0x00000004;
HWREG(ADC_BASE + ADC_O_ADC_SPARE0) = 0x00000100;
HWREG(ADC_BASE + ADC_O_ADC_SPARE1) = 0x0355AA00;
#endif
MAP_PinTypeADC(PIN_58,PIN_MODE_255);
uiChannel = ADC_CH_1; //设置ADC输入通道
MAP_ADCTimerConfig(ADC_BASE,2^17);
MAP_ADCTimerEnable(ADC_BASE);
MAP_ADCEnable(ADC_BASE);
MAP_ADCChannelEnable(ADC_BASE, uiChannel);
while(uiIndex < NO_OF_SAMPLES + 4)
{
if(MAP_ADCFIFOLvlGet(ADC_BASE, uiChannel))
{
ulSample = MAP_ADCFIFORead(ADC_BASE, uiChannel);
pulAdcSamples[uiIndex++] = ulSample;
}
}
MAP_ADCChannelDisable(ADC_BASE, uiChannel);
uiIndex = 0;
UART_PRINT("\n\r");
Brightness = (((((pulAdcSamples[4+uiIndex] >> 2 ) & 0x0FFF))*1.4)/4096)*100; //采集电压
UART_PRINT("%f\n\r",(Brightness));
UartPutChar(Brightness); //从串口输出
MAP_UtilsDelay(80000000);
cCharacter = UartGetChar(); //从串口输入
// UartPutChar(cCharacter);
cString[iStringLength] = cCharacter;
iStringLength++;
switch(cString[0])
{
case '0':
UpdateDutyCycle(TIMERA2_BASE, TIMER_B, Brightness); //更新PWM占空比
break;
case '1':
UpdateDutyCycle(TIMERA3_BASE, TIMER_B, Brightness);
break;
case '2':
UpdateDutyCycle(TIMERA3_BASE, TIMER_A, Brightness);
break;
default :
break;
}
}
}