8051/PIC/AVR


 8051 Design & Development Section

 

Download-The 8051 Microcontroller and Embedded Systems Using Assembly and C by Mazidi

 

 

 HAPPY NEW YEAR MESSAGE DISPLAY PROGRAM and SIMULATION Download here.....

 

 


8051 Microcontroller tutorials.....8051 Microcontroller






USART_Serial Examples_By Krishna Gaihre.......




Custom character generation on 16x2 LCD using 8051 microcontroller



4. C code to display single custom character program ver1.1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**********************************************************************************************************************/
// Author - Krishna gaihre
// LCDwithDelay.c - Generating single custom character on 16x2 LCD using 89s52 microcontroller -- program v 1.1
// Note: no need provide external delay after command or data, because the send_cmd and send_data functions it self
// provide 1 milliseconds after enable pulse
/**********************************************************************************************************************/
/********************************************** Header Files Declaration **********************************************/
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
/********************************************* LCD control signals declaration ****************************************/
sbit RS = P2^0;     // Register Select line
sbit RW = P2^1;     // Read/write line
sbit Enable = P2^2; // Enable line
#define LCD_PORT P0 // define port
/********************************************* LCD function prototypes ************************************************/
void LCD_init(void);
void send_cmd(unsigned char);
void send_char(unsigned char);
void send_String(unsigned char *);
void delayms(unsigned int);
void CustomCharacterStore(void);
/********************************Character array to hold custom character patterns*************************************/
unsigned char CharacterArray[8] = {0x0E,0x11,0x0E,0x04,0x1F,0x04,0x1B,0x00};
/********************************************* Main Funciton declaration **********************************************/
void main()
{
LCD_PORT = 0x00; // Make the port as output port
     /****************** Reset process from datasheet **************/
     delayms(15);   // <span class="IL_AD" id="IL_AD7">wait</span> for more than 15ms after supply rises to 4.5V
     send_cmd(0x30);
     delayms(4);        // wait more than 4.1ms
     send_cmd(0x30);
     delayms(1);        // wait more than 100us, but delayms(1) will provide 1ms
     send_cmd(0x30);
     delayms(1);
     send_cmd(0x02); // return to home
     delayms(1);
  /****************** Initialize LCD **************/       
    LCD_init();                     // LCD initialization
    CustomCharacterStore();
    while(1)
    {
            send_cmd(0x83);  // Force cursor to beginning of 1st line, if the number is 0x83 then force the cursor to 53rd position        
            send_char(0x00); // locate the first character in CGRAM
    }
     
}
void CustomCharacterStore()
{
    unsigned char i=0,j=0;
        send_cmd(0x40);       //Load the location where we want to store
         
        /**Store the custom character pattern in CGRAM area**/
        send_char(0x0E);      //Load row 1 data
        send_char(0x11);      //Load row 2 data
        send_char(0x0E);      //Load row 3 data
        send_char(0x04);      //Load row 4 data
        send_char(0x1F);      //Load row 5 data
        send_char(0x04);      //Load row 6 data
        send_char(0x1B);      //Load row 7 data
        send_char(0x00);      //Load row 8 data, this 8th won't display on LCD, because the matrix is 5x7
}
/********************************************* LCD Initialization Function declaration ********************************/
void LCD_init()
{
    send_cmd(0x38);                     // configuring LCD as 2 line 5x7 matrix in 8-bit mode
    send_cmd(0x0C);                     // Display on, Cursor blinking
    send_cmd(0x01);                     // Clear Display Screen
    send_cmd(0x06);                     // Increment Cursor (Right side)
}
/******************************************* LCD single character sending Function declaration************************************/
void send_char(unsigned char character)
{
    LCD_PORT = character;
    RS = 1;             // Select Data Register
    RW = 0;             // write operation
    Enable = 1;         // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
    delayms(1);         // 1 millisec delay
    Enable = 0;
    delayms(1);         // 1 millisec delay
}
/*********************************************LCD Command Sending Function declaration********************************/
void send_cmd(unsigned char Command)
{
    LCD_PORT = Command;
    RS = 0;             // Select Command Register
    RW = 0;             // write operation
    Enable = 1;         // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
    delayms(1);         // 1 millisec delay
    Enable = 0;
    delayms(1);         // 1 millisec delay
}
/******************************************* LCD String sending Function declaration*************************************/
void send_String(unsigned char *String)
{
unsigned char i=0;
    while(String[i]!='\0')
    {
                  
        LCD_PORT = String[i++];
        RS = 1;          // Select Data Register
        RW = 0;          // write operation
        Enable = 1;      // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
        delayms(1);      // 1 millisec delay
        Enable = 0;
        delayms(1);      // 1 millisec delay
        if(i>=16)         // If the number of characters in the string > 16, then the below command automatically
        send_cmd(0x1C);  // Shift the display right side
        delayms(100);    // 100 millisec delay
    }
     
}
/******************************************* delayms Function declaration***********************************************/
void delayms(unsigned int value)
{
    unsigned int i,j;
         
    for(i=0;i<=value;i++)
    {
        for(j=0;j<100;j++)
        _nop_();         // no operation produce 1us time delay
    }
     
}

4.1 Output in proteus software for the program1.1:

Figure 1.4 displaying animated boy on LCD using proteus simulation software 


5. C code to display more than one custom character using pointers:

In this code I have the changed CustomCharacterStore() function, which is allowed to store 1 to 8 custom characters in CGRAM with the help of pointers concept in C language. And I have created a two dimensional array to define custom character patterns and in the main function, each character is called separately using send_char() function.
5.1 C code to display eight custom characters – program ver1.2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/***************************************************************************************************************************/
// Author - Krishna gaihre
// LCDwithDelay.c - Generation of custom characters on 16x2 LCD using 89s52 microcontroller
// Note: no need provide external delay after command or data, because the send_cmd and send_data functions it self
// provide 1 milliseconds after enable pulse
/***************************************************************************************************************************/
/********************************************** Header Files Declaration ***************************************************/
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
/********************************************* LCD control signals declaration ***************************************************/
sbit RS = P2^0;     // Register Select line
sbit RW = P2^1;     // Read/write line
sbit Enable = P2^2; // Enable line
#define LCD_PORT P0 // define port
/********************************************* LCD function prototypes ************************************************/
void LCD_init(void);
void send_cmd(unsigned char);
void send_String(unsigned char*);
void send_char(unsigned char);
void delayms(unsigned int);
void CustomCharacterStore(void);
/********************************Character array to hold custom character patterns*************************************/
unsigned char CharacterArray[8][8] = {{0x0E,0x11,0x0E,0x04,0x1F,0x04,0x1B,0x00}, // boy
                                      {0x00,0x00,0x1E,0x13,0x13,0x0C,0x1F,0x00},    // cup
                                      {0x00,0x0A,0x0A,0x00,0x11,0x0E,0x06,0x00},    // simley
                                      {0x00,0x00,0x1B,0x1F,0x0E,0x04,0x00,0x00}, // love
                                      {0x00,0x19,0x1A,0x04,0x0B,0x13,0x00,0x00}, // percentage
                                      {0x1F,0x11,0x0A,0x04,0x0A,0x15,0x1F,0x00}, // hour glass
                                      {0x00,0x1B,0x15,0x11,0x11,0x1F,0x00,0x00},// mail folder
                                      {0x00, 0x0E, 0x11, 0x1F, 0x15, 0x1F, 0x0A}}; // android robo
/********************************************* Main Funciton declaration ***********************************************/
void main()
{
LCD_PORT = 0x00; // Make the port as output port
     /****************** Reset process from datasheet **************/
     delayms(15);   // wait for more than 15ms after supply rises to 4.5V
     send_cmd(0x30);
     delayms(4);        // wait more than 4.1ms
     send_cmd(0x30);
     delayms(1);        // wait more than 100us, but delayms(1) will provide 1ms
     send_cmd(0x30);
     delayms(1);
     send_cmd(0x02); // return to home
     delayms(1);
  /****************** Initialize LCD **************/       
    LCD_init();                     // LCD initialization
    CustomCharacterStore();
    while(1)
    {
            send_cmd(0x80);  // Force cursor to beginning of 1st line, if the number is 0x83 then force the cursor to 53rd position        
            send_char(0x00); // locate the first character in CGRAM
            send_char(' ');  // space
            send_char(0x01); // locate the second character in CGRAM
            send_char(' ');  // space
            send_char(0x02); // locate the third character in CGRAM
            send_char(' ');  // space
            send_char(0x03); // locate the fourth character in CGRAM
            send_char(' ');  // space
            send_char(0x04); // locate the fifth character in CGRAM
            send_char(' ');  // space
            send_char(0x05); // locate the sixth character in CGRAM
            send_char(' ');  // space
            send_char(0x06); // locate the seventh character in CGRAM
            send_char(' ');  // space
            send_char(0x07); // locate the eigth character in CGRAM    
            send_char(' ');  // space
             
     
}
void CustomCharacterStore()
{
unsigned char i=0,j=0;
        //send_cmd(0x48);       //Load the location where we want to store
        //delayms(1);               // Delay of 1millisec
        /**Store the custom character patterns in CGRAM area**/
                                                  
        while(j<8)
        {
            send_cmd(0x40 + (j*8));
                for(i=0;i<8;i++)
                {
                    send_char(CharacterArray[j][i]);
                }
                j++;
        }
     
}
/********************************************* LCD Initialization Function declaration ********************************/
void LCD_init()
{
    send_cmd(0x38);                     // configuring LCD as 2 line 5x7 matrix in 8-bit mode
    send_cmd(0x0C);                     // Display on, Cursor blinking
    send_cmd(0x01);                     // Clear Display Screen
    send_cmd(0x06);                     // Increment Cursor (Right side)
}
void send_char(unsigned char character)
{
    LCD_PORT = character;
    RS = 1;             // Select Data Register
    RW = 0;             // write operation
    Enable = 1;         // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
    delayms(1);         // 1 millisec delay
    Enable = 0;
    delayms(1);         // 1 millisec delay
}
/*********************************************LCD Command Sending Function declaration********************************/
void send_cmd(unsigned char Command)
{
    LCD_PORT = Command;
    RS = 0;             // Select Command Register
    RW = 0;             // write operation
    Enable = 1;         // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
    delayms(1);         // 1 millisec delay
    Enable = 0;
    delayms(1);         // 1 millisec delay
}
/******************************************* delayms Function declaration***********************************************/
void delayms(unsigned int value)
{
    unsigned int i,j;
         
    for(i=0;i<=value;i++)
    {
        for(j=0;j<100;j++)
        _nop_();         // no operation produce 1us time delay
    }
     
}


5.2 Output in proteus software for program 1.2

Figure 1.5 displaying different animated custom characters on LCD using proteus simulation software.


PIC/AVR Design & Development Section


AVR Tutorials...Here

Download-The AVR Microcontroller and Embedded Systems By Mazadi



PIC Tutorials...PIC Microcontroller

 Download-PIC Microcontroller and Embedded Systems Book - Mazidi...comming soon

The PIC Microcontroller Your Personal Introductory Course By John Morton Download


PIC Tutorials:-MPLAB IDE Getting Started Tutorial Video

LED Interfacing and Toggling their states with PIC18F4550 Miro-Controller.





Embedded system with AVR
Embedded programming using ATMEGA 16:Here i am going to tell you about the embedded programming using Atmega16 microcontroller with c language.This is very easy.
In the below your seeing the Pin out of Atmega-16.


To start C programming language on Atmel AVR Microcontroller you need to download these following tools:
  • Down load the latest Atmel AVR Studio which provide you with the complete IDE (integrated development environment) for managing project, program editing, compiling, debugging and downloader for all Atmel AVR Microcontroller series.
  • The Atmel AVR Studio only provide you with native microcontroller language (assembler), so you need to down load the WinAVR Project which provide you with AVR GCC (GNU C Compiler for AVR Microcontroller) base compiler and library for window environment. This AVR GCC is fully integrated and supported by Atmel AVR Studio.
After downloading, first install the Atmel AVR Studio and just follow all the default setting and secondly install the WinAVR, again follow the default setting.


My first AVR Project
To create your first AVR project go to Start -> All Programs -> Atmel AVR Tools -> AVR Studio 4, this will launch the AVR Studio 4 application and the Welcome to AVR Studio 4 form appear, click on the New Project button and it will show the Create new project screen as follow:

   

On Project Type choose AVR GCC, enter myfirstc for the project name and you could also change the project location directory as you like,  then click on the Next>> button, you will be asked for debugging platform on the following screen:

       

Choose AVR Simulator for debug platform and ATmega16 for the device, then click on the Finish button. This will create all the necessary files and directories needed for this project. Now the AVR Studio 4 IDE is ready and display the empty myfirstc.c file.

                 

Enter or cut and paste this source program bellow to the program code windows:
/***************************************************************************
#include 
#include 

void Wait()
{
   uint8_t i=0;
   for(;i<23;i++)
      _delay_loop_2(0);
}

void main()
{
   //Set PORTC0 as output

   DDRB=0b00000001;

   while(1)
   {
      //Set Port D=High(+5v)
      PORTB=0xff;  // in hexadecimal
      Wait();

      //Set Port D=Low(GND)
      PORTB&=0b00000000; // in binary it is 0x00 in hexadecimal
      Wait();
   }
}
 
The program start with the standard comment (all the statement start with 
double slash is consider as comment in standard C syntax) that give the 
information about the program, version and compiler version,etc. The next two  
include statement tell the C compiler to include the AVR microcontroller specific
library  and the delay library  when compiling the program.Inside the main 
program, first we instruct the AVR microcontroller (i.e. avr/io.h  ) to enable 
it’s 8 bit PORT-D input/output port for output by setting the data direction 
register (DDRB) on PORT-B to 0x01 that is 0b00000001 in binary.
Next inside the while(1) loops statement (it’s mean we loop continuously) we 
simple instruct the AVR microcontroller to turn on and off all it’s 8 bit 
PORT-D by assigning the value 0xFF (on) and 0×00 (off) to the PORT-B register 
(PORTB). The 100 millisecond delay is put there so our eye could catch the 
process, otherwise the process is so fast that we see the port is always on.

Building the HEX Files:-
Once you have written the code Just press F7 key of your key board to compile and 
generate the HEX code.....
 
Downloading the HEX code to Microcontroller:-
To download the code you need a programmer ckt..in the below i am giving a
ckt dig of a parallel port(BSD) programmer which is very low cost 
programmer...you need some simple wire and connectors to make it. 
             
 
 
 
 
if you have not have a PC then you can make your USB programmer which
is little costlier...You can click here to learn how to make
 USB programmer...
 Once you have made the programmer connect it to your computer and 
download the programmer using the following steps.... 

Programming Using PonyProg.

This is the programmer we made in the previous tutorial. Its use is simple. Start Ponyprog, you will get a screen similar to this.
pony prog avr programmer

Fig - Ponyprog main window.


First, you need some setup. Go to menu setup->interface setup and make setting like this.
pony prog avr programmer

Fig - Ponyprog Setup


Select the serial port in which your programmer is connected. Beware there may be more that 1 available com port showing there and usually only one is available outside the PC the rest are internal and may be connected to your modem. So make sure you have selected a port that is connected to your programmer and not to your modem. Next, go to set up->calibration. Now you are done. Connect the programmer you have made to you PCs serial port or parallel port and connect its ISP connector to the target you have made. Switch on the target. The software is self explanatory with easy to use GUI in the top tool bar there is a selection for type of chip you want to program. Select “AVR Micro” and select the type of Micro( like ATmega16) in box next to it.
  1. Select the hex file you want to program using the File->Open Program(FLASH) file.
  2. Command->Erase.
  3. Command->Write Flash
  4. Command->Verify Program(FLASH)
  5. If every thing is correct, your MCU is programmed successfully.
  6. Disconnect programmer from the Target and switch it off.
thanks to embbsys...krishna.

1 comment: