我是靠谱客的博主 怡然鸡,最近开发中收集的这篇文章主要介绍cc2530设计性实验代码五,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

主函数

#include"ioCC2530.h"
#include"hal_ADC/hal_adc.h"
#include"hal_ADC/hal_types.h"
#include"hal_ADC/LcdControl.h"
#include "stdio.h"

#define uint unsigned int
#define uchar unsigned char

#define master_node

#ifdef master_node
#define LED1 P1_0
#define LED2 P1_1
#else
#define LED1 P1_1
#define LED2 P1_0
#endif
#define ON 1
#define OFF 0
#ifdef master_node
#define K1 P1_3
#define K2 P0_1
#else
#define K1 P0_1
#define K2 P0_7
#endif
enum SYSCLK_SRC{XOSC_32MHz,RC_16MHz};

void Delay(uint);
void InitLed(void);
void InitKey(void);
uchar KeyScan(void);

char i=0;
uchar Keyvalue=0;
uint temperature = 0;
uint volt=0,voltagevalue=0;
uint16 avgTemp;
uint16 avgVcc3;
double v3;

void Delay(uint n){
  uint t;
  for(t=0;t<n;t++);
  for(t=0;t<n;t++);
  for(t=0;t<n;t++);
  for(t=0;t<n;t++);
  for(t=0;t<n;t++);
}

void SystemClockSourceSelect(enum SYSCLK_SRC source)
{
  unsigned char osc32k_bm=CLKCONCMD & 0x80;
  unsigned char __clkconcmd,__clkconsta;
  
  if(source==RC_16MHz)
    CLKCONCMD=((osc32k_bm)|
      (0x01<<6)|
        (0x01<<3)|
          (0x01<<0));
  else if(source==XOSC_32MHz)
  {
    CLKCONCMD=(osc32k_bm);
  }
  __clkconcmd=CLKCONCMD;
  do
  {
    __clkconsta=CLKCONSTA;
  }while(__clkconsta!=__clkconcmd);
}
void InitKey(void)
{
  
#ifdef master_node
  P0SEL&=~0X02;
  P0DIR&=~0X02;
  P0INP|=0x02;
  
  P1SEL&=~0X08;
  P1DIR&=~0X08;
  P1INP|=0x08;
#else
  P0SEL&=~0X82;
  P0DIR&=~0X82;
  P0INP|=0x82;
#endif
}

void InitLed(void)
{
  P1SEL&=~0x03;
  P1DIR|=0x03;
  LED1=ON;
  LED2=OFF;
}

uchar KeyScan(void)
{
   if(K1==0)
  {
    Delay(100);
    if(K1==0)
    {
      while(!K1);
      return(1);
    }
  }
  
  else if(K2==0)
  {
    Delay(100);
    if(K2==0)
    {
      while(!K2);
      return(2);
    }
  }
  else
    ;
  return(Keyvalue);
}
void main(void)
{
  char i;
  double v;
  double temperature;
  uint8 temperature1,temperature2,temperature3;
  uint8 avgV0,avgV1,avgV2,avgV3;
  uint8 avgvcc0,avgvcc1,avgvcc2,avgvcc3;
  
  SystemClockSourceSelect(XOSC_32MHz);
  InitLed();
  HalLcd_HW_Init(pic2);
  IEN0=IEN1=IEN2=0X00;
  HalAdcInit();
  Delay(60000);Delay(60000);Delay(60000);
  Display_Picture(pic3);
  while(1)
  {
    avgTemp = 0;
    avgVcc3 =0;

    TR0=0X01;//连接温度传感器到SOP-ADC
    ATEST=0X01;//使能温度传感器
    for(i = 0 ; i < 4 ; i++)//取四次值求和
    {
      avgVcc3 += HalAdcRead (HAL_ADC_CHN_VDD3, HAL_ADC_RESOLUTION_12);//获取电源电压
      avgTemp += HalAdcRead (HAL_ADC_CHN_TEMP, HAL_ADC_RESOLUTION_12);
    }
    TR0=0X00;//断开温度传感器到SOP-ADC
    ATEST=0X00;//使能温度传感器无效
    avgTemp >>= 2;//avgTemp右移2位相当于除以4,取平均
    temperature=(avgTemp-(1480-4.5*25))/4.5;//计算出温度值

    avgVcc3 >>= 2;//avgVcc3右移2位相当于除以4,取平均
    v3=1.15*avgVcc3/2047;//1/3电压
    v=v3*3.0;//电源电压
    Delay(20000);

    temperature = temperature*10;//将温度先乘10
    temperature1 = (uint16)temperature/100;//将温度十位赋值给temperature1
    temperature2 = (uint16)temperature%100/10;//将温度个位赋值给temperature2
    temperature3 = (uint16)temperature%10;//将温度十分位赋值给temperature3

    v3 = v3*1000;//将1/3电压先乘1000
    avgV0 = (uint16)v3/1000;//将1/3电压个位赋值给avgV0
    avgV1 = (uint16)v3%1000/100;//将1/3电压十分位赋值给avgV1
    avgV2 = (uint16)v3%1000/10%10;//将1/3电压百分位赋值给avgV2
    avgV3 = (uint16)v3%1000%100%10;//将1/3电压千分位赋值给avgV3

    v = v*1000;//将电源压先乘1000
    avgvcc0 = (uint16)v/1000;//将电源电压个位赋值给avgvcc0
    avgvcc1 = (uint16)v%1000/100;//将电源电压十分位赋值给avgvcc1
    avgvcc2 = (uint16)v%1000/10%10;//将电源电压百分位赋值给avgvcc2
    avgvcc3 = (uint16)v%1000%100%10;//将电源电压千分位赋值给avgvcc3

    LCDDataShow(temperature1,2,3,82,89);//显示温度
    LCDDataShow(temperature2,2,3,90,97);
    LCDDataShow(temperature3,2,3,99,106);

    LCDDataShow(avgV0,4,5,75,82);//显示1/3电源电压
    LCDDataShow(avgV1,4,5,83,90);
    LCDDataShow(avgV2,4,5,91,98);
    LCDDataShow(avgV3,4,5,99,106);

    LCDDataShow(avgvcc0,6,7,73,80);//显示电源电压
    LCDDataShow(avgvcc1,6,7,83,90);
    LCDDataShow(avgvcc2,6,7,91,98);
    LCDDataShow(avgvcc3,6,7,99,106);

    Delay(60000);
  }
}

完整代码下载

最后

以上就是怡然鸡为你收集整理的cc2530设计性实验代码五的全部内容,希望文章能够帮你解决cc2530设计性实验代码五所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部