Академический Документы
Профессиональный Документы
Культура Документы
The job is a real practical calculator code. I want to the code written in assembly and commented
The code should do more than ----8---- digit number addition, subtraction, and multiplication... No
division... The code should be written for Avr Atmega16 or Atmega8, Keypad and LCD calculator all
written in assembly language. A working assembler, simulated in Proteus, please send me the
Proteus project too .... calculator.asm Avr Atmega16 Keypad, 4X4 LCD 16x2
Answer:
#include<stdio.h>
#include<conio.h>
#include<lib.h>
while(1)
{
//get numb1
key = get_key();
writecmd(0x01); //clear display
writedata(key); //Echo the key pressed to LCD
num1 = get_num(key); //Get int number from char value, it checks for wrong input
as well
if(num1!=Error) //if correct input then proceed, num1==Error means wrong input
{
//get function
key = get_key();
writedata(key); //Echo the key pressed to LCD
func = get_func(key); //it checks for wrong func
As you can see in the above function, I have first check for the first key press.
When you pressed the first key on keypad then I get this key and converter it to integer.
After that I waited for the next key which must be some operation key like + – X otherwise it
will generate the error message.
After that code is waiting for the third key which should be some numerical digit and then I
converter it to integer again and if you entered some invalid key then it will generate the
error.
Finally waiting for the = sign. When you press the = sign it will automatically perform the
required operation which I placed in the switch case loop.
It will calculate the value and then print out the result and on next key press it will first clear
the screen and then get the value and will continue.
Below is the detailed code for the project with comments and I hope you wont get into any
trouble and will get it clearly.
1 #include<reg51.h>
2 #include<string.h>
3
4 //Define Macros
5 #define Error 13 // Any value other than 0 to 9 is good here
6
7 //Function declarations
8 void cct_init(void);
9 void delay(int);
10 void lcdinit(void);
11 void writecmd(int);
12 void writedata(char);
13 void writeline(char[]);
14 void ReturnHome(void);
15 char READ_SWITCHES(void);
16 char get_key(void);
17 int get_num(char);
18 char get_func(char);
19 void DispError(int);
20 void disp_num(int);
21 void WebsiteLogo();
22
23 //*******************
24 //Pin description
25 /*
26 P2 is data bus
27 P3.7 is RS
28 P3.6 is E
29 P1.0 to P1.3 are keypad row outputs
30 P1.4 to P1.7 are keypad column inputs
31 */
32 //********************
33 // Define Pins
34 //********************
35 sbit RowA = P1^0; //RowA
36 sbit RowB = P1^1; //RowB
37 sbit RowC = P1^2; //RowC
38 sbit RowD = P1^3; //RowD
39
40 sbit C1 = P1^4; //Column1
41 sbit C2 = P1^5; //Column2
42 sbit C3 = P1^6; //Column3
43 sbit C4 = P1^7; //Column4
44
45 sbit E = P3^6; //E pin for LCD
46 sbit RS = P3^7; //RS pin for LCD
47
48 // ***********************************************************
49 // Main program
50 //
51 int main(void)
52 {
53 char key; //key char for keeping record of pressed key
54 int num1 = 0; //First number
55 char func = '+'; //Function to be performed among two numbers
56 int num2 = 0; //Second number
57
58 cct_init(); //Make input and output pins as required
59 lcdinit(); //Initilize LCD
60 WebsiteLogo();
61 while(1)
62 {
63 WebsiteLogo();
64 //get numb1
65 key = get_key();
66 writecmd(0x01); //clear display
67 WebsiteLogo();
68 writedata(key); //Echo the key pressed to LCD
69 num1 = get_num(key); //Get int number from char value, it checks for wrong
70 input as well
71
72 if(num1!=Error) //if correct input then proceed, num1==Error means wrong
73 input
74 {
75 //get function
76 key = get_key();
77 writedata(key); //Echo the key pressed to LCD
78 func = get_func(key); //it checks for wrong func
79
80 if(func!='e') //if correct input then proceed, func=='e' means
81 wrong input
82 {
83 //get numb2
84 key = get_key();
85 writedata(key); //Echo the key pressed to LCD
86 num2 = get_num(key); //Get int number from char value, it
87 checks for wrong input as well
88
89 if(num2!=Error) //if correct input then proceed, num2==Error
90 means wrong input
91 {
92 //get equal sign
93 key = get_key();
94 writedata(key); //Echo the key pressed to LCD
95
96 if(key == '=') //if = is pressed then proceed
97 {
98 switch(func) //switch on function
99 {
100 case '+': disp_num(num1+num2); break;
101 case '-': disp_num(num1-num2); break;
102 case 'x': disp_num(num1*num2); break;
103
104 }
105 }
106 else //key other then = here
107 means error wrong input
108 {
109 if(key == 'C') //if clear screen is pressed then clear
110 screen and reset
111 {
112 writecmd(0x01); //Clear Screen
113 WebsiteLogo();
114 }
115 else
116 {
117 DispError(0); //Display wrong input error
118 WebsiteLogo();
119 }
120 }
121 }
122 }
123 }
124 }
125 }
126
127 void WebsiteLogo()
128 {
129 writecmd(0x95);
130 writedata('w'); //write
131 writedata('w'); //write
132 writedata('w'); //write
133 writedata('.'); //write
134 writedata('T'); //write
135 writedata('h'); //write
136 writedata('e'); //write
137 writedata('E'); //write
138 writedata('n'); //write
139 writedata('g'); //write
140 writedata('i'); //write
141 writedata('n'); //write
142 writedata('e'); //write
143 writedata('e'); //write
144 writedata('r'); //write
145 writedata('i'); //write
146 writedata('n'); //write
147 writedata('g'); //write
148
149 writecmd(0xd8);
150
151 writedata('P'); //write
152 writedata('r'); //write
153 writedata('o'); //write
154 writedata('j'); //write
155 writedata('e'); //write
156 writedata('c'); //write
157 writedata('t'); //write
158 writedata('s'); //write
159 writedata('.'); //write
160 writedata('c'); //write
161 writedata('o'); //write
162 writedata('m'); //write
163 writecmd(0x80);
164 }
165
166 void cct_init(void)
167 {
168 P0 = 0x00; //not used
169 P1 = 0xf0; //used for generating outputs and taking inputs from Keypad
170 P2 = 0x00; //used as data port for LCD
171 P3 = 0x00; //used for RS and E
172 }
173
174 void delay(int a)
175 {
176 int i;
177 for(i=0;i<a;i++); //null statement
178 }
179
180 void writedata(char t)
181 {
182 RS = 1; // This is data
183 P2 = t; //Data transfer
184 E = 1; // => E = 1
185 delay(150);
186 E = 0; // => E = 0
187 delay(150);
188 }
189
190
191 void writecmd(int z)
192 {
193 RS = 0; // This is command
194 P2 = z; //Data transfer
195 E = 1; // => E = 1
196 delay(150);
197 E = 0; // => E = 0
198 delay(150);
199 }
200
201 void lcdinit(void)
202 {
203 ///////////// Reset process from datasheet /////////
204 delay(15000);
205 writecmd(0x30);
206 delay(4500);
207 writecmd(0x30);
208 delay(300);
209 writecmd(0x30);
210 delay(650);
211 /////////////////////////////////////////////////////
212 writecmd(0x38); //function set
213 writecmd(0x0c); //display on,cursor off,blink off
214 writecmd(0x01); //clear display
215 writecmd(0x06); //entry mode, set increment
216 }
217
218 void ReturnHome(void) /* Return to 0 cursor location */
219 {
220 writecmd(0x02);
221 delay(1500);
222
223 }
224
225 void writeline(char Line[])
226 {
227 int i;
228 for(i=0;i<strlen(Line);i++)
229 {
230 writedata(Line[i]); /* Write Character */
231 }
232
233 ReturnHome(); /* Return to 0 cursor position */
234 }
235
236 char READ_SWITCHES(void)
237 {
238 RowA = 0; RowB = 1; RowC = 1; RowD = 1; //Test Row A
239
240 if (C1 == 0) { delay(10000); while (C1==0); return '7'; }
241 if (C2 == 0) { delay(10000); while (C2==0); return '8'; }
242 if (C3 == 0) { delay(10000); while (C3==0); return '9'; }
243 if (C4 == 0) { delay(10000); while (C4==0); return '/'; }
244
245 RowA = 1; RowB = 0; RowC = 1; RowD = 1; //Test Row B
246
247 if (C1 == 0) { delay(10000); while (C1==0); return '4'; }
248 if (C2 == 0) { delay(10000); while (C2==0); return '5'; }
249 if (C3 == 0) { delay(10000); while (C3==0); return '6'; }
250 if (C4 == 0) { delay(10000); while (C4==0); return 'x'; }
251
252 RowA = 1; RowB = 1; RowC = 0; RowD = 1; //Test Row C
253
254 if (C1 == 0) { delay(10000); while (C1==0); return '1'; }
255 if (C2 == 0) { delay(10000); while (C2==0); return '2'; }
256 if (C3 == 0) { delay(10000); while (C3==0); return '3'; }
257 if (C4 == 0) { delay(10000); while (C4==0); return '-'; }
258
259 RowA = 1; RowB = 1; RowC = 1; RowD = 0; //Test Row D
260
261 if (C1 == 0) { delay(10000); while (C1==0); return 'C'; }
262 if (C2 == 0) { delay(10000); while (C2==0); return '0'; }
263 if (C3 == 0) { delay(10000); while (C3==0); return '='; }
264 if (C4 == 0) { delay(10000); while (C4==0); return '+'; }
265
266 return 'n'; // Means no key has been pressed
267 }
268
269 char get_key(void) //get key from user
270 {
271 char key = 'n'; //assume no key pressed
272
273 while(key=='n') //wait untill a key is pressed
274 key = READ_SWITCHES(); //scan the keys again and again
275
276 return key; //when key pressed then return its value
277 }
278
279 int get_num(char ch) //convert char into int
280 {
281 switch(ch)
282 {
283 case '0': return 0; break;
284 case '1': return 1; break;
285 case '2': return 2; break;
286 case '3': return 3; break;
287 case '4': return 4; break;
288 case '5': return 5; break;
289 case '6': return 6; break;
290 case '7': return 7; break;
291 case '8': return 8; break;
292 case '9': return 9; break;
293 case 'C': writecmd(0x01); return Error; break; //this is used as a clear screen
294 and then reset by setting error
295 default: DispError(0); return Error; break; //it means wrong input
296 }
297 }
298
299 char get_func(char chf) //detects the errors in inputted function
300 {
301 if(chf=='C') //if clear screen then clear the LCD and reset
302 {
303 writecmd(0x01); //clear display
304 WebsiteLogo();
305 return 'e';
306 }
307
308 if( chf!='+' && chf!='-' && chf!='x' && chf!='/' ) //if input is not from allowed
309 funtions then show error
310 {
311 DispError(1);
312 WebsiteLogo();
313 return 'e';
314 }
315
316 return chf; //function is correct so return the correct function
317 }
318
319
320 void DispError(int numb) //displays differet error messages
321 {
322 writecmd(0x01); //clear display
323 WebsiteLogo();
324 switch(numb)
325 {
326 case 0: writeline("Wrong Input"); break;
327 case 1: writeline("Wrong Function"); break;
328 default: writeline("Wrong Input"); break;
329 }
330 }
331
332 void disp_num(int numb) //displays number on LCD
333 {
334 unsigned char UnitDigit = 0; //It will contain unit digit of numb
335 unsigned char TenthDigit = 0; //It will contain 10th position digit of numb
336
337 if(numb<0)
338 {
339 numb = -1*numb; // Make number positive
340 writedata('-'); // Display a negative sign on LCD
341 }
342
TenthDigit = (numb/10); // Findout Tenth Digit
Proteus project
Proteous project
Calculator.asm