📄 main.c
字号:
#include <hidef.h> /* common defines and macros */
#include <mc9s12dg128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
long int speed;
int i=0;
/*********************************************************/
/*********************************************************/
void surpass_fre(void) /*PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)*/
{ /*PLLCLK=2*8*(7+1)/(3+1)=32M*/
REFDV=3; /*超频为原先四倍*/
SYNR=7;
while(CRGFLG_LOCK==0);//等待直到稳定
CLKSEL=0x80;
}
/*********************************************************/
/*********************************************************/
void ini_timer(void) {
TIOS=0x00; /*输入捕捉模式*/
TSCR1=0x80; /*enable timer */
TSCR2=0x00; /*禁止定时器溢出中断,预分频系数1*/
TCTL4=0x30; /*场中断为PT0,行中断为PT1,PT2为测速模块入口,行捕捉上升沿,场上升沿,PT2测速上升下降沿*/
TFLG1=0x04;
DLYCT=0x00; /*延时0个总线时钟周期*/
TIE=0x00; /*行场中断禁止*/
INTCR &=0xBF ; /*Disable IRQ*/
}
/*********************************************************/
/*********************************************************/
void pwm_initial()//pwm初始化函数
{
PWME=0x00;
PWMPOL=0xFF;//通道01,23输出波形开始极性为1
PWMCTL=0x30;//通道01,23级联
PWMPRCLK=0x22;//AandB is Bus/4=32/4=8Mhz
PWMCLK=0x02;//通道01选择SA为时钟源
PWMSCLA=0X02;//通道01时钟SA为2MHz(8/(2*2))
PWMPER01=40000;//设定通道01输出频率(50Hz 20ms) 1/2M*40000
PWMPER23=7999;//设定通道23输出频率(1KHz 1ms ) 1/8M*8000
PWMCAE=0x00;
DDRA=0xFF;
PORTA=0x00;
}
/**********************************************************/
/**********************************************************/
void delay_4ms(int cnt) {
int i,j;
for(i=0;i<cnt;i++) {
j=0X1300;
while(j--);
}
}
void shortdelay(int cnt) {
int i,j;
for(i=0;i<cnt;i++) {
j=0X400;
while(j--);
}
}
/**********************************************************/
/**********************************************************/
void main(){
int j;
surpass_fre();
pwm_initial();
ini_timer();
PWMDTY23=500;
PWME=0x02;
speed=0;
DDRB=0xFF;
PORTB=0x55;
delay_4ms(1000);
TIE=0x04;
asm cli;
while(1){
if(TFLG2==0x80)
j++;
if(i==2) {
PWME=0x00;
speed=speed+65536*j;
}
}
}
/*SPEED detect Interrupt*/
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 10 timer_2(void) {
speed=TC2-speed;
i++;
if(i==2) {
TIE=0x00;
PORTB=0xAA;
}
TFLG1=0x04;
}
#pragma CODE_SEG DEFAULT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -