美文网首页
基于C8051F320的外部中断的使用

基于C8051F320的外部中断的使用

作者: stnevermore | 来源:发表于2019-10-24 10:38 被阅读0次

基于C8051F320的外部中断的使用


  • 官方例程
//-----------------------------------------------------------------------------
// F32x_External_Interrupts.c
//-----------------------------------------------------------------------------
// Copyright 2007 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This software shows the necessary configuration to use External Interrupt 0
// (/INT0) or External Interrupt 1 (/INT1) as an interrupt source.  The code
// executes the initialization routines and then spins in an infinite while()
// loop.  If the button on P1.6 (on the target board) is pressed, then the
// edge-triggered /INT0 input on P0.0 will cause an interrupt and toggle the
// LED.
//
// Pinout:
//
// P0.0 - /INT0
// P0.1 - /INT1
//
// P2.0 - SW1 (Switch 1)
// P2.1 - SW2 (Switch 2)
// P2.2 - LED1
// P2.3 - LED2
//
// How To Test:
//
// 1) Compile and download code to a 'F32x target board.
// 2) On the target board, connect P2.0_SW and P2.1_SW signals on J3 to P0.0
//    for /INT0 and P0.1 for /INT1.
// 3) Press the switches.  Every time a switch is pressed, the P2.2 or P2.3
//    LED should toggle.
//
// Target:         C8051F32x
// Tool chain:     Keil C51 7.50 / Keil EVAL C51
// Command Line:   None
//
//
// Release 1.0
//    -Initial Revision (TP)
//    -14 JUN 2007
//

//-----------------------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------------------

#include <C8051F320.h>

//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------

#define SYSCLK             12000000    // Clock speed in Hz

sbit SW1 = P2^0;                       // Push-button switch on board
sbit SW2 = P2^1;                       // Push-button switch on board
sbit LED1 = P2^2;                      // Green LED
sbit LED2 = P2^3;                      // Green LED

//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void Oscillator_Init (void);           // Configure the system clock
void Port_Init (void);                 // Configure the Crossbar and GPIO
void Ext_Interrupt_Init (void);        // Configure External Interrupts (/INT0
                                       // and /INT1)

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
   PCA0MD &= ~0x40;                    // Disable Watchdog timer

   Oscillator_Init();                  // Initialize the system clock
   Port_Init ();                       // Initialize crossbar and GPIO
   Ext_Interrupt_Init();               // Initialize External Interrupts

   EA = 1;

   while(1);                           // Infinite while loop waiting for
                                       // an interrupt from /INT0 or /INT1
}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Oscillator_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine initializes the system clock to use the precision internal
// oscillator as its clock source.
//-----------------------------------------------------------------------------
void Oscillator_Init (void)
{
   OSCICN = 0x83;                      // Set internal oscillator to run
                                       // at its maximum frequency
}

//-----------------------------------------------------------------------------
// Port_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures the crossbar and GPIO ports.
//
// Pinout:
//
// P0.0 - digital   open-drain  /INT0
// P0.1 - digital   open-drain  /INT1
//
// P2.0 - digital   open-drain  SW1 (Switch 1)
// P2.1 - digital   open-drain  SW2 (Switch 2)
// P2.2 - digital   push-pull   LED1
// P2.3 - digital   push-pull   LED2
//
//-----------------------------------------------------------------------------
void Port_Init (void)
{
   XBR1     = 0x40;                    // Enable crossbar and weak pullups

   P2MDOUT  = 0x0C;                    // LED1 and LED2 are push-pull outputs
}

//-----------------------------------------------------------------------------
// Ext_Interrupt_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures and enables /INT0 and /INT1 (External Interrupts)
// as negative edge-triggered.
//
//-----------------------------------------------------------------------------
void Ext_Interrupt_Init (void)
{
   TCON = 0x05;                        // /INT 0 and /INT 1 are edge triggered

   IT01CF = 0x10;                      // /INT0 active low; /INT0 on P0.0;
                                       // /INT1 active low; /INT1 on P0.1

   EX0 = 1;                            // Enable /INT0 interrupts
   EX1 = 1;                            // Enable /INT1 interrupts
}

//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// /INT0 ISR
//-----------------------------------------------------------------------------
//
// Whenever a negative edge appears on P0.0, LED1 is toggled.
// The interrupt pending flag is automatically cleared by vectoring to the ISR
//
//-----------------------------------------------------------------------------
void INT0_ISR (void) interrupt 0
{
   LED1 = !LED1;
}

//-----------------------------------------------------------------------------
// /INT1 ISR
//-----------------------------------------------------------------------------
//
// Whenever a negative edge appears on P0.1, LED2 is toggled.
// The interrupt pending flag is automatically cleared by vectoring to the ISR
//
//-----------------------------------------------------------------------------
void INT1_ISR (void) interrupt 2
{
   LED2 = !LED2;
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

  • 官方开发板原理图: C8051F32x-DK

  • 备注

    系统以P0.0作为INT0, 以P0.1作为INT1, 但是P2.0连接开关1(Switch1), P2.1连接开关2(Switch2), P2.2和P2.3驱动LED灯. 代码中说明了<font color="#dd0000 ">connect P2.0_SW and P2.1_SW signals on J3 to P0.0 for /INT0 and P0.1 for /INT1.</font> 即把P2.0连接到P0.0上, 把P2.1连接到P0.1上,这样才能产生中断边沿信号

  • 改进
    目前代码里面是使用边沿触发,下降沿触发,也可以设置为电平触发. 使用下降沿触发,按一下按键就会导致外部中断,进入中断服务子程序(反转LED的状态). 如果是电平触发,当一直按着开关才会改变状态,松手就恢复了

相关文章

  • 基于C8051F320的外部中断的使用

    基于C8051F320的外部中断的使用 官方例程 官方开发板原理图: C8051F32x-DK 备注系统以P0.0...

  • Arduino基础入门篇13—外部中断

    本篇介绍Arduino外部中断的使用,通过外部中断检测震动开关的触发来控制LED灯亮灭。 1. 中断介绍 我们已经...

  • 9月18日

    上午老师讲解NVIC中断优先级和外部中断的使用方法包括 常用的外部中断库函数和外部中一般的配置方法 比较难 接触了...

  • 51单片机实验内容及要求

    第二个实验: 外部中断实验 要求: 1、使用P3.2口连接的独立按键进行外部中断实验,要求每次中断实现D1状态翻...

  • 2017年11月13日学习总结

    今天学习中断,再arduino中,中断函数都是被打包好的直接使用就可以,要定义外部中断,中断号,上升还是下降沿中断...

  • 外部中断

    I: 为了使我们的工作和生活更加高效,我们常常会为自己制定一个计划,有时会把这个计划写出来,有时这个计划会在我们心...

  • 外部中断

    对于STM32F103来说,其含有16条中断线,每一个中断线与GPIOx.0~x.15对应起来,比方说中断线0对应...

  • STM32一文通(6) 外部中断

    一. 外部中断/事件控制器 (EXTI) 什么是中断?中断就是打断程序执行顺序的外部输入什么是事件?事件是外部输入...

  • 调整你的番茄钟,让它更适合你自己!

    上次我们说了内部中断,今天我们来讲解外部中断,以及预估和改进自己的番茄钟,从而让它更适合你自己。 外部中断 外部中...

  • 按键中断跟外部中断的区别

    1:GPIO设置上的区别 GPIO_Init(KEY_PORT,KEY_PINS,GPIO_Mode_In_PU_...

网友评论

      本文标题:基于C8051F320的外部中断的使用

      本文链接:https://www.haomeiwen.com/subject/gydrvctx.html