You are on page 1of 3

D:\08-Microprocessors\3-Assignment 3\Program V1.0\prog.

c // // // // // // // // // Higher National Diploma IEEE MCAST Malta Unit 10- Microprocessor Systems Lecturer: Mr William Dimech Assignment 3- Interface Microprocessor based systems Autor: Predrag Spasojevic Date: 24.05.2011. Software to interface Atmel 89C51AC2 and PIA 8255, read temperature from LM35 and display on LCD connected on PIA 8255 port Note: Tested on Proteus ISUS simulator with external ADC & in hardware

//Pre-processor commands #include <t89c51ac2.h> #include <absacc.h> //Port expander ports are accessed as memory locations and addresses are folowing #define PPIA #define PPIB #define PPIC #define PPICNTRL //Display command #define #define #define #define first_line second_line clear_lcd degree 0x4000 0x4001 0x4002 0x4003 0x80 0xC0 0x01 223 //Port //Port //Port //8255 // // // // A memory location B memory location C memory location Control Register memory location Force cursor to start at first line Force cursor to start at second line Command to clean LCD Degree Centigrate character for LCD

//Prototype Functions used void message(unsigned char * word,val); void PPI_data(unsigned char value); void PPI_cmd(unsigned char value); unsigned char ADC(void);

// // // //

function function function Analogue

for displaying messages to pass data to port expander to pass command to port expander to digital conversion function

//Enable Pulse void USDelay(unsigned char ustime); // delay in micro seconds //in between Characters void MSDelay(unsigned int mstime); // delay in milliseconds // ********** MAIN PROGRAM void main(void) { unsigned char temp; XBYTE[PPICNTRL]=0x80; // LCD initialisation PPI_cmd(0x38); PPI_cmd(0x0E); PPI_cmd(clear_lcd); PPI_cmd(first_line); PPI_cmd(0x0C); // A/D initialisation ADCF=0x01; ADCON=0x20; ADCLK=0x08; while(1) { //init. LCD 2 lines, 5x7 matrix, 8-bits mode // LCD on, cursor on // Clear LCD // start at first line of LCD // LCD ON cursor off *************//

// all PIA ports are defined as output

//P1.0 is set as ADC //Enable ADC function //Time=1.33us 16.7us

//repeat forever loop temp=ADC(); message("TEMPERATURE IS:",temp); PPI_cmd(first_line); // Read temperature and convert // Display message in first row // Go to first row

} }

Page: 1

D:\08-Microprocessors\3-Assignment 3\Program V1.0\prog.c //LCD Messages void message(unsigned char * word ,result) { unsigned char number,digit1,digit2,digit3; while(*word != '\0') { PPI_data(*word); *word++; MSDelay(1); } // Execute till end of message (detect null character) // Send character to LCD // Increment pointer

number=result; digit1 = number/100;//val2 = val1/100; digit2 = number/10;//val3 = val1%10; digit2 = digit2 % 10; digit3 = number % 10; digit1 = (digit1 + 0x30); digit2 = (digit2 + 0x30); digit3 = (digit3 + 0x30); PPI_cmd(second_line); if(digit1==0x30) { PPI_cmd(second_line); } else PPI_data(digit1);

// // // //

extract first digit from result extract second digit from result extract third digit

// change to ASCII // change to ASCII // change to ASCII // start in second line // avoid showing 0 as first digit

if((digit1==0x30) & (digit2==0x30)) { PPI_cmd(second_line); // avoid showing 0 as second digit } else PPI_data(digit2); PPI_data(digit3); PPI_data(degree); PPI_data('C'); PPI_data(' '); } //LCD Command void PPI_cmd(unsigned char value) { XBYTE[PPIA]=value; //strobe the Enable Pin XBYTE[PPIB]=0x04; USDelay(100); XBYTE[PPIB]=0x00; MSDelay(40); } //LCD Data void PPI_data(unsigned char value) { XBYTE[PPIA]=value; //strobe the Enable pin XBYTE[PPIB]=0x05; USDelay(100); XBYTE[PPIB]=0x01; MSDelay(40); } Page: 2

// print blank space

//RS=0 for command //R/W=0 for write //E=1 for H-to-L pulse //~100us //E=0 //delay between commands

//RS=1 for data //R/W=0 for write //E=1 for H-to-L pulse //~100us //E=0

D:\08-Microprocessors\3-Assignment 3\Program V1.0\prog.c //LCD Enable Pulse void USDelay(unsigned char ustime) { unsigned char i; //single loop for(i=0;i<ustime;i++); } //Used in between Characters void MSDelay(unsigned int mstime) { unsigned int i,j; //nested loops //outer loop for(i=0;i<mstime;i++) //inner loop for(j=0;j<190;j++); } //********** A/D converion function ******************// unsigned char ADC() { //8bit variable for result of conversion unsigned char temp_ad; ADCON=ADCON | 0x20; //enable ADC ADCON=ADCON & 0xF8; //select CH ADC 0 ADCON=ADCON|0x00; //mask ADCON=ADCON|0x08; //start ADC conversion (ADSST=1) //wait ADEOC=1 bit4 ( end of conversion flag) while((ADCON & 0x10)!=0x10); temp_ad=ADDH; //store result in variable ADCON=ADCON & 0xEF; //clear ADEOC flag=0 (clear end of conversion flag) //retur result of conversion to main function return(temp_ad); }

Page: 3

You might also like