我想要实现4位数码管从9999倒计时到0,每秒减1.
功能已经实现,现在想把中断里面数字递减的部分内容移到主函数里面,该怎么弄啊,我试了几次,数码管都乱码了。
程序如下:
#include <hidef.h>
#include "deriva
tive.h"
#define CONT1 PORTK_PK3
#define CONT2 PORTK_PK2
#define CONT3 PORTK_PK1
#define CONT4 PORTK_PK0
#define CONT1_dir DDRK_DDRK3
#define CONT2_dir DDRK_DDRK2
#define CONT3_dir DDRK_DDRK1
#define CONT4_dir DDRK_DDRK0
#define DATA PTP
#define DATA_dir DDRP
#define BUS_CLOCK 32000000
#define OSC_CLOCK 16000000
byte data1=9;
byte data2=9;
byte data3=9;
byte data4=9;
unsigned long int temp=9999;
unsigned char y=0;
byte single=1;
byte shuma[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void init_pll(void)//锁相环初始化
{
CLKSEL&=0x7f;
PLLCTL&=0x8F;
CRGINT&=0xDF;
SYNR=0x43;
REFDV=0x81;
PLLCTL=PLLCTL|0x70;
asm NOP;
asm NOP;
while(!(CRGFLG&0x08));
CLKSEL|= 0x80;
}
void init_port(void)//接口初始化
{
CONT1_dir=1;
CONT2_dir=1;
CONT3_dir=1;
CONT4_dir=1;
CONT1=0;
CONT2=0;
CONT3=0;
CONT4=0;
DATA_dir=0xff;
DATA=0x00;
}
void delay(void) //延时
{
unsigned int i,j;
for(j=0;j<200;j++)
for(i=0;i<60000;i++);
}
void init_tim(void)//定时器初始化
{
TSCR1_TFFCA = 1;
TSCR1_TEN = 1;
TIOS = 0xff;
TCTL1 = 0x00;
TCTL2 = 0x00;
TIE = 0x01;
TSCR2 = 0x07;
TFLG1 = 0xff;
TFLG2 = 0xff;
}
void init_pit(void)//PIT初始化
{
PITCFLMT=0X80;
PITCE_PCE0=1;
PITINTE_PINTE0=1;
PITMTLD0=249;
PITLD0=12799;
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt void scan(void) //扫描中断
{
if(TFLG1_C0F==1)
{
TFLG1_C0F=1;
TC0=TCNT+1250;
}
switch(single)
{
case 1:
CONT1 = 1;
CONT2 = 0;
CONT3 = 0;
CONT4 = 0;
DATA=shuma[data1];
break;
case 2:
CONT1 = 0;
CONT2 = 1;
CONT3 = 0;
CONT4 = 0;
DATA=shuma[data2];
break;
case 3:
CONT1 = 0;
CONT2 = 0;
CONT3 = 1;
CONT4 = 0;
DATA=shuma[data3];
break;
case 4:
CONT1 = 0;
CONT2 = 0;
CONT3 = 0;
CONT4 = 1;
DATA=shuma[data4];
break;
default:
break;
}
single+=1;
if(single==5) single=1;
}
interrupt void PIT_INTER(void)//数字递减
{
if(PITTF_PTF0==1)
PITTF_PTF0=1;
y++;
if(y>10){
temp=temp-1;
y=0;
data4=temp%10;
data3=temp/10%10;
data2=temp/100%10;
data1=temp/1000%10;
if(temp==0)
temp=9999;
}
}
#pragma CODE_SEG DEFAULT
void main(void)//主函数
{
DisableInterrupts;
init_pll();
init_tim();
init_port();
init_pit();
EnableInterrupts;
TFLG1_C0F = 1;
TC0 = TCNT + 1250;
for(;;) {
}
}
一周热门 更多>