Title: EECE 631 Microcomputer System Design Lecture 9 Programming for Microcontrollers
1EECE 631Microcomputer System DesignLecture 9
Programming for Microcontrollers
- Spring 2008
- Chris Lewis
- clewis_at_ksu.edu
2Help for the Lab
- 2D Table interpolation
- How can you interpolate between two values?
- 3 times for a single entry
- Example Compass Calibration table
- Every 10 degrees of heading
- Every 5 degrees of tilt -45
- Compute heading for 33 degrees with 11 degrees of
tilt - 30, 10 gt 3
- 30, 15 gt 5
- 40, 10 gt -1
- 40, 15 gt -15
C1
Value
C2
3Overview of Features in Processor
- 32 Bit and 16 Bit Thumb instruction sets
- High Speed Flash
- High Speed SRAM
- Memory Controller (external memory?)
- Periodic Interval Timer
- Watch Dog Timer
- 32 Bit Real-time Timer
- Parallel I/O (how many bits?)
- USB support
- Synchronous Serial Controller
- 2 USART
- SPI
- 16Bit Timer Counter
- 4 Channel 16bit PWM controller
- Two Wire Interface
- 10 Bit 8 Channel A2D
- 5Volt Tolerant I/O
- 3.3 Volt Operation
4Example using hardware, Running Lights
- include "Board.h
- / Global variables /
- define SPEED (MCKKHz/10)
- unsigned int LedSpeed SPEED 50
- const int led_mask8 LED1, LED2, L ED3, LED4
- void change_speed ( void )
- // Begin
- if ( (AT91F_PIO_GetInput(AT91C_BASE_PIOA)
SW1_MASK) 0 ) - if ( LedSpeed gt SPEED ) LedSpeed -SPEED
-
- if ( (AT91F_PIO_GetInput(AT91C_BASE_PIOA)
SW3_MASK) 0 ) - if ( LedSpeed lt MCK ) LedSpeed SPEED
-
- // End
- void wait ( void )
- // Begin
- unsigned int waiting_time
- change_speed ()
- for(waiting_time 0 waiting_time lt
LedSpeed waiting_time)
5int main() // Begin int i // First,
enable the clock of the PIO
AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1
ltlt AT91C_ID_PIOA ) // then, we configure
the PIO Lines corresponding to LED1 to LED4
// to be outputs. No need to set these pins to be
driven by the PIO because it is GPIO pins only.
AT91F_PIO_CfgOutput( AT91C_BASE_PIOA,
LED_MASK ) // Clear the LED's. apply a "1"
to turn off LEDs AT91F_PIO_SetOutput(
AT91C_BASE_PIOA, LED_MASK )
__enable_interrupt() \\ intrinsic function
// Loop forever for () // Once
a Shot on each led for ( i0 i lt NB_LEB i
) AT91F_PIO_ClearOutput( AT91C_BASE_PIOA,
led_maski) wait()
AT91F_PIO_SetOutput( AT91C_BASE_PIOA, led_maski
) wait() // End for //
Once a Shot on each led for (
i(NB_LEB-1) i gt 0 i-- )
AT91F_PIO_ClearOutput( AT91C_BASE_PIOA,
led_maski) wait()
AT91F_PIO_SetOutput( AT91C_BASE_PIOA, led_maski
) wait() // End for //
End
6Procedure for Using Peripheral
- Configuration Register
- Pin Assignment Register
- Data Registers
7(No Transcript)
8(No Transcript)
9Resources and Dependencies
Clocks, Pins, and Interrupts
10Peripheral Register Mapping
11Each Bit has defined Meaning
12Embedded C Issues
- Set and clear bits of specific bits in memory
mapped registers - Example from First Lab
- //-----------------------------------------------
----------------------------- - // \fn AT91F_PIO_SetOutput
- // \brief Set to 1 output PIO
- //-----------------------------------------------
----------------------------- - __inline void AT91F_PIO_SetOutput(
- AT91PS_PIO pPio, // \arg pointer to a PIO
controller - unsigned int flag) // \arg output to be set
-
- pPio-gtPIO_SODR flag
-
- //-----------------------------------------------
----------------------------- - // \fn AT91F_PIO_ClearOutput
- // \brief Set to 0 output PIO
- //-----------------------------------------------
----------------------------- - __inline void AT91F_PIO_ClearOutput(
13Call from Main
- // Once a Shot on each led
- for ( i0 i lt NB_LEB i )
-
- AT91F_PIO_ClearOutput(AT91C_BASE_PIOA,
led_maski) - wait()
- AT91F_PIO_SetOutput( AT91C_BASE_PIOA,
led_maski ) - wait()
- // End for
- AT91C_BASE_PIOA Must be defined in include
Board.h - Board.h includes another file include
AT91SAM7S256.h - define AT91C_BASE_PIOA ((AT91PS_PIO)
0xFFFFF400) // (PIOA) Address
14Structure Definition
- typedef volatile unsigned int AT91_REG//
Hardware register definition -
- typedef struct _AT91S_PIO
- AT91_REG PIO_PER // PIO Enable Register
- AT91_REG PIO_PDR // PIO Disable Register
- AT91_REG PIO_PSR // PIO Status Register
- AT91_REG Reserved01 //
- AT91_REG PIO_OER // Output Enable Register
- AT91_REG PIO_ODR // Output Disable Registerr
- AT91_REG PIO_OSR // Output Status Register
- AT91_REG Reserved11 //
- AT91_REG PIO_IFER // Input Filter Enable
Register - AT91_REG PIO_IFDR // Input Filter Disable
Register - AT91_REG PIO_IFSR // Input Filter Status
Register - AT91_REG Reserved21 //
- AT91_REG PIO_SODR // Set Output Data Register
- AT91_REG PIO_CODR // Clear Output Data
Register - AT91_REG PIO_ODSR // Output Data Status
Register - AT91_REG PIO_PDSR // Pin Data Status Register
15Embedded C Techniques
- Pointer to Structure overlaid on top of memory
mapped peripherals control and data register - Bit Setting and clearing (from Board.h)
- define LED1 (1ltlt0) / PA0 / PGMEN0
PWM0 TIOA0 48 / - define LED2 (1ltlt1) / PA1 / PGMEN1
PWM1 TIOB0 47 / - define LED3 (1ltlt2) / PA2
PWM2 SCK0 44 / - define LED4 (1ltlt3) / PA3
TWD NPCS3 43 / - Use
- Ptr-gtdata_register LED3 // set bit
- Ptr-gtdata_register LED2 // clear bit
16Bit Manipulation
- NOT ! -vs-
- AND -vs-
- OR -vs
- Shift ltlt and gtgt
17Using Digital IO
- PIOA Parallel I/O Module
- Product Dependencies
- Power Management
- Advanced Interrupt Controller
- Programming Interface
- Configure PMC to enable clock
- Configure AIC if needed
- Assign Pins to PIO
- Configure as either IN or OUT
- Write 1s to Set Register to Set
- Write 1s to Clear Register to Clear
- Read Status Register to read