First Look at Zigbee
To add a smoke sensor module to the Zigbee network, you need to use the CC2530 ADC to sample the analog signal output by the sensor. The following is my own program process for using the CC2530 ADC to collect external voltage.
The following is the configuration of the ADC:
6; ADC_ENABLE_CHANNEL(6); //Enable channel 6 as the sampling channel of ADC ADC_SINGLE_CONVERSION(ADC_REF_AVDD|ADC_12_BIT|ADC_INIT_SINGLE_CONVERSION);
//
This
function
is
order
to initialize ADC of CC2530 ADC_SINGLE_CONVERSION(ADC_REF_AVDD|ADC_12_BIT|ADC_INIT_SINGLE_CONVERSION);
// This
function is order to initialize ADC of CC2530
ADC_SINGLE_CONVERSION(ADC_REF_AVDD|ADC_12_BIT|ADC_INIT_SINGLE_CONVERSION); //This function
is order to initialize ADC of CC2530 ADC_SINGLE_CONVERSION(
ADC_REF_AVDD|ADC_12_BIT |ADC_INIT_SINGLE_CONVERSION); //This function is order to initialize ADC of CC2530
ADC_SINGLE_CONVERSION(
ADC_REF_AVDD| ADC_12_BIT|ADC_INIT_SINGLE_CONVERSION)
; //
This function is order to initialize ADC of CC2530 ADC_SINGLE_CONVERSION(ADC_REF_AVDD|ADC_12_BIT|ADC_INIT_SINGLE_CONVERSION
); //This function is order to initialize ADC of CC2530 ADC_SINGLE_CONVERSION
(ADC_REF_AVDD|ADC_12_BIT|ADC_INIT_SINGLE_CONVERSION ); //This function is order to initialize ADC of CC2530 ADC_AIN6); //Configure ADC parameters, reference voltage is AVDD5 pin voltage, decimation rate is 512 (12-bit resolution)
ADC_SAMPLE_SINGLE(); //Start single sampling
while(!(ADCCON1&0x80)) ;//Wait for AD conversion to complete
// while ( !ADCIF );
/*Get the result and convert it to voltage*/
ADCREGValue = ADCL>>4;//The program sets the precision to 12 bits, take the lower 4 bits
ADCREGValue |= ADCH<<4; //High 8 bits
ADCValue = (float)(ADCREGValue/(float)2048)*3.3;//There is a question here, originally the precision is 12 bits, the divisor should be 4096, but 2048 is required to get the accurate value
}
The following is the configuration of UART0:
#include<iocc2530.h>
#include"uart.h"
void UARTInit(void)
{
PERCFG = 0; //Configure the IO location of UART0 as spare location 1
P0SEL = 0x3c; //P0.2-PO.5 are set as the port of peripheral function
P2DIR &= ~(3<<6);//Set UART0 to the first priority and UART1 to the second priority,
U0CSR |= (1<<7); //select the mode as UART mode
U0GCR |= 0x09;
U0BAUD |= 59; //19200
UTX0IF = 1; //clear the interrupt flag
U0CSR |= (1<<6); //enable receive bit IEN0 |= 0x84; //enable receive bit
IEN1 |= 0x84;
}
/*************************************************************
note: "length" is the length of one line
**********************************************************/
void UARTSend(char *data,int length)
{
int i;
for(i=0;i<length;i++)
{
U0DBUF = *data;
data++;
while(UTX0IF==0); // complete receive
UTX0IF = 0; //clear the flag
}
U0DBUF =0x0A; //carriage return
while(UTX0IF==0); // complete receive
UTX0IF = 0;
}
The following is the main function:
#include<iocc2530.h>
#include"adc.h"
#include"uart.h"
#include"led.h"
#include<stdio.h>
#include <string.h>
void delay(uint n);
void ClockInit(void);
void main(void)
{
char i = 0;
char TempValue[10];
float average ;
char len;
P1_0 = 0;
ClockInit();
UARTInit();
SET_IO_PORT_DIR(1,0,IO_OUT); //Set LED as a flag for ADC sampling
IO_FUNC_PORT_PIN(1, 0, IO_FUNC_GIO);//INIT_LED();
IEN0 =IEN1=IEN2 =0;
while(1)
{
average = 0.0;
for(i=0;i<64;i++)//Take 64 averages
{
ADInit();
average +=ADCValue;
}
average /=64;
LED();
sprintf(TempValue,"%fV\r",(float)average);//Format the value into a string
len = strlen(TempValue);//Length of the string
UARTSend(TempValue,len);//Send data to the serial port
delay(20000);
}
}
/***************************************************************
Initialize clock parameters
************************************************************/
void ClockInit(void)
{
CLKCONCMD = 0x28; //Set the timer count clock to 1M Hz and the system clock to 32 MHz
while(CLKCONSTA & 0x40); //Wait for the crystal to stabilize
}
The serial port displays the result:
At the beginning, the AD value read out was completely wrong. Finally, it was found that there was a problem with the sampling port on the board. After changing P0.6, the effect was much better. It was probably due to the influence of port multiplexing. . After getting the basic driver, I had to start adding the program to the ZIGBEE module to realize the network. .
|