A Tutorial Guide to Programming PIC18, PIC24 and Atmega Microcontrollers with Flashforth

A Tutorial Guide to Programming PIC18, PIC24 and Atmega Microcontrollers with Flashforth

A Tutorial Guide to Programming PIC18, PIC24 and ATmega Microcontrollers with FlashForth. 2016 Revision Mechanical Engineering Report 2016/01 P. A. Jacobs School of Mechanical and Mining Engineering The University of Queensland. July 11, 2016 Abstract Modern microcontrollers provide an amazingly diverse selection of hardware peripherals, all within a single chip. One needs to provide a small amount of supporting hardware to power the chip and connect its peripheral devices to the signals of interest and, when powered up, these devices need to be configured and monitored by a suitable firmware program. These notes focus on programming the 28-pin PIC18F26K22 microcontroller and its 40- pin PIC18F46K22 sibling, the 16-bit PIC24FV32KA302 and the 8-bit AVR ATmega328P. These microcontrollers are all available in plastic DIP packages, can be run from a 5 volt supply, and can be built into very simple prototype hardware. A number of example programs, in the Forth language, are provided to illustrate the use of some of each microcontroller's peripheral devices. The examples cover the very simple “flash a LED" exercise through to driving a character-based LCD via its 4-bit parallel interface. Communication with SPI and I2C devices is also covered, with a common set of words being used to abstract away the differences between microcontrollers in terms of detailed bits and registers. 1 CONTENTS 2 Contents 1 A selection of microcontrollers4 2 Development boards7 2.1 PIC18 family boards..............................7 2.2 AVR and PIC24 boards............................ 10 3 FlashForth 14 3.1 Getting FlashForth and programming the MCU............... 14 3.2 Building for the PIC18F26K22 or PIC18F46K22............... 15 3.3 Building for the PIC24FV32KA302...................... 17 3.4 Building for the ATmega328P......................... 17 4 Interacting with FlashForth 18 5 Introductory examples 20 5.1 Hello, World: Flash a light-emitting diode with the PIC18......... 20 5.2 Flash a light-emitting diode with the PIC24................. 21 5.3 Flash a light-emitting diode with the ATmega................ 22 5.4 Set the cycle duration with a variable (PIC18)................ 23 5.5 Hello, World: Morse code........................... 24 6 Read and report an analog voltage 25 6.1 PIC18FX6K22.................................. 25 6.2 PIC24FV32KA30X............................... 26 6.3 ATmega328P.................................. 28 7 Counting button presses 30 8 Counting button presses via interrupts 32 9 Scanning a 4x3 matrix keypad 34 10 Communicating with SPI devices 36 10.1 PIC18FX6K22.................................. 37 10.2 PIC24FV32KA30X............................... 38 10.3 ATmega328P.................................. 39 10.4 Words to drive a matrix display........................ 41 11 Communicating with I2C devices 42 11.1 PIC18FX6K22.................................. 42 11.2 PIC24FV32KA30X............................... 44 11.3 ATmega328P.................................. 46 11.4 Notes on using the words............................ 48 11.5 Detecting I2C devices.............................. 49 12 Using I2C to get temperature measurements 50 CONTENTS 3 13 Making high-resolution voltage measurements 51 14 An I2C slave example 53 15 Speed of operation { bit banging 57 15.1 PIC18F26K22.................................. 57 15.2 PIC24FV32KA302............................... 59 15.3 ATmega328P.................................. 61 16 Driving an Hitachi-44780 LCD controller 64 A Using other terminal programs 68 1 A SELECTION OF MICROCONTROLLERS 4 1 A selection of microcontrollers Over the past couple of decades, microcontrollers have evolved to be cheap, powerful computing devices that even Mechanical Engineers can use in building bespoke instru- mentation for their research laboratories. Typical tasks include monitoring of analog signals, sensing pulses and providing timing signals. Of course these things could be done with a modern personal computer connected via USB to a commercial data acquisition and signal processing system but there are many situations where the small, dedicated microcontroller, requiring just a few milliamps of current, performs the task admirably and at low cost. Modern microcontrollers provide an amazingly diverse selection of hardware peripherals, all within a single chip. One needs to provide a small amount of supporting hardware to power the chip and connect its peripheral devices to the signals of interest and, when powered up, these devices need to be configured and monitored by a suitable firmware program. These following sections provide an introduction to the details of doing this with an 8-bit Microchip PIC18F26K22 or PIC18F46K22 microcontroller, a 16-bit Microchip PIC24FV32KA302 microcontroller and an 8-bit Atmel ATmega328P microcontroller, all programmed with the FlashForth version 5 interpreter [1]. Within each family of Microchip or Atmel microcontrollers, the individual microcontroller units (MCUs) all have the same core, i.e. same instruction set and memory organisation. Your selection of which MCU to actually use in your project can be based on a couple of considerations. If you are on a tight budget and will be making many units, choose an MCU with just enough functionality, however, if convenience of development is more important, choose one with \bells and whistles". For this tutorial guide, we will value convenience and so will work with microcontrollers that have: • a nice selection of features, including a serial port, several timers and an analog-to- digital converter. See the feature list and the block diagram of the PIC18F26K22 and PIC18F46K22 MCUs on the following pages. • a 28-pin narrow or 40-pin DIL package, which is convenient for prototyping and has enough I/O pins to play without needing very careful planning. • an ability to work as 3.3V or 5V systems. • a pinout as shown at the start of the datasheets (books) [2,3,4]. You will be reading the pages of these books over and over but we include the following couple of pages from the PIC18F22K26/PIC18F46K22 datasheet to give an overview. • an internal arrangement that is built around an 8-bit or 16-bit data bus. • the \Harvard architecture" with separate paths and storage areas for program in- structions and data. We won't worry too much about the details of the general-purpose registers, the internal static RAM or the machine instruction set because we will let the FlashForth interpreter handle most of the details, however, memory layout, especially the I/O memory layout is important for us as programmers. The peripheral devices, which are used to inferface with the real world, are controlled and accessed via registers in the data-memory space. 1 A SELECTION OF MICROCONTROLLERS 5 !"#$%&'()*(+'' !" !!#$%&'()*+#$*+,-'(.%/0#$,-1*-23&4,( 5%4-*4*&6-*77,-8(+%60(9)$(:,40&*7*/; ,-./012342567819:; < F"C21519$4G04G129H676.1517C 8 99':)$-+ ;)<$:$ +>" %($<+%<# +A !"#$% '()*(+''9G-C/ ($ " ;)<$',6-+B<+,>+>$,C< #%<$',C+<>+C$.,+><' 8 3-++):'>+A ,"3</)$%6- ')<$:$ + +"+,< 6,< %'>+ 8 26<%(>'.*$:+ A 7 ,"3</)$%6- 8 D)<'! 4E/<+C26<6FFGH;# 8 *$:+ !;C%$--6<' A0 ,"]78T 8 D)<' ,4 -./<+C /$,+6 G&'. 6:#+:' / ">> +CC$,. 8 G+&$)(+ 6-#'>#-+2$C6.-+ 8 D)<'701,E/<+C/$,+6 26<6#+:' /">> +CC" ;,18-6>9H-824847C24>>129 16CD21B $,. 8 D)<'!,#0G3;)+&6<$', 8 17A<'_1_A;)+&6<$',YG09!05ZZ->+4$%+C 8 !,".$<2$>+0,C< #%<$',C30".$<2$>+26<6G6<( 8 !10A<'71,A;)+&6<$',YG09!0/5ZZ->+4$%+C 8 G&$' $<//+4+-CR' 0,<+ #)<C 8 3+-R"G '. 6::6.-+#,>+ 3'R<S6 +9',< '- 8 7!"/+4+-33'R<S6 +?%%+CC$.-+T6 !S6&+3<6%8 8 T$.(C/'SA'-<6.+2+<+%<$',[T/^2?:'>#-+A 8 0B03$,.-+"9/%-+T6 >S6 +##-<$)-$+ " G&'. 6::6.-+!,"/+4+- >1"-#>19AB8->>6C429;C2D8CD21 " 0,<+ #)<',T$.(C/'SA'-<6.+2+<+%<$', 8 G&'. 6::6.-+E 'S,"'#<H+C+<[E;H?A 8 G&+%$C$',!, #T 0,<+&,6-;C%$--6<' E-'%8A " 2$<(C'R<S6 ++,6.-+')<$', " 56%<' /%6-$. 6<+><'9!: " 9',R$.# 6.-+C(#<>'S,$,3-++) " 3+-+%<6.-+R +X#+,%$+C37! 8T <'!, #T 8 FB<+,>+>26<%(>'.*$:+ [22*?A " ,4 #T )+ R'&:6,%+646$-6.-+#C$,.GL/Y " G&'. 6::6.-+)+ $'>R ': 4 :C<'!7!C ,'+B<+ ,6-%':)',+,<C +X#$ +> 8 0,"9$ %#$<3+ $6-G '. 6::$,.a[093Ga?A 8 5'# 9 /C<6-:'>+C#)<',4 #T " 3$,.-+"3#))-/7A 8 *S'FB<+ ,6-9-'%8:'>+C#)<',4 #T 8 0,"9$ %#$<2+.#.[092? 8 4ZG(6C+/'%8/'')[G//? 8 3+%',>6 /;C%$--6<' #C$,.*$:+ !]7 8T 12-,/126>9,-./>-./CB 8 56$-"36R+9-'%8#',$<' A " ?--'SCR' C6R+C(#<>'S,$R)+ $)(+ 6-%-'%8 8 D)<'7_0C;G$,C)-#C!0,)#<";,-/G$,A C<')C " T$.("9# +,<3$,8C3'# %+_ :"C_ :" " *S'"3)++>;C%$--6<' 3<6 <"#) " *(&++) '. 6::6.-++B<+ ,6-$,<+ #)<C " 5'# )&'. 6::6.-+$,<+ #)<"',"%(6,.+ " E$,+)&'. 6::6.-+S+68)#--"#)C E76>4.9 16CD21B " G&'. 6::6.-+C-+S 6<+ 8 ?,6-'."<'"2$.$<6-9',4+ <+ ["29?:'>#-+A 8 3H/6<%(A " ! ".$< +C'-#<$',3#)<'7 +B<+ ,6- %(6,,+-C " ##-<$)-+3+<CH+C+<$,)#<')<$',C " "#<'"6%X#$C$<$',%6)6.$-$</ 8 *S'96)<#&+C9':)6 +CG2#[99G?:'>#-+C " 9',4+ C$',646$-6.-+># $,.3-++) 8 *(&++F,(6,%+>99G[F99G?:'>#-+CA " 5$B+>A'-<6.+H+R+ +,%+[5AH?%(6,,+- " ;,+3<S'' R'# G2#'#<)#<C " 0,>+)+,>+,<$,)#<:#-<$)-+B$,. " 3+-+%<6.-+)'-6 $</ 8 ?,6-'.9':)6 6<' :'>#-+A " G&'. 6::6.-+>+6><$:+ " *S' 6$-"<'" 6$-6,6-'.%':)6&6<'&C " "#<'"3(#<>'S,6,>"#<'"H+C<6 < " 0,>+)+,>+,<$,)#<:#-<$)-+B$,. " *2#C<++ $,. 8 2$.$<6-"<'"",6-'.9',4+&<+ [2?9?:'>#-+A 8 *S'#6C<+ 3/,%( ','#C3+ $6-G' <[#33G? :'>#-+CA " 5$B+>A'-<6.+H+R+ +,%+[5AH?S$<(!1 4A3 " 7"S$&+3G0[C#))' <C6--4:'>+C? 1 40A6,>41 1,A'#<)#<-+4+-C " _".$< 6$-"<'" 6$- +C$C<$4+2"9S$<()'C$<$4+ " 0 9a#6C<+ 6,>3-64+:'>+CS$<(6>> +CC 6,>,+.6<$4+ +R+ +,%+C+-+%<$', :6C8 8 9(6 .+ *$:+ #+6C#&+:+,<D,$<[9*#D?:'>#-+A " 3@))' <C%6)6%$<$4+<'#%(C+,C$,.R' <'#%( C% ++,C6,>%6)6%$<$4+CS$<%(+C ! " ! #$%&'%($)*+%(,'-'./0,%1 234!4!5")6.+7 1 A SELECTION OF MICROCONTROLLERS 6 !"#$% '()*(+'' ,-./ !0!1 !"#$% '()*(+''2 34$5 6$7 +283,.34 22 89:926;< "> 2 14()E0-39(/#"!A G G 9 ) 9.1 -3.D;(.)40'-. 9 ),(>0/5 "! E< "2)))))0 E< "2F "+ ";;/(88) 9.1 E.C2" E<0 E<F))))E< C"+?C"] E/0'/ >)<0:39(/ !" 9 )";;/(88#!"A H!$ (I(4)9 .J ";;/(88) 9.1 !" BC "..(88 E/0'/ >),(>0/5 2RE2C #C+ B 3- TGD!*DH"D* R159(88 #C! E.C2B #C" !" 9 ) 9.1 CB+?CB] -3.D;(.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    69 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us