本帖最后由 chinacn1989 于 2011-6-16 16:11 编辑
#include"nuc1xx.h"
#define w GPIOA->DOUT
#define da GPIOB->DOUT
int i;
void TMR0_IRQHandler(void)
{
TIMER0->TISR.TIF = 1;
i--;
}
void delay(int i) //延时函数
{
int j,k;
for(j=0;j<i;j++)
for(k=0;k<255;k++);
}
void display(float x) //显示函数
{
unsigned char num[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //定义共阴极数码管字形编码0-9
unsigned char num1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; //定义共阴极数码管字型编码0.-9.
int a,b,c,d,e,f,g,i;long m;
*((volatile unsigned int *)((uint32_t)&GPIOB->;PMD)) &=0Xff5555ff; //定义PB0-PB7为输出模式
GPIOA->;PMD.PMD8=1; //PB8-11为输出模式
GPIOA->;PMD.PMD9=1;
GPIOA->;PMD.PMD10=1;
GPIOA->;PMD.PMD11=1;
GPIOB->SCH|=0X00000000; // 使能数字输入
SYS->GPAMFP.I2C0_SDA=0; //配置GPA8-11为IO口
SYS->GPAMFP.I2C0_SCL=0;
SYS->GPAMFP.I2C1_SDA=0;
SYS->GPAMFP.I2C1_SCL=0;
*((volatile unsigned int *)((uint32_t)&SYS->GPBMFP))= (*((volatile unsigned int *)((uint32_t)&SYS->GPBMFP))) & 0X00; //GPB为IO口
m=1000*x;
m=(long)m; //将要显示的数据分离成单独的位
a=m/1000000;
b=(m-a*1000000)/100000;
c=(m-a*1000000-b*100000)/10000;
d=(m-a*1000000-b*100000-c*10000)/1000;
e=(m-a*1000000-b*100000-c*10000-d*1000)/100;
f=(m-a*1000000-b*100000-c*10000-d*1000-e*100)/10;
g=m%10;
if(x<10)
for(i=0;i<5;i++)
{
w&=0xf7ff; da|=num[g]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0Xfbff; da|=num[f]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0xfdff; da|=num[e]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0xfeff; da|=num1[d]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
}
else if(x>=10 && x<100)
for(i=0;i<5;i++)
{
w&=0Xf7ff; da|=num[f]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0xfbff; da|=num[e]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0xfdff; da|=num1[d]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0xfeff; da|=num[c]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
}
else if(x>=100 && x<1000)
for(i=0;i<5;i++)
{
w&=0Xf7ff; da|=num[e]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0xfbff; da|=num1[d]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0xfdff; da|=num[c]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0xfeff; da|=num<<4; delay(70);
w|=0x0f00; da&=0xf00f;
}
else
for(i=0;i<5;i++)
{
w&=0Xf7ff; da|=num1[d]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0xfbff; da|=num[c]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0xfdff; da|=num<<4; delay(70);
w|=0x0f00; da&=0xf00f;
w&=0xfeff; da|=num[a]<<4; delay(70);
w|=0x0f00; da&=0xf00f;
}
}
int main()
{
i=1234;
outpw(&TIMER0->TCSR ,0 ); //关闭 Timer0
UNLOCKREG();
/* Step 1. Enable and Select Timer clock source */
SYSCLK->CLKSEL1.TMR0_S = 4; //设置TIME0时钟频率为22MHz
SYSCLK->APBCLK.TMR0_EN =1; //使能时钟模块
LOCKREG();
/* Step 2. Select Operation mode */
TIMER0->TCSR.MODE=1; //设定定时器工作模式为周期溢出模式 MODE=00(单触发模式)
// MODE=01(周期模式)
// MODE=11(连续计数模式)
/* Step 3. Select Time out period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
TIMER0->TCSR.PRESCALE=0; // 不进行时钟预分频(设定值:0-255 )
TIMER0->TICR =3539 ; // 溢出标志位设定,溢出周期计算
// 溢出周期=(1/22118400)*(0+1)*(tick)
/* Step 4. Enablelock interrupt */
TIMER0->TCSR.IE = 1; //使能time0定时器中断
TIMER0->TISR.TIF = 1; //定时器中断位清零
NVIC_EnableIRQ(TMR0_IRQn); //使能定时器中断
/* Step 5. Enable Timer module */
TIMER0->TCSR.CRST = 1; //计数器重置
TIMER0->TCSR.CEN = 1; //使能定时器
TIMER0->TCSR.TDR_EN=1; //使能time0数据寄存器
while(1)
{
display(i);
}
}
中断子程序不执行
怎么回事儿啊?
附件使我参照的例程!!
AN_1003_TimerV101.rar
(491.21 KB)
|