Tài liệu
Arm Cotex M3
Contents 1. Giớ i thiệu sản phẩm .................................. ................................................... .................................. .................................. ............................... .............. 2 1.1. Hiệu năng của kiến trúc RISC 32-Bit ......................... .......................................... .................................. ....................... ...... 2 1.2. Bộ nhớ n nội (Internal Memory) .............................. ................................................ ................................... ............................ ........... 2 1.3. ARM FiRM-compliant FiRM-compliant Watchdog Timer .................................. ................................................... ......................... ........ 3 1.4. Controller Area Network (CAN) ............................................... ............................................................... ......................... ......... 3 1.5. 10/100 Ethernet Controller.................................. ................................................... .................................. ............................... .............. 3 1.6. Synchronous Serial Interface (SSI) ................................ ................................................. ................................... .................... .. 3 1.7. UART ................................... .................................................... .................................. .................................. .................................. ............................ ...........4 1.8. ADC................. ADC................................... ................................... .................................. .................................. .................................. ............................... .............. 4 1.9. Analog Comparators Comparators ................................... ................................................... .................................. ................................... ...................... ..... 4 1.10. I2C ................................... .................................................... .................................. .................................. .................................. ............................... ..............4 1.11. PWM .................................. ................................................... .................................. .................................. .................................. ............................ ........... 5 1.12. QEI .................................. ................................................... .................................. .................................. .................................. ............................... .............. 5 1.13. GPIOs ................................. .................................................. .................................. .................................. .................................. ............................ ...........6 1.14. Power................................................ ................................................................. .................................. .................................. ............................... .............. 6 2. Thư viện điều khiển ngoại vi Stellaris® ........................................ ......................................................... ............................ ........... 8 2.1. Điều khiển hệ thống (19 System Control) ................................. .................................................. ......................... ........ 8 2.2. Điều khiển vào/ra 9 GPIO ................................ ................................................. ................................... .................................. ................ 9 2.3. Điều khiển ngắt .................................. ................................................... .................................. .................................. ............................. ............ 10 2.4. Điều khiển LCD OLED......................... OLED.......................................... .................................. .................................. .......................... ......... 10 2.5. Chuyển đổi tươ ng ng tự - số ADC ................................. .................................................. .................................. ....................... ...... 10 3. Các bướ c thiết k ế ứng dụng.................................. ................................................... ................................... ................................... ................. 11 3.1. Thiết k ế ứng dụng vớ i GPIO ..................................... ...................................................... .................................. ....................... ...... 12 3.2. Thiết k ế ứng dụng vớ i ngắt GPIO ..................................... ...................................................... ................................ ............... 13 3.3. Thiết k ế ứng dụng vớ i LCD OLED 128x96x4 ................................... ................................................. .............. 13 3.4. Thiết k ế ứng dụng vớ i ADC ................................ ................................................. .................................. ............................. ............ 14 3.5. Thiết k ế ứng dụng vớ i Timer ................................. .................................................. .................................. .......................... ......... 15 4. Các bài t ậ p thiết k ế ứng dụng .................................. ................................................... ................................... ................................ .............. 16 4.1. Lậ p trình ứng dụng vớ i GPIO ................................... .................................................... .................................. ....................... ...... 16 4.2. Lậ p trình ứng dụng ngắt GPIO ...................... ....................................... .................................. ................................... .................. 16 4.3. Lập trình điều khiển LCD OLED................................ ................................................. ................................... ..................... ... 16 4.4. Lập trình đọc ADC ........................................... ............................................................ ................................... ................................ .............. 16 4.5. Lậ p trình vớ i Timer .................................. ................................................... .................................. .................................. ....................... ...... 16
1
1. Giớ i thiệu sản phẩm Product Features The LM3S8962 microcontroller includes the following product features:
1.1. Hiệu năng của kiến trúc RISC 32-Bit
– 32-bit ARM® Cortex™-M3 v7M architecture optimized for small-footprint embedded applications – System timer (SysTick), providing a simple, 24-bit clear-on-write, decrementing, wrap-on-zero counter with a flexible control mechanism – Thumb®-compatible Thumb-2-only instruction set processor core for high code density – 50-MHz operation – Hardware-division and single-cycle-multiplication – Integrated Nested Vectored Interrupt Controller (NVIC) providing deterministic interrupt handling – 36 interrupts with eight priority levels – Memory protection unit (MPU), providing a privileged mode for protected operating system functionality – Unaligned data access, enabling data to be efficiently packed into memory – Atomic bit manipulation (bit-banding), delivering maximum memory utilization and streamlined peripheral control
1.2. Bộ nhớ nội (Internal Memory)
– 256 KB single-cycle flash • User-managed flash block protection on a 2-KB block basis • User -managed flash data programming • User -defined and managed flash-protection block – 64 KB single-cycle SRAM ■ General-Purpose Timers – Four General-Purpose Timer Modules (GPTM), each of which provides two 16-bit timers. Each GPTM can be configured to operate independently: • As a single 32-bit timer • As one 32-bit Real-Time Clock (RTC) to event capture
• For Pulse Width Modulation (PWM) • To trigger analog-to-digital conversions – 32-bit Timer modes • Programmable one-shot timer 2
• Programmable periodic timer • Real-Time Clock when using an external 32.768-KHz clock as the input • User -enabled stalling in periodic and one-shot mode when the controller asserts the CPU Halt flag during debug • ADC event trigger – 16-bit Timer modes • General-purpose timer function with an 8-bit prescaler • Programmable one-shot timer
• Programmable periodic timer • User -enabled stalling when the controller asserts CPU Halt flag during debug • ADC event trigger – 16-bit Input Capture modes • Input edge count capture • Input edge time capture – 16-bit PWM mode • Simple PWM mode with software -programmable output inversion of the PWM signal
1.3. ARM FiRM-compliant Watchdog Timer
– 32-bit down counter with a programmable load register – Separate watchdog clock with an enable – Programmable interrupt generation logic with interrupt masking – Lock register protection from runaway software – Reset generation logic with an enable/disable – User-enabled stalling when the controller asserts the CPU Halt flag during debug 1.4. Controller Area Network (CAN)
– Supports CAN protocol version 2.0 part A/B – Bit rates up to 1Mb/s – 32 message objects, each with its own identifier mask – Maskable interrupt – Disable automatic retransmission mode for TTCAN – Programmable loop-back mode for self-test operation 1.5. 10/100 Ethernet Controller
– Conforms to the IEEE 802.3-2002 Specification – Hardware assistance for IEEE 1588-2002 Precision Time Protocol (PTP) – Full- and half-duplex for both 100 Mbps and 10 Mbps operation – Integrated 10/100 Mbps Transceiver (PHY) – Automatic MDI/MDI-X cross-over correction – Programmable MAC address – Power-saving and power-down modes 1.6. Synchronous Serial Interface (SSI) 3
– Master or slave operation – Programmable clock bit rate and prescale – Separate transmit and receive FIFOs, 16 bits wide, 8 locations deep – Programmable interface operation for Freescale SPI, MICROWIRE, or Texas Instruments synchronous serial interfaces – Programmable data frame size from 4 to 16 bits – Internal loopback test mode for diagnostic/debug testing
1.7. UART
– Two fully programmable 16C550-type UARTs with IrDA support – Separate 16x8 transmit (TX) and 16x12 receive (RX) FIFOs to reduce CPU interrupt service loading – Programmable baud-rate generator allowing speeds up to 3.125 Mbps – Programmable FIFO length, including 1-byte deep operation providing conventional double-buffered interface – FIFO trigger levels of 1/8, 1/4, 1/2, 3/4, and 7/8 – Standard asynchronous communication bits for start, stop, and parity – False-start-bit detection – Line-break generation and detection
1.8. ADC
– Single- and differential-input configurations – Four 10-bit channels (inputs) when used as single-ended inputs – Sample rate of 500 thousand samples/second – Flexible, configurable analog-to-digital conversion – ________€Four programmable sample conversion sequences from one to eight entries long, with corresponding conversion result FIFOs – Each sequence triggered by software or internal event (timers, analog comparators, PWM or GPIO) – On-chip temperature sensor
1.9. Analog Comparators
– One integrated analog comparator – Configurable for output to: drive an output pin, generate an interrupt, or initiate an ADC sample sequence – Compare external pin input to external pin input or to internal programmable voltage reference
1.10. I2C 4
– Master and slave receive and transmit operation with transmission speed up to 100 Kbps in Standard mode and 400 Kbps in Fast mode – Interrupt generation – Master with arbitration and clock synchronization, multimaster support, and 7-bit addressing mode
1.11. PWM
– Three PWM generator blocks, each with one 16-bit counter, two comparators, a PWM generator, and a dead-band generator – One 16-bit counter • Runs in Down or Up/Down mode • Output frequency controlled by a 16 -bit load value
• Load value updates can be synchronized • Produces output signals at zero and load value – Two PWM comparators • Comparator value updates can be synchronized • Produces output signals on match – PWM generator • Output PWM signal is constructed based on actions taken as a result of the counter and PWM comparator output signals
• Produces two independent PWM signals – Dead-band generator • Produces two PWM signals with programmable dead-band delays suitable for driving a half-H bridge
• Can be bypassed, leaving input PWM signals unmodified – Flexible output control block with PWM output enable of each PWM signal • PWM output enable of each PWM signal • Optional output inversion of ea ch PWM signal (polarity control) • Optional fault handling for each PWM signal • Synchronization of timers in the PWM generator blocks • Synchronization of timer/comparator updates across the PWM generator blocks • Interrupt status summary of the PWM gener ator blocks – Can initiate an ADC sample sequence 1.12. QEI
– Two QEI modules – Hardware position integrator tracks the encoder position – Velocity capture using built-in timer 5
– The input frequency of the QEI inputs may be as high as 1/4 of the processor frequency (for example, 12.5 MHz PhA/PhB/IDX for a 50-MHz system) – Interrupt generation on index pulse, velocity-timer expiration, direction change, and quadrature error detection
1.13. GPIOs
– 5-42 GPIOs, depending on configuration – 5-V-tolerant input/outputs – Programmable interrupt generation as either edge-triggered or level-sensitive – Low interrupt latency; as low as 6 cycles and never more than 12 cycles – Bit masking in both read and write operations through address lines – Can initiate an ADC sample sequence – Pins configured as digital inputs are Schmitt-triggered. – Programmable control for GPIO pad configuration: • Weak pull-up or pull-down resistors • 2-mA, 4-mA, and 8-mA pad drive for digital communication; up to four pads can be configured with an 18-mA pad drive for high-current applications • Slew rate control for the 8 -mA drive
• Open drain enables • Digital input enables 1.14. Power
– On-chip Low Drop-Out (LDO) voltage regulator, with programmable output useradjustable from 2.25 V to 2.75 V – Hibernation module handles the power-up/down 3.3 V sequencing and control for the core digital logic and analog circuits – Low-power options on controller: Sleep and Deep-sleep modes – Low-power options for peripherals: software controls shutdown of individual peripherals – User-enabled LDO unregulated voltage detection and automatic reset – 3.3-V supply brown-out detection and reporting via interrupt or reset ■ Flexible Reset Sources – Power-on reset (POR) – Reset pin assertion – Brown-out (BOR) detector alerts to system power drops – Software reset – Watchdog timer reset – Internal low drop-out (LDO) regulator output goes unregulated ■ Additional Features – Six reset sources
6
– Programmable clock source control – Clock gating to individual peripherals for power savings – IEEE 1149.1-1990 compliant Test Access Port (TAP) controller – Debug access via JTAG and Serial Wire interfaces – Full JTAG boundary scan ■ Industrial and extended temperature 100-pin RoHS-compliant LQFP package ■ Industrial-range 108-ball RoHS-compliant BGA package
7
2. Thư viện điều khiển ngoại vi Stellaris® (Stellaris® Peripheral Driver Library)
2.1. Điều khiển hệ thống (19 System Control) 1.1. Giới thiệu 1.2. Mô tả chi tiết - Các sysctl API đượ c chia thành tám nhóm chức năng: Nhóm cung cấ p thông tin thiết bị, nhóm nguồn xung clock của các thiết bị, những ngườ i cung cấp điều khiển thiết bị ngo ại vi, những người mà đối phó với các sysctl gián đoạn, những ngườ i mà đối phó vớ i các LDO, những người mà đố i phó vớ i ch ế độ ng ủ, nh ững người mà đối phó vớ i lý do thi ết l ậ p l ại, những người mà đố i phó vớ i các thiết l ậ p l ại màu nâu-out, và những người mà đối phó vớ i giờ kiểm tra đồng hồ. 1.2.1. Thông tin về các thiết bị đượ c cung cấp bở i SysCtlSRAMSizeGet (), SysCtlFlashSizeGet (), SysCtlPeripheralPresent (), SysCtlPinPresent (). 1.2.2. Clocking của thiết bị C ấ u hình v ới : SysCtlClockSet () và SysCtlPWMClockSet (). Thông ti n v ề b ộ phát x ung c ủa thi ết b ị đượ c cung c ấ p b ởi : SysCtlClockGet () và SysCtlPWMClockGet (). 1.2.3. Thiết bị ngoại đượ c cho phép hay thi ết lập lại bằng các thanh ghi: SysCtlPeripheralReset() , SysCtlPeripheralEnable() , SysCtlPeripheralDisable(), SysCtlPeripheralSleepEnable() , SysCtlPeripheralSleepDisable() , SysCtlPeripheralDeepSleepEnable() , SysCtlPeripheralDeepSleepDisable() , and SysCtlPeripheralClockGating(). 1.2.4.
Điều khiển hệ thống vớ i ngắt: SysCtlIntRegister(), SysCtlIntUnregister(), SysCtlIntEnable(), SysCtlIntDisable(), SysCtlIntClear(), SysCtlIntStatus().
1.2.5. LDO được điều khiển vớ i SysCtlLDOSet() và SysCtlLDOConfigSet(). Trạng thái LDO đượ c cung cấp bở i SysCtlLDOGet(). 8
1.2.6. Thiết bị đượ c chuyển sang chế độ ngủ vớ i SysCtlSleep() and SysCtlDeepSleep(). 1.2.7.
Các nguyên nhân Reset đượ c quản lý bở i SysCtlResetCauseGet() and SysCtlResetCauseClear(). Reset bằng phần mềm được thực hiện bởi SysCtlReset(). Reset do brown-out được cấu hình với SysCtlBrownOutConfigSet() .
1.2.8. Xác minh nguồn xung nhịp của các bộ đếm đượ c quản lý bở i SysCtlIOSCVerificationSet(), SysCtlMOSCVerificationSet(), SysCtlPLLVerificationSet(), and SysCtlClkVerificationClear().
2.2. Điều khiển vào/ra 9 GPIO 2.1 Giới thiệu: - Có thể đượ c cấu hình như một đầu vào hoặc đầu ra. Ngày thiết lậ p lại, họ mặc định là một đầu vào. Trong chế độ đầu vào, có thể tạo ra ngắt ở mức cao, mức thấ p, tăng cạnh, góc xuống, hoặc cả hai cạnh. - Trong chế độ đầu ra, có thể đượ c c ấu hình cho 2 mA, 4 mA, hoặc 8 sức mạnh ổ mA. Các cấu hình mạnh ổ 8 mA có kiểm soát tốc độ quay tùy chọn để hạn chế sự tăng và giảm thờ i gian của tín hiệu. - Ngày thiết lậ p lại, họ mặc định để sức mạnh ổ 2 mA. Tùy chọn yếu kéo lên hoặc kéo xuống điện tr ở . Ngày thiết l ậ p l ại, h ọ mặc định để một yếu pull-up trên các thiết bị Sandstorm-class, và mặc định vô hiệu hóa trên tất cả các thiết bị khác. - Hoạt động mở cống tùy chọn. Ngày thiết lậ p lại, họ mặc định để hoạt động push / pull chuẩn. Có thể đượ c cấu hình là một GPIO hoặc một pin thiết bị ngoại vi. Ngày thiết lậ p lại, họ mặc định là GPIOs. - Lưu ý rằng không phải tất cả các chân trên tất cả các bộ phận có chức năng ngoại vi, trong trườ ng hợ p này là pin chỉ hữu ích như là một GPIO (có nghĩa là, khi cấu hình cho chức năng ngoại vi pin sẽ không làm bất cứ điều gì hữu ích).
2.2. Mô tả chi tiết Các GPIO API đượ c chia thành ba nhóm chức năng: cấu hình các GPIO chân, liên quan đến hoạt động ngắt, và những chức năng truy cậ p vào các giá tr ị các chân. 9
2.2.1 Các chân GPIO được cấu hình bới GPIODirModeSet() and GPIOPadConfigSet(). Các cấu hình có thể được đọc về bởi GPIODirModeGet() and GPIOPadConfigGet(). Ngoài ra còn có các chức năng thuận tiện cho việc cấu hình pin cho việc cấu hình theo yêu c ầu hoặc đề nghị của ngoại vi đặc biệt: GPIOPinTypeCAN(), GPIOPinTypeComparator(), GPIOPinTypeGPIOInput(), GPIOPinTypeGPIOOutput(), GPIOPinTypeGPIOOutputOD(), GPIOPinTypeI2C(), GPIOPinTypePWM(), GPIOPinTypeQEI(), GPIOPinTypeSSI(), GPIOPinTypeTimer(), and GPIOPinTypeUART(). 2.2.2 Các ngắt GPIO được xử lý với GPIOIntTypeSet(), GPIOIntTypeGet(), GPIOPinIntEnable(), GPIOPinIntDisable(), GPIOPinIntStatus(), GPIOPinIntClear(), GPIOPortIntRegister(), and GPIOPortIntUnregister(). 2.2.3 Tr ạng thái chân GPIO được truy cập bởi GPIOPinRead() and GPIOPinWrite().
2.3. Điều khiển ngắt
3.1 Giới thiệu: 3.2. Mô tả chi ti ết 3.2.1 Xử lý ngắt được quản lý với IntRegister () và IntUnregister (). 3.2.2 Mỗi nguồn ngắt có thể được kích hoạt và vô hiệu hóa thông qua IntEnable() and IntDisable();Các bộ xử lý ngắt có thể được kích hoạt và vô hiệu hóa thông qua IntMasterEnable() and IntMasterDisable(); 3.2.3. Các ưu tiên của từng nguồn ngắt có thể được thiết lập và xem xét thông qua IntPrioritySet() and IntPriorityGet().
2.4. Điều khiển LCD OLED 2.5. Chuyển đổi tương tự - số ADC 10
5.1 Giới thiệu: 5.2. Mô tả chi ti ết
Thư viên API của ADC đượ c chia thành 3 nhóm ch ức năng: 5.2.1 Chức năng với các mẫu - Cấu hình các chuỗi mẫu: ADCSequenceConfigure() and ADCSequenceStepConfigure(). - Cấm hoạc cho phép : ADCSequenceEnable() and ADCSequenceDisable(). - Thu dữ liệu từ bộ chuyển đổi: ADCSequenceDataGet(). 5.2.2 Chức năng kích hoạt bộ xử lý - Tín hiệu kích hoạt từ vi xử lý được tạo ra bởi : ADCProcessorTrigger(). 5.2.3 Chức năng với ngắt - Quản lý nguồn ngắt ADCIntDisable(), ADCIntEnable(), ADCIntStatus(), and ADCIntClear().
3. Các bướ c thiết k ế ứ ng dụng 11
3.1. Thiết k ế ứ ng dụng vớ i GPIO
Bướ c 1. Thiết lập hệ thống - Thiết lậ p bộ dao động bằng lệnh SysCtlClockSet(); SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_XTAL_8 MHZ|SYSCTL_OSC_MAIN);
-
Cho phép PORT GPIO tương ứng hoạt động bằng lệnh SysCtlPeripheralEnable ();
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
Bướ c 2. Thiết lập cho các GPIO - Lựa chọn hướ ng dữ liệu cho các PIN bằng 1 trong 2 lệnh GPIOPinTypeGPIOInput();GPIOPinTypeGPIOOutput(); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_0); GPIOPinTypeGPIOInput(GPIO_PORTF_BASE,GPIO_PIN_1);
- Cấu hình các đặc tính điệ n cho các PIN b ằng hàm: GPIOPadConfigSet() GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_1,GPIO_STRENGTH_2 MA, GPIO_PIN_TYPE_STD_WPU);
Bước 3. Ghi đọc dữ liệu - Đọc dữ liệu: GPIOPinRead(); - Ghi dữ liệu: GPIOPinWrite();
12
3.2. Thiết k ế ứ ng dụng vớ i ngắt GPIO
Bướ c 1. Viết khung chương trình có sử dụng ngắt - Viết khung chương trình vớ i ARM - Viết chương trình con phụ c vụ ngắt void GPIOFIntHandler (void) { } - Khai báo các thư việ n của ngắt #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" #include "driverlib/gpio.h" #include "driverlib/sysctl.h" #include "driverlib/interrupt.h" - Khai báo hàm con của ngắt trong file Start Up EXTERN GPIOFIntHandler DCD GPIOFIntHandler ; GPIO Port F Bướ c 2. Thiết lập hệ thống - Thiết lậ p bộ dao động bằng lệnh SysCtlClockSet(); - Cho phép PORT GPIO tương ứ ng hoạt động bằng lệnh
SysCtlPeripheralEnable();
Bướ c 3. Thiết lập cho các GPIO - Lựa chọn hướ ng dữ liệu cho các PIN bằng 1 trong 2 lệnh -
GPIOPinTypeGPIOInput();GPIOPinTypeGPIOOutput(); Cấu hình các đặc tính điệ n cho các PIN b ằng hàm: GPIOPadConfigSet() Thiết lậ p GPIO vớ i ngắt bằng lệnh : GPIOPinIntEnable();
GPIOPinIntEnable(GPIO_PORTF_BASE,GPIO_PIN_1);
Bướ c 4. Thiết lập các hàm trong thư việ n ngắt - Cho phép ngắt ngoại vi bằng hàm: IntEnable(); IntEnable(INT_GPIOF); - Cho phép ngắt toàn cục bằng hàm: IntEnable(); IntMasterEnable();
Bướ c 5. Viết chương trình con phục vụ ngắt - Lênh xóa cờ ngắt 3.3. Thiết k ế ứ ng dụng vớ i LCD OLED 128x96x4
Bướ c 1. Khai báo thư viện - Khai báo các thư viện điề u khiển thệ thống -
Thêm (Add) file LCD rit128x96x4.c vào Project 13
#include "driverlib/rit128x96x4.c" (Copy rit128x96x4.c và rit128x96x4.h từ thực mục driver vào thư mục driverlib ) - Thêm (Add) file thư viện ustdlib.c vào Project Bướ c 2. Thiết lập hệ thống Bướ c 3. Thiết lập LCD - Thiết lậ p LCD vớ i hàm: RIT128x96x4Init();
Bướ c 4. Hiện thị lên LCD - Hiển thị chuỗi ký tự -
-
RIT128x96x4StringDraw( "Pham Van Chien",5,50,15); Hiển thị một biến số nguyên usprintf (MANG,"So san pham: %d",i); RIT128x96x4StringDraw (MANG,5,50,15); Hiển thị một biến số thậ p phân Xóa ký màn hình LCD
3.4. Thiết k ế ứ ng dụng vớ i ADC
Bướ c 1. Khai báo thư viện #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "driverlib/adc.h" #include "driverlib/sysctl.h" #include "driverlib/rit128x96x4.h" #include "driverlib/rit128x96x4.c" #include "utils/ustdlib.h" Bướ c 2: Cau hinh he thong SysCtlClockSet(…); SysCtlPeripheralEnable(…); // cho phep ADC0
Bướ c 3: Cau hinh LCD RIT128x96x4Init(tansoISS); Bướ c 4: Cấu hình ADC ADCSequenceConfigure(...); ADCSequenceStepConfigure(...); ADCSequenceEnable(...); Bướ c 5: Đọc ADC // Chuyen doi ADC ADCProcessorTrigger(ADC0_BASE, 0); for(t=0;t<100000;t++); // Doc gia tri ADC ADCSequenceDataGet(ADC0_BASE, 0, &x); // Hien thi ra LCD usprintf(Mang,"%d ",x); 14
RIT128x96x4StringDraw(Mang,40,40,15);
3.5. Thiết k ế ứ ng dụng vớ i Timer
15
4. Các bài tập thiết k ế ứ ng dụng 4.1. Lập trình ứ ng dụng vớ i GPIO Ví dụ 1. Lập trình điều khiển LED (PF0) sáng nhấ p nháy. Ví dụ 2. Lậ p trình nhấn nút SELECT (PF1) LED sáng, nh ả LED tắt. Ví dụ 3. Lậ p trình nhấn nút LEFT (PE2) LED sáng, nh ả LED tắt. Ví dụ 4. Lậ p trình nhấn nút LEFT (PE2) LED t ắt, nhả LED sáng. Ví dụ 5. Lậ p trình nhấn nút LEFT (PE2) LED sáng, nh ấn nút RIGHT(PE3) LED t ắt.
4.2. Lập trình ứ ng dụng ngắt GPIO Ví dụ 1. Lập trình điều khiển LED sáng nhấ p nháy(nhanh), khi nhấn nút SELECT(PF1) LED chuy ển sang chế độ sáng nhấ p nháy (chậm) 10 lần, sau đó LED tiế p tục nháy nhanh. Ví dụ 2. Lập trình điều khiển LED sáng nhấ p nháy(nhanh), khi nhấn nút UP(PE0) LED chuyển sang chế độ sáng nhấ p nháy (chậm) 10 lần, sau đó LED tiế p tục nháy nhanh. Ví dụ 3. Lậ p trình điều khiển LED sáng nhấ p nháy(nhanh), khi nhấn nút UP(PE0) LED chuyển sang chế độ sáng nhấ p nháy (chậm) 10 lần, khi nhấn nút SELECT(PF1) LED chuyển sang chế độ sáng nhấ p nháy (chậm) 5 lần.
4.3. Lập trình điều khiển LCD OLED Ví dụ 1. Lậ p trình hiển thị chuỗi ký tự “họ tên của bạn” ra giữa màn hinh LCD. Ví dụ 2. Lậ p trình hiển thị biến số nguyên ra LCD. Ví dụ 3. Lậ p trình hiển thị biến số thực ra LCD.
4.4. Lập trình đọc ADC Ví dụ 1. Lập trình đọc ADC từ kênh CH0, hiển thị giá tr ị của ADC trên LCD. Ví dụ 2. Lập trình đọc ADC từ cảm biến nhiệt trên chíp và hiển thị nhiệt độ ra LCD.
4.5. Lập trình v ớ i Timer Ví dụ 1. Sử dụng ngắt Timer0-TimerA, l ập trình điều khiển led STATUS sáng nhấ p nháy vớ i tần số 1Hz(0,5s sáng; 0,5s tắt).
16
Ví dụ 2. Sử dụng ngắt Timer0-TimerA, l ập trình điều khiển led STATUS sáng nhấ p nháy vớ i tần số 1Hz (0,5s sáng; 0,5s t ắt). khi nhấn DOWN led sáng nhấ p nháy 5Hz; nhấn UP led sáng nhấ p nháy 1Hz.
17