© AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system...

18
© AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port access, bitwise manipulation. Hexadecimal, Binary, two’s complement representation and arithmetic. Computer Programming 1

Transcript of © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system...

Page 1: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0

Computer Programming

Embedded Systems Programming•Stand-alone code, system initialisation, operating systems.

•Low-level operations; port access, bitwise manipulation.

•Hexadecimal, Binary, two’s complement representation and arithmetic.

Computer Programming 1

Page 2: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0

What is … ?

• … an Embedded System? An electronic system that contains an integral processor,

programmed to perform a dedicated task

• … Real Time Software? Correct operation depends not only on logical correctness

of computation but also time at which result is produced

• … a microcontroller? A complete processor on a single IC, containing CPU,

RAM, ROM and Input/Output

Computer Programming 2

Page 3: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 3

TIC-PIC Application Trainer

Page 4: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 4

TIC-PIC Block DiagramB5B4B3B2B1B0

C4C3

C5

C1C0

C2

A4

A5

+5V

Reset

PIC18F4520

11.0592 MHz

Osc2

Osc1

MAX232A

C7/RxC6/Tx

2

3

5

RS-232

A3A2A1A0

x8D7

D0

dp

g

+5V

C AN7Temp

+5V

AN6LDR

+5V

AN5Pot

Page 5: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 5

PIC Port Connections

RC7RC6RC5RC4RC3RC2RC1RC0

PORTC

Page 6: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 6

“Hello World”

Write a C program that prints “Hello World” on the screen

#include <stdio.h> // header defining printf( ) etc

int main(void) // main returns successful code to OS{ printf(“Hello World\n”); return 0; // return “success” to OS}

Page 7: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 7

Embedded “Hello World”#include <p18F4520.h> // Processor specific header#pragma config

OSC=HS,PWRT=ON,WDT=OFF,LVP=OFF,PBADEN=OFF,DEBUG=ON

void main(void) // note void parameter/return{ TRISC=0b10000000; // program port as output while(1){ // infinite loop PORTC=0b00010101; // alternate LEDs on PORTC=0b00101010; // swap over (only 6 LSB available) } // note non-standard binary notation}

Page 8: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 8

#include <p18F4520.h>

• <p18F4520.h> defines port and other identifiers related to the PIC architecture(e.g. PORTC)

• <stdio.h> library not usually relevant to embedded applications

Page 9: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 9

#pragma config ...

• Initialises processor configuration fuses– OSC=HS high speed crystal oscillator– PWRT=ON power up timer on– WDT=OFF watchdog timer off– LVP=OFF low voltage programming off– PBADEN=OFF port B analog enable off– DEBUG=ON debugging enabled (PICkit2)

Page 10: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 10

void main(void)

• In an embedded application there is no Operating System to which a value can be returned.

As a result main( ) returns void.

Page 11: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 11

while(1)

• Embedded program ‘has no end’

• While(1)produces an infinite loop which repeats the statements between { and } indefinitely.

• Alternative is while(TRUE) or for(;;)

Page 12: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 12

PORTC = 0b00010101;

• Ports are ‘memory mapped’

• Ports are treated like variables

PORTC = 0b00010101; // output datax = PORTB; // input dataPORTC = x; // output variable

Page 13: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 13

TRISC = 0b10000000;DDRC = 0b10000000;

• Ports are programmable and must be configured before use.

• TRIS registers control tristate output buffers (also called DDR)

• A 1 in TRIS programs port bit as Input

• A 0 in TRIS programs port bit as Output

• Non ANSI-standard binary constants

Page 14: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 14

I/O Port Block Diagram - Output

x

0

Input buffer

TRISC bit 0 latch

PORTC bit 0 latch

Tri-state buffer - Enabled

I/O port pin: PORTC bit 0

x

Page 15: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 15

I/O Port Block Diagram - Input

?

1

Input buffer

TRISC bit 0 latch

PORTC bit 0 latch

Tri-state buffer - Disabled

I/O port pin: PORTC bit 0

x

x

Page 16: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 16

Configuring ports

• Write down the statements to configure:

– Port A, 6 LSB = inputsTRISA = 0b00111111;

– Port B, 6 LSB = outputsTRISB = 0; or TRISB = 0b00000000;

– Port C, 4 LSB = outputs, 4 MSB = inputsTRISC = 0b11110000;

Page 17: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 17

Time Delays

• A delay is produced by ‘doing nothing’ a large number of times, using an empty for loop

void delay(void){ unsigned int d; for(d=10000;d>0;d--); // empty loop} // creates a delay

Page 18: © AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.

© AJH 2013, V2.0TIC-PIC Application Trainer 18

Final Program including Delay#include <p18F4520.h> // Processor specific header#pragma config OSC=HS,PWRT=ON,WDT=OFF,LVP=OFF,PBADEN=OFF,DEBUG=ON

void delay(void); // function prototype

void main(void) // note void parameter{ TRISC=0b10000000; // program port as output for(;;) // infinite loop { PORTC=0b00010101; // alternate LEDs on delay(); PORTC=0b00101010; // swap over (only 6 LSB available) delay(); } // note non-standard binary notation}

void delay(void){ unsigned int d; for(d=10000;d>0;d--); // empty loop creates a delay}