An interruption with KL25Z

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

An interruption with KL25Z

跳至解决方案
3,406 次查看
calli
Contributor I

Hi everyone !

I try to do an example of an interrupt without PE. I use a KL25Z.

The program has to do a timer which count to 5seconds, then the interruption turn on the blue LED and the timer restart. The second time, the blue led turn off, and the timer restart and again and again ...

I got some difficult to do this program as a beginner.

Could you help me to do it, this is what i did for the moment.

#include "MKL25Z4.h"

static int i = 0;

void delay(void);

void TPM_Init(void);

void GPIOBlue_Init(void);

uint16_t getTPMCounter(void);

void ResetTPM(void);

int main(void)

{

    uint16_t delayTimerTick;

    /* Write your code here */

    /* This for loop should be replaced. By default this loop allows a single stepping. */

    TPM_Init();

    ResetTPM();

    delay();

    delayTimerTick=getTPMCounter();

    __asm("nop"); //set a break point here

    for (;;) {

        i++;

    }

    /* Never leave main */

    return 0;

}

void GPIOBlue_Init(void)

{

    SIM_SCGC5 = 0b01011110000000;    // Active l'horloge pour driver les ports A,B et D

    PORTD_PCR1 = 0b00100000000;

    GPIOD_PDDR = 0b10;

    GPIOD_PDOR = 0b10;

}

void TPM_Init(void)

{

    SIM_SCGC6|=0x03000000; //enable FTM0 and FTM0 module clock

    SIM_SCGC5|=0x3E00;

    SIM_SOPT2|=0x1000000; //select TPMSRC clock source as MCGIRCLK clock

    TPM0_CONF=0x00; //set up BDM in 11

    TPM0_MOD=0xFFFF;

    TPM0_C0SC=0x14; //FTM output signal toggle when the FTM counter matches with //C0V registrer

    TPM0_C0V=500;

    TPM0_SC=0x08; // system clock driving, dividing by 1

}

void ResetTPM(void)

{

    TPM0_CNT=0x00;

}

uint16_t getTPMCounter(void)

{

     uint16_t temp;

     temp=TPM0_CNT;

     return temp;

}

void delay(void)

{

    uint16_t delayCounter;

    for(delayCounter=0; delayCounter<1000; delayCounter++)

    {

        __asm("nop");

        __asm("nop");

    }

}

//void PORTD_IRQHandler(void)

    //{

        //DelayFunction();

        //GPIOD_PDOR = 0b10;                /*Turn Off Blue Led*/

        //GPIOD_PDOR = 0b00;                /*Turn On Blue Led*/

        //DelayFunction();

        //GPIOD_PDOR = 0b10;                /*Turn Off Blue Led*/

        //DelayFunction();

        //PORTD_ISFR = PORT_ISFR_ISF(0x40);        /* Clear interrupt status flag */

    //}

    //void DelayFunction(void)

    //{

        //int cnt;

        //for(cnt=0; cnt<1000000; cnt++)

        //{

        //}

    //}

标签 (1)
标记 (2)
1 解答
2,413 次查看
mr_max
Contributor IV

Hi Max,

I did not analyse your code but the first thing I can figure out is you should use more MKL25Z4.h macro. It will help the reader (and you) to make your code more self explain.

I attached some old code I wrote years ago. It work without PEx.

The first one manage the GPIO external Interrupt

#include "MKL25Z4.h"

#define PTA1    0x02

#define PTA2    0x04

#define PTD1    0x01

#define PTD4    0x10

#define PTB18    0x40000

static int i = 0;

void PORTD_IRQHandler()

{

    GPIOB->PTOR |= PTB18;

    PORTD->ISFR |= PTD4;

    //NVIC_ClearPendingIRQ(PORTD_IRQn);

}

void PORTA_IRQHandler()

{

    GPIOB->PTOR |= PTB18;

    PORTA->ISFR |= PTA1|PTA2;

    //NVIC_ClearPendingIRQ(PORTA_IRQn);

}

int main(void)

{

    /* Write your code here */

     MCG->C2 |= MCG_C2_IRCS_MASK;        //Fast internal reference clock (4MHz)

    MCG->SC &= !(MCG_SC_FCRDIV(0x01));    //Diviseur fast internal reference clock /1

    SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTD_MASK;    //Set up peripheral clock for GPIO A,B,D

    PORTA->PCR[1] |= PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(1) | PORT_PCR_IRQC(0x0A);//PTA1 as GPIO, Pull Up, interrupt on falling edge

    PORTA->PCR[2] |= PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(1) | PORT_PCR_IRQC(0x0A);//PTA2 as GPIO, Pull up, interrupt on falling edge

    PORTD->PCR[4] |= PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(1) | PORT_PCR_IRQC(0x0A);//PTD4 as GPIO, Pull up, interrupt on falling edge

    PORTD->PCR[1] |= PORT_PCR_MUX(1);//PTD1 as GPIO

    PORTB->PCR[18]|= PORT_PCR_MUX(1);//PTB18 as GPIO

    //PTA1 & PTA2 as Input

    GPIOA->PDDR &= ~(PTA1 | PTA2);//GPIO_PDDR_PDD(1)|GPIO_PDDR_PDD(2);   

    GPIOD->PDDR &= ~PTD4;//PTD4 as Input

    //Set GPIO external Interrupt pin

    GPIOD->PDDR |= PTD1;    //Set PTD1 port direction as input

    GPIOD->PCOR |= PTD1;    //Clear PTD1

    GPIOB->PDDR |= PTB18;    //SetPTB18 port direction as input

    GPIOB->PCOR |= PTB18;    //Clear PTB18

    //Enable External Interupt

    NVIC_EnableIRQ(PORTA_IRQn);   

    NVIC_EnableIRQ(PORTD_IRQn);   

    while(1);

    return 0;

}

Down here is TPM functions with initialization and delay for µs and ms purpose. Maximum dealy time is limited to 65535 µs ou ms. TPM increment every 1µs if source clock is MCGIRCLK = 4MHz.

/*

* TMP.c

*

*  Created on: 4 mai 2015

*      Author: maximedo

*/

//TPM.c v1

//Programmeur : Dolberg Maxime

//Date : 22 juillet 2014

//Librairie timer 0 de KL25

//Config par dÈfaut

//Source clock : MCGIRCLK @4MHz

//LPTM freq : 1MHz

//

#include "MKL25Z4.h"

#include "TPM.h"

void TPM_init(void)

{

    SIM->SCGC6 |= SIM_SCGC6_TPM0_MASK;//TPM0 clock

    SIM->SOPT2 |= SIM_SOPT2_TPMSRC(0x01);//TPM clock source -> MCGIRCLK

   TPM0->SC = TPM_SC_CMOD(0x00);//LPTPM

   TPM0->SC |= TPM_SC_PS(0x02);//

}

void TPM0_delai_us(int t)

{

    TPM0->MOD = TPM_MOD_MOD(t);//

    TPM0->SC |= TPM_SC_TOF_MASK;//Reset TOF bit

    TPM0->SC |= TPM_SC_CMOD(0x01);//LPTPM  compteur interne

    while((TPM0->SC & TPM_SC_TOF_MASK)!=TPM_SC_TOF_MASK);

    TPM0->SC |= TPM_SC_TOF_MASK;//clear TOF bit

}

void TPM0_delai_ms(int t)

{

    int i;

    TPM0->MOD = TPM_MOD_MOD(1000);//

    TPM0->SC |= TPM_SC_TOF_MASK;//Reset TOF bit

    TPM0->SC |= TPM_SC_CMOD(0x01);//LPTPM compteur interne

    for(i=0;i<=t;i++)

    {

        while((TPM0->SC & TPM_SC_TOF_MASK)!=TPM_SC_TOF_MASK);

        TPM0->SC |= TPM_SC_TOF_MASK;//clear TOF bit

    }

}

I attached the code with this post, so you can download it and use it as you want.

Hope it help you.

Maxmbedded.

在原帖中查看解决方案

2 回复数
2,414 次查看
mr_max
Contributor IV

Hi Max,

I did not analyse your code but the first thing I can figure out is you should use more MKL25Z4.h macro. It will help the reader (and you) to make your code more self explain.

I attached some old code I wrote years ago. It work without PEx.

The first one manage the GPIO external Interrupt

#include "MKL25Z4.h"

#define PTA1    0x02

#define PTA2    0x04

#define PTD1    0x01

#define PTD4    0x10

#define PTB18    0x40000

static int i = 0;

void PORTD_IRQHandler()

{

    GPIOB->PTOR |= PTB18;

    PORTD->ISFR |= PTD4;

    //NVIC_ClearPendingIRQ(PORTD_IRQn);

}

void PORTA_IRQHandler()

{

    GPIOB->PTOR |= PTB18;

    PORTA->ISFR |= PTA1|PTA2;

    //NVIC_ClearPendingIRQ(PORTA_IRQn);

}

int main(void)

{

    /* Write your code here */

     MCG->C2 |= MCG_C2_IRCS_MASK;        //Fast internal reference clock (4MHz)

    MCG->SC &= !(MCG_SC_FCRDIV(0x01));    //Diviseur fast internal reference clock /1

    SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTD_MASK;    //Set up peripheral clock for GPIO A,B,D

    PORTA->PCR[1] |= PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(1) | PORT_PCR_IRQC(0x0A);//PTA1 as GPIO, Pull Up, interrupt on falling edge

    PORTA->PCR[2] |= PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(1) | PORT_PCR_IRQC(0x0A);//PTA2 as GPIO, Pull up, interrupt on falling edge

    PORTD->PCR[4] |= PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(1) | PORT_PCR_IRQC(0x0A);//PTD4 as GPIO, Pull up, interrupt on falling edge

    PORTD->PCR[1] |= PORT_PCR_MUX(1);//PTD1 as GPIO

    PORTB->PCR[18]|= PORT_PCR_MUX(1);//PTB18 as GPIO

    //PTA1 & PTA2 as Input

    GPIOA->PDDR &= ~(PTA1 | PTA2);//GPIO_PDDR_PDD(1)|GPIO_PDDR_PDD(2);   

    GPIOD->PDDR &= ~PTD4;//PTD4 as Input

    //Set GPIO external Interrupt pin

    GPIOD->PDDR |= PTD1;    //Set PTD1 port direction as input

    GPIOD->PCOR |= PTD1;    //Clear PTD1

    GPIOB->PDDR |= PTB18;    //SetPTB18 port direction as input

    GPIOB->PCOR |= PTB18;    //Clear PTB18

    //Enable External Interupt

    NVIC_EnableIRQ(PORTA_IRQn);   

    NVIC_EnableIRQ(PORTD_IRQn);   

    while(1);

    return 0;

}

Down here is TPM functions with initialization and delay for µs and ms purpose. Maximum dealy time is limited to 65535 µs ou ms. TPM increment every 1µs if source clock is MCGIRCLK = 4MHz.

/*

* TMP.c

*

*  Created on: 4 mai 2015

*      Author: maximedo

*/

//TPM.c v1

//Programmeur : Dolberg Maxime

//Date : 22 juillet 2014

//Librairie timer 0 de KL25

//Config par dÈfaut

//Source clock : MCGIRCLK @4MHz

//LPTM freq : 1MHz

//

#include "MKL25Z4.h"

#include "TPM.h"

void TPM_init(void)

{

    SIM->SCGC6 |= SIM_SCGC6_TPM0_MASK;//TPM0 clock

    SIM->SOPT2 |= SIM_SOPT2_TPMSRC(0x01);//TPM clock source -> MCGIRCLK

   TPM0->SC = TPM_SC_CMOD(0x00);//LPTPM

   TPM0->SC |= TPM_SC_PS(0x02);//

}

void TPM0_delai_us(int t)

{

    TPM0->MOD = TPM_MOD_MOD(t);//

    TPM0->SC |= TPM_SC_TOF_MASK;//Reset TOF bit

    TPM0->SC |= TPM_SC_CMOD(0x01);//LPTPM  compteur interne

    while((TPM0->SC & TPM_SC_TOF_MASK)!=TPM_SC_TOF_MASK);

    TPM0->SC |= TPM_SC_TOF_MASK;//clear TOF bit

}

void TPM0_delai_ms(int t)

{

    int i;

    TPM0->MOD = TPM_MOD_MOD(1000);//

    TPM0->SC |= TPM_SC_TOF_MASK;//Reset TOF bit

    TPM0->SC |= TPM_SC_CMOD(0x01);//LPTPM compteur interne

    for(i=0;i<=t;i++)

    {

        while((TPM0->SC & TPM_SC_TOF_MASK)!=TPM_SC_TOF_MASK);

        TPM0->SC |= TPM_SC_TOF_MASK;//clear TOF bit

    }

}

I attached the code with this post, so you can download it and use it as you want.

Hope it help you.

Maxmbedded.

2,413 次查看
calli
Contributor I

Thank you very much, your codes are very clear.

0 项奖励
回复