📄 xs128_main.c
字号:
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative information */
#include <stdlib.h>
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#define uint unsigned int
#define TIME_INTER 500 //100个10Ms=1s
unsigned int m,Get_pulse;
void Delay(uint Time) //延时MS
{
uint i,j;
for(i=0;i<Time;i++)
for(j=0;j<0x7ff;j++); //0x7ff;;;busclk:24MHz--1ms
}
//-------------------------------------------------
void Pwmduoji(void)
{ static k=0;
if(k)
PWMDTY23-=100;
else
PWMDTY23+=100;
if(PWMDTY23>=12500) {
PORTB=0X00;k=1;Delay(1000);
}
if(PWMDTY23<=5500) {
PORTB=0X00;k=0;Delay(1000);
}
}
//-------------------------------------------------
void TimerOverflow(void)
{
unsigned char i=1,j=0x80;
while((i!=0)&&(j!=0))
{
PORTB=~(i|j);
i<<=1;
j>>=1;
Delay(1000);
Pwmduoji();
}
}
// uint i=PWMDTY2;
// uint j;
// j=i%100;
// switch(j) {
// case 0: {PORTB=0X1F;Delay(2000);break;}
// case 1: PORTB=0X0FE; break;
// case 2: PORTB=0X0FD; break;
// case 3: PORTB=0X0FB; break;
// case 4: PORTB=0X0F7; break;
// case 5: PORTB=0X0EF; break;
// case 6: PORTB=0X0DF; break;
// case 7: PORTB=0X0BF; break;
// case 8: PORTB=0X7F; break;
// case 9: PORTB=0X3F; break;
// }
//-----------------------------------------------------
void setbusclock(void)
{
CLKSEL=0X00; //disengage PLL to system
PLLCTL_PLLON=1; //turn on PLL
SYNR=2;
REFDV=1; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=48MHz;
_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;
}
//-----------------------------------------------------
void Timerch0Init(void)
{
TSCR2_PR =0x04; //prescale factor is 16, bus clock/16=24Mhz/16=1500000
TIOS =0x01; //0 channel output compare
TC0 =0x3A98;//channel 0 output compare holding register 0x3A98*(1/1500000)=10ms
TCTL2 =0x02; //output low level
TSCR1_TFFCA=1; // if set, TFLG1 can be cleared auto, manual clear is useless
TIE=0x81; //Timer0 Interrupt enable
TSCR1_TEN = 1; //Timer enable
//TCTL3 = 0X80; //IOC7 下降沿捕捉脉冲 edg7a deg7b
PACTL = 0x40; // 使能 0事件 0下降沿 16位累加器IOC7
PACNT = 0X0000;
}
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
void Pwminitial(void)
{
PWME=0X00;
PWMPOL=0X0B; //通道0,1,3输出波形开始极性为1 output waveform which high first then low when the duty counter is reached
PWMCAE=0x00; //左对齐输出模式 output left align waveform
PWMCLK=0X0B; //PWM时钟源为SA,SB SAClock SA is the clock source for PWM channel 0
PWMPRCLK=0X14; //时钟A 16分频 Clock A is 24MHz/16=1.5MHz
//时钟B 2分频 Clock B is 12MHz
PWMSCLA=250; //时钟SA为1.5MHz/250/2 Clock SA is 1.5MHz/250/2=3KHz
PWMSCLB=1; //时钟SB为12MHz/1/2 Clock SB is 12MHz/1/2=6MHz
PWMCTL=0X20;
PWMPER0=3; //设定输出周期=通道时钟周期3KHz/3=1khz
PWMPER1=30; //设定输出周期=通道时钟周期3KHz/30=100hz
PWMPER23=60000; //设定输出周期=通道时钟周期6MHz/60000=100Hz
PWMDTY0=1; //占空比初始为0 Duty is 50%,and PWM waveform's frequent is 16
PWMDTY1=16; //占空比初始为0 Duty is 50%,and PWM waveform's frequent is 16
PWMDTY23=9000; //占空比初始为0 Duty is 50%,and PWM waveform's frequent is 16
PWME=0X0B; //通道0,1,3使能 enable pwm channel 0
}
void ADCInit()
{
ATD0CTL1=0x0F; //8位精度
ATD0CTL2=0x40; //快速清零//AD模块上电, 快速清零, 无等待模式, 禁止外部触发, 中断禁止
ATD0CTL3=0xA0; //右对齐 NO FIFO,FREEZE继续// 每个序列4次转换, No FIFO, Freeze模式下继续转换
ATD0CTL4=0x07; //4周期 // 8位精度, 2个时钟, ATDClock=[BusClock*0.5]/[PRS+1] ; PRS=7, divider=8
ATD0CTL5=0x20; //连续,单通道//右对齐无符号,单通道采样,通道0
ATD0DIEN=0x00; // 禁止数字输入
}
void main(void)
{
DisableInterrupts;
setbusclock();
Timerch0Init();
Pwminitial();
ADCInit();
DDRB=0XFF;
PORTB=0X0F0;
//---------------------------------------------------
for(;;) {
EnableInterrupts;
//while(!ATD0STAT2_CCF0); //等待转换结束
//PORTB = (byte)ATD0DR0; //在B口显示转换值
//TimerOverflow();
} /* wait forever */
/* please make sure that you never leave this function */
}
//----------------------interrupt------------------------------
#pragma CODE_SEG NON_BANKED
//#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt 8 void timerch0(void) //10ms
{
static unsigned int number_count=0;
DisableInterrupts;
m=TCNT;
TC0=m+0x3A98;
PORTB ^=0xff ;
number_count ++;
if( number_count==TIME_INTER) //1秒取一次
{
number_count=0;
Get_pulse = PACNT;
PACNT = 0X0000;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -