Tag Archives: hi-tech c

Introduction to PIC Interrupts and their Handling in C

Interrupts are common features in almost all processor family, be it old 8051, AVR, PIC, ARM or the x86 used in desktops. So their in depth and clear knowledge is required for successful system software engineers. This guide will explain the interrupt system in general and their application using PIC18 architecture. We will also learn about handling of interrupts in HI-TECH C for PIC18. What are Interrupts? Interrupts, as the name suggests interrupts the normal execution and Requests and urgent attention of CPU. Interrupts are situations that the CPU can’t predict when they will happen, they can happen any time, so the CPU does not wait for them. So the CPU keeps on doing its normal job unless and interrupt occurs. For example when the USART (Serial Communication Hardware) will receive data is unknown, it can receive data any time. So the CPU keeps on doing its normal job, which may be for example read temperature using LM35 sensor and display on LCD. The CPU keeps on doing the "normal" job, but as soon as the USART receive data it informs the CPU using an interrupt. The CPU save its current state (so that it can resume), and jumps to the ISR (interrupt service routine) immediately. Where we can process the command or put it in a FIFO queue (to […]

Introduction to PIC18’s Timers – PIC Microcontroller Tutorial

Timers are common features of most microcontroller. In simplified terms a timer is just a register whose value keeps increasing (or decreasing) by a constant rate without the help of the CPU. The CPU can read or write this register any time. It reads it find out how much time has elapsed. The Timer register can have the following bit length 8 bit timers – These can count between between 0-255 16 bit timers – These can count between 0-65536 32 bit timers – These can count between 0-4294967296 A timer has a clock source, for example of the clock source of 10KHz is input to a timer then one increment will take 100uS (micro second). This clock source can be obtained from the CPU clock. The CPU clock of popular MCU ranges from 1 MHz to 20Mhz, this can be sometimes too fast. To help us out their is a thing called prescaler in the MCU. The job of prescaler is to divide the CPU clock to obtain a smaller frequency. The PIC Micro that we will use in this example has the following prescaler division factors available. 256 128 64 32 16 8 4 2 1 (Prescaler by-passed) Timers are also called Counters this is because they can be used to count external events. The following example illustrate […]

RS232 Communication using PIC18F4520’s USART – PIC Microcontroller Tutorial

Hello Friends! In this tutorial I will discuss how to practically do a simple communication over RS232 interface. For those who are completely new to this I clarify that the motive is to send and receive data between two device using a standard called RS232. RS232 is serial interface that means that data is transferred BIT by BIT at a time. Since data is transferred BIT by BIT so we need only a single wire two send data and an another one to receive data. One more common wire (called GND) is required between two separate circuit to enable current flow. So a total of three wire are required for communication. RS232 can be used to communicate between a variety of devices. Like your MCU and a GSM module or a PC. In this tutorial we will demonstrate a link between a PIC18F4520 MCU and a standard PC. On PC we will run a terminal program like RealTerm or Hyperterminal. A terminal program is used to send and receive text data. So any text send by the MCU will be visible on Terminal Screen and Any keypress you make on the PC keyboard will be send over RS232 to your MCU. This configuration is the simplest setup to test and understand RS232 communication. When you have enough knowledge you can […]

Multiplexed Seven Segment Display using PIC16F877A and HI-TECH C

#include <htc.h> #define _XTAL_FREQ 20000000UL typedef unsigned char UINT8; typedef signed char INT8; typedef unsigned int UINT16; typedef signed int INT16; //Connection of Seven segment display #define SEVEN_SEGMENT_LAT PORTD #define SEVEN_SEGMENT_TRIS TRISD //MUX Control #define MUX_PORT PORTB #define MUX_START_POS 1 //From which bit on port the select lines start //MUX settings #define MUX_DISP_COUNT 4 //Number of displays //Global Varriable UINT8 DisplayArray[MUX_DISP_COUNT];//Holds ‘data’ for each disp void SevenSegmentWrite(UINT16 n) { /* n=data to dislay example: n=1234 will display 1234 in a 4 segment display Working: This function breaks apart a given integer into separete digits and writes them to the display array i.e. digits[] */ UINT8 i=0; UINT8 j; while(n) { DisplayArray[i]=n%10; i++; if(i==MUX_DISP_COUNT) break; //We don’t have room for more digits n=n/10; } //Fill Unused area with 0 for(j=i;j<MUX_DISP_COUNT;j++) DisplayArray[j]=0; } void WriteSegment(UINT8 num) { switch (num) { case 0: //-GFEDCBA SEVEN_SEGMENT_LAT = 0B01000000; break; case 1: //-GFEDCBA SEVEN_SEGMENT_LAT = 0B01111001; break; case 2: //-GFEDCBA SEVEN_SEGMENT_LAT = 0B00100100; break; case 3: //-GFEDCBA SEVEN_SEGMENT_LAT = 0B00110000; break; case 4: //-GFEDCBA SEVEN_SEGMENT_LAT = 0B00011001; break; case 5: //-GFEDCBA SEVEN_SEGMENT_LAT = 0B00010010; break; case 6: //-GFEDCBA SEVEN_SEGMENT_LAT = 0B00000010; break; case 7: //-GFEDCBA SEVEN_SEGMENT_LAT = 0B01111000; break; case 8: //-GFEDCBA SEVEN_SEGMENT_LAT = 0B00000000; break; case 9: //-GFEDCBA SEVEN_SEGMENT_LAT = 0B00010000; break; } } void Wait() { UINT8 i; for(i=0;i<1;i++) __delay_ms(10); } void main() […]

Interfacing LCD Modules with PIC Microcontrollers.

A large number of embedded project require some type of user interface. This includes displaying numerical, textual and graphical data to user. For very simple numerical display we can use 7 segment displays. If the requirement is little more than that, like displaying some alphanumeric text, we can use LCD Modules. They are cheap enough to be used in low cost projects. They come in various sizes for different requirement. A very popular one is 16×2 model. It can display 2 lines of 16 characters. Other models are 16×4,20×4, 8×1,8×2 etc. In this tutorial we will learn how we can use such modules with Microchip PIC Microcontrollers. Here I will present my LCD library which you can use to create LCD based application/projects quickly. For demo I will use PIC18F4520 Microcontroller but you can use any PIC18 MCU. But you have to calculate the CONFIG values for correct setting and CPU clock selection etc. That means the chip should be configured correctly. See datasheet for more info on CONFIG bytes. MPLAB Project Creation First create a MPLAB project as described in this tutorial. Name the project LCD. Also add a main file called "lcd_test.c". To use my LCD library you need to add it to your project. Just copy/paste the following files to your project folder. Header Files lcd.h myutils.h […]

Hello World Project With PIC Microcontroller – Part I

Hello friends, welcome to this exciting tutorial were we will begin our journey with latest PIC18F micros from Microchip Technologies. This tutorial will give you information on what software/hardware you will require and basic steps on how to get, install, configure and use them. After going through this tutorial you will have a complete setup and knowledge to experiment with these powerful chips !         What you will learn ? MPLab as a powerful IDE. HI-TECH C for PIC18 MCUs as a powerful C Compiler. Creating a new HI-TECH C project in MPLab. Write a C Code to blink LED and compile it to get a HEX code. Configure the MCU for proper oscillator selection. Burn the HEX code to MCU using eXtreme Burner PIC from eXtreme Electronics. Use the programmed MCU to actually blink the LED ! So lets get started !!! First get these stuffs Microchip’s MPLab IDE or Integrated development Environment. This is the central tool from where you can access most of other tools, like the C Compiler. This also lets you create and edit program files and projects. Download this from Microchips Web site and Install it in your computer. Download MPLAB IDE Free. HI-TECH C Pro for PIC18 MCUs – This is will compile the high level human readable programs (in […]