led blinker.c
来自「8051试验程序 基础教材」· C语言 代码 · 共 112 行
C
112 行
/**
* LED Blinker example for the Infineon U-SCALE kit
* Copyright (C) 2007 IAR Systems.
*
* Permission to use, copy, and modify this software with or without fee
* is hereby granted, provided that this entire notice is included in
* all copies of any software which is or includes a copy or
* modification of this software.
*
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTY. IN PARTICULAR, THE AUTHORS MAKES NO
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
* MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
* PURPOSE.
*/
/* Uncomment the #include statement that matches the active device on your U-SCALE */
#include <ioXC866.h>
// #include <ioXC886.h>
// #include <ioXC888.h>
#define IO_vWritePort(PortName, Data) PortName##_DATA = Data
#define SFR_PAGE(pg,op) pg+op
#define _pp0 PORT_PAGE=0 // PORT_PAGE postfix
#define _pp1 PORT_PAGE=1 // PORT_PAGE postfix
#define _pp2 PORT_PAGE=2 // PORT_PAGE postfix
#define _pp3 PORT_PAGE=3 // PORT_PAGE postfix
#define noSST 0x00 // Switch page without saving
/** IO Init */
void IO_Init(void) {
SFR_PAGE(_pp1, noSST); // Switch to page 1
P1_PUDSEL = 0xBF; // Load pullup/pulldown select register
P1_PUDEN = 0x00; // Load pullup/pulldown enable register
SFR_PAGE(_pp0, noSST); // Switch to page 0
P1_DIR = 0xFF; // Load direction register
}
/** Main Init */
void MAIN_vInit(void)
{
/// Initialization of module 'GPIO'
IO_Init();
// Interrupt Priority
IP = 0x00; // Load Interrupt Priority Register
IPH = 0x00; // Load Interrupt Priority High Register
IP1 = 0x00; // Load Interrupt Priority 1 Register
IPH1 = 0x00; // Load Interrupt Priority 1 High Register
IEN0_bit.EA = 1; // Globally enable interrupts
}
/**
Timer 2 Init
Enable timer 2 interrupts.
*/
void T2_vInit(void) {
IEN0_bit.ET2 = 1; // Enable Timer 0 Interrupt
T2_T2CON_bit.TR2 = 1; // Run Timer 2
T2_T2CON_bit.TF2 = 0; // Reset Interrupt Flag
T2_T2MOD_bit.T2PRE0 = 1; // Enable Prescaler
T2_T2MOD_bit.T2PRE0 = 1; // Enable Prescaler
T2_T2MOD_bit.T2PRE0 = 1; // Enable Prescaler
T2_T2MOD_bit.PREN = 0; // Enable Prescaler
T2_T2MOD_bit.DCEN = 1; // Set counter mode
T2_T2CON_bit.EXEN2 = 1; // Set counter mode
}
int main(void)
{
MAIN_vInit(); // Main Init
T2_vInit(); // Timer 2 Interrupt Init
IO_vWritePort(P1, 0x00); // LED on.
T2_T2L = 0xFF; // Reset timer low
T2_T2H = 0xFF; // Reset timer high
while(1){}; /* stop */
}
/**
Timer 2 Interrupt.
*/
#pragma vector = timer2_ext
__interrupt void interrupt_1 ()
{
static char count = 30; // 30 Interrupts per state change
if(count-- == 0){ // Change LED state
if(P1_DATA == 0xFF)
IO_vWritePort(P1, 0x00); // LED on.
else
IO_vWritePort(P1, 0xFF); // LED off.
count = 30; //Reset the counter
}
T2_T2L = 0x00; // Reset Timer 2 Low
T2_T2H = 0x00; // Reset Timer 2 High
T2_T2CON_bit.TF2 = 0; // Reset Interrupt
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?