注册会员
- 积分
- 138
- 威望
- 79
- 贡献
- 45
- 兑换币
- 33
- 注册时间
- 2013-11-27
- 在线时间
- 7 小时
|
本人新手,写一个程序,通过调节拨码开关来控制中断频率,数码管从9999逐一减到0,拨码开关控制自减速度,通过控制中断频率实现。下面是我的代码,请问读取拨码开关值应该在哪里?#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
void main(void) {
/* put your own code here */
CLKSEL=0X00; //设置锁相环之前需要禁止锁相环,总线频率=外部晶振/2
PLLCTL_PLLON=1; //打开锁相环
SYNR=0x49;
REFDV=0x43; //Fvco=2*osc*(1+SYNDIV)/(1+REFDIV)
POSTDIV = 0x00; //Fpll=Fvco/(2*POSTDIV)-----POSTDIV!=0 Fpll=Fvco
//pllclock=fvco=2*osc*(1+SYNR)/(1+REFDV)=80MHz;
_asm(nop); //BUS CLOCK=40M
_asm(nop);
while(!(CRGFLG_LOCK==1)); // 等待锁相环初始化完成
CLKSEL_PLLSEL =1; // 使用锁相环
//读拨码开关和数码管
DDRT &= 0xF0; //设置T口低4位为输入口,作为拨码开关的输入口
DDRB = 0xFF; //设置B口为输出口,作为数码管的数据信号
DDRS |= 0x0F; //设置s口的低4位为输出口,作为数码管的控制信号
CRGINT_RTIE = 1; //开实时中断
unsigned int a0=9;
unsigned int a1=9;
unsigned int a2=9;
unsigned int a3=9;
EnableInterrupts;
for(;;) {
unsigned char LedCode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
//数组中的第i个元素代表显示数字i时数码管数据口的数据
unsigned char ch ;
ch = PTT&0x0F; //读取T口拨码开关的值
switch(ch) {
case '0x00':RTICTL = 0x0F;break; //6.10Hz
case '0x01':RTICTL = 0x1F;break;
case '0x02':RTICTL = 0x2F;break;
case '0x03':RTICTL = 0x3F;break;
case '0x04':RTICTL = 0x4F;break;
case '0x05':RTICTL = 0x5F;break;
case '0x06':RTICTL = 0x6F;break;
case '0x07':RTICTL = 0x7F;break; //1.526Hz,中断频率逐渐减小
case '0x08':RTICTL = 0x8F;break; //中断频率1000Hz
case '0x09':RTICTL = 0x9F;break;
case '0x0A':RTICTL = 0xAF;break;
case '0x0B':RTICTL = 0xBF;break;
case '0x0C':RTICTL = 0xCF;break;
case '0x0D':RTICTL = 0xDF;break;
case '0x0E':RTICTL = 0xEF;break;
case '0x0F':RTICTL = 0xFF;break; //5Hz,中断频率逐渐减小
default:return 1;break;
}
//控制数码管输出
PTS |= 0x0F;
PTS &= ~0x08;
PORTB = LedCode[a3];
count=50;
while(count--);
PTS |= 0x0F;
PTS &= ~0x04;
PORTB = LedCode[a2];
count=50;
while(count--);
PTS |= 0x0F;
PTS &= ~0x02;
PORTB = LedCode[a1];
count=50;
while(count--);
PTS |= 0x0F;
PTS &= ~0x01;
PORTB = LedCode[a0];
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 7 RTI_int(void) {
DisableInterrupts;
//控制自减
if (a0!=0){
a0--;
else if (a1!=0){
a1--;a0=9;
else if (a2!=0){
a2--;a1=9;a0=9;
else if (a3!=0){
a3--;a2=9;a1=9;a0=9;
else
a3=0;a2=0;a1=0;a0=0;
}
}
}
}
CRGFLG = 0x80; //清楚中断标志位
EnableInterrupts;
}
#pragma CODE_SEG DEFAULT
|
|