ADC
SAMR21有12-bit 300kbps ADC。因为数据要从串口输出。首先建立了一个串口的Example工程,然后在ASF向导里面添加的ADC的功能。
本实验中连接的是PA06端口,对应的ADC_PIN6
图6-1:ADC端口
图6-2:采样结果
可以看出当ADC连接vcc的时候,采样值只有4085左右,没有达到4095。
当连接GND的时候是0005左右。
当悬空的时候,采样值就飘了。
红外热释传感器,通过菲涅耳透镜,来更加精确的检测有没有热源靠近。
开始的时候我以为输出的值可以用ADC读取,和热源的远近有关系呢,后来发现我错了,只是输出高低电平而已。不过也可以用ADC来读取。
常见的自动门,一般都是采用这种传感器。
图6-3:红外热释
MQ-2烟雾传感器:
图6-4:MQ-2
应用:可以用于家庭和工厂的气体泄漏监测装置,适宜于液化气、丁烷、丙烷、甲烷、烟雾等的探测;
电气性能:
输入电压:DC5V 功耗(电流):150mA DO
输出:TTL数字量0和1(0.1和5V)
AO输出:0.1-0.3V(相对无污染),最高浓度电压4V左右
传感器通电后,需要预热20S左右,测量的数据才稳定,传感器发热属于正常现象,因为内部有电热丝,如果烫手就不正常了。
接线方式:
1、VCC:接电源正极(5V)
2、GND:接电源负极
3、DO:TTL开关信号输出
4、AO:模拟信号输出
主要代码如下:
#include
#include
#include "conf_test.h"
/* Teststring to send */
#define TEST_STRING "Helloworld!"
/* Lengthof test string */
#define TEST_STRING_LEN 13
/* Speed totest USART at */
#define TEST_USART_SPEED 115200
/*Structure for UART module connected to EDBG (used for unit test output) */
struct usart_module cdc_uart_module;
struct usart_module usart_rx_module;
struct usart_config usart_rx_config;
struct usart_module usart_tx_module;
struct usart_config usart_tx_config;
volatile bool transfer_complete;
//![result_buffer]
#define ADC_SAMPLES 128
uint16_t adc_result_buffer[ADC_SAMPLES];
//![result_buffer]
//![module_inst]
struct adc_module adc_instance;
//![module_inst]
//![job_complete_callback]
volatile bool adc_read_done = false;
void configure_adc(void);
void configure_adc_callbacks(void);
void adc_complete_callback(
const struct adc_module *const module);
void adc_complete_callback(
const struct adc_module *const module)
{
adc_read_done = true;
}
//![job_complete_callback]
//! [setup]
void configure_adc(void)
{
//![setup_config]
struct adc_config config_adc;
//![setup_config]
//![setup_config_defaults]
adc_get_config_defaults(&config_adc);
//![setup_config_defaults]
//![setup_modify_conf]
config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2;
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV8;
config_adc.reference = ADC_REFERENCE_INTVCC1;
config_adc.positive_input = ADC_POSITIVE_INPUT_PIN6;
config_adc.resolution = ADC_RESOLUTION_12BIT;
//![setup_modify_conf]
//![setup_set_config]
adc_init(&adc_instance, ADC, &config_adc);
//![setup_set_config]
//![setup_enable]
adc_enable(&adc_instance);
//![setup_enable]
}
void configure_adc_callbacks(void)
{
//![setup_register_callback]
adc_register_callback(&adc_instance,
adc_complete_callback, ADC_CALLBACK_READ_BUFFER);
//![setup_register_callback]
//![setup_enable_callback]
adc_enable_callback(&adc_instance, ADC_CALLBACK_READ_BUFFER);
//![setup_enable_callback]
}
/**
* \internal
* \brief USART interrupt callback function
*
* Called by USART driver when receiving iscomplete.
*
* * \param module USART module causing theinterrupt (not used)
*/
static void usart_callback(const struct usart_module *const module)
{
transfer_complete = true;
}
/**
* \brief Initialize the USART for unit test
*
* Initializes the SERCOM USART used forsending the unit test status to the
* computer via the EDBG CDC gateway.
*/
static void cdc_uart_init(void)
{
struct usart_config usart_conf;
/* ConfigureUSART for unit test output */
usart_get_config_defaults(&usart_conf);
usart_conf.mux_setting = CONF_STDIO_MUX_SETTING;
usart_conf.pinmux_pad0 = CONF_STDIO_PINMUX_PAD0;
usart_conf.pinmux_pad1 = CONF_STDIO_PINMUX_PAD1;
usart_conf.pinmux_pad2 = CONF_STDIO_PINMUX_PAD2;
usart_conf.pinmux_pad3 = CONF_STDIO_PINMUX_PAD3;
usart_conf.baudrate = CONF_STDIO_BAUDRATE;
stdio_serial_init(&cdc_uart_module, CONF_STDIO_USART, &usart_conf);
usart_enable(&cdc_uart_module);
}
/**
* \internal
* \brief Test single 8-bit character send andreceive.
*
* This test sends on 9-bit character andchecks it's received properly
* on the other end.
*
* \param test Current test case.
*/
static void run_transfer_single_8bit_char_test(const struct test_case *test)
{
uint16_t tx_char = 0x53;
volatile uint16_t rx_char = 0;
/* Write and readthe data */
usart_write_wait(&usart_tx_module, tx_char);
usart_read_wait(&usart_rx_module, (uint16_t*)&rx_char);
/* Make sure wereceived what we sent */
test_assert_true(test, tx_char==rx_char,
"Failed receiving sentbyte. TX=%d, RX=%d", tx_char, rx_char);
}
static void test_system_init(void)
{
/* Configure RXUSART */
usart_get_config_defaults(&usart_rx_config);
usart_rx_config.mux_setting = CONF_RX_USART_SERCOM_MUX;
usart_rx_config.pinmux_pad0 = CONF_RX_USART_PINMUX_PAD0;
usart_rx_config.pinmux_pad1 = CONF_RX_USART_PINMUX_PAD1;
usart_rx_config.pinmux_pad2 = CONF_RX_USART_PINMUX_PAD2;
usart_rx_config.pinmux_pad3 = CONF_RX_USART_PINMUX_PAD3;
usart_rx_config.baudrate = TEST_USART_SPEED;
/* Applyconfiguration */
usart_init(&usart_rx_module, CONF_RX_USART, &usart_rx_config);
/* Enable USART*/
usart_enable(&usart_rx_module);
/* Configure TXUSART */
usart_get_config_defaults(&usart_tx_config);
usart_tx_config.mux_setting = CONF_TX_USART_SERCOM_MUX;
usart_tx_config.pinmux_pad0 = CONF_TX_USART_PINMUX_PAD0;
usart_tx_config.pinmux_pad1 = CONF_TX_USART_PINMUX_PAD1;
usart_tx_config.pinmux_pad2 = CONF_TX_USART_PINMUX_PAD2;
usart_tx_config.pinmux_pad3 = CONF_TX_USART_PINMUX_PAD3;
usart_tx_config.baudrate = TEST_USART_SPEED;
/* Applyconfiguration */
usart_init(&usart_tx_module, CONF_TX_USART, &usart_tx_config);
/* Enable USART*/
usart_enable(&usart_tx_module);
}
/**
* \brief Run USART unit tests
*
* Initializes the system and serial output,then sets up the
* USART unit test suite and runs it.
*/
int main(void)
{
char temp[10];
system_init();
cdc_uart_init();
test_system_init();
usart_write_buffer_job(&usart_tx_module,"HelloWii\n",9);
//! [setup_init]
configure_adc();
configure_adc_callbacks();
//! [setup_init]
//! [main]
//![enable_global_interrupts]
system_interrupt_enable_global();
//![enable_global_interrupts]
//while (1)
{
adc_read_buffer_job(&adc_instance, adc_result_buffer, ADC_SAMPLES);
//![start_adc_job]
//![job_complete_poll]
while (adc_read_done == false) {
/* Wait for asynchronous ADC read to complete */
}
temp[0]=adc_result_buffer[0]/1000+'0';
temp[1]=(adc_result_buffer[0]%1000)/100+'0';
temp[2]=(adc_result_buffer[0]%100)/10+'0';
temp[3]=adc_result_buffer[0]%10+'0';
temp[4]='\n';
temp[5]='\0';
usart_write_buffer_job(&usart_tx_module,temp,ADC_SAMPLES);
}
while (true) {
/*Intentionally left empty */
}
}