常驻嘉宾
- 积分
- 5459
- 威望
- 969
- 贡献
- 3928
- 兑换币
- 4
- 注册时间
- 2011-7-25
- 在线时间
- 281 小时
|
同时使用PIT模块的0,1定时通道,实现两盏LED灯实现2比1频率的闪动。
程序代码如下:
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define unsigned int uint
#define BUS_CLOCK 24000000
uint i=0;
uint j=0;
byte shuma[20]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, //0~9对应的段码
0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
void INIT_PLL(void)
{
CLKSEL &= 0x7f; //set OSCCLK as sysclk
PLLCTL &= 0x8F; //Disable PLL circuit
CRGINT &= 0xDF;
#if(BUS_CLOCK == 40000000)
SYNR = 0x44;
#elif(BUS_CLOCK == 32000000)
SYNR = 0x43;
#elif(BUS_CLOCK == 24000000)
SYNR = 0x42;
#endif
REFDV = 0x81; //PLLCLK=2×OSCCLK×(SYNR+1)/(REFDV+1)=64MHz ,fbus=32M
PLLCTL =PLLCTL|0x70; //Enable PLL circuit
asm NOP;
asm NOP;
while(!(CRGFLG&0x08)); //PLLCLK is Locked already
CLKSEL |= 0x80; //set PLLCLK as sysclk
}
void PitInit(void)
{
PITCFLMT_PITE = 1; //使能PIT模块
PITCE = 0X00; //通道0,1 PIT模块禁止
PITINTE = 0x03; //通道0,1中断使能
PITMUX = 0x00; //16位定时通道0使用0号8位微计数器,16位定时器1使用1号微计数器
PITLD0 = 240-1;
PITMTLD0 = 100-1;
PITLD1 = 120-1;
PITCE = 0X03; //通道0,1 PIT模块使能
}
void InitPortb(void){
DDRB = 0XFF;
PORTB = 0XFF;
}
void main(void)
{
INIT_PLL(); //设置锁相环
PitInit();
InitPortb();
EnableInterrupts;
for(;;) {
} /* loop forever */
/* please make sure that you never leave main */
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt 66 void PIT0(void)
{
// PITTF_PTF0 = 1;
//用这PITTF_PTF0 = 1清除不了标志位,翻了下数据手册看到
//If flag clearing by writing a one and flag setting happen in the same bus clock cycle,
//the flag remains set.The flag bits are cleared if the PIT module is disabled
// or if thecorresponding timer channel is disabled.
PITCE_PCE0=0; //通道禁止
PITCE_PCE0=1;
i++;
if(i==1000)
{
i = 0;
PORTB_PB0 = ~PORTB_PB0;
}
}
interrupt 67 void PIT1(void)
{
//PITTF_PTF1 = 1;
PITCE_PCE1=0;
PITCE_PCE1=1;
j++;
if(j==1000)
{
j = 0;
PORTB_PB1 = ~PORTB_PB1;
}
} |
|