经过几天的努力,终于使5402的XF管脚可以输出方波了。网上都是汇编程序,下面是我的C程序。利用5402的两个定时器,一个把XF拉高,一个拉低。通过中断触发。 另外,我有个问题:书上说中断函数要加interrupt声明,可是我加上后反而不好用了,不知道是怎么回事。希望和大家一起讨论一下。 头文件 /*jicunqi.h*/ /*寄存器定义*/ /*5402*/ #define IMR *(int *)0x00 /*中断屏蔽寄存器*/ #define IFR *(int *)0x01 /*中断标志寄存器*/ #define SWWSR *(int *)0x28 /*软件等待状态寄存器 */ #define PMST *(int *)0x1D /*全局控制寄存器*/ #define CLKMD *(int *)0x58 /*时钟模式寄存器*/ #define ST0 *(int *)0x06 /*状态寄存器0*/ #define ST1 *(int *)0x07 /*状态寄存器1*/ #define TIM *(int *)0x24 /*定时器寄存器*/ #define PRD *(int *)0x25 /*定时器周期寄存器*/ #define TCR *(int *)0x26 /*定时器控制寄存器*/ #define TIM1 *(int *)0x30 /*定时器寄存器*/ #define PRD1 *(int *)0x31 /*定时器周期寄存器*/ #define TCR1 *(int *)0x32 /*定时器控制寄存器*/ #define A *(int *)0x08 #define AR2 *(int *)0x12 主程序: #include #include "jicunqi.h" main() { system_init(); timer();/*定时器初始化程序*/ while(1) { asm(" nop");
} } 中断向量表: .mmregs .ref _TINT0_ISR .ref _TINT1_ISR .ref _c_int00 .sect ".vectors" .align 0x80 RESET: B _c_int00 nop nop nmi: RETE NOP NOP NOP sint17 .space 4*16 sint18 .space 4*16 sint19 .space 4*16 sint20 .space 4*16 sint21 .space 4*16 sint22 .space 4*16 sint23 .space 4*16 sint24 .space 4*16 sint25 .space 4*16 sint26 .space 4*16 sint27 .space 4*16 sint28 .space 4*16 sint29 .space 4*16 sint30 .space 4*16 INT0: RETE NOP NOP NOP INT1: RETE NOP NOP NOP INT2: RETE NOP NOP NOP TINT0: B _TINT0_ISR NOP NOP
BRINT0: RETE NOP NOP NOP BXINT0: RETE NOP NOP NOP SINT6: RETE NOP NOP NOP TINT1: B _TINT1_ISR NOP NOP INT3: RETE NOP NOP NOP BRINT1: RETE NOP NOP NOP BXINT1: RETE NOP NOP NOP .end cmd文件: /* 5402.cmd */ -m map.map MEMORY { PAGE 0: PARAM: origin = 0x1400, len = 0x2000 VECT: origin = 0x3400, len = 0x80 PAGE 1: DARAM: origin = 0x80, len = 0x1380 } SECTIONS { .text :> PARAM PAGE 0 .cinit :> PARAM PAGE 0 .pinit :> PARAM PAGE 0 .vectors :> VECT PAGE 0 .stack :> DARAM PAGE 1 .bss :> DARAM PAGE 1 .data :> DARAM PAGE 1 .const : > DARAM PAGE 1 .switch : > DARAM PAGE 1 .sysmem : > DARAM PAGE 1 .cio : > DARAM PAGE 1 } 中断程序: #include "jicunqi.h" TINT0_ISR() { asm(" SSBX XF"); asm(" SSBX INTM"); TCR=0x0010;//STOP THE TIMER0 TCR1=0x0010;//STOP THE TIMER1 TIM1=0xFFFF;//TIM1=FFFF PRD1=0xFFFF;//PRD1=FFFF TCR1=0x002F;//TDDR=1111B, AUTO LOAD THE PSC, START THE TIMER1 IMR=0x0080; IFR=0x0080; asm(" RSBX INTM");//START THE TIMER1 return; } TINT1_ISR() { asm(" RSBX XF"); asm(" SSBX INTM"); TCR1=0x0010;//STOP THE TIMER1 TCR=0x0010;//STOP THE TIMER0 TIM=0xFFFF;//TIM0=FFFF PRD=0xFFFF;//PRD0=FFFF TCR=0x002F;//TDDR=1111B, AUTO LOAD THE PSC, START THE TIMER0 IMR=0x0008; IFR=0x0008; asm(" RSBX INTM");//START THE TIMER0 return; } 初始化程序: #include "jicunqi.h" void system_init() { //初始化MR,IFR,PMST,SWWSR等寄存器
IFR=0xFFFF; //clear the interrupt
IMR=0x0107; //1 stand for open the interrupt PMST=0x3428; //OVLY=1,DROM=0 SWWSR=0x7E00; } 定时器初始化程序: #include "jicunqi.h" void timer() { TCR=0x0010;//STOP THE TIMER0 TIM=0xFFFF;//TIM=FFFF PRD=0xFFFF;//PRD=FFFF TCR=0x002F;//TDDR=1111B, AUTO LOAD THE PSC, START THE TIMER0 IMR=0x0008; IFR=0x0008; asm(" RSBX INTM"); } |