题目
1.控制板载PCF8591芯片在D/A引脚上输出指定的电压 2.数码管前三位显示输入数据0-255,后两位显示输出的电压值 3.按下按键S4则输入数据+40,按下S5则输入数据-40
main.c
#include<reg52.h>
#include<iic.h>
#include<intrins.h>
#include<absacc.h>
sbit hc138_A=P2^5;
sbit hc138_B=P2^6;
sbit hc138_C=P2^7;
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
unsigned char shuzi[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char shuzi1[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned char key_value = 0;
unsigned char input_v = 0;
int real_v = 0;
void HC138(unsigned int n)
{
switch(n)
{
case 4:
hc138_A=0 ; hc138_B=0 ; hc138_C=1; break;
case 5:
hc138_A=1 ; hc138_B=0 ; hc138_C=1; break;
case 6:
hc138_A=0 ; hc138_B=1 ; hc138_C=1; break;
case 7:
hc138_A=1 ; hc138_B=1 ; hc138_C=1; break;
}
}
void delay(unsigned int k)
{
while(k--);
}
void Display()
{
char x = 0x01;
int i = 0;
for(i = 1;i<=8 ;i++)
{
HC138(6);
P0 = x;
x = _crol_(x,1);
HC138(7);
P0 = 0xff;delay(20);
switch (i)
{
case 1 :P0 = shuzi[input_v/100]; break;
case 2 :P0 = shuzi[input_v/10%10]; break;
case 3 :P0 = shuzi[input_v%10]; break;
case 4 :P0 = 0xff; break;
case 5 :P0 = 0xff; break;
case 6 :P0 = 0xff; break;
case 7 :P0 = shuzi1[real_v / 10]; break;
case 8 :P0 = shuzi[real_v % 10]; break;
}
delay(500);P0 = 0xff;
}
}
void key_scan()
{
if((S4 == 0) || (S5 == 0) || (S6 == 0) || (S7 == 0))
{
delay(100);
if(S4 == 0) key_value = 4;
if(S5 == 0) key_value = 5;
if(S6 == 0) key_value = 6;
if(S7 == 0) key_value = 7;
while((S4 == 0) || (S5 == 0) || (S6 == 0) || (S7 == 0))
{
Display();
}
}
}
void Key_real()
{
if(key_value == 4)
{
input_v = input_v + 40;
}
if(key_value == 5)
{
input_v = input_v - 40;
}
key_value = 0;
}
void dac_pcf8591(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
void main()
{
HC138(5);
P0=0x00;
HC138(4);
P0= 0xff;
while(1)
{
key_scan();
Key_real();
dac_pcf8591(input_v);
real_v = input_v * 5 * 10 / 255;
Display();
}
}
iic.c
#include "reg52.h"
#include "intrins.h"
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();}
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
somenop;
SDA = 0;
somenop;
SCL = 0;
}
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
somenop;
SDA = 1;
}
void IIC_Ack(bit ackbit)
{
if(ackbit)
{
SDA = 0;
}
else
{
SDA = 1;
}
somenop;
SCL = 1;
somenop;
SCL = 0;
SDA = 1;
somenop;
}
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(byt&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
somenop;
SCL = 1;
byt <<= 1;
somenop;
SCL = 0;
}
}
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
somenop;
da <<= 1;
if(SDA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
iic.h
#ifndef _IIC_H
#define _IIC_H
void IIC_Start(void);
void IIC_Stop(void);
void IIC_Ack(bit ackbit);
void IIC_SendByte(unsigned char byt);
bit IIC_WaitAck(void);
unsigned char IIC_RecByte(void);
#endif
iic.c和iic.h文件为蓝桥杯单片机比赛官方所提供的例程
|