?? 51
字號(hào):
//download by http://www.codefans.net
// 硬件配置
//晶震: 12M
//功能:AD 采樣輸入 頻率輸入
//AD采樣輸入:
//頻率輸入:
//創(chuàng)建:
//#include <reg51.h> //51頭文件庫(kù)
//#include <reg52.h>
#include <AT89X52.H>
#include <intrins.h> //
sbit SCL=P1^2; // 將p1.2口模擬時(shí)鐘口
sbit SDA=P1^3; // 將p1.3口模擬數(shù)據(jù)口
sbit POUT = P3^1;
sbit PIN =P2^3;
bit askflag;
#define uchar unsigned char
#define uint unsigned int
#define disdata P0 //顯示數(shù)據(jù)碼輸出口
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
sbit P14 = P1^5;
uint data dis[5]={0x00,0x00,0x00,0x00};
//定義3個(gè)顯示數(shù)據(jù)單元和1個(gè)數(shù)據(jù)存儲(chǔ)單元
//uchar code dis4[] = {"1- . V 2- . V"};
uchar code dis4[] = {"voltage: . V"};
uchar code dis5[] = {"frequency: "};
//uchar code dis5[] = {"3- . V 4- . V"};
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();};
bit bdata SystemError; //從機(jī)錯(cuò)誤標(biāo)志位
/*********************** PCF8591專用變量定義 ***********************/
#define PCF8591_WRITE 0x90
#define PCF8591_READ 0x91
#define NUM 4 //接收和發(fā)送緩存區(qū)的深度
uchar idata receivebuf[NUM]; //數(shù)據(jù)接收緩沖區(qū)
unsigned int freq; //頻率值
unsigned char freq2;
unsigned long x;
unsigned int sec_bit;
unsigned char ov_flage; //計(jì)數(shù)溢出標(biāo)志位
void lcd_flash(void);
/*******************************************************************/
/* */
/* 延時(shí)函數(shù) */
/* */
/*******************************************************************/
void delay(int ms)
{
while(ms--)
{
uchar i;
for(i=0;i<250;i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
/*******************************************************************/
/* */
/*檢查L(zhǎng)CD忙狀態(tài) */
/*lcd_busy為1時(shí),忙,等待。lcd-busy為0時(shí),閑,可寫指令與數(shù)據(jù)。 */
/* */
/*******************************************************************/
bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delayNOP();
result = (bit)(P0&0x80);
LCD_EN = 0;
return(result);
}
/*******************************************************************/
/* */
/*寫指令數(shù)據(jù)到LCD */
/*RS=L,RW=L,E=高脈沖,D0-D7=指令碼。 */
/* */
/*******************************************************************/
void lcd_wcmd(uchar cmd)
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
_nop_();
_nop_();
_nop_();
_nop_();
P0 = cmd;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
/*******************************************************************/
/* */
/*寫顯示數(shù)據(jù)到LCD */
/*RS=H,RW=L,E=高脈沖,D0-D7=數(shù)據(jù)。 */
/* */
/*******************************************************************/
void lcd_wdat(uchar dat)
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P0 = dat;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
/*******************************************************************/
/* */
/* LCD初始化設(shè)定 */
/* */
/*******************************************************************/
void lcd_init()
{
delay(15);
lcd_wcmd(0x38); //16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x0c); //顯示開,關(guān)光標(biāo)
delay(5);
lcd_wcmd(0x06); //移動(dòng)光標(biāo)
delay(5);
lcd_wcmd(0x01); //清除LCD的顯示內(nèi)容
delay(5);
}
/*******************************************************************/
/* */
/* 設(shè)定顯示位置 */
/* */
/*******************************************************************/
void lcd_pos(uchar pos)
{
lcd_wcmd(pos | 0x80); //數(shù)據(jù)指針=80+地址變量
}
/******************************************************************/
/* */
/* 數(shù)據(jù)處理與顯示 */
/* 將采集到的數(shù)據(jù)進(jìn)行16進(jìn)制轉(zhuǎn)換為ASCLL碼。 */
/* */
/******************************************************************/
show_value(uchar ad_data)
{
dis[2]=ad_data/51; //AD值轉(zhuǎn)換為3為BCD碼,最大為5.00V。
dis[2]=dis[2]+0x30; //轉(zhuǎn)換為ACSII碼
dis[3]=ad_data%51; //余數(shù)暫存
dis[3]=dis[3]*10; //計(jì)算小數(shù)第一位
dis[1]=dis[3]/51;
dis[1]=dis[1]+0x30; //轉(zhuǎn)換為ACSII碼
dis[3]=dis[3]%51;
dis[3]=dis[3]*10; //計(jì)算小數(shù)第二位
dis[0]=dis[3]/51; //
dis[0]=dis[0]+0x30; //轉(zhuǎn)換為ACSII碼
}
//-------------------------------------------------------------------
// 函數(shù)名稱: iic_start()
// 函數(shù)功能: 啟動(dòng)I2C總線子程序
//-------------------------------------------------------------------
void iic_start(void)
{ //時(shí)鐘保持高,數(shù)據(jù)線從高到低一次跳變,I2C通信開始
SDA = 1;
SCL = 1;
delayNOP(); // 延時(shí)5us
SDA = 0;
delayNOP();
SCL = 0;
}
//-------------------------------------------------------------------
// 函數(shù)名稱: iic_stop()
// 函數(shù)功能: 停止I2C總線數(shù)據(jù)傳送子程序
//-------------------------------------------------------------------
void iic_stop(void)
{
SDA = 0; //時(shí)鐘保持高,數(shù)據(jù)線從低到高一次跳變,I2C通信停止
SCL = 1;
delayNOP();
SDA = 1;
delayNOP();
SCL = 0;
}
//------------------------------------------------------------------
// 函數(shù)名稱: iicInit_()
// 函數(shù)功能: 初始化I2C總線子程序
//------------------------------------------------------------------
void iicInit(void)
{
SCL = 0;
iic_stop();
}
//-------------------------------------------------------------------
// 函數(shù)名稱: slave_ACK
// 函數(shù)功能: 從機(jī)發(fā)送應(yīng)答位子程序
//-------------------------------------------------------------------
void slave_ACK(void)
{
SDA = 0;
SCL = 1;
delayNOP();
SCL = 0;
}
//-------------------------------------------------------------------
// 函數(shù)名稱: slave_NOACK
// 函數(shù)功能: 從機(jī)發(fā)送非應(yīng)答位子程序,迫使數(shù)據(jù)傳輸過程結(jié)束
//-------------------------------------------------------------------
void slave_NOACK(void)
{
SDA = 1;
SCL = 1;
delayNOP();
SDA = 0;
SCL = 0;
}
//-------------------------------------------------------------------
// 函數(shù)名稱: check_ACK
// 函數(shù)功能: 主機(jī)應(yīng)答位檢查子程序,迫使數(shù)據(jù)傳輸過程結(jié)束
//-------------------------------------------------------------------
void check_ACK(void)
{
SDA = 1; // 將p1.1設(shè)置成輸入,必須先向端口寫1
SCL = 1;
askflag = 0;
delayNOP();
if(SDA == 1) // 若SDA=1表明非應(yīng)答,置位非應(yīng)答標(biāo)志askflag
askflag = 1;
SCL = 0;
}
//-------------------------------------------------------------------
// 函數(shù)名稱: IICSendByte
// 入口參數(shù): ch
// 函數(shù)功能: 發(fā)送一個(gè)字節(jié)
//-------------------------------------------------------------------
void IICSendByte(uchar ch)
{
unsigned char idata n=8; // 向SDA上發(fā)送一位數(shù)據(jù)字節(jié),共八位
while(n--)
{
if((ch&0x80) == 0x80) // 若要發(fā)送的數(shù)據(jù)最高位為1則發(fā)送位1
{
SDA = 1; // 傳送位1
SCL = 1;
delayNOP();
// SDA = 0;
SCL = 0;
}
else
{
SDA = 0; // 否則傳送位0
SCL = 1;
delayNOP();
SCL = 0;
}
ch = ch<<1; // 數(shù)據(jù)左移一位
}
}
//-------------------------------------------------------------------
// 函數(shù)名稱: IICreceiveByte
// 返回接收的數(shù)據(jù)
// 函數(shù)功能: 接收一字節(jié)子程序
//-------------------------------------------------------------------
uchar IICreceiveByte(void)
{
uchar idata n=8; // 從SDA線上讀取一上數(shù)據(jù)字節(jié),共八位
uchar tdata=0;
while(n--)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -