📄 ad1.c
字号:
/*""FILE COMMENT""****************************************************
*System Name : CAN DEMO PROGRAM1
*File Name : ad1.c
*Version : 1.00
*Contens :
*Customer : RSO
*Model :
*Order :
*CPU :
*Compiler : NC30 Version 5.30 Release 02
*OS :
*Programmer :
*Note :
* :
**********************************************************************
* Copyright(C)2005, Renesas Technology Corp.
* Copyright(C)2005, Renesas Solutions Corp.
* All rights reserved.
**********************************************************************
*History 2005.07.1 Ver 1.00
*""FILE COMMENT END""************************************************/
#include "sfr29.h"
#include "define1.h"
#include "declare1.h"
/*""FUNC COMMENT""****************************************************
*ID :3.0
*Description :Initialize A/D converter
*--------------------------------------------------------------------
*Include :"sfr29.h"
* :"declare1.h"
*--------------------------------------------------------------------
*Declaration :void ad_initial(void)
*--------------------------------------------------------------------
*Function :Initialize A/D converter
*--------------------------------------------------------------------
*Arguments :Nothing
*--------------------------------------------------------------------
*Returns :Nothing
*--------------------------------------------------------------------
*Input :Nothing
*Ountput :Nothing
*--------------------------------------------------------------------
*Call functions :Nothing
*--------------------------------------------------------------------
*Note :
*--------------------------------------------------------------------
*History :
* :
*""FUNC COMMENT END""***********************************************/
void ad_initial(void)
{
unsigned short i;
/* AD initial */
adcon0 = 0x08; //fAD/4 (5MHz)
adcon1 = 0x28; //
adcon2 = 0x01; //
for(i = 0; i < 1000/50; i++); //wait Vref(1us)
adst = 1; //start AD
for(i = 0; i < 1000/50; i++); //wait first AD conversion
}
/*""FUNC COMMENT""****************************************************
*ID :3.1
*Description :A/D converter function
*--------------------------------------------------------------------
*Include :"sfr29.h"
* :"declare1.h"
*--------------------------------------------------------------------
*Declaration :void ad_fnc(void)
*--------------------------------------------------------------------
*Function :A/D converter function
*--------------------------------------------------------------------
*Arguments :Nothing
*--------------------------------------------------------------------
*Returns :Nothing
*--------------------------------------------------------------------
*Input :Nothing
*Ountput :Nothing
*--------------------------------------------------------------------
*Call functions :ad_read() ;Read A/D conversion value
* :ad_check() ;Check A/D conversion value
*--------------------------------------------------------------------
*Note :
*--------------------------------------------------------------------
*History :
* :
*""FUNC COMMENT END""***********************************************/
void ad_fnc(void)
{
if(ad_read() != 0){
if(ad_check() != 0){
trm_can_ad();
}
}
}
/*""FUNC COMMENT""****************************************************
*ID :3.2
*Description :Read A/D conversion value
*--------------------------------------------------------------------
*Include :"sfr29.h"
* :"define1.h"
* :"declare1.h"
*--------------------------------------------------------------------
*Declaration :void ad_fnc(void)
*--------------------------------------------------------------------
*Function :Read A/D conversion value
*--------------------------------------------------------------------
*Arguments :Nothing
*--------------------------------------------------------------------
*Returns : Renewal code
* : 0; Not renew "ave_ad_data"
* : 1; Renew "ave_ad_data"
*--------------------------------------------------------------------
*Input :unsigned char count_ad ;A/D counter
* :unsigned char ad_data[] ;A/D conversion data
*Ountput :unsigned char count_ad ;A/D counter
* :unsigned short ave_ad_data ;Average of A/D conversion data
* :unsigned char ad_data[] ;A/D conversion data
*--------------------------------------------------------------------
*Call functions :Nothing
*--------------------------------------------------------------------
*Note :
*--------------------------------------------------------------------
*History :
* :
*""FUNC COMMENT END""***********************************************/
unsigned char ad_read(void)
{
unsigned long in_ad_sum;
unsigned short i;
ad_data[count_ad] = ad0;
count_ad++;
if(count_ad >= AD_COUNT){ //Calculate A/D average
in_ad_sum = 0;
for(i = 0; i < (sizeof(ad_data) / 2); i++){
in_ad_sum += ad_data[i];
}
ave_ad_data = in_ad_sum / (sizeof(ad_data) / 2);
count_ad = 0;
return 1;
}
return 0;
}
/*""FUNC COMMENT""****************************************************
*ID :3.3
*Description :Check A/D conversion value
*--------------------------------------------------------------------
*Include :"sfr29.h"
* :"define1.h"
* :"declare1.h"
*--------------------------------------------------------------------
*Declaration :unsigned char ad_check(void)
*--------------------------------------------------------------------
*Function :Check whether A/D conversion value differ from
* last A/D conversion value
*--------------------------------------------------------------------
*Arguments :Nothing
*--------------------------------------------------------------------
*Returns : Difference code
* : 0; No difference
* : 1; Differing
*--------------------------------------------------------------------
*Input :unsigned char last_ad_data ;Last A/D countesion data
* :unsigned char ave_ad_data ;Average of A/D conversion data
*Ountput :unsigned char last_ad_data ;Last A/D countesion data
*--------------------------------------------------------------------
*Call functions :Nothing
*--------------------------------------------------------------------
*Note :
*--------------------------------------------------------------------
*History :
* :
*""FUNC COMMENT END""***********************************************/
unsigned char ad_check(void)
{
union {
unsigned short word;
unsigned char byte;
}in_ad_data,in_last_ad_data;
in_last_ad_data.byte = last_ad_data;
in_last_ad_data.word = in_last_ad_data.word << 2;
in_ad_data.word = ave_ad_data;
if(in_last_ad_data.word > in_ad_data.word){
if((in_last_ad_data.word - in_ad_data.word) > 0x0005){
in_ad_data.word = (in_ad_data.word + 0x0004) >> 2;
last_ad_data = in_ad_data.byte;
return 1;
}
else if((in_last_ad_data.word == 0x0004) &&
(in_ad_data.word <= 0x0001)){
// in_ad_data.word = in_ad_data.word >> 2;
// last_ad_data = in_ad_data.byte;
last_ad_data = 0x00;
return 1;
}
}
else{
if((in_ad_data.word - in_last_ad_data.word) > 0x0005){
in_ad_data.word = in_ad_data.word >> 2;
last_ad_data = in_ad_data.byte;
return 1;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -