0% found this document useful (0 votes)
15 views

Programming 8051 Micro Controller 3

This document provides code samples for programming an 8051 microcontroller to read analog to digital converter (ADC) values. The first sample is in 8051 assembly language and shows how to initialize ports for ADC control signals, perform an ADC conversion by controlling the chip select and write lines, and read the converted value from the ADC data port. The second sample translates this into C code by defining the ports, creating functions for conversion and reading, and calling these functions in a loop to continuously sample the ADC and output the values.

Uploaded by

Abhishek Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Programming 8051 Micro Controller 3

This document provides code samples for programming an 8051 microcontroller to read analog to digital converter (ADC) values. The first sample is in 8051 assembly language and shows how to initialize ports for ADC control signals, perform an ADC conversion by controlling the chip select and write lines, and read the converted value from the ADC data port. The second sample translates this into C code by defining the ports, creating functions for conversion and reading, and calling these functions in a loop to continuously sample the ADC and output the values.

Uploaded by

Abhishek Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Programming 8051 Microcontroller

8051 Assembly Programming for ADC0804 CODE:

rd equ P1.0 wr equ P1.1 cs equ P1.2 intr equ P1.3 adc_port equ P2 adc_val equ 30H org 0H start: acall conv acall read mov P3,adc_val sjmp start conv: clr cs clr wr nop setb wr setb cs wait: jb intr,wait ret read: clr cs clr rd mov a,adc_port mov adc_val,a setb rd setb cs ret

;Read signal P1.0 ;Write signal P1.1 ;Chip Select P1.2 ;INTR signal P1.3 ;ADC data pins P2 ;ADC read value stored here ;Start of Program ;Start ADC conversion ;Read converted value ;Move the value to Port 3 ;Do it again ;Start of Conversion ;Make CS low ;Make WR Low ;Make WR High ;Make CS high ;Wait for INTR signal ;Conversion done ;Read ADC value ;Make CS Low ;Make RD Low ;Read the converted value ;Store it in local variable ;Make RD High ;Make CS High ;Reading done

Programming 8051 in C for ADC0804 CODE:

#include <REGX51.H>#define adc_port P2 //ADC Port #define rd P1_0 //Read signal P1.0 #define wr P1_1 //Write signal P1.1 #define cs P1_2 //Chip Select P1.2 #define intr P1_3 //INTR signal P1.3 void conv(); void read(); //Start of conversion function //Read ADC function

unsigned char adc_val; void main(){ while(1){ conv(); read(); P3 = adc_val; } } void conv(){ cs = 0; wr = 0; wr = 1; cs = 1; while(intr); } void read(){ cs = 0; rd = 0; adc_val = adc_port; rd = 1; cs = 1; } //Forever loop //Start conversion //Read ADC //Send the read value to P3

//Make //Make //Make //Make //Wait

CS low WR low WR high CS high for INTR to go low

//Make //Make //Read //Make //Make

CS low RD low ADC port RD high CS high

You might also like