ebook img

Programming 16-Bit PIC Microcontrollers in C: Learning to Fly the PIC 24 PDF

401 Pages·2007·6.06 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming 16-Bit PIC Microcontrollers in C: Learning to Fly the PIC 24

Programming 16-Bit PIC Microcontrollers in C Programming 16-Bit PIC Microcontrollers in C Learning to Fly the PIC24 By Lucio Di Jasio AMSTERDAM (cid:129) BOSTON (cid:129) HEIDELBERG (cid:129) LONDON NEW YORK (cid:129) OXFORD (cid:129) PARIS (cid:129) SAN DIEGO SAN FRANCISCO (cid:129) SINGAPORE (cid:129) SYDNEY (cid:129) TOKYO Newnes is an imprint of Elsevier Newnes is an imprint of Elsevier 30 Corporate Drive, Suite 400, Burlington, MA 01803, USA Linacre House, Jordan Hill, Oxford OX2 8DP, UK Copyright © 2007, Elsevier Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Permissions may be sought directly from Elsevier’s Science & Technology Rights Department in Oxford, UK: phone: (+44) 1865 843830, fax: (+44) 1865 853333, e-mail: [email protected]. You may also complete your request online via the Elsevier homepage (http://www.elsevier.com), by selecting “Customer Support” and then “Obtaining Permissions.” Recognizing the importance of preserving what has been written, Elsevier prints its books on acid-free paper whenever possible. Library of Congress Cataloging-in-Publication Data (Application submitted.) British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library. ISBN-13: 978-0-7506-8292-3 ISBN-10: 0-7506-8292-2 For information on all Newnes publications visit our Web site at www.books.elsevier.com 07 08 09 10 10 9 8 7 6 5 4 3 2 1 Printed in the United States of America Dedication To Sara Contents Preface .................................................................................................................xv Introduction ......................................................................................................xvii Who should read this book? ...................................................................................................xvii Structure of the book .............................................................................................................xviii What this book is not ...............................................................................................................xix Checklists ................................................................................................................................xix PART I CHAPTER 1: THE FIRST FLIGHT ...................................................................................3 Flight plan .............................................................................................................3 Prefl ight checklist .................................................................................................4 The fl ight ...............................................................................................................4 Compiling and linking ................................................................................................................6 Building the fi rst project .............................................................................................................7 PORT initialization .....................................................................................................................9 Retesting PORTA......................................................................................................................10 Testing PORTB .........................................................................................................................10 Post-fl ight briefi ng .............................................................................................12 Notes for assembly experts ...............................................................................13 Notes for PIC MCU experts ................................................................................14 Notes for C experts ............................................................................................14 Tips and tricks .....................................................................................................14 Exercises ..............................................................................................................15 Books .................................................................................................................15 Links .................................................................................................................15 CHAPTER 2: A LOOP IN THE PATTERN .....................................................................17 Flight plan ...........................................................................................................17 Prefl ight checklist ...............................................................................................17 The fl ight .............................................................................................................18 An animated simulation ............................................................................................................21 Using the Logic Analyzer ........................................................................................................24 Post-fl ight briefi ng .............................................................................................26 Notes for assembly experts ...............................................................................27 vii Contents Notes for PIC microcontroller experts ...............................................................27 Notes for C experts ............................................................................................27 Tips and tricks .....................................................................................................27 Exercises ..............................................................................................................28 Books .................................................................................................................28 Links .................................................................................................................28 CHAPTER 3: MORE PATTERN WORK, MORE LOOPS ...............................................29 Flight plan ...........................................................................................................29 Prefl ight checklist ...............................................................................................30 The fl ight .............................................................................................................30 Do Loops ..................................................................................................................................30 Variable declarations ................................................................................................................31 for loops .................................................................................................................................31 More loop examples .................................................................................................................32 Arrays .......................................................................................................................................33 A new demo ..............................................................................................................................34 Testing with the Logic Analyzer ...............................................................................................36 Using the Explorer16 demonstration board ..............................................................................37 Post-fl ight briefi ng .............................................................................................37 Notes for assembly experts ...............................................................................37 Notes for PIC microcontroller experts ...............................................................38 Notes for C experts ............................................................................................38 Tips and tricks .....................................................................................................38 Exercises ..............................................................................................................40 Books .................................................................................................................40 Links .................................................................................................................40 CHAPTER 4: NUMB3RS .............................................................................................41 Flight plan ...........................................................................................................41 Prefl ight checklist ...............................................................................................42 The fl ight .............................................................................................................42 On optimization (or lack thereof) .............................................................................................43 Testing ......................................................................................................................................44 Going long ................................................................................................................................44 Note on the multiplication of long integers ..............................................................................45 Long long multiplication ..........................................................................................................45 Floating point ............................................................................................................................46 Notes for C experts ............................................................................................46 Measuring performance ...........................................................................................................47 Post-fl ight briefi ng .............................................................................................49 Notes for assembly experts .......................................................................................................50 Notes for PIC microcontroller experts ...............................................................51 Tips and tricks .....................................................................................................51 viii Contents Math libraries ............................................................................................................................51 Complex data types ..................................................................................................................51 Exercises ..............................................................................................................52 Books .................................................................................................................52 Links .................................................................................................................52 CHAPTER 5: INTERRUPTS .........................................................................................53 Flight plan ...........................................................................................................53 Prefl ight checklist ...............................................................................................53 The fl ight .............................................................................................................54 Nesting of interrupts .................................................................................................................57 Traps .........................................................................................................................................57 A template and an example for Timer1 interrupt .....................................................................58 A real example with Timer1 .....................................................................................................59 Testing the Timer1 interrupt .....................................................................................................61 The secondary oscillator ...........................................................................................................63 The real-time clock calendar (RTCC) ......................................................................................64 Managing multiple interrupts ...................................................................................................64 Post-fl ight briefi ng .............................................................................................65 Notes for C experts ............................................................................................65 Notes for assembly experts ...............................................................................65 Notes for PIC microcontroller experts ...............................................................66 Tips and tricks .....................................................................................................66 Exercises ..............................................................................................................68 Books .................................................................................................................68 Links .................................................................................................................68 CHAPTER 6: TAKING A LOOK UNDER THE HOOD ..................................................69 Flight plan ...........................................................................................................69 Prefl ight checklist ...............................................................................................69 The fl ight ..................................................................................................................................70 Memory space allocation ..........................................................................................................71 Program space visibility ...........................................................................................................72 Investigating memory allocation ..............................................................................................73 Looking at the MAP .................................................................................................................77 Pointers .....................................................................................................................................79 The heap ...................................................................................................................................80 MPLAB C30 Memory Models .................................................................................................81 Post-fl ight briefi ng .............................................................................................81 Notes for C experts ............................................................................................81 Notes for assembly experts ...............................................................................82 Notes for PIC microcontroller experts ...............................................................82 Tips and tricks .....................................................................................................82 Exercises ..............................................................................................................83 ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.