Let's learn mini2440 bare metal development (XI) -- mini2440 timer 0 interrupt experiment

Publisher:HeavenlyMelodyLatest update time:2021-11-04 Source: eefocusKeywords:mini2440 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When explaining the system clock and timer earlier, an experiment was given. The function implemented was: using the function of timer 0 to make the LED flash once per second. At that time, it was implemented using the query method, and now the interrupt method is used to implement the above function.


The following figure shows the layout of my project files:

I'll post my code below, you can also download it here.


http://download.csdn.net/detail/mybelief321/5457371 After downloading, compile directly, click Flash/Download, download to nor flash and run.


main.c file


#include "led.h"

#include "timer.h"

#include "isrservice.h"

#include "interrupt.h"


unsigned int flag = 0;

int main()

{   

 Led_Init(); //Initialize LED

 Timer0_Init(); //Timer 0 initialization

 Timer0_Interrupt_Init(); //Timer 0 interrupt initialization


 while(1) //Loop, waiting for an interrupt to occur

 {  

  if(flag)

  {

   Led2_On();

  }

  else

  {       

   Led2_Off();

  }

 }

}


 


     led.c file


 


/****************************************************

* The GPIO ports corresponding to the 4 LED lights on my mini2440 development board

* LED1---GPB5 LED2---GPB6

* LED3---GPB7 LED4---GPB8

*********************************************************/


#include


/****************************************************

* Function name: void Led_Init(void)

* Global variables: None

* Parameter Description: None

* Return value; None

* Function: Set GPB5-8 as output function, initialize 4 LED lights off

*********************************************************/

void Led_Init(void)

{

  GPBCON&=~((3<<10)|(3<<12)|(3<<14)|(3<<16));

  GPBCON|=((1<<10)|(1<<12)|(1<<14)|(1<<16)); //Set GPB5-8 ports to output function

  GPBUP&=~((1<<5)|(1<<6)|(1<<7)|(1<<8)); //Pull-up resistor enabled

  GPBDAT|=(1<<5)|(1<<6)|(1<<7)|(1<<8); //Set GPBDAT5-8 to high level, which means all 4 LEDs will be off

}


 


    led.h file


#ifndef __LED_H__

#define __LED_H__


#include

#define Led1_On() {GPBDAT&=(~(1<<5));}

#define Led1_Off() {GPBDAT|=(1<<5);}

#define Led2_On() {GPBDAT&=(~(1<<6));}

#define Led2_Off() {GPBDAT|=(1<<6);}

#define Led3_On() {GPBDAT&=(~(1<<7));}

#define Led3_Off() {GPBDAT|=(1<<7);}

#define Led4_On() {GPBDAT&=(~(1<<8));}

#define Led4_Off() {GPBDAT|=(1<<8);}

/****************************************************

* Function name: void Led_Init(void)

* Global variables: None

* Parameter Description: None

* Return value; None

* Function: Set GPN5-8 as output function, initialize 4 LED lights off

*********************************************************/

void Led_Init(void);


#endif

    timer.c file



#include //s3c2440.h defines some addresses of S3C2440 chip

#include "timer.h"


/*******************************************************************

* Function name: void Timer0_Init(void)

* Parameter Description: None

* Global variables: None

* Return value: None

* Function: For 50MHz PCLK, obtain 62.5KHz timer 0 after frequency division

* Input clock.

*******************************************************************/

void Timer0_Init(void)

{

 TCFG0&=~(0xff); //Set the first level frequency division coefficient to 99

 TCFG0|=99;


 TCFG1&=~(0xf); //Set the second level frequency division coefficient to 8

 TCFG1|=0x02; //62.5KHz=50MHz/(99+1)/8


 TCNTB0=62500; //1s interrupt. After the above divider, the input clock frequency of timer 0 is 62.5kHz, that is, timer

                     //Count 62500 times per second. Therefore, when initialized, the initial count value of timer 0 is 62500

      //Here we can see that TCMPBn is not set, because we use its default value 0, so there is no need to set it


 TCON|=(1<<1); //Turn on the manual update bit, that is, when the timer is turned on, the values ​​in TCMPB0 and TCNTB0 will be loaded into registers TCMP0 and TCNT0

 TCON = 0x09; // Turn off the manual update bit, set the automatic load bit, and start the timer at the same time. In this way, TCNT0 counts down by 1. When the count in TCNT0 is

                 //When the value is reduced to 0, the data in TCNTB0 and TCMPB0 will be automatically loaded into TCNT0 and TCMP0 respectively and a new round of subtraction counting will be performed.

}

     timer.h file


#ifndef __TIMER_H__

#define __TIMER_H__


/*******************************************************************

* Function name: void Timer0_Init(void)

* Parameter Description: None

* Global variables: None

* Return value: None

* Function: For 50MHz PCLK, obtain 62.5KHz timer 0 after frequency division

* Input clock.

*******************************************************************/

void Timer0_Init(void);


#endif


 


    interrupt.c file


#include

#include "interrupt.h"



/****************************************************

* Function name: void Timer0_Interrupt_Init(void)

* Global variables: None

* Parameter Description: None

* Return value; None

* Function: Set the timer 0 interrupt mask bit to invalid

*********************************************************/

void Timer0_Interrupt_Init(void)

 INTMSK&=~(1<<10); //Set the timer 0 interrupt mask bit to invalid, so that when

                                     //When timer 0 is interrupted, the interrupt request signal can

} //Smoothly reach the CPU


    interrupt.h file


#ifndef __INTERRUPT_H__

#define __INTERRUPT_H__



/****************************************************

* Function name: void Timer0_Interrupt_Init(void)

* Global variables: None

* Parameter Description: None

* Return value; None

* Function: Set the timer 0 interrupt mask bit to invalid

*********************************************************/

void Timer0_Interrupt_Init(void);


#endif


 


     isrservice.c file


#include

#include "isrservice.h"


extern unsigned int flag; //Declare the external variable flag, which is defined in the main.c file

                                      //When 1s arrives, the interrupt response function inverts the variable value and

                                     //The program implements different operations by detecting the value of this variable


/****************************************************

* Function name: void __irq IRQ_Handler(void) 

* Global variables: None

* Parameter Description: None

* Return value; None

* Function: Timer 0 interrupt service function, must add __irq

*********************************************************/

void __irq IRQ_Handler(void) //Note that this function name must be the same as the jump label at S3C2440.s   

{

 flag=!flag; //1s time is up, the flag value is inverted

 SRCPND|=1<<10; //Clear interrupt flag

 INTPND|=1<<10;


}


       isrservice.h file


#ifndef __ISRSERVICE_H__

#define __ISRSERVICE_H__


/****************************************************

* Function name: void __irq IRQ_Handler(void)

* Global variables: None

* Parameter Description: None

* Return value; None

* Function: Timer 0 interrupt service function, must add __irq

*********************************************************/

void __irq IRQ_Handler(void);


#endif


Be sure to remember to download it to NOR flash. If you don't know how, please read the blog:


http://blog.csdn.net/mybelief321/article/details/8954788


Keywords:mini2440 Reference address:Let's learn mini2440 bare metal development (XI) -- mini2440 timer 0 interrupt experiment

Previous article:Let's learn mini2440 bare metal development (XII) -- mini2440 serial port interruption experiment
Next article:Let's learn mini2440 bare metal development (Part 9) --ARM interrupt control system

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号