本帖最后由 胡斯哲 于 2016-4-22 18:52 编辑
一、F207ZE+ADC+DMA+USART+DAC
就不用嗮板子了直接上程序。用keil5.14#include "gd32f20x.h"
#include <stdlib.h>
#include <stdio.h>
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address ((uint32_t)0x4001244C)
/* Private variables ---------------------------------------------------------*/
float ADC_Value;
int i;
float vu16Temperature;
__IO uint16_t ADCConvertedValue;
ADC_InitPara ADC_InitStructure;
DAC_InitPara DAC_InitStructure;
DMA_InitPara DMA_InitStructure;
/* Private function ----------------------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void ADC_Configuration(void);
void Dac1_Init(void);
void Dac1_Set_Vol(u16 vol);
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
/* __GNUC__ */
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Main program.
* @param None
* @retval None
*/
int main(void)
{
/* Config System clocks --------------------------------------------------*/
RCC_Configuration();
/* Config GPIO ports -----------------------------------------------------*/
GPIO_Configuration();
USART_Configuration();
ADC_Configuration();
Dac1_Set_Vol(1500);
//while(ADC_GetBitState(ADC1,ADC_FLAG_EOC) != SET);
//ADC_Value = ADC_GetConversionValue(ADC1);
vu16Temperature = ADCConvertedValue*3.3/4096;
printf("\n\rTemperature %f\n\r",vu16Temperature);
while (1);
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Configure system clocks.
* @param None
* @retval None
*/
void RCC_Configuration(void)
{
/* ADCCLK = PCLK2/6 */
RCC_ADCCLKConfig(RCC_ADCCLK_APB2_DIV6);
/* Enable DMA1 and GPIOC clock */
RCC_APB2PeriphClock_Enable( RCC_APB2PERIPH_GPIOA|RCC_APB2PERIPH_AF , ENABLE);
/* Enable USART1 APB clock */
RCC_APB2PeriphClock_Enable( RCC_APB2PERIPH_USART1 , ENABLE );
/* Enable ADC1 clock */
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_ADC1, ENABLE);
RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_DMA1, ENABLE);
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Configure GPIO ports.
* @param None
* @retval None
*/
void GPIO_Configuration(void)
{
GPIO_InitPara GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_3;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Configure ADC Configuration.
* @param None
* @retval None
*/
void ADC_Configuration(void)
{
DMA_DeInit(DMA1_CHANNEL1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PERIPHERALSRC;
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_MTOM = DMA_MEMTOMEM_DISABLE;
DMA_Init(DMA1_CHANNEL1, &DMA_InitStructure);
/* Enable DMA1 channel1 */
DMA_Enable(DMA1_CHANNEL1, ENABLE);
/* Config ADC1 --------------------------------------------------------------*/
ADC_InitStructure.ADC_Mode_Scan = DISABLE;
ADC_InitStructure.ADC_Mode_Continuous = DISABLE;
ADC_InitStructure.ADC_Trig_External = ADC_EXTERNAL_TRIGGER_MODE_NONE;
ADC_InitStructure.ADC_Data_Align = ADC_DATAALIGN_RIGHT;
ADC_InitStructure.ADC_Mode = ADC_MODE_INDEPENDENT;
ADC_InitStructure.ADC_Channel_Number = 1;
ADC_Init(ADC1,&ADC_InitStructure);
/* ADC1 regular channels configuration */
ADC_RegularChannel_Config(ADC1,ADC_CHANNEL_13, 1, ADC_SAMPLETIME_239POINT5);
ADC_ExternalTrigConv_Enable(ADC1,ENABLE);
ADC_TempSensorVrefint_Enable(ENABLE);
/* Enable ADC1 DMA */
ADC_DMA_Enable(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Enable(ADC1,ENABLE);
ADC_Calibration(ADC1);
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConv_Enable(ADC1,ENABLE);
}
/**
* @brief Configure USART parameter.
* @param None
* @retval None
*/
void USART_Configuration(void)
{
USART_InitPara USART_InitStructure;
GPIO_InitPara GPIO_InitStructure;
RCC_APB2PeriphClock_Enable( RCC_APB2PERIPH_GPIOB, ENABLE );
/* Enable USART1 APB clock */
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_USART1, ENABLE );
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_AF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_6 ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_Init( GPIOB , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_7;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;;
GPIO_Init( GPIOB , &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_REMAP_USART1, ENABLE);
USART_DeInit( USART1 );
USART_InitStructure.USART_BRR = 115200;
USART_InitStructure.USART_WL = USART_WL_8B;
USART_InitStructure.USART_STBits = USART_STBITS_1;
USART_InitStructure.USART_Parity = USART_PARITY_RESET;
USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX;
USART_Init(USART1, &USART_InitStructure);
/* USART enable */
USART_Enable(USART1, ENABLE);
}
void Dac1_Init(void)
{
GPIO_InitPara GPIO_InitStructure;
DAC_InitPara DAC_InitType;
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOA, ENABLE);//??GPIOA??
RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_DAC, ENABLE);//??DAC??
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_5;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AIN;//????
//GPIO_InitStructure.GPIO_PuPd = GPIO_MODE_OUT_PP;//??
GPIO_Init(GPIOA, &GPIO_InitStructure);//???
DAC_InitStructure.DAC_Trigger=DAC_TRIGGER_NONE; //??????? TEN1=0
DAC_InitType.DAC_WaveType=DAC_WAVEGENE_NONE;//???????
DAC_InitType. DAC_LFSRNoise_AmplitudeTriangle=DAC_LFSR_BIT0 ;//???????
DAC_InitType.DAC_OutputBuffer=DAC_OUTPUTBUFFER_DISABLE ; //DAC1?????? BOFF1=1
DAC_Init(DAC_CHANNEL_2,&DAC_InitType); //???DAC??1
DAC_DMA_Enable(DAC_CHANNEL_2, ENABLE); //??DAC??1
DAC_SetChannel1Data(DAC_ALIGN_12B_R, 0); //12??????????DAC?
}
//????1????
//vol:0~3300,??0~3.3V
void Dac1_Set_Vol(u16 vol)
{
double temp=vol;
temp/=1000;
temp=temp*4096/3.3;
DAC_SetChannel1Data(DAC_ALIGN_12B_R,temp);//12??????????DAC?
}
/**
* @brief Retarget the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Loop until transmit data register is empty */
while (USART_GetBitState( USART1 , USART_FLAG_TBE) == RESET)
{
}
/* Place your implementation of fputc here */
USART_DataSend( USART1 , (uint8_t) ch );
return ch;
}
截取串口图片
对于这块板子总体感觉调的也很顺利,跟stm32差不多。特别是板子上有现成的串口,还是很方便。之前用stm32f030discovery没现成串口,好多功能都没法用。
二、将STM32F030的DS18B20的程序直接改为GD上
前段时间忙工作的事,没弄板子,今天调了下板子。虽然当中有些奇怪的问题出现,但基本都属于操作不熟练。这次修改基本没有什么改动,只是头文件和加上了串口,精确延时还是用的stm32的systick,可以不用动。
在网上看到说板子丝印标的不对,我感觉没问题,对照下图可以测测
需要改动的地方
1)、GD用的是GPIOA_PIN1(这个stm32用GPIO_Pin),
2)、RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);-------》RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOA, ENABLE);
3)、GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;和GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;合并为一句GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_OD;
4)、GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1改为GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
其他没变化
硬件图
串口截图
程序
gd3218b20.rar
(360.71 KB)
确实感觉stm32可以被GD32取代哦
|