请在网上找试题题目 main.c
#include "mycode.h"
#include "key.h"
#include "iic.h"
#include "onewire.h"
unsigned char page_flag=1,key_value;
unsigned short int count=0,T_count=0;
bit T_flag=0;
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x20;
TH0 = 0xD1;
TF0 = 0;
TR0 = 1;
ET0 = 1;
}
void main()
{
SystemInit();
Timer0Init();
EA = 1;
while(1)
{
if(T_flag)Read_DS18B20_Tem();
if(page_flag==1) {HC_138(7); LED2=0;LED3=1;LED4=1;led_pattern(pattern);}
if(page_flag==2) {HC_138(7); LED2=1;LED3=0;LED4=1;led_pattern(pattern);}
if(page_flag==3) {HC_138(7); LED2=1;LED3=1;LED4=0;led_pattern(pattern);}
key_value=get_key();
switch(key_value)
{
case 9: key_value=0;pattern=!pattern;break;
case 10:key_value=0;if(page_flag==2) tem_refer++;break;
case 13:key_value=0;page_flag++;if(page_flag>=4) page_flag=1;break;
case 14:key_value=0;if(page_flag==2) tem_refer--;break;
}
}
}
void timer0() interrupt 1
{
count++;
T_count++;
if(count>=23)
{
EA=0;ET0 = 0;
switch(page_flag)
{
case 1:page_1();break;
case 2:page_2();break;
case 3:page_3();break;
}
count=0;
EA=1;ET0 = 1;
}
if(T_count>=500)
{
T_count=0;
T_flag=~T_flag;
}
}
mycode.c
#include "mycode.h"
#include "onewire.h"
#include "iic.h"
unsigned char code numb[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0xbf,0x7f,0xc6,0x8c,0x88
};
unsigned char pattern=0;
unsigned short int dac_out=0;
unsigned char tem_refer=25;
void page_1()
{
display(0,12);
display(4,temp/1000);
display(5,(temp/100)%10);
display(5,11);
display(6,temp/10%10);
display(7,temp%10);
HC_138(6); P0 = 0x00;
HC_138(7); P0 = 0xff;
}
void page_2()
{
display(0,13);
display(6,tem_refer/10);
display(7,tem_refer%10);
HC_138(6); P0 = 0x00;
HC_138(7); P0 = 0xff;
}
void page_3()
{
unsigned short int ds;
display(0,14);
if(pattern==0)
{
if((temp/100)<tem_refer)
{
Pcf8591_WriteDA(0);
display(5,0);
display(5,11);
display(6,0);
display(7,0);
}
else if((temp/100)>=tem_refer)
{
Pcf8591_WriteDA(255);
display(5,5);
display(5,11);
display(6,0);
display(7,0);
}
HC_138(6); P0 = 0x00;
HC_138(7); P0 = 0xff;
}
if(pattern==1)
{
if((temp/100)<=20)
{
Pcf8591_WriteDA(51);
display(5,1);
display(5,11);
display(6,0);
display(7,0);
}
else if((temp/100)<40)
{
ds=((float)(765.0*(temp/100)/100))-102;
Pcf8591_WriteDA(ds);
ds=(ds*100)/51;
display(5,ds/100);
display(5,11);
display(6,ds%100/10);
display(7,ds%10);
}
else if((temp/100)>=40)
{
Pcf8591_WriteDA(204);
display(5,4);
display(5,11);
display(6,0);
display(7,0);
}
HC_138(6); P0 = 0x00;
HC_138(7); P0 = 0xff;
}
}
void HC_138(unsigned char n)
{
switch(n)
{
case 4: P2&=0x1f;P2|=0x80;break;
case 5: P2&=0x1f;P2|=0xa0;break;
case 6: P2&=0x1f;P2|=0xc0;break;
case 7: P2&=0x1f;P2|=0xe0;break;
}
}
void display(unsigned char s,unsigned char n)
{
HC_138(7); P0 = 0xff;
HC_138(6); P0 = 1<<s;
HC_138(7); P0 = numb[n];
delay_us(300);
}
void led_pattern(unsigned char t)
{
if(t) {HC_138(4); LED1=1;}
if(!t) {HC_138(4); LED1=0;}
}
void SystemInit(void)
{
HC_138(7); P0=0xff;
HC_138(5); P0=0x00;
HC_138(4); P0 = 0xff;
}
void UartInit(void)
{
SCON = 0x50;
AUXR |= 0x40;
AUXR &= 0xFE;
TMOD &= 0x0F;
TL1 = 0xE6;
TH1 = 0xFF;
ET1 = 0;
TR1 = 1;
ES = 1;
}
void uart_byte(unsigned char c)
{
ES = 0;
SBUF=c;
while(!TI);
TI=0;
ES = 1;
}
char putchar(char c)
{
uart_byte(c);
return c;
}
void delay_us(unsigned int t)
{
while(t--){
_nop_();
_nop_();
_nop_();
_nop_();}
}
void delay_ms(unsigned int t)
{
unsigned char i, j;
while(t--){
i = 12;
j = 169;
do
{
while (--j);
} while (--i);}
}
mycode.h
#ifndef __MYCODE_H__
#define __MYCODE_H__
#include <reg52.h>
#include <stdio.h>
#include "absacc.h"
#include "intrins.h"
sfr AUXR = 0x8e;
#define LED 0xE000
#define UL 0xA000
sbit LED1=P0^0;
sbit LED2=P0^1;
sbit LED3=P0^2;
sbit LED4=P0^3;
extern unsigned short int dac_out;
extern unsigned char tem_refer;
extern unsigned char pattern;
void page_1();
void page_2();
void page_3();
void led_pattern(unsigned char t);
void HC_138(unsigned char n);
void delay_us(unsigned int t);
void delay_ms(unsigned int t) ;
void UartInit(void) ;
char putchar(char c);
void SystemInit(void);
void display(unsigned char s,unsigned char n);
#endif
iic.c
#include "reg52.h"
#include "intrins.h"
#include "iic.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit;
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
unsigned char read_pcf8591(unsigned char ch)
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(ch);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat = IIC_RecByte();
IIC_WaitAck();
IIC_Stop();
return dat;
}
void Pcf8591_WriteDA(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
void write_at24c02(unsigned char add,unsigned char datas)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(datas);
IIC_WaitAck();
IIC_Stop();
}
unsigned char read_at24c02(unsigned char add)
{
unsigned char datas;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
datas=IIC_RecByte();
IIC_WaitAck();
IIC_Stop();
return datas;
}
iic.h
#ifndef _IIC_H
#define _IIC_H
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
unsigned char read_pcf8591(unsigned char ch);
void Pcf8591_WriteDA(unsigned char dat);
void write_at24c02(unsigned char add,unsigned char datas);
unsigned char read_at24c02(unsigned char add);
#endif
key.c
#include "key.h"
#include "mycode.h"
#include <reg52.h>
sfr P4 = 0xC0;
sbit P42 = P4^2;
sbit P44 = P4^4;
unsigned char keyvalue;
unsigned char get_key()
{
unsigned char n,m;
P3=0xf0;P44=1;P42=1;
if(P3!=0xF0||P44!=1||P42!=1)
{
delay_ms(10);
if(P3!=0xF0||P44!=1||P42!=1)
{
P3=0xf0;P44=1;P42=1;
if(P44==0) n=1;
else if(P42==0) n=2;
else if((P3&0x20)==0) n=3;
else if((P3&0X10)==0) n=4;
P3=0x0F;P44=0;P42=0;
if((P3&0x01)==0) m=n;
else if((P3&0x02)==0) m=n+4;
else if((P3&0x04)==0) m=n+8;
else if((P3&0x08)==0) m=n+12;
while(P3!=0x0F);
}
return m;
}
return 0;
}
key.h
#ifndef __KEY_H__
#define __KEY_H__
unsigned char get_key();
#endif
onewire.c
#include "reg52.h"
#include "onewire.h"
#include "mycode.h"
sbit DQ = P1^4;
void Delay_OneWire(unsigned int t)
{
t*=10;
while(t--);
}
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
unsigned int temp;
void Read_DS18B20_Tem(void)
{
float tp;
unsigned char TH,TL;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(800);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
TL=Read_DS18B20();
TH=Read_DS18B20();
temp = TH;
temp = temp <<8;
temp |= TL;
if(temp < 0)
{
temp = ~temp;
temp = temp + 1;
tp = temp;
temp = tp*0.0625*1000+5;
temp = temp/10;
}
else
{
tp=temp;
temp = tp*0.0625*1000+5;
temp = temp/10;
}
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
unsigned char rd_temperature(void);
extern unsigned int temp;
void Read_DS18B20_Tem(void);
#endif
|