Sunday 30 October 2011

PIC Tutorial 1:


Tutorial 1 : 12F675 Flashing an LED


The first program is a flashing LED - it always is!  The reason is that there is the least hardware to go wrong so it gives a good test of your system setup.  

This project also uses the 12F675's internal oscillator and you don't need a crystal so there is even less to go wrong!


Use the solderless breadboard to construct the following circuit:

Note: Double check your connections on the breadboard.

Note: the plus sign on the 10u electrolytic capacitor which must connect to the positive input voltage and have a voltage rating stamped on it of greater than 35V (or greater than your maximum dc power block output).  The LED must be connected with the flat side to ground.

Solderless breadboard layout

12F675 Flashing LED plugblock




Circuit diagram


The following diagram shows the above Plugblock circuit in schematic form. It is exactly the same circuit but lets you view the circuit in an easier way and shows the layout of the circuit from the point of view of the circuit block functions rather than how you have to place the components (using the Plugblock).

12F675 flashing led schematic





Note: The LED current limiter resistor (1k) is not the ideal one it just lets you see the led (you don't need the maximum current to see the light from the LED) - to use the LED at higher output replace it with 220R. This gives lots more current so it is brighter I=(5-2)/220=13mA (most leds let you use 20mA but you would have to check the forward diode drop, here assumed as 2V, to get the exact resistor.  It's only an LED I always assume 2V as the slight variations for different colored LEDs won't make much difference.

Read 12F765


Using ICPROG set the device to 12F675 and hit the read button.  Remember to note down the contents of address 0x3FF.

Software


The next thing to do is to flash the LED to prove that the system you have is working as reading back data is not very interesting.



You can use the hex file directly to program the 12F675 then it will flash the led on and off or you can re-compile the files using the free compiler from Mikroelectronika. 

Some of the C source code is :


//////////////////////////////////////////////////////////////////////
void init_ports(void) {
   TRISIO = 0; // set as output
}


//////////////////////////////////////////////////////////////////////
// Start here
void main() {

   init_ports();
  
   while(1) { // infinite loop
  
      GPIO = (1<<4);
      delay_ms(200);
     
      GPIO = 0;
      delay_ms(200);
  }
}

First of all the init_ports() routine sets up the direction of pins in the GPIO port - in common with all the other PIC Micros you can change the port direction at any time using  a TRIS keyword (which is just another register location).  Setting a bit in the TRISIO register to zero sets the pin direction to an output.  Here all bits are zero so all GPIO bits are set as outputs.


As you can see main() is a very simple easily readable program the only slightly non obvious part is the (1<<4) statement.

This just takes the value 1 and bit shifts it left four times so the number 4 is the same as bit position 4.  Bits in a byte are labeled 7 to 0 from left to right and (1<<0)=1, (1<<1)=2, (1<<2)=4, (1<<3)=8 etc. so this gives an easy way of setting an individual bit in a byte that is also easy to read.  If you wanted to set bit 5 you could write GPIO = 32; (or 0x20 in hex) but GPIO = (1<<5) is much easier to read.


Note: The C programming course has more on PORT control techniques.



Try changing the delay time (in both delay_ms statements) to a smaller or larger value and re-compile and re-flash the chip to see the effect.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Raghu | Protected by - ElectroClub