0% found this document useful (0 votes)
11 views5 pages

Volt

This document contains C code for interfacing an ADC0832 analog-to-digital converter and a 7-segment display using an 8051 microcontroller. The code initializes various pins for buttons, LEDs, and the ADC, and includes functions for reading analog values, displaying them on a 7-segment display, and allowing user calibration. The main loop continuously reads the ADC value, displays it, and enters a calibration mode when a button is pressed.

Uploaded by

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

Volt

This document contains C code for interfacing an ADC0832 analog-to-digital converter and a 7-segment display using an 8051 microcontroller. The code initializes various pins for buttons, LEDs, and the ADC, and includes functions for reading analog values, displaying them on a 7-segment display, and allowing user calibration. The main loop continuously reads the ADC value, displays it, and enters a calibration mode when a button is pressed.

Uploaded by

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

#include<reg52.

h>
#include<intrins.h>

#define uint unsigned int


#define uchar unsigned char
#define pulse0832() _nop_();_nop_();CLK_0832=1;_nop_();_nop_();CLK_0832=0

/*数模转换 ADC0832 管脚定义*/


sbit CS_0832 = P0^4;
sbit CLK_0832 = P0^5;
sbit D0_0832 = P0^6;
sbit DI_0832 = P0^7;

/*数码管中 74HC595 管脚定义*/


sbit DI1 = P1^0;
sbit DI2 = P1^1;
sbit DI3 = P1^2;
sbit DI4 = P1^3;
sbit DI0 = P1^4;
sbit RCK = P1^5;
sbit SCLK = P1^6;

/*按键的管脚定义*/
sbit Botton_On = P3^7;
sbit Botton1 = P3^6;
sbit Botton2 = P3^5;
sbit Botton_Off = P3^4;

/*LED 灯的管脚定义*/
sbit Led_Green = P0^0;
sbit Led_Red = P0^1;

unsigned char code Led_0f[] = {~0xC0,


~0xF9,~0xA4,~0xB0,~0x99,~0x92,~0x82,~0xF8,~0x80,~0x90,~0x88,~0x83,~0xC6,~0xA1,~0x86
,~0x8E};
uchar LED[4];
int u = 0;

main()
{
void delay(uint k);
void LED_Display(uint uu);
uint ADC0832(uint CH);

int temp;
char adjusting_On = 0;
char adjusting = 0;

/*状态初始化*/
Led_Green = 1;
Led_Red = 1;

DI1 = 0;
DI2 = 0;
DI3 = 0;
DI4 = 0;

while(1)
{
/*ADC0832 是八位串行输出数据,所以最大输出为 255,而需要测量 0-5 伏的电压,所以将读取的数据乘 2*/
u = (ADC0832(0x00) *2);

/*adjusting 是为了用户自己校准电压*/
u = u + adjusting;
temp = u;

LED_Display(u);

/*正常状态亮绿灯*/
Led_Green = 0;
Led_Red = 1;

/*按下 K1 进入用户校准电压模式*/
if(!Botton_On)
{
delay(50);
if(!Botton_On)
{
adjusting_On = 1;
}
delay(50);
}

while(adjusting_On)
{
/*校准电压模式蓝灯亮*/
Led_Green = 1;
Led_Red = 0;

/*按下 K2 加 1*/
if(!Botton1)
{
delay(100);
if(!Botton1)
{
adjusting++;
temp++;
while(!Botton1);
}
delay(50);
}

/*按下 K3 减 1*/
if(!Botton2)
{
delay(100);
if(!Botton2)
{
adjusting--;
temp--;
while(!Botton2) LED_Display(temp);;
}
delay(50);
}

/*按下 K4 结束校验模式*/
if(!Botton_Off)
{
delay(50);
if(!Botton_Off)
{
adjusting_On = 0;
while(!Botton_Off) LED_Display(temp);;
}
delay(50);
}
LED_Display(temp);
}
}

/*1ms 延时函数*/
void delay(uint k)
{
uint i = 0;
uint j = 0;
for(i=0; i<k; i++)
{
for(j=0; j<144; j++);
}
}

/*数码管显示函数*/
void LED_Display(uint uu)
{
unsigned char code *Led_Table;
uchar i,j,t;

/*按位取出参数*/
LED[0] = uu/1000%10;
LED[1] = uu/100%10;
LED[2] = uu/10%10;
LED[3] = uu%10;

/*四个数码管,用 for 循环来选择位码*/


for(t = 0; t < 4; t++)
{
/*查表取出对应字形码的段码*/
Led_Table = Led_0f + LED[t];
i = *Led_Table;

/*根据 for 循环的 t 来选择哪个数码管亮*/


switch(t)
{
case 0: DI1 = 0; DI2 = 0; DI3 = 0; DI4 = 1;break;
/* i = i-0x80 是为了打出小数点*/
case 1: DI1 = 1; DI2 = 0; DI3 = 0; DI4 = 0; i = i-0x80; break;
case 2: DI1 = 0; DI2 = 1; DI3 = 0; DI4 = 0;break;
case 3: DI1 = 0; DI2 = 0; DI3 = 1; DI4 = 0;break;
default: break;
}

/*取出串行输出的数据*/
for(j = 0; j<8; j++)
{
if ((i<<j)&0x80)
DI0=1;
else DI0=0;

/*一个上升沿取一次数据*/
SCLK = 0;
SCLK = 1;
}
/*一个上升沿将数据发送到数码管一次*/
RCK = 0;
RCK = 1;
DI1 = 0; DI2 = 0; DI3 = 0; DI4 = 0;
}
}

/*ADC0832 数码转换函数*/
uint ADC0832(uint CH)
{
uint i, ch = 0, ch1 = 0;

/*打开采样*/
CS_0832 = 0;

/*给 DI_0832 一个高,表示起始信号*/


DI_0832 = 1;

/*给它一个下降沿*/
pulse0832();

/*给 DI_0832 一个高让其选择工作方式,它与下一个 DI_0832 配合使用*/


DI_0832=1;
pulse0832();

/*根据 CH 的值选择通道*/
if(CH==0)
{
DI_0832=0;
}
else
{
DI_0832=1;
}
pulse0832();
DI_0832=1;

/*正向取出串行输出的数据*/
for(i = 0; i < 8; ++i)
{
pulse0832();
ch <<= 1;
if(D0_0832==1)
{
ch |= 0x01;
}
}

/*反向取出串行输出的数据*/
for(i = 0; i < 8; ++i)
{
ch1 >>= 1;
if(D0_0832==1)
{
ch1 |= 0x80;
}
pulse0832();
}

/*关闭采样*/
CS_0832=1;

/*如果两次数据一样则输出数据否则输出 0*/
return (ch==ch1) ? ch : 0;
}

You might also like