我想每毫秒调用一次 pollkey 函数并每秒增加一次时间变量(timeloc)。我认为如果我call pollkey
在延迟子例程中添加一个它应该可以工作,为什么它不工作?
.equ delaycount, 16911 #set right delay value here!
.text # Instructions follow
.global delay # Makes "main" globally known
delay: beq r4,r0,fin # exit outer loop
movi r8,delaycount # delay estimation for 1ms
inner: beq r8,r0,outer # exit from inner loop
subi r8,r8,1 # decrement inner counter
br inner
outer: subi r4,r4,1 # decrement outer counter
call pollkey
br delay
fin: ret
上面我用C运行的是
#include <stdio.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
extern void puttime(int* timeloc);
extern void puthex(int time);
extern void tick(int* timeloc);
extern void delay(int millisec);
extern int hexasc(int invalue);
#define TRUE 1
#define KEYS4 ( (unsigned int *) 0x840 )
int timeloc = 0x5957; /* startvalue given in hexadecimal/BCD-code */
int RUN = 1;
void pollkey() {
int action = IORD_ALTERA_AVALON_PIO_DATA(DE2_PIO_KEYS4_BASE);
if (action == 7) {
timeloc = 0x0;
} else if (action == 13) {
RUN = 0;
} else if (action == 14) {
RUN = 1;
} else if (action == 11) {
tick(&timeloc);
}
}
int main() {
while (TRUE) {
puttime(&timeloc);
delay(1);
IOWR_ALTERA_AVALON_PIO_DATA(DE2_PIO_REDLED18_BASE, timeloc);
if (RUN == 1) {
tick(&timeloc);
puthex(timeloc);
}
}
return 0;
}
int hex7seg(int digit) {
int trantab[] = { 0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00,
0x10, 0x08, 0x03, 0x46, 0x21, 0x06, 0x0e };
register int tmp = digit & 0xf;
return (trantab[tmp]);
}
void puthex(int inval) {
unsigned int hexresult;
hexresult = hex7seg(inval);
hexresult = hexresult | (hex7seg(inval >> 4) << 7);
hexresult = hexresult | (hex7seg(inval >> 8) << 14);
hexresult = hexresult | (hex7seg(inval >> 12) << 21);
IOWR_ALTERA_AVALON_PIO_DATA(DE2_PIO_HEX_LOW28_BASE, hexresult);
}
int hex7seg2(int digit) {
int trantab[] = { 0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00,
0x10, 0x08, 0x03, 0x46, 0x21, 0x06, 0x0e };
register int tmp = digit & 0xf0;
return (trantab[tmp]);
}
如果它每秒轮询一次,但我想每毫秒轮询一次,我认为我能做到的唯一方法是从 dely 子例程调用 pollkey,但如果我这样做,它就像什么也没发生。你能帮助我吗?我之前问过怎么做,只有在我认为答案应该是更改程序集时才在 C 中得到答案。
更新
br
使用而不是我得到了更好的结果,call
所以我必须检查差异。这就是我正在使用的效果更好的方法:
.equ delaycount, 16911 #set right delay value here!
.text # Instructions follow
.global delay # Makes "delay" globally known
delay: beq r4,r0,fin # exit outer loop
movi r8,delaycount # delay estimation for 1ms
inner: beq r8,r0,outer # exit from inner loop
subi r8,r8,1 # decrement inner counter
br inner
outer: subi r4,r4,1 # decrement outer counter
br pollkey
br delay
fin: ret
现在唯一的问题是时钟走得太快了。
更新
我想我通过引入一个计数器来解决它,该计数器会在几秒钟内打破模 1000:
#include <stdio.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
extern void puttime(int* timeloc);
extern void puthex(int time);
extern void tick(int* timeloc);
extern void delay(int millisec);
extern int hexasc(int invalue);
#define TRUE 1
#define KEYS4 ( (unsigned int *) 0x840 )
int timeloc = 0x5957; /* startvalue given in hexadecimal/BCD-code */
int RUN = 0;
void pollkey() {
int action = IORD_ALTERA_AVALON_PIO_DATA(DE2_PIO_KEYS4_BASE);
if (action == 7) {
timeloc = 0x0;
puttime(&timeloc);
puthex(timeloc);
} else if (action == 13) {
RUN = 0;
} else if (action == 14) {
RUN = 1;
} else if (action == 11) {
tick(&timeloc);
puttime(&timeloc);
puthex(timeloc);
delay(1000);
}
}
int main() {
int counter = 0;
while (TRUE) {
pollkey();
delay(1);
++counter;
if (counter % 1000 == 0) {
IOWR_ALTERA_AVALON_PIO_DATA(DE2_PIO_REDLED18_BASE, timeloc);
if (RUN == 1) {
tick(&timeloc);
puttime(&timeloc);
puthex(timeloc);
}
}
}
return 0;
}
int hex7seg(int digit) {
int trantab[] = { 0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00,
0x10, 0x08, 0x03, 0x46, 0x21, 0x06, 0x0e };
register int tmp = digit & 0xf;
return (trantab[tmp]);
}
void puthex(int inval) {
unsigned int hexresult;
hexresult = hex7seg(inval);
hexresult = hexresult | (hex7seg(inval >> 4) << 7);
hexresult = hexresult | (hex7seg(inval >> 8) << 14);
hexresult = hexresult | (hex7seg(inval >> 12) << 21);
IOWR_ALTERA_AVALON_PIO_DATA(DE2_PIO_HEX_LOW28_BASE, hexresult);
}
int hex7seg2(int digit) {
int trantab[] = { 0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00,
0x10, 0x08, 0x03, 0x46, 0x21, 0x06, 0x0e };
register int tmp = digit & 0xf0;
return (trantab[tmp]);
}
.equ delaycount, 16911 #set right delay value here!
.text # Instructions follow
.global delay # Makes "delay" globally known
delay: beq r4,r0,fin # exit outer loop
movi r8,delaycount # delay estimation for 1ms
inner: beq r8,r0,outer # exit from inner loop
subi r8,r8,1 # decrement inner counter
br inner
outer: subi r4,r4,1 # decrement outer counter
br delay
fin: ret