lab 1 preparation sample c programs
play

Lab 1 Preparation Sample C Programs C review (Chapter 5, - PDF document

CEG411/611 Microprocessor EVB (Evaluation Board): Axiom CSM12C32 Based System Design (www.axman.com) What is this course about? Computer : Processor, Memory, I/O Chip: MC9S12C32 from Freescale (formerly, a Processor Memory


  1. CEG411/611 Microprocessor • EVB (Evaluation Board): Axiom CSM12C32 Based System Design (www.axman.com) • What is this course about? • Computer : Processor, Memory, I/O • Chip: MC9S12C32 from Freescale (formerly, a Processor Memory Motorola division) – For chip documentation in PDF Bus I/O files, see C:\68HC12\ 9S12C32_Chip • Microprocessor versus microcontroller • Embedded Systems • 68HC12 versus 68HCS12 Lab 1 Preparation Sample C Programs • C review (Chapter 5, sample_c.c) • Monitor, MON12, Monitor commands (Sec. 3.5.3) MD, MM, LOAD, CALL – main( ), function call • Address versus data – data types (char, unsigned char, int, short, long, float) • Hexadecimal number system and Memory map – Condition check (true, false, if, else) • C, Assembly, Machine Code – for loop, while loop • ICC12 and as12 – putchar(), puts(), printf() • Step by step instructions • tone.c : timer, output compare (Sec 8.6) • S record file, list file, map file • switch_LED.c : simple parallel port usage A simple parallel port usage example: Simple Parallel Port Usage switch_LED.c • Let Y be a generic register which can be PORTA, PORTB, PORTE, PTAD, PTT, PTP, PTS, PTM, DDRA, SW1 VCC DDRB, and so on. LED1 • Contents of Y: Y 7 Y 6 Y 5 Y 4 Y 3 Y 2 Y 1 Y 0 PE0 PA0 VCC • Output (Use Y 1 as an example) – Set bits : Y |= 0x02; – Clear bits : Y &= ~0x02; or Y &= 0xFD; – Toggle bits : Y ^= 0x02; SW2 VCC LED2 • Input – Check if set : if( Y & 0x02 ) PP5 PB4 VCC – Check if clear : if( !(Y & 0x02)) MC9S12C32 – Wait until set : while( !(Y & 0x02) ) ; – Wait until clear: while(Y & 0x02) ; 1

  2. Measuring Pulse Width Without Interrupt Programming Using Input Capture • Interrupt versus polling • Interrupt programming 6812 – Write an ISR (interrupt service routine) – Register the ISR, p. 268 – Enable ISR (locally and globally) • Use while loops and read TCNT to get T1, • tone_interrupt.c example T2 • On EVB, pressing the reset button clears • If(T2 > T1) T = T2 – T1; the MON12 (user) interrupt vector table else T = 0xffff – T1 + T2 + 1; contents • Timer Overflow? • Chapter 6 ADC (Chapter 12) ADC Timing (ATDCTL4), p. 602 • ADC basics, a 2Lbit ADC example, Sec. • ATD Clock = Bus Clock / [2(PRS+1)] 12.2.4, Example 12.1 – Bus Clock : 24 MHz on EVB Analog = V L + Digital * (V H LV L )/(2 N L1) – PRS : bits 4 to 0 of ATDCTL4 • ADC Internal: Successive Approximate • ATD Conversion Time per Sample = Method, Sec. 12.2.3 [2 + 2(SMP+1) + B] ATD clock cycles • Signal Conditioning Circuits based on OP Amp: Sec. 12.2.5 & 12.2.6 – SMP : bits 6 and 5 of ATDCTL4 • 6812 ADC Programming: Sec. 12.3 – B: 8 or 10 (for 8Lbit and 10Lbit ADC, respectively) – adc_scan.c Assembly Programming • Memory Addressing 16Lbit Address • Why assembly programming? CPU Memory 8/16Lbit Data – Understand computer/C internal operations – More efficient coding – BigLEndian versus LittleLEndian • CPU Registers (Sec. 1.8) – Memory Mapped I/O D (A:B) : accumulator; X, Y: index registers; • A sample assembly program (tonevb.asm) SP : Stack Pointer; PC: Program Counter – Label, Opcode, Operands, Comments CCR : Condition Code Register – Assembler directives (e.g., ORG) S X H I N Z V C 2

  3. • Load/Store Instructions (Sec. 1.11.1) and Example: Addressing Modes (Sec. 1.9) • Examples int m, n; // assume m is at $3000 – LDAA #$55 immediate mode // assume n is at $3002 – LDAA #55 immediate mode – LDD #$0F20 immediate mode m = n + 5; – LDAA $55 direct mode – LDAA $0F20 extended mode LDD $3002 – LDD $0F20 extended mode – LDAB 3,X indexed mode ADDD #5 • LDX, LDY, LEAS, LEAX, LEAY STD $3000 – LEAS L4,SP : SP ← (SP)L4 (to allocate space for local variables for subroutines) • STAA, STAB, STD, STS, STX, STY How about char instead of int? • Assembler Directives (Sec. 2.3) • More instructions (Sec. 1.11) – ORG (Origin), EQU (Equate) – Transfer (register to register) and Exchange – Specify Constants: fcb (Form Constant Byte, or db), (swap registers): fcw (Form Constant Word, or dw), fcc (Form Constant TAB, TAP, TBA, ....; EXG, XGDX, XGDY Character, String), fill – Reserve Space: rmb (reserve memory byte), rmw – Move (memory to memory): MOVB, MOVW (reserve memory word) – Add and Subtract (always involves CPU • Examples registers) ORG $3800 • Register + Register : ABA, ABX, ABY array fcb $11,$22,$33,’5,25,100 • Register + Memory : ADCA, ADCB, ADDA, ADDB, ADDD buf rmb 20 • Register – Register : SBA msg fcc “Hello World” • Register – Memory : SBCA, SBCB, SUBA, SUBB, tmp fill $11,20 SUBD • Examples 2.14 (p. 68), 2.15 • Program Loops (Sec. 2.6) – Draw flowchart – Loop constructs – Revise flowchart – Branch conditions/instructions • Example 2.17 compare (A to B, a register to a memory) – BRCLR oprand,mask,label or branch to label if the bit(s) is (are) clear test (A, B, or a memory location) while(!(TFLG1 & 0x01)); // wait until flag is set // the above C statement is equivalent to S X H I N Z V C CCR here BRCLR $004E,$01,here Note that $004E is the address of TFLAG1 branch (BRA, BEQ, BNE, BLS, ...) – BRSET oprand,mask,label 3

  4. Subroutine Calls (Chapter 4) • BSR, JSR, RTS, Caller, Callee (Sec. 4.4) BSR SORT • Stack, Stack Pointer, Push, Pull ........ – PSHA, PSHB, PSHD, PSHX, PSHY : Decrement (SP) first, then write data SORT ........ – PULA, PULB, PULD, PULX, PULY ........ : Read data first, then increase (SP) RTS • Example: LDS #$3E00 • Parameter Passing, Result Returning, and LDAA #$3B Allocation of Local Variables (Sec. 4.5) LDD #$302F – Using registers versus using stack PSHA PSHD • Ex. 4.1 LDD 5,sp *** b void main() • Stack Frames (Sec. 4.6), Ex. 4.3 PSHD { LDD #3 4. Callee: int a, b; Local Variables PSHD LEAS L4,SP a = sum(b, 3); 2. Caller: Saved Registers LEAS L2,sp *** result } BSR Return Address JSR Sum 3. Callee: .... Incoming 1. Caller: PSHD int sum(int x, int y) Parameters & PSHX { Sum: Return Values int s; PSH... *** save reg s = x + y; LEAS L2,sp *** s • Example C versus Assembly return s; ...... } More Timer Functions • Parallel Ports, I/O Synchronization and Handshaking (Sec. 7.4) • Timer Overflow (p. 367): TSCR2 bit 7 (TOI), – Strobe vs. Handshake TFLAG2 bit 7(TOF) – Input/Output Handshaking Protocols • Input Capture (Sec. 8.5) and Examples • Serial Interface (Chap. 9 & 10): • RealLTime Interrupt (Sec. 6.7): – SCI vs. SPI CRGINT bit 7 (RTIE), CRGFLG bit 7 (RTIF) – SCI: RSL232, StartLbit, StopLbit, Parity n = RTICTL bits 6L4; m = RTICTL bits 3L0 OSCCLK (16MHz on EVB)/[(m+1)2 (n+9) ] – SPI (p.482): SCK, SS, MISO, MOSI • Pulse Accumulator (Sec. 8.7): a 16Lbit counter – Other Serial Interface Protocols: USB, PCI • Pulse Width Modulation (PWM) (Sec. 8.10) Express, SATA, Ethernet 4

Recommend


More recommend