Title: EECE 631 Microcomputer System Design Lecture 6 Programming for Microcontrollers
1EECE 631Microcomputer System DesignLecture 6
Programming for Microcontrollers
- Spring 2008
- Chris Lewis
- clewis_at_ksu.edu
2Temperature Conversion Lab
- A2D
- A2D converter samples on a regular interval.
- ISR puts data into a specific memory location
- Data is only 10 bits, Other bits are garbage
- Convert A2D Counts to Temperature
- Celsius
- Fahrenheit
- Calibration
- Zero
- Boil
-
3Integer Arithmetic
- Why use it?
- No FPU
- Faster more compact code
- What can go wrong?
- Overflow
- Truncation
4Rounding
- // all vars ints
- D (AB)/C // truncates
- D (2A2BC)/(2C) // correct if pos
- D (2A2B-C)/(2C) // correct if neg
5Working with Hardware
- Chip Mfgs give out code to aid in the use of
their chips - They are trying to minimize your development time
- Main problem, Configure, access hardware
- Cpu modules
- A2D and DAC
- PWM and Pulse Processing
- Timers, counters
- SPI, CAN, USB, etc
6Pointer Method 1
- Three steps
- Define statement Name1 address
- define first 0x003fa00
- Type statement with Name2
- signed int data
- Set Name2 Name1
- data first //May give warning
- data (signed int ) first // No warning
- Now use Name2 to point to data
- result data // writes result to mem loc
0x3fa00
7Question
- Where is the number 0x3fa00 stored?
- // does not define a variable
- define my_new_var 10
- // It only tells the C-preprocessor to replace
- // every occurrence of the text
- // my_new_var with the text 10
- // BE VERY CAREFUL WITH defines
8Pointer Method 2
- Method 2 One step
- Use type statement to define type, pointer and
location - signed int data (signed int ) (0x003fa000)
- Memory may also be altered without variables
- ((signed int )(0x003FA000))0xAAAA
- The next two slides demonstrate how this basic
concept can result in somewhat bizarre code - ADC QADC_A.RJURR0.R
- QADC_A.QACR1.B.MQ1 0x11
9ifndef _MPC565_H_ define _MPC565_H_ /Device
specific defines/ define _UC3F_FLASH define
_MIOS14 define _MIOS14_RTC define
_CALRAM define _QADC64E / Out of RESET the
QADC64E runs in enhanced mode. If legacy mode is
needed/ / uncomment out the following
line./ /define QADC64E_LEGACY/ include
"m_usiu.h" /also includes UIMB
module/ include "m_flash.h" include
"m_tpu3.h" /also includes DPTRAM
module/ include "m_qadc64.h" include
"m_qsmcm.h" include "m_mios.h" include
"m_toucan.h" /also includes DLC module
/ include "m_sram.h" define USIU ((
struct USIU_tag ) (INTERNAL_MEMORY_BASE
0x2FC000)) define UC3F_A (( struct UC3F_tag
) (INTERNAL_MEMORY_BASE 0x2FC800)) define
UC3F_B (( struct UC3F_tag )
(INTERNAL_MEMORY_BASE 0x2FC840)) define TPU_A
(( struct TPU3_tag ) (INTERNAL_MEMORY_BASE
0x304000)) define TPU_B (( struct TPU3_tag
) (INTERNAL_MEMORY_BASE 0x304400)) define
TPU_C (( struct TPU3_tag )
(INTERNAL_MEMORY_BASE 0x305C00)) define QADC_A
(( struct QADC64_tag ) (INTERNAL_MEMORY_BASE
0x304800)) define QADC_B (( struct
QADC64_tag ) (INTERNAL_MEMORY_BASE
0x304C00)) define QSMCM_A (( struct QSMCM_tag
) (INTERNAL_MEMORY_BASE 0x305000)) define
QSMCM_B (( struct QSMCM_tag )
(INTERNAL_MEMORY_BASE 0x305400)) define MIOS14
(( struct MIOS_tag ) (INTERNAL_MEMORY_BASE
0x306000)) define CAN_A (( struct TOUCAN_tag
) (INTERNAL_MEMORY_BASE 0x307080)) define
CAN_B (( struct TOUCAN_tag )
(INTERNAL_MEMORY_BASE 0x307480)) define CAN_C
(( struct TOUCAN_tag ) (INTERNAL_MEMORY_BASE
0x307880)) define UIMB (( struct UIMB_tag
) (INTERNAL_MEMORY_BASE 0x307F80)) define
CALRAM_A (( struct CALRAM_tag )
(INTERNAL_MEMORY_BASE 0x380000)) define
CALRAM_B (( struct CALRAM_tag )
(INTERNAL_MEMORY_BASE 0x380040)) define
DPTRAM6K (( struct DPTRAM_tag )
(INTERNAL_MEMORY_BASE 0x300000)) define
DPTRAM4K (( struct DPTRAM_tag )
(INTERNAL_MEMORY_BASE 0x300040)) define DLCMD2
(( struct DLCMD2_tag ) (INTERNAL_MEMORY_BASE
0x300080)) endif / ifndef _MPC565_H /
10 union VUINT8 R struct
VUINT8 PQA71 VUINT8
PQA61 VUINT8 PQA51
VUINT8 PQA41 VUINT8 PQA31
VUINT8 PQA21 VUINT8 PQA11
VUINT8 PQA01 B
PORTQA union VUINT8 R
struct VUINT8 PQB71
VUINT8 PQB61 VUINT8 PQB51
VUINT8 PQB41 VUINT8 PQB31
VUINT8 PQB21 VUINT8
PQB11 VUINT8 PQB01 B
PORTQB
- struct QADC64_tag
- union
- VUINT16 R
- struct
- VUINT16 STOP1
- VUINT16 FRZ1
- VUINT166
- VUINT16 SUPV1
- VUINT16 MSTR1
- VUINT16 EXTCLK1
- VUINT161
- VUINT16 IARB4
- B
- QADC64MCR
- union
- VUINT16 R
- struct
- VUINT16 IRL15
11ATMEL Technique
- Library functions
- __inline void AT91F_AIC_EnableIt (
- AT91PS_AIC pAic, // \arg pointer to the AIC
registers - unsigned int irq_id ) // \arg interrupt number
to initialize - // Enable the interrupt on the interrupt
controller - pAic-gtAIC_IECR 0x1 ltlt irq_id
- Structure Defs
- typedef struct _AT91S_AIC
- AT91_REG AIC_SMR32 // Source Mode
-
- AT91_REG AIC_IECR // Interrupt Enable
Command -
- AT91S_AIC, AT91PS_AIC
12ATMELs Method Cont.
- Include file
- define AT91C_BASE_SYS ((AT91PS_SYS)
0xFFFFF000) // (SYS) Base Address - define AT91C_BASE_AIC ((AT91PS_AIC)
0xFFFFF000) // (AIC) Base Address - USAGE
- main()
- AT91PS_AIC pAic
- pAic AT91C_BASE_AIC
13Embedded development sometimes requires
manipulating memory that has NOT been allocated
- int ptr_to_int (int )0x500
- This instruction allocates one 4 byte long region
of memory pointing toward another memory location
0x500 - What keeps the compiler from using 0x500 for
other purposes? - Choosing an arbitrary memory location is risky
- Choosing to point at the address of a peripheral
modules control and data registers allows one to
manipulate and read these registers.
14Quirks of Compilers
- Sometimes, variables are deemed unnecessary, and
removed to minimize the amount of code generated
and the speed of execution. - If lines of C code are significantly altered from
original, it is hard to debug. - TURN OPTIMIZATION OFF