0% found this document useful (0 votes)
3 views2 pages

Microcontroller Codes Final

The document contains microcontroller programs for scanning keys and generating waveforms using a DAC. It includes functions for generating square, triangle, and sawtooth waves based on switch inputs, as well as reading ADC values to control LED outputs. Additionally, it features code for displaying values on a 7-segment display based on ADC readings.

Uploaded by

gudisettisai0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Microcontroller Codes Final

The document contains microcontroller programs for scanning keys and generating waveforms using a DAC. It includes functions for generating square, triangle, and sawtooth waves based on switch inputs, as well as reading ADC values to control LED outputs. Additionally, it features code for displaying values on a 7-segment display based on ADC readings.

Uploaded by

gudisettisai0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Microcontroller Programs (Final 2-Page Sheet)

#include <reg51.h>

// Define Rows
sbit R0 = P1^0;
sbit R1 = P1^1;
sbit R2 = P1^2;
sbit R3 = P1^3;

// Define Columns
sbit C0 = P1^4;
sbit C1 = P1^5;
sbit C2 = P1^6;
sbit C3 = P1^7;

unsigned char key;

void delay()
{
int i, j; // DAC Wave Generation
for(i = 0; i < 100; i++) #include <reg51.h>
for(j = 0; j < 100; j++); #define DAC_PORT P2
} sbit SW1 = P1^0;
sbit SW2 = P1^1;
unsigned char scan_key() sbit SW3 = P1^2;
{
R0 = R1 = R2 = R3 = 1; unsigned char i;

R0 = 0; void delay(){
if(C0 == 0) { delay(); return '1'; } int x,y;
if(C1 == 0) { delay(); return '2'; } for(x=0;x<100;x++)
if(C2 == 0) { delay(); return '3'; } for(y=0;y<500;y++);
if(C3 == 0) { delay(); return 'A'; } }

R0 = 1; R1 = 0; void square_wave(){
if(C0 == 0) { delay(); return '4'; } while(SW1==0){
if(C1 == 0) { delay(); return '5'; } DAC_PORT=0xFF; delay();
if(C2 == 0) { delay(); return '6'; } DAC_PORT=0x00; delay();
if(C3 == 0) { delay(); return 'B'; } }
}
R1 = 1; R2 = 0;
if(C0 == 0) { delay(); return '7'; } void triangle_wave(){
if(C1 == 0) { delay(); return '8'; } while(SW2==0){
if(C2 == 0) { delay(); return '9'; } for(i=0;i<255;i++){ DAC_PORT=i; delay();}
if(C3 == 0) { delay(); return 'C'; } for(i=255;i>0;i--){ DAC_PORT=i; delay();}
}
R2 = 1; R3 = 0; }
if(C0 == 0) { delay(); return '*'; }
if(C1 == 0) { delay(); return '0'; } void sawtooth_wave(){
if(C2 == 0) { delay(); return '#'; } while(SW3==0){
if(C3 == 0) { delay(); return 'D'; } for(i=0;i<255;i++){ DAC_PORT=i; delay();}
}
return 0; }
}
void main(){
void main() while(1){
{ if(SW1==0) square_wave();
while(1) else if(SW2==0) triangle_wave();
{ else if(SW3==0) sawtooth_wave();
key = scan_key(); else DAC_PORT=0x00;
} }
} }
// ADC to LED
#include <reg51.h>
#define ADC_DATA P1
#define LED_PORT P2

sbit RD=P3^7;
sbit WR=P3^6;
sbit INTR=P3^2;

unsigned char adc_value, led_output;

void delay(){
int i,j;
for(i=0;i<200;i++)
for(j=0;j<500;j++);
}

unsigned char read_adc(){


WR=0; delay(); WR=1;
while(INTR==1);
RD=0;
adc_value=ADC_DATA; // 7 Segment
RD=1; #include <reg51.h>
return adc_value;
} unsigned char seg_code[10]={
0x3F,0x06,0x5B,0x4F,0x66,
void main(){ 0x6D,0x7D,0x07,0x7F,0x6F};
while(1){
adc_value=read_adc(); void delay(){
int i,j;
if(adc_value<32) led_output=0x01; for(i=0;i<200;i++)
else if(adc_value<64) led_output=0x03; for(j=0;j<1000;j++);
else if(adc_value<96) led_output=0x07; }
else if(adc_value<128) led_output=0x0F;
else if(adc_value<160) led_output=0x1F; void main(){
else if(adc_value<192) led_output=0x3F; int i;
else if(adc_value<224) led_output=0x7F; while(1){
else led_output=0xFF; for(i=0;i<10;i++){
P1=seg_code[i];
LED_PORT=led_output; delay();
delay(); }
} }
} }

You might also like