programming microcontroller assembly and c · pdf file prerequisite for programming a...
TRANSCRIPT
![Page 1: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/1.jpg)
www.telkomuniversity.ac.id
Programming Microcontroller Assembly and C
Course Number : TTH2D3
CLO : 2
Week : 5-7 CLO#2 Student have the knowledge to create basic programming for microcontroller [C3] Understand how to program in Assembly [C3] Understand how to program a microcontroller using C [C3] Understand how to store the program in microcontroller
![Page 2: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/2.jpg)
www.telkomuniversity.ac.id
development board development board
microcontroller microcontroller
What is the difference between P, C, and Development Board?
microprocessor microprocessor
CU CU Register Register
ALU ALU memory memory
Flash Flash SRAM SRAM EEPROM EEPROM
Programmable Input / Output Programmable Input / Output
Programmable Interrupt Controller
Programmable Interrupt Controller
Programmable Timer / Counter Programmable
Timer / Counter
External Memory External Memory
Power Regulator
Power Regulator
(Crystal) Clock Regulator
(Crystal) Clock Regulator
Pin
Co
nn
ecto
r P
in C
on
nec
tor
![Page 3: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/3.jpg)
www.telkomuniversity.ac.id
MICROCONTROLLER (C) AVR ATMEGA 8535
![Page 4: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/4.jpg)
www.telkomuniversity.ac.id
ATMega 8535 Architecture
• 4 I/O port (4x8) Port A, B, C and D
• ADC (Analog to Digital Converter)
• 3 Timer/Counter
• 32 register
• 512 byte SRAM
• 8kb Flash memory
• Internal and external interrupt
• SPI interface port to download program into flash
• 512 byte EEPROM
• Analog comparator interface
• USART port for serial communication
![Page 5: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/5.jpg)
www.telkomuniversity.ac.id
Prerequisite for Programming a Microcontroller
• Before you start to program a C, you need to understand 2 things:
1. The Instruction Set of the C
2. The hardware capability of the C
• Most C comes with an IDE (Integrated Development Environment) to build and run your program
• For Atmel C, you may choose either Assembly or C for your programming language
![Page 6: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/6.jpg)
www.telkomuniversity.ac.id
Instruction Set
ATMega 32
![Page 7: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/7.jpg)
www.telkomuniversity.ac.id
What is Instruction Set?
• Instruction is like a command to be executed by Control Unit within the P
• Instruction is usually grouped by 4 types:
– Data transfer (ex. MOV)
– Arithmetic and Logic (ex. ADD)
– Control Transfer (ex. JMP)
– Miscellaneous (ex. NOP)
• Every P has their own instruction
• Instruction set is the list of instruction understood only by a specific P
![Page 8: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/8.jpg)
www.telkomuniversity.ac.id
Basic Instruction (1/2)
• LDI (Load Immediate): writes a constant into a register ex. LDI R16,0xFF (writes FFH into register 16)
• OUT: writes data from register to a specific I/O port ex. OUT DDRA,R16 (writes data from register 16 to port A)
• IN: reads data from a specific I/O port into a register ex. IN R16, PORTA (reads data from port A and stores it to register 16)
• SBI (Set bit in I/O): set (put into High Voltage) a bit in I/O port ex. SBI PORTA,0 (set bit 0 in port A to High Voltage)
• CBI (Clear bit in I/O): unset (put into Low Voltage) a bit in I/O port ex. CBI PORTA,1 (set bit 1 in port A to Low Voltage)
![Page 9: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/9.jpg)
www.telkomuniversity.ac.id
Basic Instruction 2/2
• SBIS (Skip if bit in I/O is set): skip 1 instruction below if a specific bit in I/O port is set (High Voltage) ex. SBIS PORTA,2 RJMP RPT “RJMP RPT” will be skipped if bit 2 in port A is High Voltage
• SBIC (Skip if bit in I/O is cleared): skip 1 instruction below if a specific bit in I/O port is unset (Low Voltage) ex. SBIC PORTA,2 RJMP RPT “RJMP RPT” will be skipped if bit 2 in port A is Low Voltage
![Page 10: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/10.jpg)
www.telkomuniversity.ac.id
Arithmetic Instruction (1/4)
![Page 11: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/11.jpg)
www.telkomuniversity.ac.id
Arithmetic Instruction (2/4)
![Page 12: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/12.jpg)
www.telkomuniversity.ac.id
Arithmetic Instruction (3/4)
![Page 13: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/13.jpg)
www.telkomuniversity.ac.id
Arithmetic Instruction (4/4)
![Page 14: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/14.jpg)
www.telkomuniversity.ac.id
Branch Instruction (1/4)
![Page 15: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/15.jpg)
www.telkomuniversity.ac.id
Branch Instruction (2/4)
![Page 16: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/16.jpg)
www.telkomuniversity.ac.id
Branch Instruction (3/4)
![Page 17: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/17.jpg)
www.telkomuniversity.ac.id
Branch Instruction (4/4)
![Page 18: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/18.jpg)
www.telkomuniversity.ac.id
Data Transfer Instruction (1/4)
![Page 19: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/19.jpg)
www.telkomuniversity.ac.id
Data Transfer Instruction (2/4)
![Page 20: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/20.jpg)
www.telkomuniversity.ac.id
Data Transfer Instruction (3/4)
![Page 21: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/21.jpg)
www.telkomuniversity.ac.id
Data Transfer Instruction (4/4)
![Page 22: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/22.jpg)
www.telkomuniversity.ac.id
Bit Test Instruction
![Page 23: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/23.jpg)
www.telkomuniversity.ac.id
Control Instruction
![Page 24: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/24.jpg)
www.telkomuniversity.ac.id
Assembly Example
![Page 25: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/25.jpg)
www.telkomuniversity.ac.id
Hardware Capability
![Page 26: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/26.jpg)
www.telkomuniversity.ac.id
Port A Port B
Port D Port C
ATMega 32: Pin Layout
![Page 27: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/27.jpg)
www.telkomuniversity.ac.id
Mega32/Mega16
(XCK/T0) PB0
(T1) PB1
(INT2/AIN0) PB2
(OC0/AIN1) PB3
(SS) PB4
(MOSI) PB5
(MISO) PB6
(SCK) PB7
RESET
VCC
XTAL2
GND
XTAL1
(RXD) PD0
(TXD) PD1
(INT0) PD2
(INT1) PD3
(OC1B) PD4
(OC1A) PD5
(ICP) PD6
PA0 (ADC0)
PA1 (ADC1)
PA2 (ADC2)
PA3 (ADC3)
PA4 (ADC4)
PA5 (ADC5)
PA6 (ADC6)
PA7 (ADC7)
AREF
AGND
PC7 (TOSC2)
AVCC
PC6 (TOSC1)
PC5 (TDI)
PC4 (TDO)
PC3 (TMS)
PC2 (TCK)
PC1 (SDA)
PC0 (SCL)
PD7 (OC2)
DDRAPORTA
PINA
DD
RB
PIN
B
PO
RT
B
DDRCPORTC
PINC
ATMega32 Pin out & Descriptions
![Page 28: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/28.jpg)
www.telkomuniversity.ac.id
Port Description
![Page 29: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/29.jpg)
www.telkomuniversity.ac.id
ATMega32 Pin out & Descriptions
![Page 30: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/30.jpg)
www.telkomuniversity.ac.id
Single pin accessing
![Page 31: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/31.jpg)
www.telkomuniversity.ac.id
Example
![Page 32: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/32.jpg)
www.telkomuniversity.ac.id
Using C Language to Program your Microcontroller
![Page 33: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/33.jpg)
www.telkomuniversity.ac.id
An Example of ATMEL Program in C Language
![Page 34: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/34.jpg)
www.telkomuniversity.ac.id
Comment
• Ignored by compiler but very useful for other to understand the program
/* for writing comment in a paragraf */
// for writing a 1 line comment
![Page 35: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/35.jpg)
www.telkomuniversity.ac.id
Preprocessor (6-10)
• Preprocessor #include can be used to attach a library function (h header file) so we may use many built in functions
• Header “io.h” contains definition for SFR (Special Function Register) and all pins and bits in C
• Preprocessor #define is used for defining a constant or macro
![Page 36: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/36.jpg)
www.telkomuniversity.ac.id
Variable Declaration
• Variable is used to store a value within a program
• A global variable is defined outside any function and can be accessed by all functions
• A local variable is defined inside a function and can only be accessed by that function
• How to define variable: – DataType VariableName;
![Page 37: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/37.jpg)
www.telkomuniversity.ac.id
Function Prototype
• Used to define a function to be called by other function (usually by main function)
• How to define function: – DataType FunctionName (DataType
Parameter1, DataType Parameter 2)
![Page 38: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/38.jpg)
www.telkomuniversity.ac.id
Main Function
• The first function to be executed starting from the first line
• How to call a function from main function: – Without return value and without input parameter function()
– With return value but without input parameter variable = functionName();
– With return value and with input parameter variable = functionName(variable_or constant)
![Page 39: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/39.jpg)
www.telkomuniversity.ac.id
Sub-Program or Function
• Function is a sub module to solve a specific problem (ex. calculate factorial)
• How to define a function: – Without return value and without input parameter void function(void)
– With return value but without input parameter DataType functionName(void);
– With return value and with input parameter DataType functionName(DataType parameter1, ...)
![Page 40: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/40.jpg)
www.telkomuniversity.ac.id
Data Type
Data Type Byte Bit min Max
char 1 8 -128 127
signed char 1 8 -128 127
unsigned char 1 8 0 255
Int 2 16 -32768 32767
signed int 2 16 -32768 32767
unsigned int 2 16 0 65535
long 4 32 -2147483648 2147483647
signed long 4 32 -2147483648 2147483647
unsigned long 4 32 0 4294967295
float 4 32 1,28E-38 3,4E38
![Page 41: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/41.jpg)
www.telkomuniversity.ac.id
ATMEL Studio
• Please try to explore the ATMEL Studio using provided example projects
![Page 42: Programming Microcontroller Assembly and C · PDF file Prerequisite for Programming a Microcontroller •Before you start to program a C, you need to understand 2 things:](https://reader031.vdocuments.mx/reader031/viewer/2022022001/5a788d9e7f8b9a87198d04ff/html5/thumbnails/42.jpg)
www.telkomuniversity.ac.id
See you on next class