Showing posts with label PIC. Show all posts
Showing posts with label PIC. Show all posts

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.

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.

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.