Vous êtes sur la page 1sur 126
ys 3} 2 cl Camp) 5 gee joa e AVR Oe An Introductory Course Sohn Morton * soit ig, Hou % SS mer > ima = 521 3815 % “anes * . . TOR - nb ated ¢ . A. ) Newnes OXFORD AMSTERDAM BOSTON LONDON NEW YORK PARIS SAN DIEGO SAN FRANCISCO SINGAPORE SYDNEY TOKYO ‘Newnes An imprint of Elsevier Science Linacre House, Jordan Hill, Oxford OX2 SDP 225 Wildwood Avenue, Woburn, MA 01801-2041 First published 2002, Copyright © 2002, John Morton. All rights reserved ‘The right of John Morton to be identified as the author ofthis work has been asserted in accordance with the Copyright. Designs and Patent Act 1988. \No part ofthis 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 ofthis publication) without the writen permission of the copyright holder excep in accordance withthe provisions ofthe Copyright. Designs and Patents Act 1988 or under the terms ofa licence issued by the Copyright Licensing Agency Lid. 90 Totenham Coun Road. London. England WIT ALE. Applications for the copyright holder's wrinen permission to epraduce any part of this publication shouldbe addressed to the publish: British Library Cataloguing in Publication Data Morton. John, 1980- AVR: an introductory coufse 1. Programmable controllers 2. Integrated circuits - Design and construction 3- Microelectronics 1 Title 626.89 ISBN 0 7506 56352 | |For information on all Newnes publications visit our website at worw.newnespress.com ‘Typeset by Avocet Typeset, Brill, Aylesbury, Bucks Printed in Great Britain by Biddles Lid Guildford and King's Lynn —___Conienis Acknowledgements Preface i 1 Shor bit fr PIC uses 2 Number systems 3 Adding in binary 5 Negative numbers 6 An 8-bit Flash microcontroller 7 Initial steps 8 Choosing your mode! 8 Flowchart 10 Writ 2 Astebing 8 Registers B Instructions 7 Progam plat u 2 Basle Operations with AT90S1200 and Tinyt2 4 Program A: LED on 24 ‘AVR Studio ~ Assembling 25 AVR Studio ~ Simulating 26 Emulating 27 Hardware 27 AVR Studio ~ Programming 30 Fuse bits 31 Programs B and C: Push Button 31 Seven Segment displays and indirect addressing 34 Programs D and E: Counter 39 Timing ? 44 Program F: Chaser 47 “Timing without a timer? 2 ‘The Program Counter and Subroutines 54 Program G: Counter v3.0 37 bs s. ae vi Contents Program H: Traffic Lights ‘Logie Gates Program I: Logic Gate Simulator ‘SREG - The Status Register ‘Watchdog Timer Sleep ‘More Instructions ~ loose ends Major Program J: Frequency Counter 3. Introducing the rest of the family 4 Intermediate Operations Interrupts Program K: Reaction Tester ‘Analogue Comparator Program L: 4-bit analogue to digital converter 10-bit analogue to digital conversion (ADC) Program M: Voltage Inverter ‘EEPROM: 16-bit Timer/Counterl Input Capture Output Compare Major Program N: Melody Maker 5 Advanced Operations PWM ~ Pulse Width Modulation UART Program O: Keyboard Converter ‘Serial Peripheral Interface (SPI) Tiny15's Eccentric Timer] Shrets ‘A Mega Summary Final Program P: Computer Controlled Robot Conclusions Appendix A: Specifications for some PICs ‘Appendix B: Pin layouts of various AVRS ‘Appendix C: Instruction averview Instruction glossary Interrupt vector tables ‘Hex conversion ASCII conversion When al else fails, read this setae 4 39 63 67 " 18 16 1 1 ” o 99 105 106 109 2 16 ww 19 121 12 Oe 127 129 134 138 vay 144 145 145 1S] 152 153 13) 165 166 167 168 Contents Appendix 1: Contacts and further reading Appendix J: Sample programs Program A. Led on ~ turns an LED on Program B. Push button (1,0) ~ turns an LED on when a button is pressed Program C. Push button (2.0) ~ shorter version of push button (1.0) Program D. Counter (1.0) ~ counts push button presses on @ seven segment display Program E. Counter (2.0) ~ as counter (1.0) reduces multiple reads : Program F. Chaser ~ chases LED display at variable speeds Program G. Counier (3.0) ~ as counter (2.0) with button bounce solved Program H. Traffic ~ pedestrian crossing traffic lights simulator Program I. Logie gates - simulates eigh different logic gates Program J. Frequency counter ~ measures frequency between 1 Hz and | MHz Program K. Reaction tester ~ measures human reaction time Program L. 4-bit analogue to digital converter ~ how 10 make a comparator into an ADC Program M. inverter ~ converts V into (5 ~ V) Program N. Melody maker ~ plays programmable tunes Program . Keyboard converter ~ turns your computer keyboard into a musical one Program P. Computer controlled robot Answers to exercises Index. 169 170 170 171 2 173 174 176 178 180 183 134 191 197 199 200 204 208 218 237

Vous aimerez peut-être aussi