本帖最后由 wxjlive 于 2014-10-23 17:02 编辑
本人水平很low,在ucoII上移植一个ADC采样程序都不是太会,麻烦大神帮我看看这个程序啊,哪方面需要改进呢,我把原程序和我移植后的程序给大家看一下,看哪里不对,板子是没反应,串口助手也没反应,哎!
源程序:
#include "stm32f4xx.h"
#include <stdio.h>
#include "stm324xg_lcd.h"
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define PRINT_ON_LCD
#define ADC1_DR_ADDRESS ((uint32_t)0x4001204C) //ADC1 DR寄存器基地址
ADC_InitTypeDef ADC_InitStructure; //ADC初始化结构体声明
DMA_InitTypeDef DMA_InitStructure; //DMA初始化结构体声明
__IO uint16_t ADCConvertedValue; // ADC为12位模数转换器,只有ADCConvertedValue的低12位有效
__IO uint16_t ADC1ConvertedValue = 0;
__IO uint32_t ADC1ConvertedVoltage = 0;
void ADC_GPIO_Configuration(void);
void ADC1_CH13_DMA_Config(void);
void Display(void);
void Display_Init(void);
static void Delay_ARMJISHU(__IO uint32_t nCount)
{
for (; nCount != 0; nCount--)
{
}
}
void STM32_Shenzhou_COMInit(USART_InitTypeDef* USART_InitStruct)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Enable UART clock */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); /* Connect PXx to USARTx_Tx*/
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_Init(USART1, USART_InitStruct); /* USART configuration */
USART_Cmd(USART1, ENABLE); /* Enable USART */
}
void Printf_Init(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
STM32_Shenzhou_COMInit(&USART_InitStructure);
}
int main(void)
{
u16 ADCConvertedValueLocalTemp, ADCConvertedValueLocal, Precent = 0, Voltage = 0;
/* 详见左侧工程窗口的STM32_EVAL中的Printf.c */
Printf_Init();
printf("\n\r\n");
printf("\n\r 论坛后续还会有更多精彩的示例,欢迎访问论坛交流与学习.");
printf("\n\r 本示例为AD转换示例,串口输出转换结果,模拟信号来自板上的电位器! \n\r");
printf("\n\r==============================================================================");
printf("\n\r");
/* ADC1 configuration *******************************************************/
/* - Enable peripheral clocks */
/* - DMA2_Stream0 channel2 configuration */
/* - Configure ADC Channel7 pin as analog input */
/* - Configure ADC1 Channel7 */
ADC1_CH13_DMA_Config(); //ADC模数转化的初始化
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConv(ADC1); //ADC模数转化的初始化
printf("\r\n ARMJISHU.COM神舟王STM32F439IGT开发板 AD模数转换实验\n\n\r");
#ifdef PRINT_ON_LCD
/* Display ADC converted value on LCD */
Display_Init();
#endif
while (1)
{
#ifdef PRINT_ON_LCD
/* Display ADC converted value on LCD */
Display();
#endif
// 取两次采样的平均值
ADCConvertedValueLocalTemp = ADC1ConvertedValue;
Delay_ARMJISHU(80000);
ADCConvertedValueLocal = ADC1ConvertedValue;
ADCConvertedValueLocal = (ADCConvertedValueLocalTemp + ADCConvertedValueLocal) >> 1;
Precent = (ADCConvertedValueLocal*100/0x1000); //算出百分比
Voltage = Precent*33; // 3.3V的电平,计算等效电平
printf("\r 当前AD转换结果为:0x%X, 百分比为:%d%%,电压值:%d.%d%dV. \r",
ADCConvertedValueLocal, Precent, Voltage/1000, (Voltage%1000)/100, (Voltage%100)/10);
Delay_ARMJISHU(6000000);
}
}
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
void ADC1_CH13_DMA_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADC1, DMA2 and GPIO clocks ****************************************/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA2 Stream0 channel2 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC1ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
/* Configure PC.03 (ADC Channel13) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADC Common Init **********************************************************/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 Init ****************************************************************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1; //顺序进行规则转换的ADC通道的数目,这个数目的取值范围为1-16//
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel13 configuration *************************************/
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 1, ADC_SampleTime_3Cycles);
/* Enable DMA request after last transfer (Single-ADC mode) */
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
}
#ifdef PRINT_ON_LCD
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Display ADC converted value on LCD
* @param None
* @retval None
*/
void Display(void)
{
uint32_t v=0,mv=0;
uint8_t text[50];
ADC1ConvertedVoltage = ADC1ConvertedValue *3300/0xFFF;
v=(ADC1ConvertedVoltage)/1000;
mv = (ADC1ConvertedVoltage%1000)/10;
sprintf((char*)text," ADC = %d.%d V ",v,mv);
LCD_DisplayStringLine(LINE(6),text);
}
void Display_Init(void)
{
/* Initialize the LCD */
STM324xG_LCD_Init();
/* Clear the LCD */
LCD_Clear(White);
LCD_DisplayWelcomeStr(LCD_LINE_8);
/* Set the LCD Text size */
LCD_SetFont(&Font8x12);
/* Set the LCD Back Color and Text Color*/
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
/* Display */
LCD_DisplayStringLine(LINE(0x13), " ADC conversion w/ DMA transfer example ");
/* Set the LCD Text size */
LCD_SetFont(&Font16x24);
/* Display */
LCD_DisplayStringLine(LINE(0), "ADC Conversion @2Msps");
/* Set the LCD Back Color and Text Color*/
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
/* Display */
LCD_DisplayStringLine(LINE(2)," Turn R118(PC.03) ");
LCD_DisplayStringLine(LINE(4)," Potentiometer ");
}
#endif /* PRINT_ON_LCD */
移植后:
#include "stm32f4xx.h"
#include "ucos_ii.h"
#include <stdio.h>
#include "Gpio.h"
#include "stm324xg_lcd.h"
#include "fonts.h"
//#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define ADC1_DR_ADDRESS ((uint32_t)0x4001204C) //ADC1 DR寄存器基地址
ADC_InitTypeDef ADC_InitStructure; //ADC初始化结构体声明
DMA_InitTypeDef DMA_InitStructure; //DMA初始化结构体声明
__IO uint16_t ADCConvertedValue; // ADC为12位模数转换器,只有ADCConvertedValue的低12位有效
__IO uint16_t ADC1ConvertedValue = 0;
__IO uint32_t ADC1ConvertedVoltage = 0;
#define TASKSamp_PRIO 5
#define TASKSamp_STK_SIZE 128
#define TASKUsart_PRIO 6
#define TASKUsart_STK_SIZE 128
#define TASKDisp_PRIO 7
#define TASKDisp_STK_SIZE 128
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
void STM32_Shenzhou_COMInit(USART_InitTypeDef* USART_InitStruct)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Enable UART clock */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); /* Connect PXx to USARTx_Tx*/
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_Init(USART1, USART_InitStruct); /* USART configuration */
USART_Cmd(USART1, ENABLE); /* Enable USART */
}
void Printf_Init(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
STM32_Shenzhou_COMInit(&USART_InitStructure);
}
void ADC1_CH13_DMA_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADC1, DMA2 and GPIO clocks ****************************************/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA2 Stream0 channel2 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC1ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
/* Configure PC.03 (ADC Channel13) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADC Common Init **********************************************************/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 Init ****************************************************************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1; //顺序进行规则转换的ADC通道的数目,这个数目的取值范围为1-16//
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel13 configuration *************************************/
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 1, ADC_SampleTime_3Cycles);
/* Enable DMA request after last transfer (Single-ADC mode) */
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Display ADC converted value on LCD
* @param None
* @retval None
*/
void Display_Init(void)
{
/* Initialize the LCD */
STM324xG_LCD_Init();
/* Clear the LCD */
LCD_Clear(White);
LCD_DisplayWelcomeStr(LCD_LINE_8);
/* Set the LCD Text size */
LCD_SetFont(&Font8x12);
/* Set the LCD Back Color and Text Color*/
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
/* Display */
LCD_DisplayStringLine(LINE(0x13), " ADC conversion w/ DMA transfer example ");
/* Set the LCD Text size */
LCD_SetFont(&Font16x24);
/* Display */
LCD_DisplayStringLine(LINE(0), "ADC Conversion @2Msps");
/* Set the LCD Back Color and Text Color*/
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
/* Display */
LCD_DisplayStringLine(LINE(2)," Turn R118(PC.03) ");
LCD_DisplayStringLine(LINE(4)," Potentiometer ");
}
void Display(void)
{
uint32_t v=0,mv=0;
uint8_t text[50];
ADC1ConvertedVoltage = ADC1ConvertedValue *3300/0xFFF;
v=(ADC1ConvertedVoltage)/1000;
mv = (ADC1ConvertedVoltage%1000)/10;
sprintf((char*)text," ADC = %d.%d V ",v,mv);
LCD_DisplayStringLine(LINE(6),text);
}
static void taskSamp(void *p_arg);
static void taskUsart(void *p_arg);
static void taskDisp(void *p_arg);
static OS_STK taskSamp_stk[TASKSamp_STK_SIZE];
static OS_STK taskUsart_stk[TASKUsart_STK_SIZE];
static OS_STK taskDisp_stk[TASKDisp_STK_SIZE];
static void systick_init(void)
{
RCC_ClocksTypeDef rcc_clocks;
RCC_GetClocksFreq(&rcc_clocks);
SysTick_Config(rcc_clocks.HCLK_Frequency/OS_TICKS_PER_SEC);
}
int main(void)
{
OSInit();
printf("\n\r\n");
printf("\n\r 论坛后续还会有更多精彩的示例,欢迎访问论坛交流与学习.");
printf("\n\r 本示例为AD转换示例,串口输出转换结果,模拟信号来自板上的电位器! \n\r");
printf("\n\r==============================================================================");
printf("\n\r");
OSTaskCreate(taskSamp,(void*)0,&taskSamp_stk[TASKSamp_STK_SIZE - 1],TASKSamp_PRIO);
OSStart();
return 0;
}
static void taskSamp(void *p_arg)
{
systick_init();
OSTaskCreate(taskUsart,(void*)0,&taskUsart_stk[TASKUsart_STK_SIZE - 1],TASKUsart_PRIO);
OSTaskCreate(taskDisp,(void*)0,&taskDisp_stk[TASKDisp_STK_SIZE - 1],TASKDisp_PRIO);
while(1)
{
ADC1_CH13_DMA_Config(); //ADC模数转化的初始化
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConv(ADC1); //ADC模数转化的软件启动
OSTimeDly(8);
}
}
static void taskUsart(void *p_arg)
{
u16 ADCConvertedValueLocalTemp, ADCConvertedValueLocal, Percent = 0, Voltage = 0;
Printf_Init();
/* ADC1 configuration *******************************************************/
/* - Enable peripheral clocks */
/* - DMA2_Stream0 channel2 configuration */
/* - Configure ADC Channel7 pin as analog input */
/* - Configure ADC1 Channel7 */
while(1)
{
// 取两次采样的平均值
ADCConvertedValueLocalTemp = ADC1ConvertedValue;
OSTimeDly(10);
ADCConvertedValueLocal = ADC1ConvertedValue;
ADCConvertedValueLocal = (ADCConvertedValueLocalTemp + ADCConvertedValueLocal) >> 1;
Percent = (ADCConvertedValueLocal*100/0x1000); //算出百分比
Voltage = Percent*33; // 3.3V的电平,计算等效电平
printf("\r\n ARMJISHU.COM神舟王STM32F439IGT开发板 AD模数转换实验\n\n\r");
printf("\r 当前AD转换结果为:0x%X, 百分比为:%d%%,电压值:%d.%d%dV. \r",
ADCConvertedValueLocal, Percent, Voltage/1000, (Voltage%1000)/100, (Voltage%100)/10);
OSTimeDly(5);
}
}
static void taskDisp(void *p_arg)
{
Display_Init();
while (1)
{
Display();
OSTimeDly(2);
}
}
感觉自己只是作了简单的修改,还请大家多指点指点哇~ |
|