Sie sind auf Seite 1von 30

1.

Stepper Motor with HTI:


#include <REG52.H> //--------------Funtion declaration void MSDelay (unsigned int); //Delay void Send (unsigned char); //Serial data transfer void main() { unsigned char rdata; //Serially recieved data //-----------------The message unsigned char idata msg[8][30] = {" Sitec Electronics ", " Stepper Motor Test Program ", Please Choose An Option ", "1. Clockwise Rotation ", "2. AntiClockwise Rotation ", "3. Clockwise 'xx' RPM", "4. Clockwise 'xx' RPM", "5. Clockwise 'xx' RPM"}; P1 = 00; TMOD = 0x20; TH1 = -3; SCON = 0x50; TR1 = 1; //Set port 1 as output //timer 1,mode 2(auto-reload) //9600 baud rate //8-bit,1 stop, REN enabled //start timer 1

while (1) { //-------------Display message on hyper terminal unsigned char i,j; Send (0x0C); for (i=0;i<8;i++) { for (j=0;j<30;j++) { Send (msg[i][j]); } Send (0x0D); Send (0x0A); } while (RI==0); rdata = SBUF; RI =0; switch (rdata) { //Wait until recive flag is set //get data recived //clear flag for next incoming data //loop to respective subroutine

case (49): { while (RI==0) funtion until next command is recived

//keep doing the

{ P0 =0x66; //Clockwise Rotation MSDelay (1); P0 =0xCC; MSDelay (1); P0 =0x99; MSDelay (1); P0 =0x33; MSDelay (1); } break; } case (50): { while (RI==0) { P0 =0x33; //AntiClockwise Rotation MSDelay (1); P0 =0x99; MSDelay (1); P0 =0xCC; MSDelay (1); P0 =0x66; MSDelay (1); } break; } case (51): { while (RI==0) { P0 =0x66; //Clockwise Rotation with reduced speed MSDelay (3); P0 =0xCC; MSDelay (3); P0 =0x99; MSDelay (3); P0 =0x33; MSDelay (3); } break; } case (52): { while (RI==0) {

P0 =0x66; with reduced speed MSDelay (6); P0 =0xCC; MSDelay (6); P0 =0x99; MSDelay (6); P0 =0x33; MSDelay (6); } break; } case (53): { while (RI==0) { P0 =0x66; with reduced speed MSDelay (9); P0 =0xCC; MSDelay (9); P0 =0x99; MSDelay (9); P0 =0x33; MSDelay (9); } break; } } } } void MSDelay (unsigned int value) // Delay rouitne { unsigned int x,y; for (x=0;x<600;x++) for (y=0;y<value;y++); } void Send (unsigned char x) { SBUF = x; while (TI==0); TI=0; } // Routine to send data serially

//Clockwise Rotation

//Clockwise Rotation

2.

Stepper Motor with Matrix Keypad:

#include <REG52.H> //--------------Funtion declaration void MSDelay (unsigned int); //Delay unsigned char getkey (); unsigned char key=0; unsigned char idata keypad[4][4]= { 'A','C','X','X', '1','2','3','X', 'X','X','X','X', 'X','X','X','X',}; void main() { P1 = 00; while (1) { getkey (); MSDelay(10); switch (key) { case ('C'): { while (key=='C') the funtion until next command is recived { P0 =0x66; Rotation MSDelay (1); P0 =0xCC; MSDelay (1); P0 =0x99; MSDelay (1); P0 =0x33; MSDelay (1); getkey (); } break; } case ('A'): { while (key=='A') { P0 =0x33; //AntiClockwise Rotation //Clockwise //keep doing //get data from matrix key pad //loop to respective subroutine //Set port 1 as output

MSDelay (1); P0 =0x99; MSDelay (1); P0 =0xCC; MSDelay (1); P0 =0x66; MSDelay (1); getkey (); } break; } case ('1'): { while (key=='1') { P0 =0x66; Rotation with reduced speed MSDelay (3); P0 =0xCC; MSDelay (3); P0 =0x99; MSDelay (3); P0 =0x33; MSDelay (3); getkey (); } break; } case ('2'): { while (key=='2') { P0 =0x66; Rotation with reduced speed MSDelay (6); P0 =0xCC; MSDelay (6); P0 =0x99; MSDelay (6); P0 =0x33; MSDelay (6); getkey (); } break; } case ('3'): { while (key=='3') { P0 =0x66; Rotation with reduced speed

//Clockwise

//Clockwise

//Clockwise

MSDelay (9); P0 =0xCC; MSDelay (9); P0 =0x99; MSDelay (9); P0 =0x33; MSDelay (9); getkey (); } break; } } } } void MSDelay (unsigned int value) // Delay rouitne { unsigned int x,y; for (x=0;x<600;x++) for (y=0;y<value;y++); } unsigned char getkey () { unsigned char colloc, rowloc; TMOD = 0x20; TH1 = -3; SCON = 0x50; TR1 = 1; do P2 = 0x0f; { do { MSDelay (1); colloc = P2; colloc &= 0x0f; } while (colloc == 0x0f); //Check whether any ket is pressed MSDelay (1); colloc = P2; colloc &= 0x0f; ket is pressed after delay } while (colloc == 0x0f); while(1) { P2 = 0xfE; presses //to aviod spikes //Confirm whether any // Routine to read matrix keyboard

//get the row

colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 0; break; } P2 = 0xfd; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 1; break; } P2 = 0xfb; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 2; break; } P2 = 0xf7; colloc = P2; colloc &= 0xf0; rowloc = 3; break; } //get the coloum presses if (colloc == 0xe0) key = (keypad[rowloc][0]); else if (colloc == 0xd0) key = (keypad[rowloc][1]); else if (colloc == 0xb0) key = (keypad[rowloc][2]); else key = (keypad[rowloc][3]); return(key); }

3. ADC_Temperature
#include <reg52.h> sbit rd = P1^1; sbit wr = P1^2; sbit intr = P1^3; sbit aen = P1^0; sfr mydata1 = 0xA0; sfr ldata = 0x80; sbit rs = P3^7; sbit rw = P3^6; sbit en = P3^5; sbit busy = P0^7; //Variables declaration for ADC

//ADC data //Variables declaration for LCD

//--------------Funtion declaration void convert_display(unsigned char);//Hex to Binary converter void lcdcmd (unsigned char value); //LCD command void lcddata (unsigned char value); //LCD data void lcdready (); //Initialization LCD 2 lines,5x7 matrix void MSDealy(unsigned int itime); //Delay void main() { unsigned char value; lcdcmd(0x38); lcdcmd(0x0e); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x83); lcddata('T'); lcddata('e'); lcddata('m'); lcddata('p'); lcddata('e'); lcddata('r'); lcddata('a'); lcddata('t'); lcddata('u'); lcddata('r'); lcddata('e'); lcddata('r'); mydata1 = 0xff; ldata = 0x00; //ADC data recived //LCD command

//LCD data

//Set port 2 as input //Set port 0 as output

//---------ADC routine while (1) { aen = 0; wr = 0; wr = 1; CONVERSION while (intr == 1) ; rd = 0; FINISHED,ENABLE RD value = mydata1; value = value - 114; lcdcmd(0xC7); convert_display(value); rd = 1; MSDealy(10); } } //----------------Hex to Binary convertion routine void convert_display(unsigned char value) { unsigned char x, d1, d2, d3, data1, data2, data3; x = value / 10; //divide by 10 d1 = value % 10; //save low digit (reminder of division) d2 = x % 10; d3 = x / 10; //divide by 10 once more data1 = d1 | 0x30; //make it ASCII and save LSB data2 = d2 | 0x30; data3 = d3 | 0x30; lcddata(data3); lcddata(data2); lcddata(data1); return; } //--------------LCD command void lcdcmd (unsigned char value) { lcdready(); ldata = value; rs = 0; rw = 0; en = 1; MSDealy(1); en = 0; return; } //display converted output MSB first //READ THE DATA //WAIT FOR END OF CONVERSION //CONVERSION //enable ADC //Write=0 //WR=1 L-TO-H TO START

//MAKE RD=1 FOR NEXT ROUND

//is LCD ready? //issue command code //RS=0 for command //R/W=0 to write to LCD //E=1 for H-to-L pulse //E=0 ,latch in

//-------------LCD data void lcddata (unsigned char value) { lcdready(); ldata = value; rs = 1; rw = 0; en = 1; MSDealy(1); en = 0; return; }

//is LCD ready? //issue data //RS=1 for data //R/W=0 to write to LCD //E=1 for H-to-L pulse //E=0, latch in

void lcdready () { busy = 1; //make P1.7 input port rs = 0; //RS=0 access command reg rw = 1; //R/W=1 read command reg ;read command reg and check busy flag while (busy == 1) //stay until busy flag=0 { en = 0; //E=1 for H-to-L pulse MSDealy(1); en = 1; //E=0 H-to-L pulse } return; } //------------Delay Routine void MSDealy(unsigned int itime) { unsigned int i,j; for (i=0;i<itime;i++) for (j=0;j<1275;j++); }

4. CALC_LCD
#include <REG52.H> sfr ldata = 0x80; sbit rs = P3^7; sbit rw = P3^6; sbit en = P3^5; sbit busy = P0^7; //Variables declaration for LCD

//--------------Funtion declaration void convert_display(unsigned char);//Hex to Binary converter void MSDelay (unsigned int); //Delay void lcdcmd (unsigned char value); void lcddata (unsigned char value); void lcdready (); unsigned char getkey (); unsigned char j,countl,countr, key=0,Data1,Data2,Funct,Ans ; recieved data //-----------------The message unsigned char idata msg[11] = {"Calculator "}; unsigned char idata keypad[4][4]= { '1','2','3','/', '4','5','6','*', '7','8','9','-', 'C','0','C','+',}; void main() { while(key!='C') { lcdcmd(0x38); lcdcmd(0x0e); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x83); //-------------Display message on lCD terminal for (j=0;j<11;j++) { lcddata(msg[j]); from lookup table } while(1) { lcdcmd(0xC3); //Get data //Serially

do { getkey (); MSDelay(10); Data1 = key; key = key & 0xF0; }while (key!=0x30); lcddata(Data1); do { getkey (); MSDelay(10); Funct = key; key = key & 0xF0; }while (key!=0x20); lcddata(Funct); do { getkey (); MSDelay(10); Data2 = key; key = key & 0xF0; }while (key!=0x30); lcddata(Data2); lcddata('='); Data1= Data1 & 0x0F; Data2= Data2 & 0x0F; switch (Funct) { //loop to respective subroutine case ('+'): { Ans = Data1 + Data2; convert_display(Ans); break; } case ('-'): { Ans = Data1 - Data2; convert_display(Ans); break; } case ('*'): { Ans = Data1 * Data2; convert_display(Ans); break; } //get data from matrix key pad

//get data from matrix key pad

//get data from matrix key pad

case ('/'): { Ans = Data1 / Data2; convert_display(Ans); break; } }}}} void MSDelay (unsigned int value) { unsigned int x,y; for (x=0;x<900;x++) for (y=0;y<value;y++); } void lcdcmd (unsigned char value) { lcdready(); ldata = value; rs = 0; rw = 0; en = 1; MSDelay(1); en = 0; return; } void lcddata (unsigned char value) { lcdready(); ldata = value; rs = 1; rw = 0; en = 1; MSDelay(1); en = 0; return; } void lcdready () { busy = 1; rs = 0; rw = 1; while (busy == 1) { en = 0; MSDelay(1); en = 1; // Delay routine

} return; } unsigned char getkey () { unsigned char colloc, rowloc; TMOD = 0x20; TH1 = -3; SCON = 0x50; TR1 = 1; P2 = 0xff; do released colloc = P2; colloc &= 0x0f; } while (colloc != 0x0f); do { do { MSDelay (1); colloc = P2; colloc &= 0x0f; } while (colloc == 0x0f); //Check whether any ket is pressed MSDelay (1); colloc = P2; colloc &= 0x0f; ket is pressed after delay } while (colloc == 0x0f); while(1) { P2 = 0xfE; presses colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 0; break; } P2 = 0xfd; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 1; //to aviod spikes //Confirm whether any { P2 = 0x0f; //Wait untill all keys are // Routine to read matrix keyboard

//get the row

break; } P2 = 0xfb; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 2; break; } P2 = 0xf7; colloc = P2; colloc &= 0xf0; rowloc = 3; break; } //get the coloum presses if (colloc == 0xe0) key = (keypad[rowloc][0]); else if (colloc == 0xd0) key = (keypad[rowloc][1]); else if (colloc == 0xb0) key = (keypad[rowloc][2]); else key = (keypad[rowloc][3]); return(key); } //----------------Hex to Binary convertion routine void convert_display(unsigned char value) { unsigned char x, d1, d2, d3, data1, data2, data3; x = value / 10; //divide by 10 d1 = value % 10; //save low digit (reminder of division) d2 = x % 10; d3 = x / 10; //divide by 10 once more data1 = d1 | 0x30; //make it ASCII and save LSB data2 = d2 | 0x30; data3 = d3 | 0x30; lcddata(data3); lcddata(data2); lcddata(data1); return; } //display converted output MSB first

5. DC Motor Speed Control


#include <reg52.h> //--------------Funtion declaration void MSDelay (unsigned int); //Delay void Send (unsigned char); //Serial data transfer sbit enablr = P0^6; sbit mtr_1 = P0^7; sbit mtr_2 = P0^4; void main() { unsigned char rdata; //Serially recieved data //-----------------The message unsigned char idata msg[8][30] = {" Sitec Electronics ", " DC Motor Test Program ", " Please Choose An Option ", "1. AntiClockwise Rotation ", "2. Clockwise Rotation ", "3. AClkwise Rotation 'xx' RPM", "4. AClkwise Rotation 'xx' RPM", "5. AClkwise Rotation 'xx' RPM"}; P1 = 00; TMOD = 0x20; TH1 = -3; SCON = 0x50; TR1 = 1; //Set port 1 as output //timer 1,mode 2(auto-reload) //9600 baud rate //8-bit,1 stop, REN enabled //start timer 1 //Variables declaration for ADC

while (1) { //-------------Display message on hyper terminal unsigned char i,j; Send (0x0C); for (i=0;i<8;i++) { for (j=0;j<30;j++) { Send (msg[i][j]); } Send (0x0D); Send (0x0A); } enablr = 1; while (RI==0); //emable H-Bridge //Wait until recive flag is set

rdata = SBUF; RI =0; data switch (rdata) {

//get data recived //clear flag for next incoming //loop to respective subroutine case (49): { while (RI==0)

//keep doing the funtion //Clockwise

until next command is recived { mtr_1 = 1; Rotation mtr_2 = 0; } break; } case (50): { while (RI==0) { mtr_1 = 0; mtr_2 = 1; Rotation } break; } case (51): { while (RI==0) { mtr_2 = 0; mtr_1 = 1; //Clockwise Rotation with PWM speed control MSDelay (5); mtr_1 = 0; MSDelay (5); } break; } case (52): { while (RI==0) { mtr_2 = 0; mtr_1 = 1; //Clockwise Rotation with PWM speed control MSDelay (1); mtr_1 = 0; MSDelay (20); } break; } case (53): { while (RI==0) {

//AntiClockwise

mtr_2 = 0; mtr_1 = 1; //Clockwise Rotation with PWM speed control MSDelay (1); mtr_1 = 0; MSDelay (40); } break; } } } } //------------Delay Routine void MSDelay (unsigned int value) { unsigned int x,y; for (x=0;x<50;x++) for (y=0;y<value;y++); } //------------Data Transfer Routine void Send (unsigned char x) { SBUF = x; while (TI==0); TI=0; }

6. Elevator
#include <REG52.H> sfr ldata = 0x80; sbit rs = P3^7; sbit rw = P3^6; sbit en = P3^5; sbit busy = P0^7; sbit carry = PSW^7; //Variables declaration for LCD

//--------------Funtion declaration void convert_display(unsigned char); void MSDelay (unsigned int); void lcdcmd (unsigned char value); void lcddata (unsigned char value); void lcdready (); void down (); void up (); unsigned char getkey ();

//Hex to Binary converter //Delay

unsigned char i,j,countl,countr, key=0,Data1='0',Data2='0',Funct,Ans ; //Serially recieved data //-----------------The message unsigned char idata msg[9] = {"Floor No:"}; unsigned char idata keypad[4][4]= { '4','5','6','X', '7','8','9','X', 'X','0','X','X',}; void main() { lcdcmd(0x38); lcdcmd(0x0e); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x80); //-------------Display message on hyper terminal for (j=0;j<9;j++) { lcddata(msg[j]); lookup table } lcdcmd(0x8A); lcddata('0'); lcddata('0'); while(1) { do { getkey (); MSDelay(10); Data1 = key; key = key & 0xF0; }while (key!=0x30); carry =0; if (Data1<Data2) down(); else up(); }} void down() { for(i=(Data2-Data1);i>0;i--) { for (j=3;j>0;j--) { //get data from matrix key pad //Get data from '1','2','3','X',

lcdcmd(0x8E); lcddata('v'); lcddata('v'); MSDelay(30); lcdcmd(0x8E); lcddata(' '); lcddata(' '); MSDelay(30); } Data2--; lcdcmd(0x8b); lcddata(Data2); } } void up() { for(i=(Data1-Data2);i>0;i--) { for (j=3;j>0;j--) { lcdcmd(0x8E); lcddata('^'); lcddata('^'); MSDelay(30); lcdcmd(0x8E); lcddata(' '); lcddata(' '); MSDelay(30); } Data2++; lcdcmd(0x8b); lcddata(Data2); } } void MSDelay (unsigned int value) { unsigned int x,y; for (x=0;x<900;x++) for (y=0;y<value;y++); } void lcdcmd (unsigned char value) { lcdready(); ldata = value; rs = 0; rw = 0; en = 1; MSDelay(1); // Delay routine

en = 0; return; } void lcddata (unsigned char value) { lcdready(); ldata = value; rs = 1; rw = 0; en = 1; MSDelay(1); en = 0; return; } void lcdready () { busy = 1; rs = 0; rw = 1; while (busy == 1) { en = 0; MSDelay(1); en = 1; } return; } unsigned char getkey () { unsigned char colloc, rowloc; TMOD = 0x20; TH1 = -3; SCON = 0x50; TR1 = 1; P2 = 0xff; do released colloc = P2; colloc &= 0x0f; } while (colloc != 0x0f); do { do { MSDelay (1); colloc = P2; colloc &= 0x0f; } while (colloc == 0x0f); //Check whether any ket is pressed MSDelay (1); colloc = P2; { P2 = 0x0f; //Wait untill all keys are // Routine to read matrix keyboard

colloc &= 0x0f; whether any ket is pressed after delay } while (colloc == 0x0f); while(1) { P2 = 0xfE; the row presses

//Confirm //to aviod spikes

//get

colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 0; break; } P2 = 0xfd; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 1; break; } P2 = 0xfb; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 2; break; } P2 = 0xf7; colloc = P2; colloc &= 0xf0; rowloc = 3; break; } //get the coloum presses if (colloc == 0xe0) key = (keypad[rowloc][0]); else if (colloc == 0xd0) key = (keypad[rowloc][1]); else if (colloc == 0xb0) key = (keypad[rowloc][2]); else key = (keypad[rowloc][3]); return(key); } /*----------------Hex to Binary convertion routine void convert_display(unsigned char value) { unsigned char x, d1, d2, d3, data1, data2, data3; x = value / 10; //divide by 10 d1 = value % 10; //save low digit (reminder of division) d2 = x % 10; d3 = x / 10; //divide by 10 once more

data1 = d1 | 0x30; data2 = d2 | 0x30; data3 = d3 | 0x30; lcddata(data3); lcddata(data2); lcddata(data1); return; } */

//make it ASCII and save LSB

//display converted output MSB first

7. DAC with HTI


#include <REG52.H> //--------------Funtion declaration void MSDelay (unsigned int); //Delay void Send (unsigned char); //Serial data transfer void main() { unsigned char rdata; //Serially recieved data //-----------------The message unsigned char xdata msg[8][29] = {"******Sitec Electronics******", " DAC Interface Program ", "1. Ramp ", "2. Rectangle Wave ", "3. Sawtooth Wave "4. Sine Wave ", "5. Square Wave "6. Triangle Wave unsigned char idata table[180] = {128,132,136,141,154,150,154,158,163,167,171,175,180,184,188, 192,195,199,203,206,210,213,216,220,223,226,228,231,234,236, 238,241,243,244,246,247,248,249,250,251,252,253,254,255,255, 255,255,255,254,254,253,252,251,249,246,244,243,241,238,236, 234,231,228,226,223,220,216,213,210,206,203,199,195,192,188, 184,180,175,171,167,163,158,154,150,145,141,136,132,128, 123,119,114,110,105,101,97,92,88,84,80,75,71,67,64,60,56,52, 49,45,42,39,35,32,29,27,24,21,19,17,14,12,11,9,7,6,4,3,2,1,1,0,0,0,0, 0,0,0,0,1,1,2,3,4,6,7,9,11,12,14,17,19,21,24,27,29,32,35,39,42,45,49, 52,56,60,64,67,71,75,80,84,88,92,97,101,105,110,114,119,123,128}; ", ", "};

P0 = 00; TMOD = 0x20; TH1 = -3; SCON = 0x50; TR1 = 1;

//Set port 1 as output //timer 1,mode 2(auto-reload) //9600 baud rate //8-bit,1 stop, REN enabled //start timer 1

while (1) { //-------------Display message on hyper terminal unsigned char i,j; Send (0x0C); for (i=0;i<8;i++) { for (j=0;j<29;j++) { Send (msg[i][j]); } Send (0x0D); Send (0x0A); } while (RI==0); rdata = SBUF; RI =0; switch (rdata) { //Wait until recive flag is set //get data recived //clear flag for next incoming data //loop to respective subroutine case (49): { while (RI==0) until next command is recived { for (i=0;i<255;i++) { P0= i; } } break; } case (50): { while (RI==0) { MSDelay (50); P0 = 0xff; MSDelay (100); } break; } case (51): { while (RI==0) { for (i=0;i<255;i=i+5) { //Ramp

//keep doing the funtion

//Rectangular Wave

//Sawtooth

P0= i; } for (i=255;i>1;i--) { P0= i; } } break; } case (53): { while (RI==0) { MSDelay (50); //Square Wave P0 = 0xff; MSDelay (50); } break; } case (52): { while (RI==0) { for (i=0;i<180;i++) { P0= (table[i]); //Sine Wave } break; } case (54): { while (RI==0) { for (i=0;i<255;i++) //Triangle { P0= i; } for (i=255;i>0;i--) { P0= i; } } break; } } } } } void MSDelay (unsigned int value) { unsigned int y; for (y=0;y<value;y++); } void Send (unsigned char x)

{ SBUF = x; while (TI==0); TI=0; }

8. 7 Segment with matrix Keypad


#include <REG52.H> sbit srck = P0^6 ; sbit ser = P0^5 ; sbit rck = P0^7 ; //--------------Funtion declaration void display(unsigned char); //Hex to Binary converter void MSDelay (unsigned int); //Delay unsigned char getkey (); unsigned char temp,numb,key=0; //Serially recieved data //-----------------The message unsigned char idata keypad[4][4]= { '1','2','3','0', '4','5','6','0', '7','8','9','0', '0','0','0','0',}; unsigned char idata disp[10] = { 0x40,0xcf,0xa4,0x30,0x19,0x12,0x02,0xf8,0x00,0x10 }; void main() { while(1) { getkey(); temp= key | 0x0F ; numb = disp [temp]; display(numb); } } void MSDelay (unsigned int value) { unsigned int x,y; for (x=0;x<900;x++) for (y=0;y<value;y++); } unsigned char getkey () { unsigned char colloc, rowloc; TMOD = 0x20; TH1 = -3; SCON = 0x50; TR1 = 1; P2 = 0xff; // Delay routine

// Routine to read matrix keyboard

do

{ P2 = 0x0f; colloc = P2; colloc &= 0x0f; } while (colloc != 0x0f); //Wait untill all keys are released

do

{ do { MSDelay (1); colloc = P2; colloc &= 0x0f; } while (colloc == 0x0f); //Check whether any ket is

pressed MSDelay (1); colloc = P2; colloc &= 0x0f; ket is pressed after delay } while (colloc == 0x0f); while(1) { P2 = 0xfE; presses colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 0; break; } P2 = 0xfd; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 1; break; } P2 = 0xfb; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 2; break; } P2 = 0xf7; colloc = P2; colloc &= 0xf0; rowloc = 3; break; } //get the coloum presses if (colloc == 0xe0) key = (keypad[rowloc][0]); //get the row //to aviod spikes //Confirm whether any

else if (colloc == 0xd0) key = (keypad[rowloc][1]); else if (colloc == 0xb0) key = (keypad[rowloc][2]); else key = (keypad[rowloc][3]); return(key); } //----------------7 segment display void display(unsigned char value) { /* SER CON P2 ; serial output SRCK CON P0 ; shift clock RCK CON P0 ; output latch Delay CON 100 counter VAR Byte ' ------------------------------------------------------------------------main for counter=0 to 255 gosub Out595 pause 100 ;Change to in/decrease speed next goto main ' ------------------------------------------------------------------------Out595: Shiftout SER,SRCK,MSBPRE,[counter\8] ; send pattern to 74hc595 Pulsout RCK,8 ; latch outputs(min 8us) can be as little as 4us Return */ unsigned char bits; srck = 1; rck = 0; ACC = value; for (bits=0;bits<8;bits++) { ser = ACC^0 ; srck = 1; srck = 0; ACC = ACC >> 1; } rck =1 ; srck =1 ; return; }

9. ADC with LED


#include <reg52.h> sbit rd = P1^1; sbit wr = P1^2; sbit intr = P1^3; sbit aen = P1^0; sfr mydata1 = 0xA0; //Variables declaration for ADC

//ADC data

sbit srck = P0^1 ; sbit ser = P0^2 ; sbit rck = P0^0 ; //--------------Funtion declaration void convert_display(unsigned char); void display(unsigned char); void MSDealy(unsigned int itime); void main() { unsigned char value; mydata1 = 0xff; //---------ADC routine while (1) { aen = 0; wr = 0; wr = 1; CONVERSION while (intr == 1) ; rd = 0; FINISHED,ENABLE RD value = mydata1; value = value - 114; convert_display(value); rd = 1; ROUND MSDealy(10); } } //----------------Hex to Binary convertion routine void convert_display(unsigned char value) { unsigned char x, d1, d2, d3, data1, data2, data3; x = value / 10; //divide by 10 d1 = value % 10; //save low digit (reminder of division) d2 = x % 10; d3 = x / 10; //divide by 10 once more display(0x3F); display(data3); display(data2); display(data1); return; } //----------------7 segment display void display(unsigned char value) { /* SER CON P2 ; serial output SRCK CON P0 ; shift clock //display converted output MSB first //READ THE DATA //MAKE RD=1 FOR NEXT //WAIT FOR END OF CONVERSION //CONVERSION //enable ADC //Write=0 //WR=1 L-TO-H TO START //ADC data recived //Set port 2 as input //Hex to Binary converter //Delay

RCK CON P0 ; output latch Delay CON 100 counter VAR Byte ' ------------------------------------------------------------------------main for counter=0 to 255 gosub Out595 pause 100 ;Change to in/decrease speed next goto main ' ------------------------------------------------------------------------Out595: Shiftout SER,SRCK,MSBPRE,[counter\8] ; send pattern to 74hc595 Pulsout RCK,8 ; latch outputs(min 8us) can be as little as 4us Return */ unsigned char idata disp[10] = { 0x40,0xcf,0xa4,0x30,0x19,0x12,0x02,0xf8,0x00,0x10 }; unsigned char bits; srck = 1; rck = 0; ACC = disp [value]; for (bits=0;bits<8;bits++) { ser = ACC^0 ; l srck = 1; srck = 0; ACC = ACC >> 1; } rck =1 ; srck =1 ; return; } //------------Delay Routine void MSDealy(unsigned int itime) { unsigned int i,j; for (i=0;i<itime;i++) for (j=0;j<1275;j++); }

Das könnte Ihnen auch gefallen