Editing A Really Basic Guide to the PIC Microprocessor and BoostC

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 42: Line 42:
 
I suppose an input/output port is a peripheral, but normally the term is used for something more complicated. A counter/timer is one example. A counter is connected to some source ( a pin for example ) and each time the pin changes from a 0 to a 1 the counter increase by 1. Some counters are 8 bit, some 16 bits( this is the number of bits for the highest number the counter can count to )..  
 
I suppose an input/output port is a peripheral, but normally the term is used for something more complicated. A counter/timer is one example. A counter is connected to some source ( a pin for example ) and each time the pin changes from a 0 to a 1 the counter increase by 1. Some counters are 8 bit, some 16 bits( this is the number of bits for the highest number the counter can count to )..  
  
Other peripherals are: PWM outputs ( pulse width modulation ) Serial communications, Analog to digital converters.
+
Other peripherals are: PWM outputs ( pulse with modulation ) Serial communications, Analog to digital converters.
  
 
All the peripherals discussed so far are built right into the chip, they are called internal peripherals, peripherals can also be outside the PIC chip, these are external peripherals.  
 
All the peripherals discussed so far are built right into the chip, they are called internal peripherals, peripherals can also be outside the PIC chip, these are external peripherals.  
Line 170: Line 170:
 
Now lets read through the program and add some comments ( I could give you this program with the comments, but if you add the notes yourself it will mean more to you ) First line:
 
Now lets read through the program and add some comments ( I could give you this program with the comments, but if you add the notes yourself it will mean more to you ) First line:
  
  '''#include <system.h>'''
+
'''#include <system.h>'''
  
 
( note I will use the font above for all lines in the C source file ) Start all programs with this statement, it adds the contents of the file system.h to your project, and most BoostC programs want this file. It makes the symbolic names ( like portb ) for PortB available to the compiler. To add a note about this to the file start the line with //. So you might add:
 
( note I will use the font above for all lines in the C source file ) Start all programs with this statement, it adds the contents of the file system.h to your project, and most BoostC programs want this file. It makes the symbolic names ( like portb ) for PortB available to the compiler. To add a note about this to the file start the line with //. So you might add:
  
  '''// start all BoostC programs with this include.'''
+
'''// start all BoostC programs with this include.'''
  
 
A line like the above is called a comment and is ignored by the compiler, but can give the human reader useful information. Use comments, if you do not even you will not understand your code at a later date.
 
A line like the above is called a comment and is ignored by the compiler, but can give the human reader useful information. Use comments, if you do not even you will not understand your code at a later date.
Line 180: Line 180:
 
By the way note that C is case sensitive, do not change that capitalizations, also there are conventions on capitalization that you should follow, I will mention them later. Now read down to the line:
 
By the way note that C is case sensitive, do not change that capitalizations, also there are conventions on capitalization that you should follow, I will mention them later. Now read down to the line:
  
  '''#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_OFF & _CPD_OFF & _DEBUG_OFF & _XT_OSC & _CP_OFF'''
+
'''#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_OFF & _CPD_OFF & _DEBUG_OFF & _XT_OSC & _CP_OFF'''
  
 
this line does some set up of the compiler and the chip. There is a section in the manual about this, the are normally called configuration fuses. Setting them up the wrong way can be a real problem, learning all about them can take awhile. Leave them as they are until you learn more. Now the line:
 
this line does some set up of the compiler and the chip. There is a section in the manual about this, the are normally called configuration fuses. Setting them up the wrong way can be a real problem, learning all about them can take awhile. Leave them as they are until you learn more. Now the line:
  
  '''#pragma CLOCK_FREQ 4000000'''
+
'''#pragma CLOCK_FREQ 4000000'''
  
 
This tell the compiler how fast your clock will be. For this chip this is the frequency of the crystal you are using. ( we are planning a 4 mega Hz clock and crystal. This is important because the compiler will generate some programs for delaying the computer for a time. To do this it needs to know how fast the clock is.
 
This tell the compiler how fast your clock will be. For this chip this is the frequency of the crystal you are using. ( we are planning a 4 mega Hz clock and crystal. This is important because the compiler will generate some programs for delaying the computer for a time. To do this it needs to know how fast the clock is.
  
  '''void interrupt( void ) {'''
+
'''void interrupt( void ) {'''
  
 
this line begins a subroutine that handles processor interrupts ( whatever they are ). For now you will not be using them, but for now leave the code in. If you have an interrupt by some mistake you program will work better with this in.
 
this line begins a subroutine that handles processor interrupts ( whatever they are ). For now you will not be using them, but for now leave the code in. If you have an interrupt by some mistake you program will work better with this in.
Line 196: Line 196:
 
Next look at:  
 
Next look at:  
  
  '''void main( void ) {'''
+
'''void main( void ) {'''
  
 
this is the so called main program, when the PIC starts up it starts here. What does this code do? Lets take a look:
 
this is the so called main program, when the PIC starts up it starts here. What does this code do? Lets take a look:
  
  '''//Configure port B
+
'''//Configure port A
  trisb = 0x00;'''
+
trisa = 0x00;'''
  
this line sets the control register for port b: called trisb to all 0's ( every bit = 0 ). 0 makes the bit an output bit, that is the pin connected to port pin is an output pin. To find what pins this is on the chip look in the 877 manual The 0x means that the number is expressed in hexadecimal or base 16. You can also use regular decimal, or binary. In binary you can clearly see each bit.
+
this line sets the control register for port a: called trisa to all 0's ( every bit = 0 ). 0 makes the bit an output bit, that is the pin connected to port pin is an output pin. To find what pins this is on the chip look in the 877 manual The 0x means that the number is expressed in hexadecimal or base 16. You can also use regular decimal, or binary. In binary you can clearly see each bit.
 
0x0 is 0b0 in binary or 0b00000000 the last notation showing all 8 bits in the byte.  
 
0x0 is 0b0 in binary or 0b00000000 the last notation showing all 8 bits in the byte.  
  
 
The code continues working through other ports setting them all to output. The code then continues setting up some other configuration stuff that I would rather not explain. Pretty much all this stuff is safe, that is it will not do anything bad. The exception to this is the line:
 
The code continues working through other ports setting them all to output. The code then continues setting up some other configuration stuff that I would rather not explain. Pretty much all this stuff is safe, that is it will not do anything bad. The exception to this is the line:
  
  '''intcon = 0xA0;'''
+
'''intcon = 0xA0;'''
  
 
this will turn on the interrupts which we would rather have off. Change it to  
 
this will turn on the interrupts which we would rather have off. Change it to  
  
  '''intcon = 0b0;'''
+
'''intcon = 0b0;'''
  
 
this will have the interrupts off. Also fix the comment just above this line so it is correct.
 
this will have the interrupts off. Also fix the comment just above this line so it is correct.
Line 218: Line 218:
 
Now we are almost done: we have next
 
Now we are almost done: we have next
  
  '''while( 1 ) ;'''
+
'''while( 1 ) ;'''
  
 
This is a loop that runs forever doing nothing.
 
This is a loop that runs forever doing nothing.
Line 224: Line 224:
 
Change this to:
 
Change this to:
  
  '''while( 1 ) {
+
'''while( 1 ) {
  }'''
+
}'''
  
 
This is still a loop that runs forever doing nothing but the difference is that the loop begins at the { and extends to the }. Writing it this way gives us a place to add code to the loop so that it will do something. Note that this is typical for a PIC program to have a loop like this, but normally doing something. Start up, set up, and do something forever. Just a note about a loop if you are not familiar with the term. A loop in a program is a bit like running in a circle, you go around and around. Since the program is written as sequential lines this means we start at some line, the beginning of the loop, work down to a lower line, the end of the loop, and then go back to the beginning of the loop. Most loops ( unless the purpose is just to take up time ) do something each time the loop, perhaps count from 0 to 99. Many loops in fact count just to determine how many times to execute the code in the loop, this may be done to delay the program a bit. At the end of the count they “drop out of the bottom” of the loop.
 
This is still a loop that runs forever doing nothing but the difference is that the loop begins at the { and extends to the }. Writing it this way gives us a place to add code to the loop so that it will do something. Note that this is typical for a PIC program to have a loop like this, but normally doing something. Start up, set up, and do something forever. Just a note about a loop if you are not familiar with the term. A loop in a program is a bit like running in a circle, you go around and around. Since the program is written as sequential lines this means we start at some line, the beginning of the loop, work down to a lower line, the end of the loop, and then go back to the beginning of the loop. Most loops ( unless the purpose is just to take up time ) do something each time the loop, perhaps count from 0 to 99. Many loops in fact count just to determine how many times to execute the code in the loop, this may be done to delay the program a bit. At the end of the count they “drop out of the bottom” of the loop.
Line 231: Line 231:
 
Finally we have the final
 
Finally we have the final
  
  '''}'''
+
'''}'''
  
 
which ends the routine main, and since main is also where the program starts it ends the whole thing. Also since the loop never ends we never get through this point.
 
which ends the routine main, and since main is also where the program starts it ends the whole thing. Also since the loop never ends we never get through this point.
Line 250: Line 250:
 
We can put this together like this:
 
We can put this together like this:
  
  '''while( 1 ) {'''
+
'''while( 1 ) {'''
 
+
 
      '''set_bit( portb, 0 ); // connect the led to port B,'''  
+
'''set_bit( portb, 0 ); // connect the led to port B,'''  
      '''// bit 0 this is RB0 or pin33'''  
+
'''// bit 0 this is RB0 or pin33'''  
      '''// the led will turn on'''
+
'''// the led will turn on'''
      '''delay_ms( 250 ); // just wait for ¼ of a second'''
+
'''delay_ms( 250 ); // just wait for ¼ of a second'''
 
+
 
      '''clear_bit( portb, 0 ); // the led will turn off'''
+
'''clear_bit( portb, 0 ); // the led will turn off'''
 
+
 
      '''delay_ms( 250 ); // just wait for ¼ of a second'''
+
'''delay_ms( 250 ); // just wait for ¼ of a second'''
 
+
 
 
+
 
  '''}'''
+
'''}'''
  
 
The led should cycle through on, then off, all in one half of a second.
 
The led should cycle through on, then off, all in one half of a second.
Line 278: Line 278:
 
Debugging.... should show in lower panel, a green bar should appear in the edition across  
 
Debugging.... should show in lower panel, a green bar should appear in the edition across  
  
  '''trisa = 0x00;'''
+
'''trisa = 0x00;'''
  
 
the first line of the main program. The program has started running and then has stopped on this line in the program.
 
the first line of the main program. The program has started running and then has stopped on this line in the program.
Line 303: Line 303:
 
I recommend that you build the circuit on a poto or breadboard. The board has many connections prewired and allows many components and wires to simply be pushed in holes in the board to make the connections. Here is a picture:
 
I recommend that you build the circuit on a poto or breadboard. The board has many connections prewired and allows many components and wires to simply be pushed in holes in the board to make the connections. Here is a picture:
  
    still need to upload the picture
+
 
  
 
I normally use the connections marked in red ( all connected together on the top, all connected together on the bottom, but top and bottom not connected together ) as +5 volts and the blue as 0 volts or ground. Use jumper wires to connect the red top line to the red bottom line, and similarly for the blue.
 
I normally use the connections marked in red ( all connected together on the top, all connected together on the bottom, but top and bottom not connected together ) as +5 volts and the blue as 0 volts or ground. Use jumper wires to connect the red top line to the red bottom line, and similarly for the blue.
Line 325: Line 325:
  
 
Here is the resulting circuit for me ( with unused pins not connected, but configured as outputs in the code )
 
Here is the resulting circuit for me ( with unused pins not connected, but configured as outputs in the code )
 
    still need the picture
 
  
 
== Programming the Chip  ==
 
== Programming the Chip  ==
Line 497: Line 495:
 
PIC16F87XA Datasheet:
 
PIC16F87XA Datasheet:
 
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1335&dDocName=en010242 under datasheets at the right hand panel on the page
 
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1335&dDocName=en010242 under datasheets at the right hand panel on the page
 +
  
 
BoostC Compiler Help:  
 
BoostC Compiler Help:  
Line 502: Line 501:
  
  
Lots of PIC Links use page search on PICIntro, and/or Tutorial, and/or Info for more or less instructional materials.
+
Lots of PIC Links ( mostly beginner level ) at
* [[PIC Links]]
+
 
* [[Use C for PIC Programming!]]
+
http://home.comcast.net/~russ_hensel/RClub/ProjectLinks.html
* [[BoostC tiny Wiki]]
 
* [[BitWacker PIC and Other Microcontroller to Java Communications]]
 
* [[A Tutorial on PIC interrupts using BoostC including Example Programs]]
 
* [[PIC Programmers, In Circuit Programming and BootLoaders]]
 
  
 +
once in this document search on the word: PIC to find only introductions to the PIC search on the word: PICIntro
  
 
More help with C on the web:
 
More help with C on the web:
Line 520: Line 516:
 
Programming in C: A Tutorial - Brian W. Kernighan - Bell Laboratories, Murray Hill, N. J. http://www.lysator.liu.se/c/bwk-tutor.html
 
Programming in C: A Tutorial - Brian W. Kernighan - Bell Laboratories, Murray Hill, N. J. http://www.lysator.liu.se/c/bwk-tutor.html
  
* [[Microcontroller]]
+
 
  
 
== Appendix: What you Need for this Project  ==
 
== Appendix: What you Need for this Project  ==
Line 561: Line 557:
 
CF1 This capacitor helps smooth out ripple and noise in the output. 50 micro F should do.
 
CF1 This capacitor helps smooth out ripple and noise in the output. 50 micro F should do.
 
CF2 This capacitor helps smooth out high frequency in the output. Normally capacitors like this are placed close to the power pins of each integrated circuit. A ceramic capacitor of .1 or .01 micro F should serve.
 
CF2 This capacitor helps smooth out high frequency in the output. Normally capacitors like this are placed close to the power pins of each integrated circuit. A ceramic capacitor of .1 or .01 micro F should serve.
 
 
[[category:Microcontroller]][[category:PIC]]
 

Please note that all contributions to OpenCircuits may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see OpenCircuits:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)