1、流水灯四种效果:#include /* common defines and macros */#include #include /* derivative information */#pragma LINK_INFO DERIVATIVE “mc9s12xdp512“#include “main_asm.h“ /* interface to the assembly module */unsigned char temp;/unsigned char pa 0x200;/unsigned char pb 0x202;unsigned char key;static void delay(
2、void) volatile unsigned long i;for(i=0;i=1;c-) delay();PORTB = (1=1;c-) delay();PORTB = (3 /* common defines and macros */#include “derivative.h“ /* derivative-specific definitions */#include#includeunsigned int x,y;unsigned long int n,Th,Tl;unsigned char t,a;void TIM_Init()TSCR1=0x00;TSCR2=0x84;TIO
3、S_IOS0=0;TCTL4=0x01;TIE_C0I=0x01;TSCR1|=0x80; /启动定时器#pragma CODE_SEG_NEAR_SEG NONBANKEDvoid interrupt 8 TIM_Tie()y=x;x=TC0;if(TCTL4=0x02)Th=(x-y+(n#include #include “IO_Map.h“/#include “derivative.h“/ global variables definitionsstatic int waittime = 5;static unsigned char redButtonDown = FALSE, blu
4、eButtonDown = FALSE;static long absoluteTime = 0;unsigned int count=0;unsigned char time3=0,0,0; unsigned char cmd_buf8;unsigned char pos=0; static const char segs=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;/static const char /segss=0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef;/#pragma C
5、ODE_SEG DEFAULTstatic void WriteToSCI0(const char *text)while (*text != 0)while (!(SCI0SR1 / wait for output buffer emptySCI0DRL = *text+;void execute_cmd() unsigned char tmp3;if (cmd_buf0=t)tmp1=(cmd_buf3-0)*10+cmd_buf4-0;tmp2=(cmd_buf5-0)*10+cmd_buf6-0; if (tmp0 12MHz 11 / pllclock=2*osc*(1+SYNR)/
6、(1+REFDV)=32MHz;POSTDIV=0x00; / 4:0, fPLL= fVCO/(2xPOSTDIV)/ If POSTDIV = $00 then fPLL is identical to fVCO (divide by one)._asm(nop); / BUS CLOCK=16M_asm(nop);while(!(CRGFLG_LOCK=1); /when pll is steady ,then use it;CLKSEL_PLLSEL =1; /engage PLL to system; void interrupt 26 MDC_ISR(void) /定时器 MDC
7、模块中断服务程序,当定时时间到时自动执行该程序MCFLG|=0X80;/MCFLG_MCZF=1;count+;if (count%10=0)count=0;time2+;if(time2=60)time2=0;time1+;if(time1=60)time1=0;time0+;if(time0=24)time0=0; /*/#pragma CODE_SEG DEFAULTstatic void SCI0Init(void) SCI0BDL = (unsigned char)(16000000UL /* OSC freq */ / 2) / 9600 /* baud rate */ / 16
8、/*factor*/); SCI0CR2 = 0x2C;void MDC_Init() unsigned char i;MCCTL=0b11101111; /MCZI=1,MODMC=1,RDMCL=1, ICLAT=0,FLMC=1,MCEN=0,MCPR1-0=0b11(16 分频) MCCNT=10000; /定时 10000us=10ms/MCCTL_MCEN=1;void main(void) unsigned char tmp;DDRA = 0xFF;DDRB = 0xFF;DDRE = 0xFF;DDRP = 0xFF;DDRM = 0xFF;DDRT = 0xFF;PORTA
9、=0xFF; PORTB =0xFF; PORTE =0xFF; PTP =0xFF; PTM =0xFF; PTT =0xFF; setbusclock();/startTimeBase();SCI0Init();MDC_Init();WriteToSCI0(“nn* Timer Demo *n“);WriteToSCI0(“nEnter t123456 to set time to 12:34:56n“);INTCR_IRQEN=0; /屏蔽 IRQ 中断EnableInterrupts; /使能全局中断for (;) if(time2!=tmp) /if 秒钟数有更新 tmp=time2;PORTA = segstime0/10;PORTB = segstime0%10PORTE = segstime1/10;PTP = segstime1%10PTM = segstime2/10;PTT = segstime2%10;