Moderadores: andre_luis, 51
void NVIC_EnableIRQ ( IRQn_Type IRQn)
denis escreveu:AN4062
STM32F0DISCOVERY peripheral firmware examples
#define led GPIOA_ODR_bit.ODR11
unsigned int i;
__irq void TIM2_IRQHandler(void)
{
// TIM2_CR1_bit.URS=0;//reset da interrupt
TIM2_SR_bit.TIF=0;//reset da interrupt
//TIM2_SR_bit.UIF=0;//reset da interrupt
led^=1;
i++;
}
void setupports()
{
//stm051c8
#ifdef f051
GPIOA_OSPEEDR=GPIOB_OSPEEDR=0; //velocidade
GPIOA_OTYPER=GPIOB_OTYPER=0; //push pull
GPIOA_ODR=GPIOB_ODR=0; //saida
GPIOA_MODER=0x55555555; //tudo
GPIOB_MODER=0x55555555; //saída
RCC_APB2ENR=0xffffffff; //clock em tudo
// RCC_APB1ENR_bit.TIM2EN=1; //clock no timer 2 ... quase tudo
RCC_APB1ENR=0xffffffff; //agora sim .. tudo mesmo
SETENA0|=(1<<(NVIC_TIM2-16));//de fato coloquei 0xffffffff pra liberar geral
void Timer2_interrupt() iv IVT_INT_TIM2 {
TIM2_SR.UIF = 0;
GPIOB_ODR ^= 0xffff;// ~GPIOB_ODR; // Toggle PORTE led's
}
void main() {
GPIO_Digital_Output(&GPIOB_BASE, _GPIO_PINMASK_ALL); // Enable digital output on PORTE
GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_ALL); // Enable digital output on PORTE
// GPIOB_CRL=GPIOB_CRH=0x232323;
// GPIOA_CRL=GPIOA_CRH=0x232323;
// GPIOB_MODER=0x55555555;
// GPIOA_MODER=0x55555555;
GPIOB_ODR = 0x5555;
GPIOA_ODR = 0xaaaa;
RCC_APB1ENR.TIM2EN = 1; // Enable clock gating for timer module 2
RCC_APB1ENR=0xffffffff;
RCC_APB2ENR=0xffffffff;
TIM2_CR1.CEN = 0; // Disable timer
TIM2_PSC = 2; // Set timer prescaler.
DIR_TIM2_CR1_bit =1;//down-up count
TIM2_ARR = 5;
NVIC_IntEnable(IVT_INT_TIM2); // Enable timer interrupt
TIM2_DIER.UIE = 1; // Update interrupt enable
TIM2_CR1.CEN = 1; // Enable timer
while(1)
{
GPIOA_ODR ^= 0xffff;
} ;
Access to register of unclocked peripheral at 0x40000000 cause BUS_FAULT [U1_CM3CORE]
#include "stm32f10x.h"
/********************************************************************
Configuração do TIM3 - > CH2 -> PA7 PWM output
Saída PWM: Alternate function Push-pull.
Frequência: aproximadamente 3516 Hz.
Config: init 14/01/2016
Fonte de clock: APB2 = 36 MHz.
********************************************************************/
void TIM3_Config(void)
{
// enable interrupt Overflow TIM3.
NVIC->ISER[0] |= (1 << TIM3_IRQn); // Necessário #define STM32F10X_MD em "stm32f10x.h"
// Configuro os pinos de saída como função alternativa
/* PA4 saída Alt. func. PWM TIM3 CH2 push-pull output */
GPIOA -> CRL &= ~(BIT_30 + BIT_28);
GPIOA -> CRL |= (BIT_31 + BIT_29); // PA7 Alt. Func. push-pull
RCC->APB1ENR |= BIT_1; // Ligo clock do TIM3
/* Prescale de clock do timer, <13:11> PPRE2 = 0bxxx : APB2 prescaler clock = 72/xxx = yy MHz */
//RCC -> CFGR |= BIT_13; // Já configurado na inicialização de clock.
//RCC -> CFGR &= ~(BIT_12 + BIT_11);
TIM3 -> PSC = 4; // Prescaler 1:5 (1+PSC), entrada 72 MHz, clock TIM1 = 14,4 MHz.
// O módulo será configurado de "main.c", mas inicializo ele para 4 ms de Ts.
TIM3 -> ARR = 4095; // Auto reload; 14,4 MHz/4095 = 3516 Hz
TIM3 -> SR &= ~BIT_0; // Limpo o flag de interrupção por update (UIE).
TIM3 -> DIER |= BIT_0; // Ligo a interrupção de Update.
TIM3 -> CCR2 = 0; // Razão cíclica zero no início.
TIM3 -> CCMR1 |= (BIT_14 + BIT_13 + BIT_11); // PWM MODE 1 ch2, OC2PE pre-load CCP2 enab.
TIM3 -> CCER |= BIT_4; // Habilito pino de saída CH2 TIM3
//TIM3 -> BDTR |= BIT_15; // Main output En
/* Config. Dead Time: p. 333 (CR1 <9:8>), p. 356 */
/*
TIM3 -> CR1 &= ~BIT_9;
TIM3 -> CR1 |= BIT_8; // tDTS = 2*tCK_INT (tCK_INT = clock timer = 72 MHz).
TIM3 -> BDTR |= (BIT_2 + BIT_0); // Dead time = 5*tDTS = 5*27 ns.
TIM3 -> BDTR &= ~(BIT_7 + BIT_6 + BIT_5 + BIT_4 + BIT_3 + BIT_1); //Limpo os demais bits.
*/
TIM3 -> CR1 |= BIT_0; // Ligo o Timer.
TIM3 -> CCR2 = duty; // Ajusto o ciclo ativo inicial.
TIM3 -> CR1 |= BIT_2; // Only overflow update:
TIM3 -> CR1 |= BIT_0; // Ligo o Timer.
}
/********************************************************************
Interrupção TIM3
Acontece quando o timer transborda - 3.5 kHz
********************************************************************/
void TIM3_IRQHandler(void)
{
TIM3 -> SR &= ~BIT_0; // Limpo o flag de interrupção por update (UIE).
}
eletroinf escreveu:Segue um código que eu já utilizei para gerar interrupção por meio do Timer 3, quem sabe ajude.
sim é o stlink mesmo amigo. Agora no debug do mikro c, não há meio do danado do mc fazer PORTA=saída (exceto PA14,13 hein!)denis escreveu:este gravador que você está utilizando.
GPIOB_MODER=0b01010101010101010101010101010101; // tudo bem, o debug mostra direitin mas
GPIOA_MODER=0b01000001010101010101010101010101; // pqp.. tnc.. num vai.. só fica no default mesmo 0x280000000000
//PA14,13 ^^^^ --> prestenção!
Devo estudar o que a função
GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_ALL); // Enable digital output
eletroinf escreveu:Pois éh, entender essas funções é algo sinistro.
É muito mais fácil olhar no datasheet os registradores e escrever diretamente neles, a exemplo do código que eu postei aí em cima.
Pelo menos é o que eu acho.
Além disso, se tu usar um debug, vai ver que estas funções chamam funções e defines no meio de uma montanha de includes, totalmente ridículo isso aí.
#define led GPIOB_ODRbits.ODR2
void Timer2_interrupt() iv IVT_INT_TIM2 {
TIM2_SR.UIF = 0;
led ^= 1; // não funciona
}
void delay (unsigned int dl)
{
while (dl--);
}
void main() {
GPIO_Digital_Output(&GPIOB_BASE, _GPIO_PINMASK_ALL); // Enable digital output on PORT
GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_15|_GPIO_PINMASK_12|_GPIO_PINMASK_11|_GPIO_PINMASK_10|\
_GPIO_PINMASK_9|_GPIO_PINMASK_8|_GPIO_PINMASK_7|_GPIO_PINMASK_3|\
_GPIO_PINMASK_2|_GPIO_PINMASK_1|_GPIO_PINMASK_0);//
GPIOB_MODER=0b01010101010101010101010101010101;
// GPIOA_MODER=0b01000001010101010101010101010101;
apesar que as funções que teu exemplo não necessariamente mostra o acesso ao hw do mc
#define BIT_0 1
#define BIT_1 2
#define BIT_2 4 (...)
eletroinf escreveu:Aquelas define de Bits são nesse estilo:
GPIO_Digital_Output(&GPIOB_BASE, _GPIO_PINMASK_ALL); // Enable digital output on PORT
GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_11|_GPIO_PINMASK_10|_GPIO_PINMASK_9|_GPIO_PINMASK_8|\
_GPIO_PINMASK_7|_GPIO_PINMASK_3|_GPIO_PINMASK_2|_GPIO_PINMASK_1);
GPIO_Clk_Enable(&GPIOB_BASE);
GPIO_Clk_Enable(&GPIOA_BASE);
RCC_AHBENR|=((1<<18)|(1<<17));//0b0000000000001100000000000000000 //clk PORTB, PORTA
#define BIT_0 1
#define BIT_1 2
#define BIT_2 4
#define BIT_3 8
#define BIT_4 16
#define BIT_5 32
#define BIT_6 64
#define BIT_7 128
#define BIT_8 256
#define BIT_9 512
#define BIT_10 1024
#define BIT_11 2048
#define BIT_12 4096
#define BIT_13 8192
#define BIT_14 16384
#define BIT_15 32768
#define BIT_16 65536
#define BIT_17 131072
#define BIT_18 262144
#define BIT_19 524288
#define BIT_20 1048576
#define BIT_21 2097152
#define BIT_22 4194304
#define BIT_23 8388608
#define BIT_24 16777216
#define BIT_25 33554432
#define BIT_26 67108864
#define BIT_27 134217728
#define BIT_28 268435456
#define BIT_29 536870912
#define BIT_30 1073741824
#define BIT_31 2147483648
Voltar para STMicroelectronics
Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante