Coding For Controllers
Coding For Controllers
Lcd:
#include<REGX51.H>
void lcdinit();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void lcdready();
void lcddelay(unsigned int time);
void putstr(unsigned char *ptr);
void putch(unsigned char c);
#define ldata P1
sbit rs = P3^5;
sbit rw = P3^6;
sbit en = P3^7;
sbit mot = P2^0;
sbit busy = P1^7;
void main(){
lcdinit();
// lcddata('a');
//lcddata('b');
// putstr("LCD DISPLAY");
while(1){
lcdcmd(0x80);
mot=1;
putstr("ON ");
lcddelay(200);
lcdcmd(0x80);
mot=0;
putstr("OFF");
lcddelay(200);
}
}
unsigned int i, j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void lcdinit(){
lcdcmd(0x38);
lcdcmd(0x0E);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x80);
}
lcdready();
P1 = value;
rw = 0;
rs = 0;
en = 1;
lcddelay(1);
en = 0;
return;
}
lcdready();
P1 = value;
rw = 0;
rs = 1;
en = 1;
lcddelay(1);
en = 0;
return;
}
void lcdready(){
busy = 1;
rs = 0;
rw = 1;
while(busy==1){
en = 0;
lcddelay(1);
en = 1;
}
}
while(!(*ptr=='\0')){
putch(*ptr);
ptr++;
}
}
lcddata(c);
}
Adc:
#include<regx51.h>
#include<lcd.c>
sbit OE = P3^0;
sbit EOC = P3^1;
sbit ALE = P3^2;
void main(){
P0=0x00;
P3=0x02;
P2=0xFF;
P1=0x00;
lcdinit();
putstr("TEMPERATURE :");
OE =0;
ALE=0;
while(1){
P0=~P0;
lcdcmd(0x8C);
//if(P0==0x00) lcddata('1');
//else lcddata('2');
start();
datain();
valtolcd(value);
}
}
void start(){
ALE=1;
delay(1000);
ALE=0;
}
void datain(){
while(EOC==0);
OE=1;
delay(1000);
value=P2;
delay(1000);
OE=0;
delay(1000);
}
Serial communication:
Tx:
#include<regx51.h>
void putst(unsigned char *str);
void putch(unsigned char c);
void serinit();
void serinit(){
TMOD = 0X20;
TH1 = 0XFD;
SCON = 0X50;
IE = 0X90;
TR1 = 1;
TI = 0;
RI = 0;
}
while(!(*str=='\0')){
putch(*str);
str++;
}
}
SBUF=c;
while(TI==0);
TI=0;
}
Rx:
#include<regx51.h>
#include"tx.c"
#include"lcd.c"
char x;
void main(){
P2=0x00;
lcdinit();
serinit();
putst("Enter Characters");
// putlcdst("Receiver Check");
lcdcmd(0x80);
while(1);
}
if (RI==1){
x=SBUF;
putch(x);
lcddata(x);
RI=0;
}
else{ ;
// TI=0;
}
}
Motor:
#include <REGX51.H>
sbit sw1=P1^0;
sbit sw2=P1^1;
sbit motor=P2^0;
void main()
{
sw1=1;
sw2=1;
motor=0;
while(1)
{
}
Keypad:
#include <REGX51.H>
#include<lcd.c>
void main(){
P2=0XFF;
lcdinit();
while(1){
P2=0x70;
col=P2&0X70;
col=col>>4;
while(!(col==7)){
coltest=col;
for(i=0;i<4;i++){
P2=rowtest[i];
col=P2&0X70;
col=col>>4;
if(col==coltest)
datasel(i,coltest);
}
}
}
}
lcdcmd(0XCF);
if(x==0&&y==0x06) lcddata('1');
if(x==0&&y==0x05) lcddata('2');
if(x==0&&y==0x03) lcddata('3');
if(x==1&&y==0x06) lcddata('4');
if(x==1&&y==0x05) lcddata('5');
if(x==1&&y==0x03) lcddata('6');
if(x==2&&y==0x06) lcddata('7');
if(x==2&&y==0x05) lcddata('8');
if(x==2&&y==0x03) lcddata('9');
if(x==3&&y==0x06) lcddata('*');
if(x==3&&y==0x05) lcddata('0');
if(x==3&&y==0x03) lcddata('#');
}
Software:
KEIL SOFTWARE
"Using nothing more than the provided simulation support and debug
scripts, developers can create a high-fidelity simulation of their actual
target hardware and environment. No extra hardware or test equipment
is required. The Logic Analyzer and Execution Profiler will help
developers when it comes time to develop and tune signaling
algorithms." said Jon Ward, President of Keil Software USA, Inc.
µVision3 Overview
The µVision3 IDE offers numerous features and advantages that help
you quickly and successfully develop embedded applications. They are
easy to use and are guaranteed to help you achieve your design goals.
The µVision3 IDE and Debugger is the central part of the Keil
development toolchain. µVision3 offers a Build Mode and a Debug
Mode.
In the µVision3 Build Mode you maintain the project files and generate
the application.
In the µVision3 Debug Mode you verify your program either with a
powerful CPU and peripheral simulator or with the Keil ULINK USB-
JTAG Adapter (or other AGDI drivers) that connect the debugger to
the target system. The ULINK allows you also to download your
application into Flash ROM of your target system.
The µVision3 screen provides you with a menu bar for command entry,
a tool bar where you can rapidly select command buttons, and windows
for source files, dialog boxes, and information displays. µVision3 lets
you simultaneously open and view multiple source files.
Build Mode: Allows you to translate all the application files and
to generate executable programs. The features of the Build Mode
are described under Creating Applications.
Debug Mode: Provides you with a powerful debugger for testing
your application. The Debug Mode is described in Testing
Programs.
In both operating modes you may use the source editor of µVision3 to
modify your source code. The Debug mode adds additional windows
and stores an own screen layout. The following picture shows a typical
configuration of µVision3 in the Debug Mode.
The tabs of the Project Workspace give you access to:
Files and Groups of the project.
CPU Registers during debugging.
When you use the Keil µVision3, the project development cycle is
roughly the same as it is for any other software development project.
1. Create a project, select the target chip from the device database,
and configure the tool settings.
2. Create source files in C or assembly.
3. Build your application with the project manager.
4. Correct errors in source files.
5. Test the linked application.
Library Manager
The library manager allows you to create object library from the object
files created by the compiler and assembler. Libraries are specially
formatted, ordered program collections of object modules that may be
used by the linker at a later time. When the linker processes a library,
only those object modules in the library that are necessary to create the
program are used.
Linker/Locator
The µVision3 Debugger provides several ways for you to test your
programs on real target hardware.
Shortcu
Menu Item or Command t
New Ctrl+N
Creates a new source or text file.
Open Ctrl+O
Opens an existing file.
Close
Closes the active file.
Save Ctrl+S
Saves the active file.
Save as...
Renames and saves the active file.
Save All
Saves all open source and text files including the
project.
Device Database
Opens the Device Database Dialog where you may
maintain the µVision3 Device Database.
License Management
Opens the License Management Dialog where you
may maintain and review the installed software
components.
Print Setup...
Opens the Printer Setup Dialog.
Print Ctrl+P
Prints the active file.
Print Preview
Displays pages in print view.
1 - 9
Opens the most recent used source or text files.
Exit
Exits µVision3 and prompts to save any files that are
open.
Flash Menu
The Flash menu allows you to configure and run Flash Programming
Utilities. With Configure Flash Tools… the programming tool is
selected and configured. For details refer to Configure Flash Menu.
Shortcu
Menu Item t
Download
Downloads program to Flash using the programming
utility configured under Options for Target — Utilities.
Erase
Erases the Flash ROM (only available for some
devices).
Status Bar
The Status Bar shown at button of the screen (when enabled in the
View Menu) displays messages and information about the current
µVision command and other status information as explained below:
Debug Channel shows the active debug tool and displays the name of
the Advanced GDI Driver or Simulation when the program is tested
with build-in µVision Simulator.
Cursor Position shows the position in the current editor file (L: line
number, C: column number).
Breakpoints
With the File Toolbar buttons. Just select the code line in the
Editor or Disassembly window and click on the breakpoint buttons.
With the breakpoint commands in the local menu. The local menu
opens with a right mouse click on the code line in the Editor or
Disassembly window.
The Debug – Breakpoints… dialog lets you review, define and
modify breakpoint settings. This dialog allows you to define also
access breakpoints with different attributes. Refer to the examples
below.
In the Output Window – Command page you can use the
BreakSet, BreakKill, BreakList, BreakEnable, and
BreakDisable commands.
The Breakpoint dialog lets you view and modify breakpoints. You can
quickly disable or enable the breakpoints with a mouse click on the
check box in the Current Breakpoints list. A double click in the
Current Breakpoints list allows you to modify the selected break
definition.
You define a breakpoint by entering an Expression in the Breakpoint
dialog. Depending on the expression one of the following breakpoint
types is defined:
CPU Registers
If you select the Disassembly Window as the active window all program
step commands work on CPU instruction level rather than program
source lines. You can select a text line and set or modify code
breakpoints using toolbar buttons or the context menu commands.
You may use the dialog Debug – Inline Assembly... to modify the CPU
instructions. That allows you to correct mistakes or to make temporary
changes to the target program you are debugging.
Logic Analyzer
The Logic Analyzer window contains in the top border several buttons
and display fields.
The Setup... dialog defines variables for signal recording and configures
the display output. Some configuration settings may be also changed
from the context menu that opens with a right click on the signal name.
Export... writes the current recorded signals to a tab-delimited file for
analysis with external tools.
The Zoom buttons define the time range of the display. Zoom All shows
the complete content of the signal recording buffer. Zoom Sel changes
the display to a selection that you have previously marked with a mouse
drag while holding down the Shift key.
Set cursor line with a mouse click. The cursor line displays all
variable values at the selected time stamp.
Move cursor line to previous or next signal change with Left or
Right cursor key. The signal is selected with a mouse click in the
left border.
Show detailed information by placing the mouse cursor near a
signal change (the Logic Analyzer snaps to the closes signal
change). The information box shows the signal delta values based
on the current cursor line position.
Memory Window
In the Address field of the Memory Window, you can enter any
expression that evaluates to a start address of the area you want to
display. To change the memory contents, double click on a value.
This opens an edit box that allows you to enter new memory
values. To update the memory window while a target program is
running enable View – Periodic Window Update.
Watch Window
The Watch Window lets you view and modify program variables and
lists the current function call nesting. The contents of the Watch
Window are automatically updated whenever program execution stops.
You can enable View – Periodic Window Update to update variable
values while a target program is running.
Variable Values
The Locals page shows all local function variables of the current
function. The Watch pages display user-specify program variables.
Select the text <type F2 to edit> with a mouse click and wait a
second. Another mouse click or the F2 key starts the edit mode
that allows you to add variables. In the same way you can modify
existing variables.
In an editor window open the context menu with a right mouse
click and use Add to Watch Window. µVision3 automatically
selects the variable name under the cursor position, alternatively
you may mark an expression before using that command.
In the Output Window – Command page you can use the
WatchSet command to enter variable names.
To modify a variable value, select the value with a mouse click and wait
a second. Another mouse click or the F2 key starts the edit mode.
To remove a variable, click on the line and press the Delete key or use
the WatchKill command.