© 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 Programming1
© 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 Programming2
© AJH 2013, V2.0TIC-PIC Application Trainer3
© AJH 2013, V2.0TIC-PIC Application Trainer4 TIC-PIC Block Diagram B5 B4 B3 B2 B1 B0 C4 C3 C5 C1 C0 C2 A4 A5 +5V Reset PIC18F MHz Osc2 Osc1 MAX 232A C7/Rx C6/Tx RS-232 A3 A2 A1 A0 x8 D7 D0 dp g +5V CC AN7 Temp +5V AN6 LDR +5V AN5 Pot
© AJH 2013, V2.0TIC-PIC Application Trainer5 PIC Port Connections RC7 RC6 RC5 RC4 RC3 RC2 RC1 RC0 PORTC
© AJH 2013, V2.0TIC-PIC Application Trainer6 “Hello World” Write a C program that prints “Hello World” on the screen #include // header defining printf( ) etc int main(void) // main returns successful code to OS { printf(“Hello World\n”); return 0; // return “success” to OS }
© AJH 2013, V2.0TIC-PIC Application Trainer7 Embedded “Hello World” #include // 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=0b ; // program port as output while(1){ // infinite loop PORTC=0b ; // alternate LEDs on PORTC=0b ; // swap over (only 6 LSB available) } // note non-standard binary notation }
© AJH 2013, V2.0TIC-PIC Application Trainer8 #include defines port and other identifiers related to the PIC architecture (e.g. PORTC) library not usually relevant to embedded applications
© AJH 2013, V2.0TIC-PIC Application Trainer9 #pragma config... Initialises processor configuration fuses –OSC=HShigh speed crystal oscillator –PWRT=ONpower up timer on –WDT=OFFwatchdog timer off –LVP=OFFlow voltage programming off –PBADEN=OFFport B analog enable off –DEBUG=ONdebugging enabled (PICkit2)
© AJH 2013, V2.0TIC-PIC Application Trainer10 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.
© AJH 2013, V2.0TIC-PIC Application Trainer11 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(;;)
© AJH 2013, V2.0TIC-PIC Application Trainer12 PORTC = 0b ; Ports are ‘memory mapped’ Ports are treated like variables PORTC = 0b ; // output data x = PORTB; // input data PORTC = x; // output variable
© AJH 2013, V2.0TIC-PIC Application Trainer13 TRISC = 0b ; DDRC = 0b ; 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
© AJH 2013, V2.0TIC-PIC Application Trainer14 I/O Port Block Diagram - Output
© AJH 2013, V2.0TIC-PIC Application Trainer15 I/O Port Block Diagram - Input
© AJH 2013, V2.0TIC-PIC Application Trainer16 Configuring ports Write down the statements to configure: –Port A, 6 LSB = inputs TRISA = 0b ; –Port B, 6 LSB = outputs TRISB = 0; or TRISB = 0b ; –Port C, 4 LSB = outputs, 4 MSB = inputs TRISC = 0b ;
© AJH 2013, V2.0TIC-PIC Application Trainer17 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
© AJH 2013, V2.0TIC-PIC Application Trainer18 Final Program including Delay #include // 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=0b ; // program port as output for(;;) // infinite loop { PORTC=0b ; // alternate LEDs on delay(); PORTC=0b ; // 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 }