金牌会员
- 积分
- 1844
- 威望
- 999
- 贡献
- 803
- 兑换币
- 0
- 注册时间
- 2011-12-14
- 在线时间
- 21 小时
|
3#
楼主 |
发表于 2012-2-6 15:48:37
|
只看该作者
天才大空翼 发表于 2012-2-5 17:08
程序没写全吧
没看出来,我粘贴少了 对不起啊
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include "math.h"
#include "stdio.h"
unsigned char i=0;
unsigned int a=4250;
unsigned int b=3250;
unsigned int ad[12];
void delay1(unsigned int a)
{
while(a--)
{
}
}
void delay(unsigned int a)
{
while(a--)
{
delay1(1000);
}
}
void PLL_init(void) //设定总线时钟40MHz
{
SYNR=0X09;
REFDV=0X03;
while(CRGFLG_LOCK==0);
CLKSEL=0x80;
}
void PWM_init(void)
{
PWME=0;
PWMPRCLK=0x23; //A clock 2.5MHz , B clock 5MHz
PWMSCLA=0x01; //SA 1.25MHz=(2.5MHz/(2*1))
PWMSCLB=0x01; //SB 2.5MHz=(5/(2*1))
PWMCLK=0xFD; //选择ClockSA,ClockSB时钟
PWMPOL_PPOL1=1; //周期开始,PWM输出高电平
PWMPOL_PPOL7=1;
PWMPOL_PPOL3=1;
PWMPOL_PPOL5=1;
PWMPER45 = 50000; //产生50hz的时钟
PWMDTY45 = 3750; //中间舵机值2350 2750 3250 3750;4250 4750 5250
PWMCAE=0x00; //对齐方式为输出左对齐
PWMCTL=0xf0; //通道01,23,67级联
PWMPER01=4000; //PWM占空比初始化
PWMDTY01=300;
PWMPER23=4000;
PWMDTY23=1000; //设定初始化的占空比速度(1/4)
PWMPER67=4000;
PWMDTY67=500;
PWME=0xFF; //启动PWM,下一个时钟输出PWM
}
void ChangePWM45(unsigned int a)
{
PWMDTY45=a;
}
void ChangePWM23(unsigned int a)
{
PWMDTY23=a;
}
void ChangePWM67(unsigned int a)
{
PWMDTY67=a;
}
void AD_Init(void)
{
ATD0CTL1=0b00000000;//8位精度
ATD0CTL2=0b01000000;//禁止外部触发,标志位快速清零,中断禁止
ATD0CTL3=0b11100000;//右对齐无符号.转换序列长度为16,No FIFO模式,Freeze模式下继续转换?
ATD0CTL4=0b01101001;//6AD采样周期,ATDClock=[BusClock*0.5]/[PRS+1] ; PRS=1, divider=32
ATD0CTL5=0b01110000;//特殊通道禁止,单通道采样,扫描模式连续采样,ch AN0
ATD0DIEN=0b00000000;//禁止数字输入
}
void Sensor_collect(void)
{
ATD0CTL5=0b00010000;
while(!ATD0STAT0_SCF);
ad[0]= ATD0DR0L;
ad[1]= ATD0DR1L;
ad[2]= ATD0DR2L;
ad[3]= ATD0DR3L;
ad[4]= ATD0DR4L;
ad[5]= ATD0DR5L;
ad[6]= ATD0DR6L;
ad[7]= ATD0DR7L;
ad[8]= ATD0DR8L;
ad[9]= ATD0DR9L;
ad[10]= ATD0DR10L;
ad[11]= ATD0DR11L;
}
void Sensor_judge(void)
{
}
void SCI_Init(void) {
SCI0CR1 =0x00;
SCI0CR2 =0x2c; //enable Receive Full Interrupt,RX enable,Tx enable
SCI0BDH = 0x01;
SCI0BDL = 0x04; //SCI0BDL=busclk/(16*SCI0BDL)
//busclk 40MHz, 9600bps,SCI0BD=0x104
}
void SCI_Transmit(unsigned char d)
{
while(!(SCI0SR1&0x80));
SCI0DRL=d;
}
void sent_AD(void)
{
unsigned char ii;
for(ii=0;ii<=11;ii++)
{
SCI_Transmit(ad[ii]);
}
}
void main(void)
{
/* put your own code here */
PLL_init();
PWM_init();
DisableInterrupts;
EnableInterrupts;
for(;;)
{
delay(10000);
ChangePWM45(3750);
delay(10000);
sent_AD();
ChangePWM45(5250);
}
}
|
|