1. Trang chủ
  2. » Tất cả

the pic microcontroller - your personal introductory course 3rd ed_3

239 481 6

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 239
Dung lượng 1,25 MB

Nội dung

The PIC Microcontroller H6664-Prelims.qxd 8/13/05 12:32 PM Page i To Mum & Dad H6664-Prelims.qxd 8/13/05 12:32 PM Page ii The PIC Microcontroller: Your Personal Introductory Course Third edition John Morton AMSTERDAM • BOSTON • HEIDELBERG • LONDON • NEW YORK • OXFORD PARIS • SAN DIEGO • SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO Newnes is an imprint of Elsevier H6664-Prelims.qxd 8/13/05 12:32 PM Page iii Newnes An imprint of Elsevier Linacre House, Jordan Hill, Oxford OX2 8DP 30 Corporate Drive, Burlington, MA 01803 First published 1998 Second edition 2001 Third edition 2005 Copyright © 1998, 2001, 2005, John Morton. All rights reserved The right of John Morton to be identified as the author of this work has been asserted in accordance with the Copyright, Designs and Patents Act 1988 No part of this publication may be reproduced in any material form (including photocopying or storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication) without the written permission of the copyright holder except in accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London, England W1T 4LP. Applications for the copyright holder’s written permission to reproduce any part of this publication should be addressed to the publisher. Permissions may be sought directly from Elsevier’s Science and Technology Rights Department in Oxford, UK; phone: (ϩ44) (0) 1865 843830; fax: (ϩ44) (0) 1865 853333; e-mail: permissions@elsevier.co.uk. You may also complete your request on-line via the Elsevier Science homepage (http://www.elsevier.com), by selecting ‘Customer Support’ and then ‘Obtaining Permissions’. British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library ISBN 0 7506 66641 Typeset by Charon Tec Pvt. Ltd, Chennai, India www.charontec.com Printed and bound in Great Britain For information on all Newnes publications visit our web site at www.newnespress.com H6664-Prelims.qxd 8/13/05 12:32 PM Page iv Contents Acknowledgements ix Preface to the third edition xi 1 Introduction 1 Some tips before starting 2 Binary, decimal and hexadecimal 2 An 8-bit system 5 Initial steps 5 Choosing your PIC microcontroller 6 Writing 10 Assembling 10 The file registers 10 A program template 13 2 Exploring the PIC5x series 20 Your first program 20 Configuration bits 22 Testing the program 23 Simulating 23 Emulating 23 Blowing the PIC microcontroller 24 Hardware 24 Using the testing instructions 29 Timing 32 Seven-segment displays 44 The program counter 46 Subroutines and the stack 48 Logic gates 65 The watchdog timer 69 Final instructions 73 The STATUS file register 74 The carry and digit carry flags 75 Pages 76 What caused the reset? 79 Indirect addressing 80 Some useful (but not vital) tricks 82 Final PIC5x program – ‘Bike buddy’ 85 H6664-Prelims.qxd 8/13/05 12:32 PM Page v 3 The PIC12F50x series (8-pin PIC microcontrollers) 90 Differences from the PIC16F54 90 The STATUS register 90 The OSCCAL register 91 Inputs and outputs 92 The OPTION register 92 The TRIS register 93 The general purpose file registers 93 The MCLR 93 Configuration bits 93 Example project: ‘PIC dice’ 94 Random digression 95 4 Intermediate operations using the PIC12F675 100 The inner differences 101 The OPTION and WPU registers 102 The TRISIO register 103 Calibrating the internal oscillator 103 PCLATH: Higher bits of the program counter 104 Remaining differences 105 Interrupts 105 INTCON 106 The interrupt service routine 107 Interrupts during sleep 109 Maintaining the STATUS quo 109 New program template 110 Example project: ‘Quiz game controller’ 112 EEPROM 116 EECON1 116 Reading from the EEPROM 116 Writing to the EEPROM 117 Example project: ‘Telephone card chip’ 118 Further EEPROM examples: Music maker 122 Power monitor 122 Analogue to digital conversion 122 ADCON0 123 ANSEL: Analogue select register 124 A/D conversion interrupt 125 Example project: ‘Bath monitor’ 125 Comparator module 129 Voltage reference 130 Comparator interrupts 130 Comparator example: ‘Sun follower’ 131 Comparator example: Reading many buttons from one pin 132 Final project: Intelligent garden lights 134 vi Contents H6664-Prelims.qxd 8/13/05 12:32 PM Page vi 5 Advanced operations and the future 138 Extra timers: TMR1 & … 138 Capture/Compare/PWM 139 USART: Serial communication 140 Programming tips 142 6 A PIC development environment 143 7 Sample programs 145 Program A LedOn – Turns an LED on 145 Program B PushButton (1.0) – If a push button is pressed, turns on an LED 146 Program C PushButton (2.0) – Shorter version of PushButton 1.0 147 Program D Timing – LED states toggled every second, and buzzer on every five seconds 148 Program E Traffic – Pedestrian traffic lights junction is simulated 150 Program F Counter (1.0) – Counts signals from a push button, resets after 16 152 Program G Counter (2.0) – Stop reading button twice (otherwise, as Counter 1.0) 154 Program H Counter (3.0) – Solves button bounce (otherwise, as Counter 2.0) 156 Program I StopClock – A stop clock displaying tenths of seconds to minutes 158 Program J LogicGates – Acts as the eight different gates 162 Program K Alarm – An alarm system which can be set or disabled 164 Program L BikeBuddy – A speedometer and mileometer for bikes 165 Program M PIC Dice – A pair of dice are simulated 171 Program N Quiz – Indicates which of three push buttons has been pressed first 175 Program O Phonecard – To act like a phonecard which decrements a file register 177 Program P TempSense – Displays whether temperature is too hot, too cold or OK 181 Program Q 183 Appendix A Specifications of some Flash PIC microcontrollers 189 Appendix B Pin layouts of some Flash PIC microcontrollers 191 Appendix C Instructions glossary 192 Appendix D Number system conversion 195 Appendix E Bit assignments of various file registers 196 Appendix F If all else fails, read this 203 Appendix G Contacts and further reading 204 Appendix H PICKit™ 1 & BFMP Info 205 Appendix I Answers to the exercises 207 Appendix J Some BASIC commands in assembly 222 Index 223 Contents vii H6664-Prelims.qxd 8/13/05 12:32 PM Page vii This page intentionally left blank Acknowledgements Max Horsey, Head of Electronics at Radley College in Abingdon and a great driving force for technological advancement, first introduced me to PIC micro- controllers in 1995. With the help of Philip Clayton I was shown a new concept in circuit design which opened up the possibility of new and more elaborate electronic devices. I would like to take this opportunity to thank all those who have contributed, directly or indirectly, to make this book possible. First I must thank Richard Morgan, Warden of Radley College, for persuading me to try and get published, and my parents for their continual support with it. Chris, my brother, was an invaluable proof-reader and I must also thank Pear Vardhanabhuti who started out with no knowledge of programming, and bravely took on the task of learning all about PIC microcontrollers using just the book. He then went on to design and build the ‘diamond brooch’ project circuit board. Also helping to build projects were Ed Brocklebank, James Bentley and Matt Fearn, and Matt Harrison helped me with the artwork involved. My work was greatly facilitated by Philip Clayton, an immaculate technical proof-reader and advisor. Finally comes the most important thanks of all, to Max Horsey – a constant provider of assistance and advice, and fountain of new ideas; he has helped me immeasurably. H6664-Prelims.qxd 8/13/05 12:32 PM Page ix . The PIC Microcontroller H6664-Prelims.qxd 8/ 13/ 05 12 :32 PM Page i To Mum & Dad H6664-Prelims.qxd 8/ 13/ 05 12 :32 PM Page ii The PIC Microcontroller: . (ϩ44) (0) 1865 8 438 30; fax: (ϩ44) (0) 1865 8 533 33; e-mail: permissions@elsevier.co.uk. You may also complete your request on-line via the Elsevier Science

Ngày đăng: 12/12/2013, 22:42

TỪ KHÓA LIÊN QUAN

w