常驻嘉宾
- 积分
- 4900
- 威望
- 346
- 贡献
- 4356
- 兑换币
- 0
- 注册时间
- 2010-11-30
- 在线时间
- 99 小时
|
#include <hidef.h> /* common defines and macros */
#include "derivative.h"/* derivative-specific definitions */
#define key1 PORTE_PE2
#define key2 PORTE_PE3
#define key3 PORTE_PE4
float x;
uint sintab[128]={
128,134,140,147,153,159,165,171,177,182,188,193,199,204,209,213,
218,222,226,230,234,237,240,243,245,248,250,251,253,254,254,255,
255,255,254,254,253,251,250,248,245,243,240,237,234,230,226,222,
218,213,209,204,199,193,188,182,177,171,165,159,153,147,140,134,
128,122,116,109,103,97,91,85,79,74,68,63,57,52,47,43,
38,34,30,26,22,19,16,13,11,8,6,5,3,2,2,1,
1,1,2,2,3,5,6,8,11,13,16,19,22,26,30,34,
38,43,47,52,57,63,68,74,79,85,91,97,103,109,116,122} ;
//*****延时函数*******//
void delay(int ms)
{
int i;
while(ms--)
{
for(i=0;i<120;i++);
}
} //*****PIT初始化******//
void PIT_init(void)
{
PITCFLMT_PITE=0; //定时中断通道0关
PITCE_PCE0=1; //定时器通道0使能
PITINTE_PINTE0=1; //定时器中断通道0中断使能
PITMTLD0=199; //8位微定时器初值设定
PITLD0=6000-1; //16位定时器初值设定
PITCFLMT_PITE=1; //定时器通道0使能
}
//*********时钟初始化********//
void PLL_INIT()
{
REFDV=2;
SYNR=1;
while(!(CRGFLG&0x08));
CLKSEL=0x80;
}
//********PWM模块*********//
void PWM_INIT( )
{
PWME_PWME0=0x00; //PWM 禁止
PWMPER0=0x03; //时钟预分频寄存器设置
PWMSCLA=15; //时钟设置
PWMSCLB=15; //时钟设置
PWMCLK_PCLK0=1; // 时钟源的选择
PWMPOL_PPOL0=1; //极性设置
PWMCAE_CAE0=0; //对齐方式设置
PWMCTL=0x00; //控制寄存器设置
PWMPER0=255; //对齐方式设置
PWMDTY0=0; //对齐方式设置
PWME_PWME0=1; //PWM使能
}
//*********按键函数(防抖)**************//
void key(void)
{
if(key1==0)
{
delay(1);
if(key1==0) x=1;
}
if(key2==0)
{
delay(1);
if(key2==0 ) x=2;
}
if(key3==0)
{
delay(1);
if(key3==0 ) x=3;
}
}
//******主函数********//
void main()
{
PLL_INIT();
PIT_init();
PWM_INIT();
DDRE=0x00;
EnableInterrupts;
for(;;)
{
key();
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
#pragma CODE_SEG NON_BANKED
void interrupt 66 seg( )
{ static int i=0,j=255,a=255,b=0;
PITTF_PTF0=1;
if(x==1) {//*********三角波**********//
if(i<255)
{ i++;
PWMDTY0=i;
}
if(i==255)
{
j--;
PWMDTY0=j;
if(j==0)
{
i=0 ;
j=255;
}
}
}
if(x==2) { //*********锯齿波**************//
a--;
PWMDTY0=a;
if(a==0)
a=255;
}
if(x==3) {//*********正弦波 ********//
b++;
PWMDTY0=sintab[b];
if(b>128)
b=0;
}
} |
|