chapter 1 the first flight

17
Chapter 1 Chapter 1 The First Flight The First Flight Creating the first project and saying “Hello to the Embedded World” Part 2

Upload: leanne

Post on 12-Jan-2016

20 views

Category:

Documents


0 download

DESCRIPTION

Chapter 1 The First Flight. Creating the first project and saying “ Hello to the Embedded World ” Part 2. Debugging and the Watches window. I/O ports. Debugging PORTA. int main( void) { PORTA = 0xff; TRISA = 0; // all PORTA pins output return 0; } - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 1  The First Flight

Chapter 1Chapter 1 The First Flight The First Flight

Creating the first project and saying “Hello to the Embedded World”

Part 2

Page 2: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Debugging and the Watches windowDebugging and the Watches window

Page 3: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

I/O portsI/O ports

Page 4: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Debugging PORTADebugging PORTAint main( void) { PORTA = 0xff; TRISA = 0; // all PORTA pins output return 0;}

Debugging functions: Build Project for debugging and enter Debug Mode: Exit Debug Mode Step Over Run (Debug Mode)

Page 5: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Configuring the PIC24Configuring the PIC24/*** config.h** ** "Flying PIC24" projects device configuration*/#include <xc.h> _CONFIG1( JTAGEN_OFF // disable JTAG interface & GCP_OFF // disable general code protection & GWRP_OFF // disable flash write protection & ICS_PGx2 // ICSP interface (2=default) & FWDTEN_OFF) // disable watchdog timer _CONFIG2( IESO_OFF // two speed start up disabled & FCKSM_CSDCMD // disable clk-swithcing/monitor & FNOSC_PRIPLL // primary oscillator: enable PLL & POSCMOD_XT) // primary oscillator: XT mode

Page 6: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Creating an Creating an IncludeInclude folder folder Create a folder called “include” at the top

level of your working directory (Flyingpic24) Save the config.h file in it Example:

FlyingPIC24/ include/ config.h 1-HelloWorld.X/ Hello1.c nbproject/ ...

To calculate a path, start from the nbproject folder:

../../include

To calculate a path, start from the nbproject folder:

../../include

Page 7: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Add the Add the Include PathInclude Path Add the Include Path to the project properties

2-Select the Preprocessing and Messages options

2-Select the Preprocessing and Messages options

3-Provide a path to the Include folder

3-Provide a path to the Include folder

1-Select the xc16-gcc options1-Select the xc16-gcc options

Page 8: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Hello2.c – Using Hello2.c – Using config.hconfig.h/*** Hello Embedded World**** Hello2.c controlling PORTA pin direction*/#include <config.h> // set the configuration bits int main( void){ PORTA = 0xff; TRISA = 0; // configure all PORTA pins as outputs return 0;}

Page 9: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Hello3.c – Using Port BHello3.c – Using Port B/*** Hello Embedded World**** Hello3.c Testing PORTB, another surprise!*/#include <config.h>

int main( void){ PORTB = 0xff; TRISB = 0; // configure all PORTB pins as outputs return 0;}

Page 10: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

AD1PCFG registerAD1PCFG register

Page 11: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Hello4.c – Port B under ControlHello4.c – Port B under Control/*** Hello Embedded World**** Hello4.c learning to control the Analog Pins*/#include <config.h> int main( void){ PORTB = 0xff; AD1PCFG = 0xffff; // all PORTB as digital TRISB = 0; // all PORTB as output return 0;}

Page 12: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Notes for Assembly ExpertsNotes for Assembly Experts You can still take a look at the assembly code

generated by the compiler Learn to trust it!

Page 13: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Meet the Meet the DashboardDashboard All the information about your project at a

glance

Page 14: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Tips and TricksTips and Tricks Interfacing to 5V input and output signals is

possible with some caution: Digital Input pins are 5V tolerant Digital Output pins can be configured as Open Drain Use the ODCx registers to configure an output pin for

Open Drain mode. Watch Out! Pins that are multiplexed with analog

functions are NOT 5V tolerant!

Page 15: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Suggested ExcercisesSuggested Excercises To test the PORTB example (Hello4.c):

Connect a Voltmeter to pin RB0 Watch the needle move as you single step through the code.

Page 16: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Recommended ReadingsRecommended ReadingsKernighan, B. & Ritchie, D.The C Programming LanguagePrentice-Hall, Englewood Cliffs, NJ

When you read or hear a programmer talk about the “K&R” … they mean this book!

Also known as “the white book”, the C language has evolved quite a bit since the first edition was published in 1978!

The second edition (1988) includes the more recent ANSI C standard definitions of the language

The MPLAB C32 compiler adheres to the ISO/IEC 9899:1990 (also known as C90) standard

Page 17: Chapter 1  The First Flight

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)

Online ResourcesOnline Resources http://en.wikibooks.org/wiki/C_Programming

This is a Wiki-book on C programming and as such it is a bit of a work in progress. It’s convenient if you don’t mind doing all your reading online.

Hint: look for the chapter called “A taste of C” to find the omnipresent “Hello World!” example.

http://publications.gbdirect.co.uk/c_book/ This is the online version of The C Book, second

edition by Mike Banahan, Declan Brady and Mark Doran, originally published by Addison Wesley in 1991. This version is made freely available.