Get Started With PIC

Here i am going to Share Tutorials for the PIC


Introduction
Welcome to the start of the PIC Tutorial. These pages will take you form the basic structure of the device, right through to programming methods and techniques. Also, there will be suggestions on how to modify the code so that you can adapt the PIC to suit your applications within Cybot. I will not be including any internal architecture diagrams, as this may only lead to confusion. If you want to look at the datasheet, then this can be downloaded from Microchips' web site.
To start, let us take a look at the PIC.

Microchip PIC 16F84 Microcontroller
Microchip manufacture a series of microcontrollers called PIC. You can see the range of their microcontrollers Smart | Connected | Secure | Microchip Technology. There are many different flavours available, some basic low memory types, going right up through to ones that have Analogue - To- Digital converters and even PWM built in. I am going to concentrate on the 16F84 PIC. Once you have learnt how to program one type of PIC, learning the rest is easy.
There are several ways of programming the PIC - using BASIC, C, or Assembly Language. I am going to show you the Assembly Language. Don't be put off by this. There are only 35 instructions to learn, and it is the cheapest way to program the PICs, as you do not need any extra software other than the freebies.

The 16F84 Pins
Below is a diagram showing the pin-outs of the PIC 16F84. I will go through each pin, explaining what each is used for.
[​IMG]

RA0 To RA4
RA is a bidirectional port. That is, it can be configured as an input or an output. The number following RA is the bit number (0 to 4). So, we have one 5-bit directional port where each bit can be configured as Input or Output.

RB0 To RB7
RB is a second bidirectional port. It behaves in exactly the same way as RA, except there are 8 - bits involved.

VSS And VDD
These are the power supply pins. VDD is the positive supply, and VSS is the negative supply, or 0V. The maximum supply voltage that you can use is 6V, and the minimum is 2V
OSC1/CLK IN And OSC2/CLKOUT
These pins is where we connect an external clock, so that the microcontroller has some kind of timing.

MCLR
This pin is used to erase the memory locations inside the PIC (i.e. when we want to re-program it). In normal use it is connected to the positive supply rail.

INT
This is an input pin which can be monitored. If the pin goes high, we can cause the program to restart, stop or any other single function we desire. We won't be using this one much.

T0CK1
This is another clock input, which operates an internal timer. It operates in isolation to the main clock. Again, we won't be using this one much either.

Replies

  • Harshad Italiya
    Harshad Italiya
    In Next Section i will put the tutorial for programming of PIC
  • Rifaa
    Rifaa
    Nice intro about PIC's.
    Umm! I like to know more ASAP.
  • debu
    debu
    @godfather: Good Job!

    As an addition to what godfather has written, #-Link-Snipped-# should be useful for anyone learning how to use PIC's (and microcontrollers in general).

    Regards,

    Debu 😀
    #-Link-Snipped-#
  • Ashraf HZ
    Ashraf HZ
    Good start, dude! I really need to brush up on using assembly on a PIC for sure. I'm being too reliant on C alone 😔
  • silverscorpion
    silverscorpion
    Good job done.
    I'm very much familiar with 8051 series. But I've been very much in need of learning about PIC.
    I'm glad you help me learn PIC. And sure enough, the start is damn good.

    keep up the good work!!
  • Harshad Italiya
    Harshad Italiya
    ash
    Good start, dude! I really need to brush up on using assembly on a PIC for sure. I'm being too reliant on C alone 😔
    silverscorpion
    Good job done.
    I'm very much familiar with 8051 series. But I've been very much in need of learning about PIC.
    I'm glad you help me learn PIC. And sure enough, the start is damn good.

    keep up the good work!!
    Guys me also working on 8051 only and i am also trying to learn PIC and one more thing if you want to learn NXP's LPC series then i can help you very well cause i had done a lot of work on this..
  • Harshad Italiya
    Harshad Italiya
    Guys let us make a Programmer for PIC !!?

    There are so many Programmer available but let us make our Own..
    will post the Schametic Circuit Diagram here and from that we will design PCB layout.

    Feel Free to Post Comments.
  • Ashraf HZ
    Ashraf HZ
    Yeah, thats a good idea! Though I already have a programmer.. making another one might be useful.

    Also, a PCB layout for the basic PIC circuit (inc oscillator, caps, RS232, etc.. ) is also another good idea =) In fact, we could use that for our CE Bot project if xhx wont mind.
  • Harshad Italiya
    Harshad Italiya
    I have circuit diagram and PCB layout made by me... its for 16F877... lets make its programmer and Also one Dev. board that may be useful for beginners...
  • Rifaa
    Rifaa
    Godfather !!! where's the rest ot the tut.. hurry , pleeeeeeeeeeeeeeeeeeeeeeeese
  • Harshad Italiya
    Harshad Italiya
    Rifaa
    Godfather !!! where's the rest ot the tut.. hurry , pleeeeeeeeeeeeeeeeeeeeeeeese

    Dear Rifaa,

    I will post a tutorial also.... but there is one more idea that we will make a programmer plus Test board for PIC so we can Test our Module on that Testboard.. like LED blinking and etc module.....

    so i am working on that Programmer right now and yes Posting new Section soon
  • Harshad Italiya
    Harshad Italiya
    Dear Friends,

    i Just Received the PCB which was Designed by me for PIC 16F877... if you guys want it then i will share with you...

    @ Ash... We can use this as CE Project also!!

    PS: i am working on that PCB hop it will Function properly
  • Ashraf HZ
    Ashraf HZ
    Well, it seems we are going to use an Atmel chip for the CE Bot. However, we can still pursue with the PIC as a back up. (robot is modular, after all).

    .. and, this can be a basis for other projects in the future =) Yeah, please do share the PCB layout.

    I've run out of PIC16F's.. gotta buy some more next week 😔 I still got one PIC18F left. Its able to use a USB connection.. might be useful 😉
  • Harshad Italiya
    Harshad Italiya
    Sure Ash i will post the Layout here...
  • Harshad Italiya
    Harshad Italiya
    Here is the Circuit Diagram for PIC.
    #-Link-Snipped-#

    please take a look at that and suggest the modification...

    i had completed the Layout also.
  • Ashraf HZ
    Ashraf HZ
    Good work, though we could also have a basic diagram without the DG411DJ & MC34063. Are you using them for a specific project?
  • Harshad Italiya
    Harshad Italiya
    Nop Not for Specific Project but with this We can Vary the Vcc. in my case i am using for 5V Vcc.


    So is this OK?
  • Harshad Italiya
    Harshad Italiya
    Actually i Got the Fabricated PCB and right now Working on it.. to Make it Run properly
  • Ashraf HZ
    Ashraf HZ
    Yeah, I guess thats fine. Good luck!
  • Harshad Italiya
    Harshad Italiya
    Ok so In two to Three Days i will post the Layout cause i want to do some changes...

    I am Using Protel For PCB Designing any one aware by this Tool?
  • Ashraf HZ
    Ashraf HZ
    Yeah, heard of it.

    Though, Eagle Lite has been very sufficient for me so far. Pretty damn good for a free software 😀
  • Harshad Italiya
    Harshad Italiya
    Yep its also nice to use crack software. 😉
    Well i will upload "gerber" file so its easy to fabricate.

    Cheers.
  • Ashraf HZ
    Ashraf HZ
    GF, you know CrazyEngineers is against the promotion of pirated software. I suggest you keep it to yourself next time 😀
  • Harshad Italiya
    Harshad Italiya
    Buddy i was Jocking actually i got training on PROTEL thats why i know how to use it and i am using a Copyright Version already.

    and yes as i am CEan now i will never use pirated tools.
  • Rifaa
    Rifaa
    OK GF, Here's what's bothering me about CE's in here. Pls don't take me personally, and I am not disappointed in any way but I just thought I'll let you all know.
    Some where in another thread some one discussed about electronic simulating software's and it's level of functionality. So then I asked about the best of the best there is, cause I like to get my hands on one and touch up What I lost and secondly I was dealing with a job tht uses PIC's and also I want to learn about schematic and PCB layout and all, and guess what, no one answered, and I was rather dissented then. 😔
    But I got over it since I did a lot of research, why I asked CE's was to save time but now I have gone through this all, LT Spice, P spice, protel, and NI Multisim.
    What worked for me best was Cadence, Pretty powerful and free too, but still I can only make a shcema and to transfer the layout is still Greek to me, and am working on it ( I will be grateful if some one point me the right way ).
    Now I found out most of you is using one software at least and why I though none of you just took some time to give a suggestion.
    Well, what's done is done, and I don't drag it behind me.

    So, concerning the schematic GF had posted, I just want to know what it is and how CE's like us that has no way of fabricating a PCB soon for the time being are gonna do with it.
    And having a PIC in it concerns me considering other CE's.

    I do have a programmer and that PIC you have it in the schematic. But I don't have the HEX file or what ever is used to program.
    Or am going too quickly on this. Just let me know.

    Taking about piracy, I think I know more about them than any of you, cause when a CE talks about pirated soft's in case of schematic capture software's, most of them are free, even the good ones, so any one of you can download them and use it, don't believe me then you can check it out. 😉
  • Ashraf HZ
    Ashraf HZ
    Alright rifaa, I got the hint 😛 Sorry, I forgot about replying to that thread since I had exams and project submissions.

    lets try to stick on topic here 😀

    Regarding the hex file, GF will hopefully explain about the basics of programming first.
  • Harshad Italiya
    Harshad Italiya
    Sorry Rifaa,

    Ok you want to Make Your Own PIC Programmer i am giving you the PCB layout gerber file so you can give it to PCB manufacturer and about HEX file..

    That Hex file which we have to Load is available online i will send it to you also. yes you need one ICD2 which will load that file to PIC16F877A and mount that chip on our programmer. After that Connect it to MPLAB it will Load the Software for ICD2 automatically.

    I had posted Everything which i know may be i am wrong in case then let me know i will improve it..

    PS:- About PCB software.. if know how to USE PROTEL if you want to learn that then i can help you please post your Doubts regarding Protel i will try my best to answer you.
  • Harshad Italiya
    Harshad Italiya
    [FONT=Arial, Arial, Helvetica]Good Programming Techniques.[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Before we get to the nitty gritty of programming the PIC, I think now is a good time to explain some good programming techniques.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]If you type a ; (semicolon) anywhere in your program, the compiler will ignore anything after it until the carriage return. This means we can add comments in our program to remind us of what on earth we were doing in the first place. This is good practice, even for the simplest programs. You may well fully understand how your program works now, but in a few months time, you may be scratching your head. So, use comments wherever you can – there is no limit.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Secondly, you can assign names to constants via registers (more about these later). It makes it far easier to read in English what you are writing to, or what the value is, rather than trying to think of what all these numbers mean. So, use real names, such as COUNT. Notice that I have put the name in capitals. This makes it stand out, and also means that (by convention) it is a constant value.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Thirdly, put some kind of header on your programs by using the semi-colons. An example is below:[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; Author : ;
    ; Date : ;
    ; Version: ;
    ; Title: ;
    ; ;
    ; Description: ;
    ; ;
    ; ;
    ; ;
    ; ;
    ; ;
    ; ;
    ; ;
    ; ;
    ; ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Notice that I have made a kind of box by using the semi-colons. This just makes it look neat.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT] [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Finally, try and document the program on paper as well. You can either use flow charts or algorithms or anything else you want. This will help you in writing your program, step by step.[/FONT]
  • Harshad Italiya
    Harshad Italiya
    Just Got one good mini tutorial for 16F87X
    #-Link-Snipped-# it
  • Harshad Italiya
    Harshad Italiya
    Hello Ceans,

    before i post the Layout of ICD2 here i am stuck with one problem.
    MPLAB is not installing the bootloader file in 16F877A.
    i am using bl010101.hex as a bootloader... ICD661.hex is also suitable for same chip

    Godfather
  • Harshad Italiya
    Harshad Italiya
    i have to make this Urgent so need to find the Alternative of this Programmer

    😔
  • Harshad Italiya
    Harshad Italiya
    I can Replace my Circuit with This one? !!

    [​IMG]
  • Ashraf HZ
    Ashraf HZ
    GF, can you try WinPic800 to bootload the 16F?
  • Rifaa
    Rifaa
    OK GF and ash ..seriously. I really donno what in god's name you two are talking about.
    I mean I can repair TV's ..hi fi's, DVD players and anything concerning power electronics, but i really have no idea when it comes to PIC's and boot load,ICD2 or what ever.
    So you have to be very specific if you are telling me about PIC's.
    And about the schema, do you want me to make a PCB from it or what. I mean I can vero board it, no biggie.
    As for the PIC I have a 16F628-041/P and max 232 ..no problem. So I am good to go with tht schema.
    Cheers
    Ps I comment on programmer I have on the other tread
  • Ashraf HZ
    Ashraf HZ
    Yeah, you can solder all the components together with the vero board. Don't forget to get a DB-9 connector and a serial cable for communicating between the PIC and computer.
  • Harshad Italiya
    Harshad Italiya
    ash
    GF, can you try WinPic800 to bootload the 16F?
    Nop I had not tried on that i had tried on MPLAB only. and now Shifted to The new circuit cause this is very urgent for me. i will work on older circuit later but i am sure i will run that PCB also..
  • Harshad Italiya
    Harshad Italiya
    Rifaa
    OK GF and ash ..seriously. I really donno what in god's name you two are talking about.
    I mean I can repair TV's ..hi fi's, DVD players and anything concerning power electronics, but i really have no idea when it comes to PIC's and boot load,ICD2 or what ever.
    So you have to be very specific if you are telling me about PIC's.
    And about the schema, do you want me to make a PCB from it or what. I mean I can vero board it, no biggie.
    As for the PIC I have a 16F628-041/P and max 232 ..no problem. So I am good to go with tht schema.
    Cheers
    Ps I comment on programmer I have on the other tread

    So Rifaa you are also going to Make One?
  • Harshad Italiya
    Harshad Italiya
    @ ASH.. winpic800 compitible with 16F? on its Site there in no 16F in that List.!!
  • Ashraf HZ
    Ashraf HZ
    godfather
    @ ASH.. winpic800 compitible with 16F? on its Site there in no 16F in that List.!!
    I think you were looking at recently supported devices. The full list is here:

    #-Link-Snipped-#

    Don't worry, I've used it before. Works fine 😀
  • Harshad Italiya
    Harshad Italiya
    Oh yes you are right.
    thanks for this link Ash.

    i am using ICD2 clone to load the bootloader in 16F877A.. so this winpic800 is compitible with that hardware or there is any specific Hardware required?
  • Ashraf HZ
    Ashraf HZ
    Ah, that I'm not sure. Give it a shot.
  • Rifaa
    Rifaa
    Yeah GF. If I make one, then only I am improving my knowledge about the basic concepts of how it works.
    I would rather make one than buy one.
    I bought one because I cannot get help on this topic.
    I owe a lot to CE. I'll do my part to this community, and U can count on it CE's
    Thanks again to all
  • Rifaa
    Rifaa
    One more Q? GF.
    Do I have to solder the PIC. Or can I use a ZIF socket.
  • Harshad Italiya
    Harshad Italiya
    you can use ZIF socket buddy.. i am going for Lunch...i will answer your Question soon.
  • Rifaa
    Rifaa
    So which means that we can replace with another PIC to programmer something else, if the ZIF socket is there.
    Still, I've been thinking, doesn't the programmer have a PIC to do all the work.
    See, this is what confuses me...
    At first ( few years back )when I thought of learning programing I researched a lot and found some shema's of programmers. I wanted to build one then, but the thing is every damn programmer has a PIC in it and what I have leaned is that, if a PIC is used, then it should be programmed to do whatever one want's.

    So the point is how the heck am I going to program the programmer in the first place, if I don't have a programmer. get my point....😕
    I mean, I need to have a programmer to built a programmer so that I can program the programmer in order to program other PIC's 😕
    Confused HUH!!!.

    This was my predicament.. and I cannot get any help from any one, my colleges who I used to work with are too proud to teach me anything, cause they have their Masters and PhD's and all, and I am just certificate holder...so they hesitated to tell me any thing, cause when it comes to field work and trouble shooting, none of them could beat me even slightly.
    My knowledge and skills on repairing consumer electronics and power electronics are far better than they ever hope to be, so they did not want me to know anything about programming and they call themselves my friends. the #@$@2 lot. Still I am proud of what I am, I have worked hard to earn my reputation here and I deserved more, but fate has a funny sense of irony. Well..that's that. past is history. I'm moving on, on my terms now, and I will learn one way or the other. Someday they will see too. and I guess I'll have CE to thank for this. hmmmm.

    So...without a programmer I was getting no where, until you gf, started this thread, and the best part was, it started when I got my hands on my very first programmer. The Willem PRO...😁. I am sooooooooooooo happy.

    Now what I thought was the PIC is the brain of the programmer.. well something like tht.
    I was wondering where is the brain of the programmer you showed GF
  • Ashraf HZ
    Ashraf HZ
    That was also a confusion I faced when I first started out with PICs. There are different types of programmers out there with multiple features and electrical characteristics. Some directly load the firmware (or bootloader) without additional frills, while others provide debugging options. Keeping that in mind, they also have different types of software that can be used. Some programmers use low voltages, while others use high (like 13V).

    One of the most simplest types of programmers is the JDM one. This is what I started out with anyway. Theres no PIC component in there (except the one you are trying to program). Below is a very popular one:
    #-Link-Snipped-#

    Then I moved on and bought a PicKit2 clone. So far its okay.

    The one GF is attempting to do is another type called ICD2. It is more reliable and stable compared to cheaper programmers (like JDM), I believe. And has a debugging feature 😀

    Once you have loaded the firmware on the PIC, you do not have to use the programmer again. You can directly load the HEX files via the TX/RX pins of the PIC, using RS232.
  • Harshad Italiya
    Harshad Italiya
    Well first of sorry Rifaa that you are not getting much help from my side...

    See here i am going to share my knowledge about this Programmer may be i am wrong in some cases but here it is...

    The programmer which i am going to make is a ICD2.... and its heart is PIC16F876. yes you need one programmer first to load the bootloader in the PIC16F876. after that you have to connect that ICD2 clone with MPLAB via RS232 Cable that MPLAB will automatically Load the Operting System file in the 16F876 and after that your ICD2 Clone is ready and by using that you can programm the other PIC controller with 5 programming lines. MCLR,PGD,PGC,Vcc,GND...

    i hop you get the overview and functionality of the programmer which i am going to make..

    i had studied about your programmer it sounds good and universal also will get knowledge of that more when got some time.

    @ash... Thanks for your help
  • Harshad Italiya
    Harshad Italiya
    as said by ASH some controller available which loads OS automatically with RX TX but here in our case first of all we have to load bootloader file must.
  • Rifaa
    Rifaa
    Oh goody.. my willem is quite good u say, now that's nice to know.
    So I was right huh!.. I need a programmer to make a programmer to program programmable IC'S. ..KEEKKE here we go again.
    SO GF , do you suggest that I use a ZIF socket where the PIC should be, Just like to be sure of what I am doing. only that needs to be detachable. Any other pointers I should no about, cause I am done with the LCD's ( almost , I'll put a thread on it, it will be quite interesting ), and after that I'll just vero board the schema u gave.
    Thanks to you all for this opportunity
  • Harshad Italiya
    Harshad Italiya
    Rifaa... the PIC16F876 shown here in Circuit diagram is Fixed if you place it in ZIF or Solder it no problem this is the Via IC means the Code which you want to load in your Target IC is connected at the target side and this 16F876 works as ICD2 and transfers that code to Target side so if you want to program other IC then just connect that at Connector... means here 16F876 is Fixed in which there is a ICD2 Clone Operating System.
  • Rifaa
    Rifaa
    So, u mean to say is that PIC will always be there, I don't have to take it out.
    To program a PIC it will be connected separately..right.
  • Harshad Italiya
    Harshad Italiya
    Yes Now you got the point Rifaa.
  • Harshad Italiya
    Harshad Italiya
    [FONT=Arial, Arial, Helvetica]The Registers.[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]A register is a place inside the PIC that can be written to, read from or both. Think of a register as a piece of paper where you can look at and write information on.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT] [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]The figure below shows the register file map inside the PIC16F84. Don’t worry if you haven’t come across anything like this before, it is only to show where the different bits and pieces are inside the PIC, and will help explain a few of the commands.[/FONT]
    [FONT=Arial, Arial, Helvetica][​IMG][/FONT]
    [FONT=Arial, Arial, Helvetica]First thing you will notice is that it is split into two - Bank 0 and Bank 1. Bank 1 is used to control the actual operation of the PIC, for example to tell the PIC which bits of Port A are input and which are output. Bank 0 is used to manipulate the data. An example is as follows: Let us say we want to make one bit on Port A high. First we need to go to Bank 1 to set the particular bit, or pin, on Port A as an output. We then come back to Bank 0 and send a logic 1 (bit 1) to that pin.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT] [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]The most common registers in Bank 1 we are going to use are STATUS, TRISA and TRISB. The first allows us to come back to Bank 0, TRISA allows us to select which pins on Port A are output and which are input, TRISB allows us to select which pins on Port B are output and which are input. The SELECT register in Bank 0 allows us to switch to Bank 1.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Let us take a closer look at these three registers.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]STATUS [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]To change from Bank 0 to Bank 1 we tell the STAUS register. We do this by setting bit 5 of the STATUS register to 1. To switch back to Bank 0, we set bit 5 of the STATUS register to 0. The STATUS register is located at address 03h (the ‘h’ means the number is in Hexadecimal).[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]TRISA and TRISB.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]These are located at addresses 85h and 86h respectively. To program a pin to be an output or an input, we simply send a 0 or a 1 to the relevant bit in the register. Now, this can either be done in binary, or hex. I personally use both, as the binary does help visualize the port. If you are not conversant with converting from binary to hex and vice versa, then use a scientific calculator.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]So, on Port A we have 5 pins, and hence 5 bits. If I wanted to set one of the pins to input, I send a ‘1’ to the relevant bit. If I wanted to set one of the pins to an output, I set the relevant bit to ‘0’. The bits are arranges in exactly the same way as the pins, in other words bit 0 is RA0, bit 1 is RA1, bit 2 is RA2 and so on. Let’s take an example. If I wanted to set RA0, RA3 and RA4 as outputs, and RA1 and RA2 as inputs, I send this: 00110 (06h). Note that bit zero is on the right, as shown:[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica]Port A Pin RA4 RA3 RA2 RA1 RA0[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica]Bit Number 4 3 2 1 0[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] Binary 0 0 1 1 0[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]The same goes for TRISB.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]PORTA and PORTB[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] To send one of our output pins high, we simply send a ‘1’ to the corresponding bit in our PORTA or PORTB register. The same format follows as for the TRISA and TRISB registers. To read if a pin is high or low on our port pins, we can perform a check to see if the particular corresponding bit is set to high (1) or set to low (0)[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Before I give an example code, I need to explain just two more register – w and f.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]W[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]The W register is a general register in which you can put any value that you wish. Once you have assigned a value to W, you can add it to another value, or move it. If you assign another value to W, its contents are overwritten. [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]An Example Code.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]I am going to give you some example code on what we have just learnt. Don’t try and compile this yet, we will do that when we come to our first program. I am just trying to show how the above is actually programmed and introduce a couple of instructions along the way. I am going to set up Port A as per the example above.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]First, we need to switch from Bank 0 to Bank 1. We do this by setting the STATUS register, which is at address 03h, bit 5 to 1.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] BSF 03h,5[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]The BSF Means Bit Set F. The letter F means that we are going to use a memory location, or register. We are using two numbers after this instruction – 03h, which is the STATUS register address, and the number 5 which corresponds to the bit number. So, what we are saying is “Set bit 5 in address 03h to 1”.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]We are now in Bank 1.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica]MOVLW b'00110'[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]We are putting the binary value 00110 (the letter b means the number is in binary) into our general purpose register W. I could of course have done this in hex, in which case our instruction would be:[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica]MOVLW 06h[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Either works. The MOVLW means ‘Move Literal Value Into W’, which in English means put the value that follows directly into the W register.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Now we need to put this value onto our TRISA register to set up the port:[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica]MOVWF 85h [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]This instruction means “Move The Contents Of W Into The Register Address That Follows”, in this case the address points to TRISA.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Our TRISA register now has the value 00110, or shown graphically:[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica]Port A Pin RA4 RA3 RA2 RA1 RA0[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] Binary 0 0 1 1 0[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica]Input/Output O O I I O[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Now we have set up our Port A pins, we need to come back to Bank 0 to manipulate any data.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica]BCF 03h,5 [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]This instruction does the opposite of BSF. It means “Bit Clear F”. The two numbers that follow are the address of the register, in this case the STATUS register, and the bit number, in this case bit 5. So what we have done now is set bit 5 on our STAUS register to 0[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]We are now back in Bank 0.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Here is the code in a single block:[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT] [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]BSF 03h,5 ;Go to Bank 1
    MOVLW 06h ;Put 00110 into W
    MOVWF 85h ;Move 00110 onto TRISA
    BCF 03h,5 ;Come back to Bank 0
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Read this through a couple of times, until it is you can follow it. So far we have looked at 4 instructions. Only 31 to go![/FONT]
  • Harshad Italiya
    Harshad Italiya
    [FONT=Arial, Arial, Helvetica] Writing To the Ports.[/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]In the last tutorial, we I showed you how to set up the IO port pins on the PIC to be either input or output. In this tutorial, I am going to show you how to send data to the ports. In the next tutorial, we will finish off by flashing an LED on and off which will include a full program listing and a simple circuit diagram so that you can see the PIC doing exactly what we expect it to. Don’t try and compile and program your PIC with the listings here, as they are examples only.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]First, let us set up Port A bit 2 as an output:[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]bsf 03h,5 ;Go to Bank 1
    movlw 00h ;Put 00000 into W
    movwf 85h ;Move 00000 onto TRISA – all pins set to output
    bcf 03h,5 ;Come back to Bank 0
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]This should be familiar from the last tutorial. The only difference is that I have set all of the pins on Port A as output, by sending 0h to the tri-state register.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Now what he have to do is turn an LED on. We do this by making one of the pins (the one with the LED connected to it) high. In other words, we send a ‘1’ to the pin. This is how it’s done (note the comments for an explanation of each line):[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]movlw 02h ;Write 02h to the W register. In binary this is 00010, which
    ;puts a ‘1’ on bit 2 (pin 18) while keeping the other pins to ‘0’
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]movwf 05h ;Now move the contents of W (02h) onto the PortA, whose
    ;address is 05h
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]So, now our LED is on, we now need to turn it off:[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]movlw 00h ;Write 00h to the W register. This puts a ‘0’ on all pins.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]movwf 05h ;Now move the contents of W (0h) onto the Port A, whose
    ;address is 05h
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]So, what we have done is turn the LED on then off once.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]What we want is for the LED to turn on then off continuously. We do this by getting the program to go back to the beginning. We do this by first defining a label at the start of our program, and then telling the program to keep going back there. [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]We define a label very simply. We type a name, say START, then type the code:[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Start movlw 02h ;Write 02h to the W register. In binary this is
    ;00010, which puts a ‘1’ on pin 2 while keeping
    ;the other pins to ‘0’
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT] [FONT=Arial, Arial, Helvetica] movwf 05h ;Now move the contents of W (02h) onto the
    ;PortA, whose address is 05h
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT] [FONT=Arial, Arial, Helvetica] movlw 00h ;Write 00h to the W register. This puts a ‘0’ on
    ;all pins.
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT] [FONT=Arial, Arial, Helvetica] movwf 05h ;Now move the contents of W (0h) onto the Port
    ;A, whose address is 05h
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] goto Start ;Goto where we say Start[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]As you can see, we first said the word ‘Start’ right at the beginning of the program. Then, right at the very end of the program we simply said ‘goto Start’. The ‘goto’ instruction does exactly what it says.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]This program will continuously turn the LED on and off as soon as we power up the circuit, and will stop when we remove power.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]I think we should look at our program again:[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT] [FONT=Arial, Arial, Helvetica] bsf 03h,5
    movlw 00h
    movwf 85h
    bcf 03h,5
    Start movlw 02h
    movwf 05h
    movlw 00h
    movwf 05h
    goto Start
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]OK, I know I have left the comments off. But, do you notice that all we can see are instructions and numbers? This can be a little confusing if you are trying to debug the program later, and also when you write the code you have to remember all of the addresses. Even with the comments in place, it can get a bit messy. What we need is to give these numbers names. This is accomplished by another instruction: ‘equ’.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]The ‘equ’ instruction simply means something equals something else. It is not an instruction for the PIC, but for the assembler. With this instruction we can assign a name to a register address location, or in programming terms assign a constant. Let us set up some constants for our program, then you will see how much easier to read the program is.[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]STATUS equ 03h ;this assigns the word STATUS to the value of 03h,
    ;which is the address of the STATUS register.
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]TRISA equ 85h ;This assigns the word TRISA to the value of 85h,
    ;which is the address of the Tri-State register for PortA
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]PORTA equ 05h ;This assigns the word PORTA to 05h which is the
    ;address of Port A.
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]So, now we have set up our constant values, let us put these into our program. The constant values must be defined before we can use them, so to be sure always put them at the start of the program. I will re-write the program without comments again, so that you can compare the previous listing to the new one:[/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]STATUS equ 03h
    TRISA equ 85h
    PORTA equ 05h

    bsf STATUS,5
    movlw 00h
    movwf TRISA
    bcf STATUS,5
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Start movlw 02h
    movwf PORTA
    movlw 00h
    movwf PORTA
    goto Start
    [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT]​
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica] [/FONT]
    [FONT=Arial, Arial, Helvetica] [/FONT][FONT=Arial, Arial, Helvetica]Hopefully, you can see that the constants make following the program a little easier, even though we still have not put the comments in. However, we are not quite finished.[/FONT]
  • Harshad Italiya
    Harshad Italiya
    #-Link-Snipped-#
    After Reading all the Tutorials let us make some Projects based on PIC...

    Yes First of all we are going to make PIC programmer which is under Construction at my Side...

    @Rifaa... Have you Made the one.?
  • Rifaa
    Rifaa
    on the way GF. Almost done now
  • Harshad Italiya
    Harshad Italiya
    My PCB is also on way.!!
  • Kaustubh Katdare
    Kaustubh Katdare
    Are these tutorials authored by you, Godfather?
  • debu
    debu
    The_Big_K
    Are these tutorials authored by you, Godfather?
    Nope. These tutorials were originally witten by Mr. Mike Stracey, and are available on his #-Link-Snipped-#.

    Regards,

    Debu 😀
  • Kaustubh Katdare
    Kaustubh Katdare
    Thank you, Debu. Godfather - it would be nice if you could post original content.
  • Rifaa
    Rifaa
    Heh GF will a 16F872 PIC do, I can't get my hands on a 16F876
  • Harshad Italiya
    Harshad Italiya
    I had already said that this is the collection of good tutorials which i got when i was learning PIC.

    Sure biggie i will come up with LPC9xx information soon..
    i will display them nicely.
  • Rifaa
    Rifaa
    GF .... you didn't read my post?
    It's like next to the one you just read 😔
  • Harshad Italiya
    Harshad Italiya
    I had read that buddy whats the problem 16F876 is not available in Market!?

    let me search that is 16F872 is compatible with it...

    as i had seen in datasheet 16F87x is the common series then also i will check the pin detail and functional working then i will let you know that is it ok or not..

    Sorry for missed that post :worthy:
  • Rifaa
    Rifaa
    😔 nope....
  • Harshad Italiya
    Harshad Italiya
    Rifaa 872 is not used instead of 876 cause of they have different RAM size. will read more on that if i got something but i am sure that 872... we cant use this
  • Rifaa
    Rifaa
    too bad... so I'm checkin ebay to buy one PIC, for the time being I have to do with the Willem one.
    Is there any thing else (any more components ) that I should get my hands on, since I don't wanna buy twice, u know postage charges.
    Thanks anyways
    Rifaa
  • Harshad Italiya
    Harshad Italiya
    Sorry for that Rifaa.

    i will give you information if i come to know about any new things
  • paleng
    paleng
    nice and informative post.......
  • Harshad Italiya
    Harshad Italiya
    welcome to CE paleng
    can you please introduce yourself in introduction area please so we can know each other
  • shahrul
    shahrul
    I have done some example for PIC like
    1. LED #-Link-Snipped-#
    2. Seven Segment #-Link-Snipped-#
    3. Switch/Push Button #-Link-Snipped-#
    4. Keypad #-Link-Snipped-#
    5. Dot Matrix #-Link-Snipped-#
    6. LCD #-Link-Snipped-#
    7. Servo Motor #-Link-Snipped-#
    and tutorial for using MPLAB #-Link-Snipped-#
  • Rifaa
    Rifaa
    Is this thread dead or what.
    I have been studying PIC while I was away and I am proud to say I was able to write my very first code to a 12F629 and it works.

    C'mon guys, can we keep this thread alive

    Rifaa
  • shahrul
    shahrul
    Rifaa
    C'mon guys, can we keep this thread alive

    Rifaa
    Yes, I'm supporting. I also continue to do PIC Lesson for interface various type of module for PWM, UART, I2C and many more.
  • Rifaa
    Rifaa
    hey shahrul, can u tell me what softy do u use to write and compile and what PIC's you are familiar with. I like to chit chat, cause I'm just beginning programing and I like to have some one to help me from time to time.
    If you are willing that is.

    Thanks
    Rifaa
  • shahrul
    shahrul
    Rifaa
    hey shahrul, can u tell me what softy do u use to write and compile and what PIC's you are familiar with. I like to chit chat, cause I'm just beginning programing and I like to have some one to help me from time to time.
    If you are willing that is.

    Thanks
    Rifaa
    I'm using MPLAb Software with Hi-Tech C compiler. Programming C language. Programmer USB ICSP.
  • Rifaa
    Rifaa
    that's good, I'm using the same thing to compile.
    I'm using a Willem Pro Programmer.
    So we can share codes, right?

    Rifaa
  • shahrul
    shahrul
    Yes, I don't mind to share coding.
  • Rifaa
    Rifaa
    thanks. I'll keep in touch

    Rifaa
  • Harshad Italiya
    Harshad Italiya
    Rifaa
    Is this thread dead or what.
    I have been studying PIC while I was away and I am proud to say I was able to write my very first code to a 12F629 and it works.

    C'mon guys, can we keep this thread alive

    Rifaa
    Sorry for the dead thread Rifaa actually i am busy with my 8th sem and not working on PIC for some time thats why not updated.
    shahrul
    Yes, I'm supporting. I also continue to do PIC Lesson for interface various type of module for PWM, UART, I2C and many more.
    Thanks Shahrul for keeping this thread alive. I know you have good Experience with PIC.
  • Rifaa
    Rifaa
    Hey shahrul I'm having a bit of confusion with a some code directive
    I presume u know them better than me.
    I would refer to a code to base my question.
    example code, taken from a counter programe which I am trying to figure out how it works, PIC is 16F88

    pls note his counter is tested by me practically, and taken from a pic project site, since the author has told no to emails, I have to ask somewhere else.

      org    0x2100
        de    0xFF    ; mode 
    
        org    0x2101
        de 0,0,0,0        ; counter preset
        
        org    0x2108
        de "counter.asm"
    
             errorlevel -302     ; suppress banksel warning messages
              errorlevel -311     ; suppress HIGH operator warning messages
    
        cblock 0x20
        debounce
        Unit
        Tens
        Hund
        Thou
        unit7seg
        tens7seg
        hund7seg
        thou7seg
        indLED
        mplxrot
        keyNow
        keyLast
        keyEdge
        loadUnit
        loadTens
        loadHund
        loadThou
        state
    
        endc
    
        cblock 0x70
        Wisr
        Sisr
        flags
    
        endc
    
    #define   bank0     bcf       STATUS,RP0          ; Sel Bank 0
    #define   bank1     bsf       STATUS,RP0          ; Sel Bank 1
    
    ; indicator LEDs 
    overflow    equ 0
    hold    equ 1
    up    equ 2
    down    equ 3
    
    
    ; flag bits
    invert    equ 0
    suppressZeros    equ 1
    resetNext    equ 2
    keysReady    equ 3
    intEdge    equ 4
    
    #define    digitDrive     PORTA
    #define    segmentDrive    PORTB
    #define    overflowPulse    PORTA,7
    #define    switchIn    PORTA,6
    #define    countIn    PORTB,0
    I'll base my questions from top.
    I understand the org 0x00, which means the reset vector, or the way I understand it, it is where the PC starts. Am I right.
    But in this code it states
    Q1: ORG 0x2100 ; why does it states 0x2100, I mean if "2100" is in HEX, then I cannot find that value in the F88 register map, maximum value is 1FFh, so how the heck 2100 comes here.

    Q2: "de 0xFF ; mode"
    what is "de" and why "0xFF" ?

    Q3: "ORG 0x2101
    de 0,0,0,0 ; counter preset"
    again why "0x2101" and what is "de 0,0,0,0" why this part is commented as counter preset?

    Now I understand cblock 0x20, which is used assign 8 bit variables to sram or PGR's for context saving, but there is one thing I like to clarify.

    Q4: The GPR starts at 20h from bank0 and ends at 7Fh, which is 96 bytes
    Correct me if I am wrong. cblock 0x20 to 7Fh gives me a total of 96 8bit registers to save whatever data I want. By looking at the cblock in the above code it starts from "debounce" and ends to "state". which assigns 19 8bit variables, Right?

    Q5: So does this mean that I can assign 77 more variables and if I were to go above that I have to end the first cblock and start another "cblock at 0xA0h in bank1. below is an example of what I would write when assigning variables , say 100 variables.

    ; Variables
    cblock 0x20
    1st variable
    x
    go's
    till
    x
    96th Variable
    endc

    cblock 0xA0h
    97th variable
    x
    to
    x
    100th variable
    endc
    Is this correct, ( or does it require banksel directive?

    Q5: Looking back to the code which states
    ; indicator LEDs
    overflow equ 0
    hold equ 1
    up equ 2
    down equ 3


    ; flag bits
    invert equ 0
    suppressZeros equ 1
    resetNext equ 2
    keysReady equ 3
    intEdge equ 4


    The counter has indicator leds which are multiplex with the segment drive PORTB, which are overflow, hold, up and down. now thw codes states that these are equal to 0 thru 3, now
    one problem is whether "EQU" is same as "RES" directive, and
    what does the 0 thru 3 states ( this numeric value is what I do not understand). I ask this because the code has the same value equate to invert, suppressZero, resetNext and keysReady.
    Why the same value assigned to different things
    Why is that, and how does the assembler know that some are indicators and other are flags bits without a label.

    No tutorial or no one has ever explained these, and I have read all there is on the net and I am tired of searching

    Q6:
    Last is the define directive
    #define digitDrive PORTA
    #define segmentDrive PORTB
    #define overflowPulse PORTA,7
    #define switchIn PORTA,6
    #define countIn PORTB,0

    Now I understand define up to some extent what confuses me are these.
    #define digitDrive PORTA
    this definition manipulates the PORTA when ever a digitDrive is used in the code. Now in this counter PORTA has inputs and outputs set. so if the codes tells to write to digitdrive won't it effect the overflowpulse at PORTA, 7, or is it that by defining PORTA,7 seperately, the digitdrive command excludes PORTA,7.
    Same goes for PORTB, segmentdrive outputs and switchin and countin inputs.

    If any one can clarify me these I would really appreciate it.

    Thanks

    Rifaa
  • shahrul
    shahrul
    Rifaa, That is assembly code. I did't use assembly anymore. Also, I never seen the 'de' command.

    You can refer my C language #-Link-Snipped-#.

You are reading an archived discussion.

Related Posts

hi everyone, i m new on this place, when i was searching some ideas for my mechanical mini project then i found this place n joined, n i hope that...
these settings seem to be very confusing.........😒 1) are these settings static or dynamic???????? if dynamic what factors affect toe in and toe out????????? how do they vary with slip...
in simple loop generator when conductor move in magnetic field ,voltages induced in conductor. i am confused abt the position of the conductor. the maximum voltages induces when conductor is...
pls help me... i doing my CCNA but i m not certified yet. My primary interest in networking. Is there any growth in Networking so can go ahead. & what...
I dont know about companies which give training for electronics and communication stream..plz tell me...and also the fee structure of that... Which is cheap and best