The Startup File start08'c - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

The Startup File start08'c

Description:

After the startup file is run, the compiler moves to the main file ... This limits flexibility as you can only enable interrupts once ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 16
Provided by: robynve
Category:
Tags: file | run | start08 | startup | up

less

Transcript and Presenter's Notes

Title: The Startup File start08'c


1
The Startup File start08.c
  • When the processor is booted up several things
    need to be set up, These include
  • The stackpointer needs to be initialized to the
    correct position
  • Variables need to be initialized
  • These things need to be done in assembly
  • We have a C file called start08.c which contains
    pieces of C code as well as all the assembly
    start up routines
  • The processor starts executing this file when it
    is booted
  • The file sets up what it needs to and then passes
    control to void main(void), the main C program

2
The main file
  • After the startup file is run, the compiler moves
    to the main file
  • Here is an example of a main file that defines
    Port A as an output and turns on some LEDs
  • void main(void)
  • SOPT 0x53 // Disable the COP watchdog timer
  • PTADD 0xFF // All LEDs are outputs
  • PTAD 0x00 // Turn all LEDs ON (active low)

3
Some Useful Type Qualifiers
  • Type qualifiers are used to modify declarations
    to give them special qualities
  • In order to access IO registers of the GT16 we
    simply used the names of the IO registers
  • This is enabled by the compiler which has an
    include file which maps all the register names to
    the appropriate addresses

4
Example of a Header File
  • define VectorNumber_Vrti 25
  • define VectorNumber_Viic1 24
  • define VectorNumber_Vatd1 23
  • define VectorNumber_Vkeyboard1 22
  • define VectorNumber_Vsci2tx 21
  • define VectorNumber_Vsci2rx 20
  • define VectorNumber_Vsci2err 19
  • define VectorNumber_Vsci1tx 18
  • define VectorNumber_Vsci1rx 17
  • define VectorNumber_Vsci1err 16
  • define VectorNumber_Vspi1 15
  • define VectorNumber_Vtpm2ovf 14

5
Assigning Memory Space to a Variable
  • If we need to force a variable to an address we
    can do it as follows
  • volatile unsigned char SGPORTA _at_0X00000000
  • This line of code declares a variable of type
    unsigned char and forces it to occupy the address
    0x0000 in the memory space of the microcontroller
  • Remember normally the compiler will automatically
    assign spaces in memory to variables in a fairly
    optimal way
  • Force variables to specific location might
    actually hinder the compiler and result in less
    efficient code

6
The volatile Keyword
  • The keyword volatile is important in some cases
  • It tells the compiler to suppress optimisation of
    all code that accesses that variable
  • This is very important and we will see why in the
    following example

7
Example
  • void main()
  • PTADD 0xFF
  • PTAD 0
  • PTAD 1
  • PTAD 0
  • You might expect the bit zero of port A to first
    go LOW then HIGH then LOW
  • This is useful if you want to put a very brief
    pulse on a line
  • However if the compiler was allowed to optimise
    this code we would get the following

8
Example
  • void main()
  • PTADD 0xFF
  • PTAD 0
  • As you can see this clearly does not pulse the
    line
  • The reason for this is that the compiler will
    detect redundant code in the function and remove
    it
  • The volatile keyword suppresses this optimisation
    and an output pulse will appear on the line
  • This problem could also occur when we attempt to
    read from a port
  • All memory registers are declared as volatile
  • Volatile should only be used when necessary as it
    reduces compiler efficiency

9
Interrupt Handlers
  • Interrupt handlers are written like normal
    functions except that they are placed with a line
    above them which tells the compiler
  • That they are interrupt handlers
  • And the interrupt vector number
  • interrupt 2
  • void irq_int(void)
  • asm(nop)
  • IRQSC0x04

10
Interrupt Handlers Cont.
  • Interrupt handlers must have
  • A void return type
  • No input parameters
  • This is because an interrupt handler is called
    asynchronously with respect to the main program
    and therefore has no call to supply arguments or
    return a value
  • When the C compiler sees that a function is an
    interrupt handler it will automatically make
    provision to stack and unstack the index register
    and end the procedure with a return from
    interrupt (RTI) instruction instead of a return
    from subroutine for normal functions
  • The linker places the address of the interrupt
    handler at the correct location in the interrupt
    vector table based on which number is specified

11
CLI instruction
  • Before any interrupt can be called the CLI
    instruction must be used to unmask interrupts
  • There are many ways of issuing this instruction
  • Add it to the start up file before the main()
    function is called
  • This limits flexibility as you can only enable
    interrupts once
  • We will soon see how to use inline assembly to do
    use this function in the main source file

12
Global Variables and Volatile
  • Very often use interrupt handlers to modify
    global variables that are shared with other
    program segments
  • Consider the following example

13
Example
  • int sync
  • void main()
  • sync 1
  • while (sync)
  • PTB 0xFF
  • interrupt 2
  • void IRQ_line(void)
  • sync 0
  • IRQSC0x04

14
Example
  • If sync is not declared as volatile then the
    while loop in the main function will never
    terminate
  • This is because the compiler will optimize it
  • sync is not modified inside the loop and the
    instruction modify PTB will never execute

15
Assembly Inclusion
  • It is often useful to be able to include assembly
    instructions inside of the C code
  • The simplest method is to use the inline
    assembler directive
  • This directive inserts a single line of assembler
    into the C code
  • asm(cli)
  • This line executes the assembly instruction CLI
    to enable interrupts
  • If you wish to insert a block of assembly code
    use the assembler block directive
  • asm
  • clra
  • cli
Write a Comment
User Comments (0)
About PowerShow.com