⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adc.c

📁 基于cc1000和avr128处理器的指定路由多跳网络的试验程序
💻 C
字号:
/*
****************************************************************************
*              宁波中科集成电路设计中心  版权所有 Copyright 2005
*						http:\\www.nbicc.com
*文件名:  adc.c
*程序员:  夏鹏		xpsonny@nbicc.com
*主要内容:adc驱动相关
*
*如有问题或BUG,请登录www.wsn.net.cn 提问或用邮件和作者联系
****************************************************************************
*/

#include "adc.h"
#include "os.h"
#include "radiocontrol.h"
#include "sensor.h"
static result_t ADCDataReady(uint8_t port, uint16_t value);	// 加入对应port的数据接收处理调用

static result_t ADCHPLDataReady(uint16_t data);
static result_t ADCStartGet(uint8_t newState, uint8_t port);
static void     ADCHPLInitPortmap(void);
static result_t ADCHPLInit(void);
static result_t ADCHPLSamplePort(uint8_t port);
static uint16_t ADCM_ReqPort;
static uint16_t ADCM_ReqVector;
static uint16_t ADCM_ContReqMask;
static bool HPLADCM_init_portmap_done;
static uint8_t adcPortmap[OSH_ADC_PORTMAPSIZE];


result_t ADCHPLInit(void)
{
	ADCHPLInitPortmap();
	
	{ uint8_t atomicState = AtomicStart();
    {
		* (volatile unsigned char *)(0x06 + 0x20) = (1 << 3) | (6 << 0);
		
		* (volatile unsigned char *)(0x07 + 0x20) = 0;
    }
    AtomicEnd(atomicState); }
	return SUCCESS;
}

result_t ADCControlInit(void)
{
	{ uint8_t atomicState = AtomicStart();
    {
		ADCM_ReqPort = 0;
		ADCM_ReqVector = ADCM_ContReqMask = 0;
    }
    AtomicEnd(atomicState); }
	
	return ADCHPLInit();
}

result_t ADCBindPort(uint8_t port, uint8_t adcPort)
{
	if (
		port < OSH_ADC_PORTMAPSIZE && 
		port != OS_ADC_BANDGAP_PORT && 
		port != OS_ADC_GND_PORT) {
		ADCHPLInitPortmap();
		{ uint8_t atomicState = AtomicStart();
        adcPortmap[port] = adcPort;
        AtomicEnd(atomicState); }
		return SUCCESS;
    }
	else {
		return FAIL;
    }
}

static result_t ADCDataReady(uint8_t port, uint16_t value){
	unsigned char result;
	
	switch (port) {
		
    case OS_ADC_CC_RSSI_PORT:
		result = RadiocontrolRSSIADCDataReady(value);
		break;
		
    case OS_ADC_PHOTO_PORT:
		result = SensorInternalPhotoADCDataReady(value);
		break;
		
    case OS_ADC_TEMP_PORT:
		result = SensorInternalTempADCDataReady(value);
		break;
		
    default:
		result = 0;
    }
	
	return result;
}

static result_t ADCHPLDataReady(uint16_t data)
{
	uint16_t doneValue = data;
	uint8_t donePort;
	result_t Result;
	
	
	{ uint8_t atomicState = AtomicStart();
    {
		donePort = ADCM_ReqPort;
		
		if (((1 << donePort) & ADCM_ContReqMask) == 0) {
			ADCM_ReqVector &= ~(1 << donePort);
        }
		
		if (ADCM_ReqVector) {
			do {
				ADCM_ReqPort++;
				ADCM_ReqPort = ADCM_ReqPort == OSH_ADC_PORTMAPSIZE ? 0 : ADCM_ReqPort;
            }
			while (((
				1 << ADCM_ReqPort) & ADCM_ReqVector) == 0);
			ADCHPLSamplePort(ADCM_ReqPort);
        }
    }
    AtomicEnd(atomicState); }
	
	
	Result = ADCDataReady(donePort, doneValue);
	
	{ uint8_t atomicState = AtomicStart();
    {
		if (Result == FAIL && ADCM_ContReqMask & (1 << donePort)) {
			ADCM_ContReqMask &= ~(1 << donePort);
        }
    }
    AtomicEnd(atomicState); }
	
	return SUCCESS;
}

static result_t ADCStartGet(uint8_t newState, uint8_t port)
{
	uint16_t PortMask;
	uint16_t oldReqVector;
	result_t Result = SUCCESS;
	
	if (port > OSH_ADC_PORTMAPSIZE) {
		return FAIL;
    }
	
	PortMask = 1 << port;
	
	{ uint8_t atomicState = AtomicStart();
    {
		if ((PortMask & ADCM_ReqVector) != 0) {
			
			Result = FAIL;
        }
		else {
			oldReqVector = ADCM_ReqVector;
			ADCM_ReqVector |= PortMask;
			if (newState == ADCM_CONTINUOUS_CONVERSION) {
				ADCM_ContReqMask |= PortMask;
            }
			if (oldReqVector == 0) {
				
				ADCM_ReqPort = port;
				Result = ADCHPLSamplePort(port);
            }
        }
    }
    AtomicEnd(atomicState); }
	
	
	return Result;
}

result_t ADCGetData(uint8_t port)
{
	return ADCStartGet(ADCM_SINGLE_CONVERSION, port);
}

void ADCHPLInitPortmap(void)
{
	
	{ uint8_t atomicState = AtomicStart();
    {
		if (HPLADCM_init_portmap_done == FALSE) {
			int i;
			
			for (i = 0; i < OSH_ADC_PORTMAPSIZE; i++) 
				adcPortmap[i] = i;
			
			
			adcPortmap[OS_ADC_BANDGAP_PORT] = OSH_ACTUAL_BANDGAP_PORT;
			adcPortmap[OS_ADC_GND_PORT] = OSH_ACTUAL_GND_PORT;
			HPLADCM_init_portmap_done = TRUE;
        }
    }
    AtomicEnd(atomicState); }
}

void __attribute((signal))   __vector_21(void)
{
	uint16_t data = * (volatile unsigned int *)(unsigned int )& * (volatile unsigned char *)(0x04 + 0x20);
	
	data &= 0x3ff;
	* (volatile unsigned char *)(0x06 + 0x20) |= 1 << 4;
	* (volatile unsigned char *)(0x06 + 0x20) &= ~(1 << 7);
	EnableInterrupt();
	ADCHPLDataReady(data);
}

static result_t ADCHPLSamplePort(uint8_t port)
{
	{ uint8_t atomicState = AtomicStart();
    {
		* (volatile unsigned char *)(0x07 + 0x20) = adcPortmap[port] & 0x1F;
    }
    AtomicEnd(atomicState); }
	* (volatile unsigned char *)(0x06 + 0x20) |= 1 << 7;
	* (volatile unsigned char *)(0x06 + 0x20) |= 1 << 6;
	
	return SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -