Thursday, July 14, 2011

MikroC

MikroC is a C Compiler for PIC as well as some other microcontroller. It has large precompiled library to be used in accessing the peripheral that are connected the PIC microcontroller. MikroC has an independent IDE which doesn't need to be plugged in to MPLAB. In addition, MikroC IDE has numerous tools such as ASCII chart, EEPROM Editor, UART Terminal, GLCD Editor and etc which will further be described later.

Setting up MikroC project



  1. Click Next.




Figure 1: New Project Wizard


  1. Select the Device that you want to use. Click Next.




Figure 2: Choosing Device


  1. Select the clock frequency. Click Next.




Figure 3: Setting the clock


  1. Choose the location where the files are to be saved. Click Next.




Figure 4: Specifying the location of the file to be saved


  1. Here, you can choose to include all library provided by MikroC or select a few that you will be using if you know which has to be used. Click Next


All the libraries that are not used have to be removed by unselect all the libraries later.



Figure 5: Choosing the library


  1. Select Open Edit window to set Configuration bits. Click Finish.




Figure 6: Last step in new project Wizard


  1. Figure 7 will be prompt. Choose the appropriate configuration fuse settings. Click OK.




Figure 7: Project configuration fuse settings


  1. Add the related .c files to sources and .h to header files. Other files may also be added. You should have similar layout as Figure 8.




Figure 8: Project Manager


  1. Figure 9 shows the MikroC libraries. All libraries that are not used should be unselected. Otherwise, it might cause compilation error for some cases.




Figure 9: MikroC libraries


  1. Go under tools you will find the list of additional tools that might help you perform your codings.




Figure 10: Additional useful tools in MikroC


  1. Click Build. If your code is error-free, you should get Figure 11 stating Finished successfully.




Figure 11: Compilation status

Conclusion


Learning how to use a compiler such as MikroC would help beginners to build their project quickly as advance peripheral libraries are provided. These libraries are: USB, GLCD as well as Ethernet and etc.

Sunday, July 3, 2011

Using Serial UART

UART is an essential interface to PC. Below shows how to use and set the configuration bits in C18. The purpose of the codes below is to loop back to the Hyperterminal the word "Hello" to make sure the settings of the PIC works accordingly. OpenUSART() function is provided by C18 and it is used to set the neccessary registers in PIC18F series microcontroller.



For some that uses a USB to Serial converter, they are required to test the converter is working properly. Some have been reported not working. The converter can be test by connecting pin 2 and pn 3 together. This allows the data sent from Hyperterminal to loop back to the terminal. After this is done, the above codes can be used to make sure the serial is function properly.



Figure 1: Connecting pin 2 and pin 3 of seriall




Figure 2: Terminal showing the loop back

Conclusion


These test allows you to save time by debugging the necessary erroneous parts in your development.

Simple power supply for PIC MCU

The power supply for PIC is either 5V or 3.3V. Let us begin with simple 5V regulated power supply. Figure 1 illustrates Proteus simulation for the 5V supply. Note that the supply voltage to the 7805 has to be at least 2V from the regulated voltage. Thus, in this case, the supply voltage has to be 7V and above.

In some cases, you might want to use it with PIC24F and also dsPIC devices that needs 3.3V. Thus, it is better to have both 3.3V and 5V.



Figure 1: 5V using 7805


Figure 2 and figure 3 shows a simple circuit that will provide 5V and 3.3V. Here, a voltage divider is used to convert the 5V to 3.3V. The switch, SW1 can be replaced with a 3-pin connector. Thus, this will enable you to have, 5V, 3V and GND on the 3 pins.

Figure 2 shows direct output 5V from the regulator while Figure 3 shows the output from the regulator is passed through a voltage divider to get 3.3V.



Figure 2: Direct output from regulator




Figure 3: Output from a regulator through a voltage divider

Conclusion


Having 5V and 3.3V, power supply could be very handy. IC's such as PIC24F would require 3.3V while Max232 requires 5V. Therefore, in this case, you would require both 5V and 3.3V

Simulating PIC with Proteus

Simulating circuits in virtual environment can save both time and cost to produce the hardware. So far, Proteus provides a wide range of PIC simulation from PIC16F to dsPIC33F. It also enables the developer to check for the functionality as well as coding errors such as logical conditioning errors when coding.

Figure 1 shows the Proteus simulation environment.

  1. Click on component mode -> P to pick the devices.




Figure 1: Selecting a device in Proteus


  1. Figure 2 will pop up. Here, type the device for simulation. In this case, type PIC18F4550 in keywords. Then, click OK.




Figure 2: Pick the device


  1. Put the device selected in the blue box. Figure 3 shows the pin layout of the PIC18F4550.




Figure 3: PIC18F4550


  1. Other devices such as LED and resistors are added by repeating steps 1 to 3.




Figure 4: Adding resistor and LED


  1. After adding the necessary devices, connect them together by clicking on the end of one device to another device. Once this has been done, you should have the same layout connection as Figure 5.




Figure 5: Full circuit for simulation


  1. Double click on PIC18F4550 will bring up Edit Component GUI. Here, you can set additional settings for your PIC. Then, load the hex code generated by the MPLAB in Program File column and click OK.


The sample source code using C18 is provided below.



Figure 6: Configuration in PIC18F4550


  1. At the far left, where the simulation panel is located, click on run to begin the simulation. Alternatively, you can pause, halt as well as simulation by frame.




Figure 7: Simulation panel

Source Code



Conclusion


This simulation will provide another platform to test and debug your PIC program in virtual environment. In addition, the developer can focus in troubleshooting their codes without having to worry about their hardware failure.

Troubleshooting Pickit 2 Clone

Pickit 2 is a multipurpose tool : PIC programmer, debugger, UART and Logic analyzer provided by Microchip and the schematics is provided in Pickit 2 datasheet.

Schematics:



Specifications:



  1. Easy to use Windows® programming interface for programming Microchip's Flash family of microcontrollers

  2. UART Tool software for direct serial communications with a microcontroller RX/TX pins through the PICkit 2.

  3. Logic Tool software for simple logic signal stimulus and monitoring, with a 3-channel logic analyzer.

  4. PICkit 2 Programmer-To-Go support for programming devices without a PC.

  5. Code Examples in assembly and C.


Test methods:


Hardware – based:



  1. Ensure that PIC18F4550 is programmed.

  2. The VPP generated voltage should be about 11V to 13V while VDD is 5V.

  3. The P MOS used need to be IRLML6402 to provide enough power to programming.


Software-based:


Pickit 2 Troubleshoot can be found in TOOLS -> TROUBLESHOOT.. After you have clicked on the button, a pop up menu as shown below will appear. Click on NEXT.


The first step of troubleshooting is to verify the VDD. For PIC18F, the VDD is 4.5V, while for PIC16F, it is about 3.6V. Click on TEST to measure the VDD. The VDD can be further verified by using a multimeter at pin 2 and pin 3 of the ICSP.


Once the TEST button is clicked,  there will be a message whether it passed the VDD test or failed. If the VDD test failed, then, it is necessary to check whether the installation of components is correct.



Assuming the VDD test has passed, we will go on to verify the VPP now. The VPP for PIC18F is about 12V. This can also be verified further using a multimeter. For PICKIT 2 clones, it is often caused by the MOSFET( BS250) which is most likely used as a substitute for IRLML6402. Here, you can also test for MLCR 'high' and 'low'.


Once the VPP test has passed, we will move on to troubleshoot the PGC and PGD using a frequency counter or frequency measurement equipment. The range of the frequency when it is toggled at 30kHZ is about 26 – 28kHz. You may also try to pull the PGC and PID pin to 'high' or 'low' and measure it with your multimeter.


After everything has been done, click on the FINISHED button. Now, you are ready to use your pickit 2.

Conclusion


Pickit 2 is a great multipurpose tool that enables you to program and debug a PIC microcontroller. In addition, it also has a UART tool, and logic analyzer.

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.