Arkdaşlar bu yazıda CoCoox ide ile ADC okuma uygulamasını gösterceğim.Faydalı olması dileğiyle.
MAIN.C Dosya içeriği:
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx.h"
#include "stm32f0xx_adc.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_tim.h"
__IO uint16_t ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0;
/* Private function prototypes -----------------------------------------------*/
void ButtonInit(void);
void ADC1_Config(void);
void ADC_read(void)
{
/* User button configuration */
ButtonInit();
/* Configure ADC1 Channel 11 */
ADC1_Config();
/* Infinite loop */
while (1)
{
/* Press User button to get the converted data */
while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == RESET);
/* Get ADC1 converted data */
ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
/* Compute the voltage */
ADC1ConvertedVoltage = (ADC1ConvertedValue * 3300)/0xFFF;
}
}
void ButtonInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable the BUTTON Clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Configure Button pin as input */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void ADC1_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* GPIOC Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* TIM3 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Configure ADC Channel11 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* TIM3 Configuration *******************************************************/
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 0xFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* TIM3 TRGO selection */
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
/* ADC1 Configuration *******************************************************/
/* ADCs DeInit */
ADC_DeInit(ADC1);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits*/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
/* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_28_5Cycles);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
/* Enable the auto delay feature */
ADC_WaitModeCmd(ADC1, ENABLE);
/* Enable the Auto power off mode */
ADC_AutoPowerOffCmd(ADC1, ENABLE);
/* Enable ADCperipheral[PerIdx] */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* TIM2 enable counter */
TIM_Cmd(TIM3, ENABLE);
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
}
int main(void)
{
ADC_read();
while(1){
}
}
Elektronik Klavuzu
8 Mayıs 2014 Perşembe
6 Mayıs 2014 Salı
Stm32f0 Discovery Board PWM driver
Bu yazım da Stm32f0 Discovery Board da gene ücretsiz bir yazılım olan CoCoox ide ile PWM sürmeyi deneyeceğim.Gösterge olarak boarddaki ledleri kullanacağız.
Main Dosya içeriği :
#include "stm32f0xx.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_tim.h"
#include "pwm.h"
int main(void)
{
TIM_Config();
PWM_Init(15625);
PWM_Cmd(ENABLE);
PWM_ConfigChannel(0,5);
PWM_ConfigChannel(1,95);
while(1)
{
}
}
PWM.H Dosya içeriği :
#ifndef __PWM_H__
#define __PWM_H__
#include "stm32f0xx.h"
#include "stm32f0xx_conf.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_tim.h"
#ifdef __cplusplus
extern "C"
{
#endif
void TIM_Config(void);
void PWM_Init(uint32_t Frecuency);
void PWM_ConfigChannel(uint32_t Channel, uint32_t DutyCycle);
void PWM_Cmd(FunctionalState NewState);
#ifdef __cplusplus
}
#endif
#endif // __PWM_H__
Main Dosya içeriği :
#include "stm32f0xx.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_tim.h"
#include "pwm.h"
int main(void)
{
TIM_Config();
PWM_Init(15625);
PWM_Cmd(ENABLE);
PWM_ConfigChannel(0,5);
PWM_ConfigChannel(1,95);
while(1)
{
}
}
PWM.H Dosya içeriği :
#ifndef __PWM_H__
#define __PWM_H__
#include "stm32f0xx.h"
#include "stm32f0xx_conf.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_tim.h"
#ifdef __cplusplus
extern "C"
{
#endif
void TIM_Config(void);
void PWM_Init(uint32_t Frecuency);
void PWM_ConfigChannel(uint32_t Channel, uint32_t DutyCycle);
void PWM_Cmd(FunctionalState NewState);
#ifdef __cplusplus
}
#endif
#endif // __PWM_H__
PWM.C Dosya içeriği:
#include "pwm.h"
static uint32_t TimerPeriod;
void TIM_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOC Clocks enable */
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_0);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_0);
}
void PWM_Init(uint32_t Frequency)
{
Frequency/=2;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_Config();
TimerPeriod = (SystemCoreClock / Frequency ) - 1;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
}
void PWM_ConfigChannel(uint32_t Channel, uint32_t DutyCycle)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
uint32_t ChannelPulse = 0;
DutyCycle=100-DutyCycle ;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
ChannelPulse = (uint16_t) ((DutyCycle * 10 *(TimerPeriod - 1)) / 1000);
TIM_OCInitStructure.TIM_Pulse = ChannelPulse;
if(Channel == 0){
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
}else if(Channel == 1){
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
}
}
void PWM_Cmd(FunctionalState NewState)
{
TIM_Cmd(TIM3, NewState);
}
5 Mayıs 2014 Pazartesi
Stm32f0 Discovery Board 2x16 LCD driver
Merhaba, ilk yayınımda sizlere CoCoox ide'de STM32F0 Discovery Board için kullanabileceğiniz 2x16 LCD kaynak kodunu paylaşıyorum .Faydalı olması dileğiyle...
Main Dosya İçeriği:
Kaydol:
Kayıtlar (Atom)