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() […]

Obstacle Avoiding Robot using AVR ATmega32 – Part I

Hi and Welcome All, In this tutorial series I will show you how to make a simple obstacle avoiding robot using the xBoard v2.0 microcontroller board. xBoard v2.0 is well suited for developing small intelligent robots as it is compact in size, has four DC motor controller, can be programmed using USB Port and many other features. It is also very easy to learn and use. The xAPI, which is a set of C functions which makes complex programming tasks such as PWM ,LCD, Remote Control etc very easy for beginners. You can buy it from here Its design is open so if you don’t want to buy the board you can make it yourself at home by the help of its schematic. The Robo’s Task The task of our robo is simple. To move randomly in an area avoiding obstacles, that’s it ! Though the task is simple, its a complete autonomous robot in itself. It has a brain which reads sensors and makes decisions and command the motors. In the course of making the robot you will learn various basic techniques which will be of great use for your further projects. Our Final Robot will look like this. AVR ATmega32 Based Obstacle Avoiding Robot The following Video Demo Shows the Robo in Action The following Video was […]

Interfacing 12 bit SPI ADC (MCP3204) with AVR Micro

Hello All, Sometimes the Internal ADC is not enough. Like when you need more resolution or high speed. The internal ADC of AVR generally has the following specifications. 15K samples per second 10 bit resolution. If you need more than that you need an external ADC. You may also need external ADCs if you have already used the internal ones. This tutorial will guide you how to install an external ADC with AVR MCU and write a test program to get data from it. A very common external ADC is from Microchip the MCP3204. It has the following configuration. 100K samples per second. (More than 6 times faster than AVRs inbuilt) 12 bit resolution (4 times more detailed) 4 input channels (MCP3208 has 8 channels). SPI Bus Compatible. Basic SPI Tutorial These ADCs are SPI Bus based which is a serial bus. So the number of pins in IC is very low. Total of 4 lines are required to interface it with AVR MCU. MISO (Master In Slave Out) MOSI (Master Out Slave In) SCK (Serial Clock) CS (Chip Select) As you know in synchronous serial communication their is a clock line (SCK in case of SPI) which synchronizes the transfer. Please read the article :- Synchronous Serial Communication Tutorial – The Basics of I2C and SPI. The clock is […]

Sound Generation by AVR Micro – Tutorial I

Many project requires some kind of Audio output. For example a burglar alarm, an automated school bell or simple electronic games or even a robot! In Old days we used some some dedicated Music and Audio Effect chip to do that. At that time ICs such as UM66 were very popular. Now in the days of microcontrollers, a good design is to use least number of external components to get the job done. So if you still use external audio ICs with a MCU based design then your design is inefficient and costly. The smart idea is to get most of the job done in software. In this article we will learn step by step how to produce different kinds of audio effect by just using an AVR MCU and A speaker. After reading this you would be able to provide simple sound output in many AVR based projects. So lets get started! I will start this series with a direct runnable example so that you can burn it into an AVR and see how it sounds! In latter parts I will elaborate how this was achieved. Some techniques that were used to achieve audio generation are. PWM or Pulse width modulation: It is a technique to generate analog voltage levels by a digital device (say a MCU). Generally a […]


RF Communication Between Microcontrollers – Part III

Welcome to the 3rd part of RF Communication tutorial. In the last two parts I have introduced the basics of RF Communication. RF Communication Between Microcontrollers – Part I : Introduction to RF Communication and Modules. RF Communication Between Microcontrollers – Part II : Algorithm and general description of data transfer. Part III will be covering mostly the practical part, i.e. we will build a complete & working data transfer system. Here you will get circuit and program to implement the solution. The application is very simple in this case, just to transfer a byte of data from Tx station to the Rx station. Once you implement it and get it working you will have enough information and experience to make other RF based projects. I request all users to follows the instruction exactly as given (unless they are smart enough to know what they are doing). The most important thing in this article is timing of the MCU, so Use the exact frequency crystals as used in the designs. Write High Fuse = C9 (HEX Value) and Low Fuse FF (HEX Value) to enable external crystal. Hardware We will have two units. One is Tx (Transmitter) and Other is Rx (Receiver). Both units are based around ATmega16 MCU(you can use ATmega32 also) on external 16MHz crystal. On the Tx […]


Interfacing DS1307 RTC Chip with AVR Microcontroller

Real Time Clocks, as the name suggests are clock modules. They are available as integrated circuits (ICs) and manages timing like a clock. Some RTC ICs also manages date like a calendar. The main advantage is that they have a system of battery backup which keeps the clock/ca lender running even in case of power failure. A very small current is required for keeping the RTC alive. This in most case is provided by a miniature 3v lithium coin cell. So even if the embedded system with RTC is powered off the RTC module is up and running by the backup cell. This same technique is used in PC timing also. If you have opened your computer case you will notice a small coin cell in the mother board. In this tutorial we will learn to use a very famous RTC IC named DS1307. The DS1307 is described in the datasheet as follows The DS1307 is a low-power clock/calendar with 56 bytes of battery-backed SRAM. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The DS1307 operates as a slave device on the I2C bus. So the aim of the project will be to […]

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 […]

AVR Project – Relay Timer with ATmega8 AVR MCU

Timers are widely used in industrial and domestic application for automating tasks. Microcontrollers can be used to design versatile and accurate timers with ease. Here I present a simple timer that can be used to turn on/off a load after user specified time. The Timer uses a standard 16×2 lcd module for user interface (UI). User can set the time using a 3 button keypad. After that Timer is started. While count down is in progress, the time left is displayed on screen. The program use our LCD driver library more details of which can be found in here. Use avr-gcc + AVR Studio to compile. The prototype was developed using xBoard MINI, a low cost easy to use ATmega8 development board. The program was burned to the MCU’s flash memory using eXtreme Burner – AVR Software and Hardware. A basic knowledge of working with different tools of AVR development is required, so please refer to following articles. Note: Fuse Must be set as follows, HIGH FUSE=C9 LOW FUSE=FF (Very Important) If display is blank please adjust RV1 Part List 01 ATmega8-16 PU U1 02 16×2 LCD Module LCD1 03 16 MHz Crystal X1 04 BC548 Transistor Q1 05 1N4007 Diode D1 06 4.7K Resistor R1,R2 07 10K Variable Resistor VR1 08 22pF Disk Capacitor c1,c2 09 0.1uF Disk Capacitor […]

AVR Development Board – Assembly Instructions

A well designed development board is always essential for smooth development/experimentation with microcontrollers. Every hobbyist must have some kind of development setup. Some people use breadboard while others use veroboard to fabricate their development system. Many also use ready made off the self development board. Development is not very easy if you use only bread boards or veroboard. If you use bread board, after few days of use the wire will become loose or come out of hole. While making large circuit in a veroboard is a tedious job. To solve these problem I decided to make a low cost development board that has the following features. Low Cost All Basic connection required for developing application with AVR MCUs A Serial Port Prototyping area (like a veroboard) Incircuit Programmable with USB AVR Programmer Supports 40 PIN MCUs like ATmega16 and ATmega32. Buy Fully Assembled and Tested AVR Development Board So if you use this board, its lots easier. The basic circuit for MCU (like power supply,reset,ISP,serial port etc) are already done on a high quality PCB and some space is left for you to add other project specific components. You can also design other part of your project in bread board. But this time your bread board will be lot cleaner as major tested circuit is on the development board. […]

RF Communication Between Microcontrollers – Part II

Hello Friends, Welcome back to the Part II of RF Communication tutorial. Here I will show you the basic working of RF modules and how to send and receive data. Please see the Part I of this tutorial for basic introduction. You should also be familiar with RS232 communication. If you are new to it please see RS232 Serial Communication Tutorial. I also recommend using wireless link only after you have successfully tried wired RS232 communication. Here I will not go deep in how RS232 works because it is already discussed in RS232 Serial Communication Tutorial. I will use my interrupt driven fully buffered USART library for communication. How RF Module Works Working of RF Modules is simple but with a little trick. The working is shown in figure below. Fig- Working of RF Modules.   Here what ever digital data you input on "Data In" of TX is available on "Data Out" of RX. Say, if you set "data in" high, the "data out" will become high as well. But here lies the trick! The fact is that you cannot Keep Logic HIGH or LOW for a Long period of time, say for a few millisecond second. If you apply a logic low on "data in" the "data out" will become low but only for few millisecond and it […]


Programming in C – Tips for Embedded Development.

Here I will highlight some features of C language commonly used in 8 bit embedded platforms like 8051, AVR and PICs. While programming microcontrollers in C most of the time we have to deal with registers. Most common tasks are setting and clearing bits in a register and check whether a bit is 0 or 1 in a given register. So here I will give detail on those topics, it will help you if you are new to embedded programming in C and if you get confused when you see some codes. A Register A register is simply a collection of some bits (mostly 8 bits in case of 8bit MCUs). Either each different bit in a register has some purpose or the register as a whole holds a value. Registers serves as connection between a CPU and a Peripheral device (like ADC or TIMER). By modifying the register the CPU is actually instructing the PERIPHERAL to do something or it is configuring it in some way. And by reading a register, the CPU can know the state of peripheral or read associated data. Fig.: CPU writing to Peripheral Register   Fig.: CPU Reading from Peripheral Register Binary Numbers in C When you write a=110; in C it means you are setting the value of variable"a" to "one hundred and […]

RF Communication Between Microcontrollers – Part I

In many situations a communication link between to devices becomes essential. This communication can be wired or wireless. If two devices are close to each other (like a MCU and a Memory) a wired link is preferred. How ever in many situations two devices are reasonably far apart. In that case a wireless link is preferred. Two popular wireless communication technologies are IR Communications Used in IrDA, and Remote controls Short Range Requires two devices to be in line of sight. There should be no Opaque Obstacle in between the devices. Easy and low cost to implement RF Communication Widely used, including Bluetooth,Radios,Cell phones, Satellite etc Wide range, from few meters to millions of kilometers (Can be Used to control Robots in Mars) Does not requires two devices to be in line of sight. Can cross many obstacles Circuits can be complicated and costly. In this tutorial we will learn how to practically implement a wireless link between two MCUs. This link will be used to send and receive digital data. We will create a Radio Frequency link. How ever as I said the RF circuits are little complicated so we will use ready made RF Modules. These are easily available and low cost. What is a RF Module ? A RF Module is a small circuit pre built and […]