Sunday, July 3, 2011

Using PIC18F4550 ADC in MCC18

ADC is essentially important as most of the sensors acquire data in analogue manner. Thus, for the microcontroller to process, these data are required to be converted to digital form. In PIC18F4550, the ADC span from portA to portB giving the developer 13 channels of ADC. Figure 1 and 2 shows the Registers related. Here, we would focus on ADCON1 register.



Figure 1: Registers related to portA




Figure 2: Registers related to portB


Let's begin with ADCON0 register. This register is used to select the channel for ADC from channel 0 to channel 12 by controlling CHS3: CHS0. ADON will be used to on/off the ADC module while to determine whether the ADC conversion has completed, GO/DONE' can be checked. When GO/DONE' = 1, the ADC conversion is completed. Vice versa, when GO/DONE' = 0, this means that the A/C conversion is completed and idle.



Figure 3: ADCON0 control registers


Another important register to be taken care of when using ADC is ADCON1. This register allows to set whether the input channel will be digital or analogue by using PCFG3: PCFG0, which is bit 0 to bit bit 3 of ADCON1. VCFG1 and VCFG2 allows you to have some external voltage reference.



Figure 4: ADCON1 control registers


Upon setting ADCON0 and ADCON1, ADCON2 has to be set also. This register is used to control the format and the A/D acquisition time. In addition, it is also used to select the A/D conversion clock. By referring to Figure 6 and 7, the appropriate minimum acquisition time is calculated and AD clock source is chosen based on the PIC clock frequency. After doing so, then, the appropriate settings can be set.



Figure 5: ADCON2 control registers





Figure 6: Equations to calculate minimum acquisition time




Figure 7: AD clock source settings


Figure 8 shows the A/D block diagram. From here, we should notice that this device's ADC has some limitation. That is, only one channel can be active at a time by controlling CHS3: CHS0. Thus, simultaneous acquiring for data more than 1 channel is impossible but parallel ADC IC can be added to perform this task.



Figure 8: A/D Diagram

Steps to configure ADC




  1. Configure ADC:


    1. Configure analogue pins, voltage references as well as digital I/O through ADCON 1

    2. Set A/D input channel through ADCON0 (12 channels but only one can be selected at a time)

    3. Select A/D time acquisition time and conversion clock through ADCON 2

    4. Turn on ADC through ADCON0




  2. If interrupt is acquired, it can be set by:


    1. ADIF = 0

    2. ADIE = 1

    3. GIE = 1



  3. Wait for acquisition time if acquired

  4. Start conversion by setting GO/DONE' bit in the ADCON0


  5. Wait for the ADC to complete by:


    1. Checking for the GO/DONE' bit to be cleared

    2. Wait for ADC interrupt



  6. Read the A/D Result Register ( ADRESH: ADRESL) ADIF = 0 if interrupt is used

  7. Go to step 1 to reconfigure another pin to acquire data or continue by going through step 3 to 5.

  8. A/D conversion per bit is defined as TAD.
    Before the next acquisition can be done, a minimum of 3 TAD is required.


Sample coding







The sample code above acquires data from AN0 or channel 0 and display them to PC using serial. This is to verify that our sampling data is correct.

Conclusion


Setting up the ADC correctly is important to make sure that we acquire the correct data.

Starter to MPLAB

MPLAB is the main tool used to compile programs for PIC microcontrollers from PIC10F to dsPIC33F. The program can be written in Assembly, which the default MPLAB is set or can be plug-in from other compilers such as C18 and Hi-Tech C.

Setting up MPLAB



  1. Project -> Project Wizard…




Figure 1: Main MPLAB interface


  1. Click on Next




Figure 2: Project Wizard Interface


  1. The device needed has to be selected and click Next.




Figure 3: Selecting device




Figure 4: Selecting language toolsuite


  1. Save the new project files.




Figure 5: Creating a new project file


  1. If previous codes or the codes have been saved in the computer, add the codes using Add or remove using Remove. Then, click Next.




Figure 6: Add existing files to project


  1. Click on Next after confirmed the device and toolsuite has been correctly selected. If not, click on Back to edit.




Figure 7: Summary of the project created


  1. Click Save to save the workspace in the same directory with the same name.




Figure 8: Save workspace


Figure 9 shows you the interface you should get after performing the above steps.



Figure 9: Project view


  1. Source file is added by Right click on Source Files -> Add Files…




Figure 10: Adding source to project


  1. If other compiler is used instead of Assembly, then the Linker- Script Search location has to be added. This can be done by Project -> Build Options.. -> Project. By doing so, this will bring up Figure 11.




Figure 11: Linking the project to toolsuite linker file


  1. Select the Show directories for: drop down and select Linker-Script Search Path -> New and redirect it to ..\..\MCC18\lkr depending on where you installed your MCC18. For this example, C18 compiler is chosen.




Figure 12: Adding linker file to project


  1. Before building the program, the linker file for the chosen device has to be added as well in Linker Script folder as shown above.


Instead of just building the hex file to program the device, MPLAB also includes a simulator to simulate the program. The simulator can be selected by: Debugger -> MPLAB Sim.



Figure 13: Using MPLAB Sim and Breakpoint


  1. By clicking on the different line of the code, breakpoints can be insert. The breakpoints is used to halt the program so that the developer can view the content of the register through Watch. To bring out the watch window, View -> Watch.




Figure 14: Register view in Watch


Figure 15 shows the execution of the codes until the for loop with the green arrow indicating the current executed line of codes.



Figure 15: TRISD and PORTD initialized to 0


  1. By stepping through the for loop, the register values in the watch window changes. This will ease the verification of the code.




Figure 16: TRISD and PORTD values changes after step through

Conclusion


MPLAB is a great tool that in-cooperate the generation of hex for programming as well as debugging using virtual tool, MPLAB Sim or Proteus VSM plug-in. Additionally, MPLAB also provides an interface for programming using various microchip programmer from Pickit 2 to Real ICE.

Souce Code Management with Tortoise Hg

Tortoise Hg is open source, source management software. With this software, user can modify the content of the file without having to rename the file. In addition, you can even see the versions of your files in graphical form. That is, how your file is being modified. I often used it to keep track my PCB layout as most of the time, once too much editing is done, the PCB layout tools will stop you from returning to the previous steps. Thus, this tool can be used not only with codes, but also with files. The method of setting up a repository and method of using it will be further discussed below:

Using TortoiseHg




Figure 1-1: Files to be added


Create a folder with the file or files to commit. Right click anywhere in the folder -> TortoiseHg -> Create Repository Here. By doing so, TortoiseHg init will be prompted as shown in Figure 1-2.



Figure 12: Setting up a repository in the folder itself


If you need to run the commit directly, select "Run Commit after init". If not, just click on create. In Figure 1-3, you can see that .hg and .hgignore has been created.



Figure 13: .hg folder and .hgignore is created




Figure 14: Adding comment and select files to commit


If you have selected "Run commit after init", then, this window will be prompt. If not, click on the file -> Right hand click -> Hg Commit. Then, this window will be prompt. Select the files to commit and press commit.



Figure 15: Files that are committed to the repository


Once the files or folder has been committed, a green tick is created on the files or folder icon.



Figure 16: Test1.doc has been modified


Any further modifications on the file will give the indication as shown in Figure 1-6.



Figure 17: update to the previous version of the file


If you have modified your initial files too much and you intend to return to the last commited template, you can do so, by right click on the file -> Hg Repository Explorer -> click on the previous file, which you want to return to. Then, the window in Figure 1-7, will be prompt. After that, click update.


Figure 1-8: Shows the time the file has been modified and new v1.00 is created from the initial document file


After updating, you can edit your current documents and commit them to the repository. Once it is done, open up the Hg Repository Explorer, again by right click on the file -> Hg Repository Explorer. You should be able to see similar layout as in Figure 1-8.

Conclusion


This software is very useful to manage your source codes, as well as your files without having too many versions on the screen. Additionally, it allows the user to return to the previous files, which is limited by the undo button itself in most of the software.

Note: Remember to commit the files each time you are edit your files. Without doing so, you will not be able to return to your previous documents.

Setting up PIC oscillator




Setting up and verified the oscillator's settings are essentially important in order to determine the timing. Some functions such as ADC, timer, interrupt, I2C, SPI, USB, CAN and Serial require the oscillator to be set first as the settings of these functions depends the frequency of the oscillator. There are two modes of oscillator's settings: Primary Oscillator and Secondary oscillator. The frequency that the PIC is oscillating is usually mistaken by taking the crystal oscillator directly. Instead, the frequency should be the frequency measured at CPU or peripherals after the multiplexer.



Steps in setting up oscillator



  1. Choose the frequency that you want your PIC to run. Lower frequency will gives more power saving while higher frequency will give better performance.

  2. If Serial communication is used, survey on the best frequency for you to choose to minimize the baud rate error. This will lower the ranges of frequencies you can choose .

  3. For higher accuracy, external crystal oscillator should be used.

  4. External crystal oscillator is chosen by selecting the best combination that can be found through the settings of PLLDIV and CPUDIV.

  5. USB usually requires 48MHz. Thus, this is provided by dividing the 96MHz frequency from the PLL by 2 or 48MHz can be supplied directly through the crystal oscillator. It can also be provided by dividing the primary clock by 4. Extra registers that has to be configured are USBDIV,FSEN, and FSOC3: FSOC0

  6. Use PLLDIV and CPUDIV with HSPLL, ECPLL, XTPLL, ECPIO or XT, HS EC and ECIO to get to the intended frequency as close as possible.

  7. A simple LED toggling code is used to verify the CPU frequency.


Examples using different compilers




  1. MCC18




Information of definitions of the oscillator registers can be found in the PIC header files or directly in MPLAB.


This setting uses external crystal oscillator directly.



Sample LED toggling programme based on MCC18







  1. HiTech C




Information of definitions of the oscillator registers can be found in the PIC header files.


The same configuration setting as shown using MCC18



Sample LED toggling program based on HiTech C



Figure 01: Sample programme based on HiTech C



Conclusion


Setting oscillator settings of any microcontroller is very important to ensure that our programs running correctly. Here, PIC18F4550 is chosen as an example but the settings of most microcontrollers are the same.

Using Oshonsoft to simulate PIC18 circuits

Oshonsoft is software used to simulate PIC microcontrollers. Here, Oshonsoft PIC18 IDE is used. This software has a lot of peripheral that developers can use to simulate their system under Tools.



Figure 1: Oshonsoft PIC18 IDE interface

The settings



  1. Set the clock frequency by: double clicking on the 20MHz. Then, Figure 2 will be shown. Here, input your desired frequency and click ok.




Figure 2: Setting of clock frequency of PIC18F


  1. Next, set the PIC18F series to be simulated by clicking on the PIC18F series after microcontroller.




Figure 3: Selecting PIC18F model


  1. Before loading the program for simulation, set the speed of simulation : Rate -> Ultimate (for fastest simulation) .

  2. To load the program, File -> Load Program




Figure 4: Simulating with different speed


  1. In tools, you can bring out the peripherals you need that are in your system to simulate it virtually.




Figure 5: Peripheral tools and instruments that are provided


  1. Options allow you to tweak the peripherals, IDE layout, and the microcontroller to meet the specification of the hardware parts that will be used.




Figure 6: Changing the settings for peripherals, IDE and other schemes

Example


Using PIC18F Oshonsoft IDE Simulator, a program to light up an LED at PORTD,0 when a logic 1 is detected at PORTE,1 .



Figure 7: Example to detect a logic 1