13.11.2014 Views

ARM Embedded Systems

ARM Embedded Systems

ARM Embedded Systems

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ARM</strong> <strong>Embedded</strong> <strong>Systems</strong><br />

การใช้งาน Timer/Counter บน LPC2148<br />

(2)


Programming the Timer Counter<br />

●<br />

Registers ท่ีเก่ียวข้อง<br />

– TCR : Timer Control Register<br />

●<br />

Reset, enable/disable ตัว timer<br />

– CTCR : Count Control Register<br />

●<br />

กำาหนด mode การใช้งาน timer/counter – capture mode<br />

– TC, PR, PC : Counter และ Prescaler<br />

– MCR, CCR : Match/Capture Control Register<br />

– EMR : External Match Register<br />

●<br />

ควบคุม external match pin<br />

– MR[0..3]/CR[0..3] : Match/Capture Register


Example Application<br />

●<br />

●<br />

●<br />

Free running counter<br />

–<br />

สัง่ ให้ counter เร่ิมทำางานแล้วนับไปเร่ือยๆ<br />

Blink LED โดยการใช้ match output<br />

–<br />

กำาหนด match register เพ่ือให้ counter นับไปครบ 1<br />

วินาที แล้วใช้ match pin สำาหรับ toggle LED<br />

ใช้ Timer สำาหรับการตรวจสอบการ bounce ของ push<br />

button switch<br />

–<br />

–<br />

ใช้ capture mode โดยต่อ switch เข้ากับ capture pin<br />

แล้ว เปรียบเทียบเวลา ในแต่ละครัง้ ท่ี capture ได้<br />

ถ้าเวลาระหว่าง event


Free Running Counter<br />

●<br />

●<br />

กำาหนดให้ Timer start<br />

void timerinit(void) {<br />

T0CTCR = 0; // ใช้ timer mode<br />

T0PR = 0; // ไม่ใช้ prescaler, count ทุกๆ clock<br />

T0TCR = 2; // Reset Timer0<br />

T0TCR = 1; // เร่ิมต้นทำงำน<br />

}<br />

เอาค่า counter ไปใช้งาน<br />

#define CPS 15000 // Cycle Per Second (15 MHz PCLK)<br />

#define FC 16 // Function call overhead<br />

void delay_ms(unsigned long int count) {<br />

unsigned long int done=T0TC + count*CPS ­ FC;<br />

while(T0TC < done);<br />

}


Free Running Counter (cont'd)<br />

// original delay_ms() function<br />

void delay_ms(unsigned long int count) {<br />

int i,j;<br />

}<br />

for (i=0;i


Modified delay_ms() functions<br />

// original delay_ms() function<br />

void delay_ms(unsigned long int count) {<br />

int i,j;<br />

}<br />

for (i=0;i


Blink LED using Timer Match<br />

// Blink LED at port P0.22 ­­ MAT0.0<br />

// PCLK = 15 MHz<br />

void timerInit(void) {<br />

PINSEL1 |= (3


Push Button Switch<br />

Bounce Detection<br />

void timerInit(void) {<br />

PINSEL1 |= (1


Push button switch (cont'd)<br />

// using forever loop<br />

int main(void) {<br />

int count=0;<br />

...<br />

while(1) {<br />

if (count!=CR0) {<br />

count=CR0;<br />

...<br />

}<br />

}<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!