我是新手,单片机刚刚入门老师就让我们用KL25Z128来写秒表
要求是:用六位计数管来显示时间,然后用按键来控制,刚开始计数管全亮,按一次开始计时,再按一次暂停。
我写的程序第一次按下之后不能开始计时,而是在某一位计数管上显示一个‘0’,有人能帮忙分析一下吗?
#include "derivative.h" /* include peripheral declarations */
#define LED0 0x0106
#define LED1 0x0105
#define LED2 0x0103
#define LED3 0x000E
#define LED4 0x000D
#define LED5 0x000B
#define LED_0 0X48
#define LED_1 0XFC
#define LED_2 0X0E
#define LED_3 0X8C
#define LED_4 0XB8
#define LED_5 0X89
#define LED_6 0X09
#define LED_7 0XEC
#define LED_8 0X08
#define LED_9 0X88
void LED(void)
{ SIM_SCGC5|=0X0C00;
PORTB_PCR0=0X0100;
PORTB_PCR1=0X0100;
PORTB_PCR2=0X0100;
PORTB_PCR3=0X0100;
PORTB_PCR8=0X0100;
PORTC_PCR0=0X0100;
PORTC_PCR1=0X0100;
PORTC_PCR2=0X0100;
PORTC_PCR3=0X0100;
PORTC_PCR4=0X0100;
PORTC_PCR5=0X0100;
PORTC_PCR6=0X0100;
PORTC_PCR7=0X0100;
GPIOB_PDDR|=0X010F;
GPIOC_PDDR|=0X000000FF;
GPIOB_PDOR|=0X010F;
GPIOC_PDOR=0X00;
}
void delay(unsigned int a)
{unsigned short i,j;
for(i=0;i<30;i++)
for(j=0;j<a;j++)
asm("nop");
void system_Init()
{
SIM_SOPT2=SIM_SOPT2_UART0SRC(1);
SIM_SOPT2&=~SIM_SOPT2_PLLFLLSEL_MASK;
SIM_SCGC4|=0X400;
SIM_SCGC5|=0X200;
PORTA_PCR1=0X200;
PORTA_PCR2=0X200;
}
void UART0_Init()
{
UART0_BDH=0x00;
UART0_BDL=0x8F;
UART0_C4=0x0F;
UART0_C2=0x0c;
}
//几个全局变量
unsigned int i;
unsigned int time_hour,time_min,time_sec;
unsigned int time_hour1,time_hour2,time_min1,time_min2,time_sec1,time_sec2;
//主函数
void main(void)
{ system_Init();
UART0_Init();
LED();
SIM_SCGC5|=0X600;
asm("CPSIE i");
NVIC_ISER|=0X40000000;
PORTA_PCR4=0X0A0100;//下降沿触发终端
SYST_RVR=0X033324;
SYST_CVR=~0X00;
SYST_CSR=~0X07;//时钟中断关闭
time_hour=0;
time_min=0;
time_sec=0;
time_hour1=0;
time_min1=0;
time_sec1=0;
time_hour2=0;
time_min2=0;
time_sec2=0;
i=0;
for(;;)
{
GPIOB_PDOR=LED0;
switch((time_sec1))
{ case 0: GPIOC_PDOR=LED_0;break;
case 1: GPIOC_PDOR=LED_1;break;
case 2: GPIOC_PDOR=LED_2;break;
case 3: GPIOC_PDOR=LED_3;break;
case 4: GPIOC_PDOR=LED_4;break;
case 5: GPIOC_PDOR=LED_5;break;
case 6: GPIOC_PDOR=LED_6;break;
case 7: GPIOC_PDOR=LED_7;break;
case 8: GPIOC_PDOR=LED_8;break;
case 9: GPIOC_PDOR=LED_9;break;
default: ;}
delay(20);
GPIOB_PDOR=LED1;
switch((time_sec2))
{ case 0: GPIOC_PDOR=LED_0;break;
case 1: GPIOC_PDOR=LED_1;break;
case 2: GPIOC_PDOR=LED_2;break;
case 3: GPIOC_PDOR=LED_3;break;
case 4: GPIOC_PDOR=LED_4;break;
case 5: GPIOC_PDOR=LED_5;break;
case 6: GPIOC_PDOR=LED_6;break;
case 7: GPIOC_PDOR=LED_7;break;
case 8: GPIOC_PDOR=LED_8;break;
case 9: GPIOC_PDOR=LED_9;break;
default:;}
delay(20);
GPIOB_PDOR=LED2;
switch((time_min1))
{ case 0: GPIOC_PDOR=LED_0;break;
case 1: GPIOC_PDOR=LED_1;break;
case 2: GPIOC_PDOR=LED_2;break;
case 3: GPIOC_PDOR=LED_3;break;
case 4: GPIOC_PDOR=LED_4;break;
case 5: GPIOC_PDOR=LED_5;break;
case 6: GPIOC_PDOR=LED_6;break;
case 7: GPIOC_PDOR=LED_7;break;
case 8: GPIOC_PDOR=LED_8;break;
case 9: GPIOC_PDOR=LED_9;break;
default: ;}
delay(20);
GPIOB_PDOR=LED3;
switch((time_min2))
{ case 0: GPIOC_PDOR=LED_0;break;
case 1: GPIOC_PDOR=LED_1;break;
case 2: GPIOC_PDOR=LED_2;break;
case 3: GPIOC_PDOR=LED_3;break;
case 4: GPIOC_PDOR=LED_4;break;
case 5: GPIOC_PDOR=LED_5;break;
case 6: GPIOC_PDOR=LED_6;break;
case 7: GPIOC_PDOR=LED_7;break;
case 8: GPIOC_PDOR=LED_8;break;
case 9: GPIOC_PDOR=LED_9;break;
default: ;}
delay(20);
GPIOB_PDOR=LED4;
switch((time_hour1))
{ case 0: GPIOC_PDOR=LED_0;break;
case 1: GPIOC_PDOR=LED_1;break;
case 2: GPIOC_PDOR=LED_2;break;
case 3: GPIOC_PDOR=LED_3;break;
case 4: GPIOC_PDOR=LED_4;break;
case 5: GPIOC_PDOR=LED_5;break;
case 6: GPIOC_PDOR=LED_6;break;
case 7: GPIOC_PDOR=LED_7;break;
case 8: GPIOC_PDOR=LED_8;break;
case 9: GPIOC_PDOR=LED_9;break;
default: ;}
delay(20);
GPIOB_PDOR=LED5;
switch((time_hour2))
{ case 0: GPIOC_PDOR=LED_0;break;
case 1: GPIOC_PDOR=LED_1;break;
case 2: GPIOC_PDOR=LED_2;break;
case 3: GPIOC_PDOR=LED_3;break;
case 4: GPIOC_PDOR=LED_4;break;
case 5: GPIOC_PDOR=LED_5;break;
case 6: GPIOC_PDOR=LED_6;break;
case 7: GPIOC_PDOR=LED_7;break;
case 8: GPIOC_PDOR=LED_8;break;
case 9: GPIOC_PDOR=LED_9;break;
default: ;}
delay(20);
}
}
void SysTick_Handler(void)
{ time_sec++;
if(time_sec==100)
{time_min++;time_sec=0;}
if(time_min==60)
{time_hour++;time_min=0;}
if(time_hour==60)
{time_hour=0;time_min=0;time_sec=0;}
time_sec1=time_sec%10;
time_sec2=(time_sec-time_sec1)/10;
time_min1=time_min%10;
time_min2=(time_min-time_min1)/10;
time_hour1=time_hour%10;
time_hour2=(time_hour-time_hour1)/10;
}
void PROTA_IRQHandler(void)
{
//if "up",begin PORTA_PCR4|=0X01000000;}
if((GPIOA_PDIR&0X20)==0)
{ delay(30);
if((GPIOA_PDIR&0X20)==0)
i++;}
switch(i%2)
{case 0:SYST_CVR=~0X00;SYST_CSR=~0X07;break;
case 1:SYST_CVR=0X00;SYST_CSR=0X07;break;//如果i是奇数,那么打开时钟中断计数
}
}
希望有人能帮忙修改一下,谢谢~
|