CT107D ADC 光敏电阻
#include<iic.h> #include "reg52.h" #include "intrins.h" typedef unsigned char BYTE; typedef unsigned int WORD; #define FOSC 11059200L //系统频率 #define BAUD 115200 //串口波特率 #define NONE_PARITY 0 //无校验 #define ODD_PARITY 1 //奇校验 #define EVEN_PARITY 2 //偶校验 #define MARK_PARITY 3 //标记校验 #define SPACE_PARITY 4 //空白校验 #define PARITYBIT NONE_PARITY //定义校验位 sfr P0M1 = 0x93; sfr P0M0 = 0x94; sfr P1M1 = 0x91; sfr P1M0 = 0x92; sfr P2M1 = 0x95; sfr P2M0 = 0x96; sfr P3M1 = 0xb1; sfr P3M0 = 0xb2; sfr P4M1 = 0xb3; sfr P4M0 = 0xb4; sfr P5M1 = 0xC9; sfr P5M0 = 0xCA; sfr P6M1 = 0xCB; sfr P6M0 = 0xCC; sfr P7M1 = 0xE1; sfr P7M0 = 0xE2; sfr AUXR = 0x8e; //辅助寄存器 sfr P_SW1 = 0xA2; //外设功能切换寄存器1 #define S1_S0 0x40 //P_SW1.6 #define S1_S1 0x80 //P_SW1.7 sbit P22 = P2^2; bit busy; void SendData(BYTE dat); void SendString(char *s); void main() { P2=0xA0; P0=0x00; P2=0x00; P2=0x80; P0=0xFF; P2=0x00; P0M0 = 0x00; P0M1 = 0x00; P1M0 = 0x00; P1M1 = 0x00; P2M0 = 0x00; P2M1 = 0x00; P3M0 = 0x00; P3M1 = 0x00; P4M0 = 0x00; P4M1 = 0x00; P5M0 = 0x00; P5M1 = 0x00; P6M0 = 0x00; P6M1 = 0x00; P7M0 = 0x00; P7M1 = 0x00; ACC = P_SW1; ACC &= ~(S1_S0 | S1_S1); //S1_S0=0 S1_S1=0 P_SW1 = ACC; //(P3.0/RxD, P3.1/TxD) // ACC = P_SW1; // ACC &= ~(S1_S0 | S1_S1); //S1_S0=1 S1_S1=0 // ACC |= S1_S0; //(P3.6/RxD_2, P3.7/TxD_2) // P_SW1 = ACC; // // ACC = P_SW1; // ACC &= ~(S1_S0 | S1_S1); //S1_S0=0 S1_S1=1 // ACC |= S1_S1; //(P1.6/RxD_3, P1.7/TxD_3) // P_SW1 = ACC; #if (PARITYBIT == NONE_PARITY) SCON = 0x50; //8位可变波特率 #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY) SCON = 0xda; //9位可变波特率,校验位初始为1 #elif (PARITYBIT == SPACE_PARITY) SCON = 0xd2; //9位可变波特率,校验位初始为0 #endif AUXR = 0x40; //定时器1为1T模式 TMOD = 0x00; //定时器1为模式0(16位自动重载) TL1 = (65536 - (FOSC/4/BAUD)); //设置波特率重装值 TH1 = (65536 - (FOSC/4/BAUD))>>8; TR1 = 1; //定时器1开始启动 ES = 1; //使能串口中断 EA = 1; adc_init(); while(1) { SendData(adc_get()); } } /*---------------------------- UART 中断服务程序 -----------------------------*/ void Uart() interrupt 4 { if (RI) { RI = 0; //清除RI位 P0 = SBUF; //P0显示串口数据 P22 = RB8; //P2.2显示校验位 } if (TI) { TI = 0; //清除TI位 busy = 0; //清忙标志 } } /*---------------------------- 发送串口数据 ----------------------------*/ void SendData(BYTE dat) { while (busy); //等待前面的数据发送完成 ACC = dat; //获取校验位P (PSW.0) if (P) //根据P来设置校验位 { #if (PARITYBIT == ODD_PARITY) TB8 = 0; //设置校验位为0 #elif (PARITYBIT == EVEN_PARITY) TB8 = 1; //设置校验位为1 #endif } else { #if (PARITYBIT == ODD_PARITY) TB8 = 1; //设置校验位为1 #elif (PARITYBIT == EVEN_PARITY) TB8 = 0; //设置校验位为0 #endif } busy = 1; SBUF = ACC; //写数据到UART数据寄存器 } /*---------------------------- 发送字符串 ----------------------------*/ void SendString(char *s) { while (*s) //检测字符串结束标志 { SendData(*s++); //发送当前字符 } }
iic.c
/* 程序说明: IIC总线驱动程序 软件环境: Keil uVision 4.10 硬件环境: CT107单片机综合实训平台 8051,12MHz 日 期: 2011-8-9 */ #include "reg52.h" #include "intrins.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; // 0:应答,1:非应答 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; } //通过I2C总线发送数据 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; } //从I2C总线上接收数据 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; } void adc_init() { IIC_Start(); IIC_SendByte(0x90);//0x90写入 0x91读取 IIC_WaitAck(); IIC_SendByte(0x01);//0x01光敏电阻 0x03电位器 0x40 DAC IIC_WaitAck(); IIC_Stop(); } unsigned char adc_get() { unsigned char v; IIC_Start(); IIC_SendByte(0x91); IIC_WaitAck(); v=IIC_RecByte(); IIC_SendAck(0); IIC_Stop(); return(v); }
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); void adc_init(); unsigned char adc_get(); #endif