中级会员
- 积分
- 243
- 威望
- 201
- 贡献
- 40
- 兑换币
- 0
- 注册时间
- 2010-9-1
- 在线时间
- 1 小时
|
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include "math.h"
#include"includes.h"
#include"SCIDrivers.h"
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
word AD_value[6]={ };
/***************毫秒延时函数2/3ms*********************************/
void Dly_ms(int ms){
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<2770;jj++); //16MHz--1ms
}
/**********IO初始化**********************************/
void IO_init(void){
DDRA = 0x00; // A口输入
DDRB = 0xFF; // B口输出
DDRE = 0x00; //E口输入
DDRM = 0x00; // M口输入
ATD0CTL1 = 0x00; //8位精度,选择AD为外部触发
ATD0CTL2 = 0x40; //使能A/D,自动清零,禁止中断
ATD0CTL3 = 0xD4; //右对齐 ,转换序列长度为6:AN0~AN5,转换队列映射到到结果寄存器open FIFO
ATD0CTL4 = 0x01; //4个AD采样周期?RS=1,6MHZ
ATD0CTL5 = 0x30; //连续转换scan=1,多通道转换MULT=1,起始通道0,AN0~AN5
ATD0DIEN = 0x00; //disable AD as input输入允许寄存器关闭缓冲区
}
/***********总线频率64M*************************************/
void SetBusCLK_24M(void){//锁相环倍频
CLKSEL = 0X00; //CRG始终选择寄存器的选择disengage PLL to system
PLLCTL_PLLON = 1; //打开PLL turn on PLL
SYNR = 2; //对PLLCLK增频的因子
REFDV = 1; //对PLLCLK分频的因子
POSTDIV = 0x00; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=48MHz;osc =16M
_asm(nop); //BUS CLOCK=24M
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL = 1; //engage PLL to system;
}
/**************************PWM初始化**************************************/
void PWMInit(void){
PWME = 0x00; //关PWM
PWMCTL = 0x70; //01级联23级联45级联 01舵机
PWMCAE = 0x00; //左对齐
PWMPOL = 0xFF; //先高后低
PWMCLK = 0xFF; //SA SB
PWMPRCLK = 0x22; //A4分频 A=6M B4分频 B=6M
PWMSCLA = 1; //SA=3M
PWMSCLB = 1; //SB=3M
PWMPER01 = 10000; //300Hz
PWMDTY01 = 4700; //居中4700 6000left 2900right
PWMPER45 = 150;
PWMPER23 = 150; //20k
PWMDTY23 = 0;
PWMDTY45 = 0;
PWME = 0xFF;
}
/************AD convert value*************/
void AD_Getvalue(word *AD_value){
while(!ATD0STAT0_SCF);
asm ("nop");
AD_value[0]=ATD0DR0;
AD_value[1]=ATD0DR1;
AD_value[2]=ATD0DR2;
AD_value[3]=ATD0DR3;
AD_value[4]=ATD0DR4;
AD_value[5]=ATD0DR5;
AD_value[6]=ATD0DR6;
}
/************主函数***********************/
void main(void)
{
/* put your own code here */
int tmp;
IO_init(); //IO初始化
SetBusCLK_24M(); //倍频
SCIInit();
PWMInit(); //
PWME_PWME1 = 1; //
Dly_ms(6000); //
Dly_ms(3000); //
PWME_PWME5 = 1; //
PWME_PWME3 = 1; //
EnableInterrupts;
while(1)
{
SCISend1(0xFF) ;
SCISendN(12,AD_value) ;
SCISend1(0xFF) ;
}
} |
|