programming - neff siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · atxmega128a1 (3) •alternate...
TRANSCRIPT
![Page 1: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/1.jpg)
Programming
Networks and Embedded Software
Introduction
by Wolfgang Neff
![Page 2: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/2.jpg)
ATxmega128A1 (1)
• Atmel AVR 8-bit Microcontroller
– Introduced 2009
– Harvard RISC architecture
– 32 registers (8 bit)
– 138 machine code instructions
– 100 multiplexed and configurable pins
– Flash and SRAM integrated on chip
– Wide range of peripheral modules
07-Nov-19 Embedded Programming 2
![Page 3: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/3.jpg)
ATxmega128A1 (2)
• Block Diagram
– Power lines
– I/O Ports
• A, B, C, D, E, F, …
– Serial Ports
• USART, SPI, I²C
– Converter
• ADC, DAC
– Timer etc.
07-Nov-19 Embedded Programming 3
Timer USART SPI I²CPort
sP
WR
AD
CD
AC
EEPROM
Flash SRAM
CPU
Inte
rru
pts
![Page 4: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/4.jpg)
ATxmega128A1 (3)
• Alternate Port Functions
– Pins are multiplexed (consult datasheet for details)
07-Nov-19 Embedded Programming 4
Mic
roco
ntr
olle
r
PC7
SYNC
TXD1
SCK
MU
X Pin 7
PC6
SYNC
RXD1
MISO
MU
X Pin 6
Port INT USARTC1 SPIC
PC0 SYNC
PC1 SYNC
PC2 SYNC
PC3 SYNC
PC4 SYNC SS
PC5 SYNC XCK1 MOSI
PC6 SYNC RXD1 MISO
PC7 SYNC TXD1 SCK
![Page 5: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/5.jpg)
ATxmega128A1 (4)
• Peripheral Modules
– Peripherals are organised as modules
– Modules have several instances
• Eight timers, eight USARTs, four SPIs etc.
– Modules and instances are like classes and objects
– Example
• PORTA.DIR PORTE.DIRPORTA.OUT PORTE.OUT
07-Nov-19 Embedded Programming 5
Two Instances of Module Port Two Registers of Ports
![Page 6: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/6.jpg)
C Programming (1)
• Code file organization
Header File(Interface)
Source Code(Implementation)
Program(Executable)
07-Nov-19 Embedded Programming 6
![Page 7: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/7.jpg)
C Programming (2)
• Code generation
07-Nov-19 Embedded Programming 7
Program(Executable)
IDE(compiler & linker)
Header Files#include <…>
Object Files
System files
Header Files#include "…"
Source Files
User files
![Page 8: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/8.jpg)
C Programming (3)
• Header file
#ifndef ADD_H_#define ADD_H_
#define FIVE 5#define ADD(x,y) x+y
extern int result;void add5(int x);
#endif /* ADD_H_ */
Preprocessor directives
Definition of a constant value
Definition of a macro
Declaration of a global variable
Declaration of a function
07-Nov-19 Embedded Programming 8
![Page 9: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/9.jpg)
Programming C (4)
• Source file
#include "add.h"
int result;
void add5(int x) {result = ADD(x,FIVE);
}
Include header file
Definition of the global variable
Implementation of the function
07-Nov-19 Embedded Programming 9
![Page 10: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/10.jpg)
Programming C (5)
• Main file
#include <avr/io.h>#include "add.h"
int main(void) {
add5(4);
while (1) {/* do something */
}
}
Include a user header file
Main program
Call function declared in add.h
Infinite main loop(embedded systems, only)
(result now in global variable)
Include a system header file
No good idea!
07-Nov-19 Embedded Programming 10
![Page 11: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/11.jpg)
C Programming (6)
• Data types
Standard C Alias Embedded Systems* Size
signed char int8_t 8 Bits
unsigned char char uint8_t 8 Bits
signed short int int16_t 16 Bits
unsigned short uint16_t 16 Bits
signed long int32_t 32 Bits
unsigned long uint32_t 32 Bits
float 32 Bits
double (* Defined in <stdint.h>) 64 Bits
07-Nov-19 Embedded Programming 11
![Page 12: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/12.jpg)
C Programming (7)
• Indirect addressing with pointers
*p = 5; // indirect
int x = 4; // immediate
int y = x; // direct
int *p = &y;
SRAM0108hex…0107hex4y:0106hex?0105hex?0104hex?0103hex107hexp:0102hex4x:0101hex…
SRAM0108hex…0107hex5y:0106hex?0105hex?0104hex?0103hex107hexp:0102hex4x:0101hex…
p points to y
07-Nov-19 Embedded Programming 12
![Page 13: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/13.jpg)
C Programming (8)
• Input / Output
– Special drivers
• E. g. usart_put, usart_get
– Standard input / output
• #include <stdio.h>
• Simple input / output– Characters: getchar, putchar
– Strings: gets, puts
• Complex input / output– scanf / printf
07-Nov-19 Embedded Programming 13
![Page 14: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/14.jpg)
C Programming (9)
• Formatted output
– int printf(const char *format, ...);
– Format Specifiers
• %d decimal signed integer
• %x hex integer
• %u unsigned integer
• %c character
• %s string
• %p pointer
07-Nov-19 Embedded Programming 14
![Page 15: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/15.jpg)
C Programming (10)
• Example
– char s = 'A'; // define sensoruint16_t d = adc_read(s); // read sensorint v = d / 15; // convert to voltage
– printf("Sensor %c read '%x'. This are %d V.",s,d,v);
– Result:
• Sensor A read 'A1'. This are 10 V.
• Drawback
– Very expensive – storage and performance
07-Nov-19 Embedded Programming 15
![Page 16: Programming - Neff Siteneff.site/courses/nwes/nwes_5/0.2_intro.pdf · ATxmega128A1 (3) •Alternate Port Functions –Pins are multiplexed (consult datasheet for details) 07-Nov-19](https://reader034.vdocuments.net/reader034/viewer/2022042122/5e9cfe16e4b3113a007cb6ab/html5/thumbnails/16.jpg)
C Programming (11)
• Volatile values
– Not under the control of the C compiler
– Change asynchronously
– Optimization would be harmful
– Example
• volatile uint8_t i = USART.DATA;volatile uint8_t j = USART.DATA;if (i != j) { … } // has USART.DATA changed?Dead code is optimized away if volatile is omitted.
07-Nov-19 Embedded Programming 16