demo.c
来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 473 行 · 第 1/2 页
C
473 行
/*************************************************************************************/
/* */
/* Copyright (C) 2005 Oki Electric Industry Co., LTD. */
/* */
/* System Name : ML674051/ML67Q4061 */
/* Module Name : Demo code for AME-51 Board */
/* File Name : demo.c */
/* Revision : 1.00 */
/* Date : 2005/05/18 */
/* Initial version */
/* */
/*************************************************************************************/
#include "ML674061.h"
#include "common.h"
#include "irq.h"
#ifdef __IAR__
#include "intrinsics.h"
#endif
/* constants */
#define debug 1 /* set debug to 1 for debug mode */
#if debug ==1
#define SRAM_BASE 0x10000 /* base address of unused SRAM in bank0 */
#define SRAM_SIZE 0x100 /* SRAM size - 256 x 32 */
#elif debug ==0
#define SRAM_BASE 0xC8000000 /* base address of external SRAM */
#define SRAM_SIZE 0x40000 /* SRAM size - 256K x 32 */
#endif
#define sample (3) /* number of samples for debounce */
#define on 0x00 /* this value sent to port E will turn on all of the LEDs */
#define off 0x07 /* this value sent to port E will turn off all of the LEDs */
#define MHz (1000000L)
#define TMRCYC (10) /* interval of timer interrupt (ms) */
//#define CCLK (32*MHz) /* CCLK (Hz) */
#define CLKGEAR (1) /* clock gear */
//#define CLKTMR (CCLK/CLKGEAR) /* frequency of CLKTMR terminal (Hz) */
#define SYSCLK (32) /* SYSCLK (MHz)*/
#define VALUE_OF_TMRLR /* reload value of timer */\
(65536 - (TMRCYC * SYSCLK * 1000) / 16)
#if ((VALUE_OF_TMRLR) < 0 || 0x10000 <= (VALUE_OF_TMRLR))
#error "Invalid value : VALUE_OF_TMRLR"
#endif
/* functions */
int main(void); /* main routine */
static void reg_irq_handler(void); /* registration of IRQ handler */
static void set_timer(void); /* setup of timer */
static void timer_handler(void); /* timer handler */
static void init_switches(void); /* initialize switches */
void test_failure(void); /* flashes LEDs when a test fails */
void both_switches(void); /* checks both switches pushed simultaneously */
extern void uart_entry(void);
extern void uart_test(void);
extern void i2c_test(void);
extern void i2c_entry(void);
extern int adc_entry(void);
extern int rtc_entry(void);
/* global variables */
volatile int counter; /* timer counter */
volatile char both_switches_flag; /* this flag will be cleared after switches SW2 and SW3
are both released (if both were pushed at the same time) */
static volatile unsigned int led_count;
static volatile int sw4;
static volatile int led_seg;
static volatile int clk_reg; /* clkcnt register value */
UHWORD switch2[sample]; /* switch 2 samples */
UHWORD switch3[sample]; /* switch 3 samples */
volatile UBYTE switch2_state; /* 0=opened 1=closed */
volatile UBYTE switch3_state; /* 0=opened 1=closed */
/* LED lighting pattern table */
UHWORD LED_TABLE [18] = {LED_0,LED_1,LED_2,LED_3, /* "0","1","2","3" */
LED_4,LED_5,LED_6,LED_7, /* "4","5","6","7" */
LED_8,LED_9,LED_A,LED_b, /* "8","9","A","b" */
LED_C,LED_d,LED_E,LED_F, /* "C","d","E","F" */
LED_all,LED_off}; /* all on ,all off */
UBYTE SW2 = PI1 & 0x04; /* switch 2 */
UBYTE SW3 = PI1 & 0x08; /* swithc 3 */
WORD CLK_REG;
/****************************************************************************/
/* Entry point */
/* Function : main */
/* Parameters */
/* Input : Nothing */
/* Output : 0 */
/****************************************************************************/
int main(void)
{
int i;
for (i=0; i<80000; i++)
put_value(PM4,7); /* init PE 0-2 as output */
init_irq(); /* initialize IRQ */
reg_irq_handler(); /* registration of IRQ handler */
set_timer(); /* setup of timer */
init_led(); /* initialize LED */
init_switches(); /* initialize switch buffers */
led_count = 0; /* initialize variable */
counter = 0;
/* enable IRQ */
#ifdef __IAR__
__enable_interrupt();
#else
irq_en();
#endif
/* timer start */
put_wvalue(TMEN, 0x01); /* enable timer (write '1' in TMEN[0]) */
//=============================================================
// LED test
//=============================================================
led_on(LED_off); /* turn off all segments on numeric LED display*/
put_value(PO4,6); /* turn on red LED */
counter = 0;
while (counter<35) /* wait 0.35 seconds */
{
}
counter = 0;
put_value(PO4,5); /* turn on yellow LED */
while (counter<35) /* wiat 0.35 seconds */
{
}
counter = 0;
put_value(PO4,3); /* turn on green LED */
while (counter<35) /* wait 0.35 seconds */
{
}
counter = 0;
put_value(PO4,7); /* turn off all LEDs */
led_on((UBYTE)LED_TABLE[LED_off]); /* turn off all segments */
led_seg = 1;
for (i=0;i<6;i++) /* do first 6 segments */
{
put_value(PO5,led_seg); /* turn on segment a */
while (counter<35) /* wait 0.35 seconds */
{
}
counter = 0;
led_seg = led_seg*2;
}
put_value(PO5,0); /* turn off segments */
put_value(PO3,0x08); /* turn on segment g */
while (counter<35) /* wait 0.35 seconds */
{
}
counter = 0;
put_value(PO3,0x10); /* turn on dp */
while (counter<35) /* wait 0.35 seconds */
{
}
counter = 0;
led_on(LED_all); /* turn on all segments */
while (counter<35) /* wait 0.35 seconds */
{
}
counter = 0;
led_on(LED_off); /* turn off all segments */
//=============================================================
// RAM test
//=============================================================
led_on(LED_2); /* put "2" on LED display */
put_value(PO4,6); /* turn on red LED */
put_wvalue(BWC,0x3c); /* set RAM to 32 bit bus */
put_wvalue(RAMAC,0x00); /* set RAM access timing */
for (i = 0; i < SRAM_SIZE; i++)
{
put_wvalue(SRAM_BASE+(i*4),0x55555555); /* write 55 to memory */
}
put_value(PO4,3); /* turn on green LED */
for (i = 0; i < SRAM_SIZE; i++)
{
if (get_wvalue(SRAM_BASE+(i*4)) != 0x55555555) /* check memory */
{
test_failure(); /* flash LEDs forever */
i=SRAM_SIZE; /* quit this test */
}
}
put_value(PO4,5); /* turn on yellow LED */
for (i = 0; i < SRAM_SIZE; i++)
{
put_wvalue(SRAM_BASE+(i*4),0xAAAAAAAA); /* write AA to memory */
}
put_value(PO4,3); /* turn on green LED */
for (i = 0; i < SRAM_SIZE; i++)
{
if (get_wvalue(SRAM_BASE+(i*4)) != 0xAAAAAAAA) /* check memory */
{
test_failure(); /* flash LEDs forever */
i=SRAM_SIZE; /* quit this test */
}
}
put_value(PO4,7); /* turn off all LEDs */
//=============================================================
// I2C test
//=============================================================
i2c_test();
//=============================================================
// UART loopback test - tests both UARTs
//=============================================================
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?