?? lna3.c
字號:
// buf=tempinit-Prmter1.TEMP;
// buff=-4;
// if(tempcheck==0)
// {
// tempinit=Prmter1.TEMP;
// tempcheck=1;
// }
// else if(tempcheck==1)
// {
// if(buf>4)
// {
// tempinit=Prmter1.TEMP;
// init_sign_ctl();
// }
// else if((buf<buff)&&(buf>0x80))
// {
// tempinit=Prmter1.TEMP;
// init_sign_ctl();
// }
// }
//
// return;
uchar TEMP_adc;
uchar buf;
uchar buff;
TEMP_adc=adc_read(2);
temp_cnt++;
tempbuf=tempbuf+TEMP_adc;
if(temp_cnt>=TEMP_CNT)
{
tempbuf=tempbuf/temp_cnt;//取平均值
Prmter1.TEMPU =tempbuf & 0x00ff;
temp_cnt = 0x00;
tempbuf= 0x0000;
Prmter1.TEMP= (100*Prmter1.TEMPU)/51-60;
buf=tempinit-Prmter1.TEMP;
buff=-4;
if(tempcheck==0)
{
tempinit=Prmter1.TEMP;
tempcheck=1;
init_sign_ctl();
}
else if(tempcheck==1)
{
if((buf>4)&&(buf<0x80))
{
tempinit=Prmter1.TEMP;
init_sign_ctl();
}
else if((buf<buff)&&(buf>0x80))
{
tempinit=Prmter1.TEMP;
init_sign_ctl();
}
}
}
return;
}
/**********************************************************
** 功能說明: 電流檢測
** 輸入?yún)?shù):
** 輸出參數(shù):
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
void CHECK_id()
{
uchar ID_adc;
//uchar buf;
ID_adc=adc_read(4);
//ID_adc=adc_read(4);
id_cnt++;
idbuf=idbuf+ID_adc;
if(id_cnt>=ID_CNT)
{
idbuf=idbuf/id_cnt;//取平均值
Prmter1.IDU =idbuf & 0x00ff;
id_cnt = 0x00;
idbuf= 0x0000;
Prmter1.ID1 =Prmter1.IDU;
if ((Prmter1.IDU>140)&&(Prmter1.IDU<225)) //2。8V~4。4V為正常
{
Prmter1.IDST=0;
}
else
{
Prmter1.IDST=1;
}
}
}
/**********************************************************
** 功能說明: 對sda端口進行賦值
** 輸入?yún)?shù): buf 類型:uchar :0或1
** 輸出參數(shù):
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
void sda(uchar buf)
{
if(buf)
{
portc_wbuf = portc_wbuf | 0x02;
PORTC = portc_wbuf;
}
else
{
portc_wbuf = portc_wbuf & 0xfd;
PORTC = portc_wbuf;
}
}
/**********************************************************
** 功能說明: 對scl端口進行賦值
** 輸入?yún)?shù): buf 類型:uchar :0或1
** 輸出參數(shù):
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
void scl(uchar buf)
{
if(buf)
{
portc_wbuf = portc_wbuf | 0x01;
PORTC = portc_wbuf;
}
else
{
portc_wbuf = portc_wbuf & 0xfe;
PORTC = portc_wbuf;
}
}
/**********************************************************
** 功能說明: 讀eeprom的值
** 輸入?yún)?shù): addr 類型:uint :讀數(shù)的地址
** 輸出參數(shù): buf 類型:uchar :讀出的數(shù)據(jù)
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
uchar rrom(uint addr)
{
uchar buf;
union buf_union comm;
dog();
T1CON=0x00;
RCIE=0;
CREN=0;
comm.buf1 = addr;
_nop();
iic_stop();
iic_star();
iic_send((comm.buf2[1] <<1)| 0xa0);
iic_ack();
iic_send(comm.buf2[0]);
iic_ack();
iic_star();
iic_send((comm.buf2[1] <<1)|0xa1);
iic_ack();
buf = iic_get();
iic_stop();
T1CON=0x31;
CREN=1;
RCIE=1;
return buf;
}
/**********************************************************
** 功能說明: 寫eeprom
** 輸入?yún)?shù): addr 類型:uchar :寫數(shù)據(jù)的地址
buf 類型:uchar :讀出的數(shù)據(jù)
** 輸出參數(shù):
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
void wrom(uint addr,uchar buf)
{
uchar ubuf;
uchar wcount;
union buf_union comm;
dog();
T1CON=0x00;
RCIE=0;
CREN=0;
wcount = 0;
comm.buf1 = addr;
iic_write_re:
if( wcount > 4 )
{
iic_stop();
_nop();
wcount = 0;
err_eeprom = 1;
T1CON=0x31;
CREN=1;
RCIE=1;
return ;
}
_nop();
iic_star();
iic_send((comm.buf2[1]<<1) | 0xa0);
ubuf = iic_ack();
if(ubuf)
{
wcount++;
_nop();
goto iic_write_re;
}
iic_send(comm.buf2[0]);
ubuf = iic_ack();
if(ubuf)
{
wcount++;
_nop();
goto iic_write_re;
}
iic_send(buf);
ubuf = iic_ack();
if(ubuf)
{
wcount++;
_nop();
goto iic_write_re;
}
iic_stop();
T1CON=0x31;
CREN=1;
RCIE=1;
return ;
}
/**********************************************************
** 功能說明: 延時函數(shù)
** 輸入?yún)?shù):
** 輸出參數(shù):
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
void iic_delay()
{
uchar a;
for(a = 0; a < IIC_DELAY ; a++ )
{
asm("NOP");
}
}
/**********************************************************
** 功能說明: 操作eeprom時發(fā)起始命令
** 輸入?yún)?shù):
** 輸出參數(shù):
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
void iic_star()
{
TRISC0 = 0;
TRISC1 = 0;
scl(1);
sda(1);
// _nop();
sda(0);
scl(0);
_nop();
return;
}
/**********************************************************
** 功能說明: 操作eeprom完成時發(fā)結束命令
** 輸入?yún)?shù):
** 輸出參數(shù):
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
void iic_stop()
{
TRISC0 = 0;
TRISC1 = 0;
sda(0);
scl(1);
_nop();
sda(1);
_nop();
scl(1);
_nop();
return;
}
/**********************************************************
** 功能說明: 發(fā)數(shù)據(jù)給eeprom
** 輸入?yún)?shù): buf 類型:uchar :需要發(fā)送的數(shù)據(jù)
** 輸出參數(shù):
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
void iic_send(uchar buf)
{
uchar i;
TRISC0 = 0;
TRISC1 = 0;
for(i = 0 ; i < 8 ;i++)
{
if( buf & 0x80 )
{
sda(1);
}
else
{
sda(0);
}
_nop();
scl(1);
//_nop();
scl(0);
//_nop();
buf = buf << 1;
}
return;
}
/**********************************************************
** 功能說明: 從eeprom獲取數(shù)據(jù)
** 輸入?yún)?shù):
** 輸出參數(shù): buf 類型:uchar :獲取的數(shù)據(jù)
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
uchar iic_get()
{
uchar i,buf;
buf = 0x00;
TRISC0 = 0;
TRISC1 = 1;
for(i = 0 ; i < 8 ;i++)
{
scl(1);
_nop();
buf = buf <<1;
if(PORTC & 0x02)
{
buf = buf|0x01;
}
scl(0);
_nop();
}
return buf;
}
/**********************************************************
** 功能說明: eeprom回應命令體
** 輸入?yún)?shù): buf 類型:uchar :0響應/1無響應
** 輸出參數(shù):
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
uchar iic_ack()
{
uchar buf;
TRISC1 = 1;
TRISC0 = 0;
sda(1);
_nop();
scl(1);
if(PORTC & 0x02)
buf = 1;
else
buf = 0;
scl(0);
_nop();
TRISC1 = 0;
_nop();
return buf;
}
/**********************************************************
** 功能說明: 收到數(shù)據(jù)發(fā)出響應
** 輸入?yún)?shù):
** 輸出參數(shù):
** 返 回 值: 無
** 引用函數(shù):
***********************************************************/
void ack_iic(uchar buf)
{
if(buf == 0)
{
sda(0);
}
else
{
sda(1);
}
_nop();
scl(1);
_nop();
scl(0);
_nop();
return;
}
/**********************************************************
** 功能說明: 485串口數(shù)據(jù)處理
** 輸入?yún)?shù): 無
** 輸出參數(shù):
** 返 回 值: 無
** 引用函數(shù):
*********************************************************/
void serial_data()
{
auto uchar start,length,buf,buf1;
// uchar mokuaiadd;
uchar bufver;//接收到的版本號
if(receive_over == 1)//如果接收完畢(包頭,包尾都收到)
{
length = receive_cnt - 2;
//if(Prmter1.TYPE != receive_buf[1]) //判斷協(xié)議類型
// if(TYPE != receive_buf[1]) //判斷協(xié)議類型
// {
// send_reply_flag(0x10,0x00);//協(xié)議類型錯誤
// goto go_return;
// }
if((Prmter1.ADDR != receive_buf[4]) && (receive_buf[4] != 0xff))//模塊地址錯(0xff為廣播群發(fā))0X00為沒有設置地址
{
goto go_return; //模塊地址不符合,沒返回
}
dog();
bufver=receive_buf[2] & 0x0f;//取緩沖數(shù)組的第二個字節(jié)的低4位字節(jié),高4位是信息來源
if(bufver!= PROTL)//協(xié)議版本號判斷
{
send_reply_flag(0x02,0x00);//0X02表示版本錯誤
goto go_return;
}
if(receive_buf[7] > 69) //命令體長度不能大于69
{
send_reply_flag(0x05,0x00);
goto go_return;
}
if(receive_buf[6]!= 0xff) //命令頭應答標志錯
{
send_reply_flag(0x06,0x00);
goto go_return;
}
length = receive_buf[7] + 7; //長度=命令體長度+……(不包括起始、結束、ASCII校驗這4個字節(jié))
start = 1;
CheckCRC(receive_buf,length,start,pChecksum);
buf1 = length + 1;
if((pChecksum[0] != receive_buf[buf1]) || (pChecksum[1] != receive_buf[buf1+1]))//CRC檢驗判斷
{
send_reply_flag(0x03,0x00);
goto go_return;
}
if(receive_buf[5] == 0x40)//查詢基本參數(shù)
{
buf = Query_Basic_Parameter();//模塊基本信息參數(shù)查詢
send_reply_flag(0x00,buf);//發(fā)送成功標志
goto go_return;
}
if(receive_buf[5] == 0x60)//設置基本信息項
{
buf = Set_Basic_Parameter();//模塊基本信息參數(shù)設置
if(erro&0x02)//設置出錯
{
receive_buf[6] = 0x07;
erro = erro & 0xfd;
}
else
{
receive_buf[6] = 0x00;
}
send_reply_flag(receive_buf[6],buf);
goto go_return;
}
if(receive_buf[5] == 0xc1)//設置參數(shù)
{
buf = Set_Parameter();//設置參數(shù)
if(erro&0x02)//設置出錯
{
receive_buf[6] = 0x07;
erro = erro & 0xfd;
}
else
{
receive_buf[6] = 0x00;
}
send_reply_flag(receive_buf[6],buf);
goto go_return;
}
else if(receive_buf[5] == 0xc2)//查詢參數(shù)
{
buf = Query_Parameter();
if(erro&0x01)//查詢出錯
{
receive_buf[6] = 0x08;
erro = erro & 0xfe;
}
else
{
receive_buf[6] = 0x00;
}
send_reply_flag(receive_buf[6],buf);
goto go_return;
}
else if(receive_buf[5] == 0x50)//查詢模塊參數(shù)地址
{
buf = Query_Address();
send_reply_flag(0x00,buf);
goto go_return;
}
else if(receive_buf[5]==0xd1)//查詢表格參數(shù)
{
if (receive_buf[8]==0x01)//查詢所有溫度值
{
buf=Query_tp(tempd,0x01);
}
else if (receive_buf[8]==0x02)//查詢所有頻率值
{
buf=Query_tp(freqd,0x02);
}
else if (receive_buf[8]==0x03)//查詢所有溫補參數(shù)編碼
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -