linux下读取ADC采样值,写入SD卡

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <locale.h>
#include <sys/timeb.h>

#include <fcntl.h>

int main(void)
{

struct tm *tm_now = NULL;    
struct timeb t;    
char  path[30]="/sdcard/";
char   name[9]; 
int    i;        
int    ADC_result_mV; 
int    read_ADC_return;
int    write_count=0;
int    file_fd; 

char  string_write_count[50];
char  string_ADC_result[5]; 

    ftime(&t);
    
tm_now = localtime(&t.time);
if(!tm_now) 
     {
      printf("cannot get right tm_now\n\r ");
      return -1;
     }
    sprintf(&name[0], "%d%02d%02d", tm_now->tm_year+1900, tm_now->tm_mon+1,tm_now->tm_mday);
    
    
   strcat(path,name);
   strcat(path,".txt");
   printf("Path=%s \n\r",path); 
   file_fd = open(path, O_RDWR | O_CREAT, 0777);
   if(file_fd < 0)
{
  printf("failed open file\n\r");
  return -1;
}
   else
      printf(" open file success\n\r");
   lseek(file_fd,0,SEEK_END);
   

  int ADC_fd = open("/dev/adc",O_RDWR);
  if(ADC_fd == -1)
    {
      printf("Open /dev/adc Error!\n\r");
      return -1;
    }
  else
    printf("Open /dev/adc success!\n\r");        

while(write_count<100)
{
read_ADC_return = read(ADC_fd,&string_ADC_result,sizeof(string_ADC_result));
if(read_ADC_return<0)//
{printf(“read ADC occur error\n\r”);
continue;
}

   //ADC_result_mV =  write_count;
   printf("result is = %s \n\r",string_ADC_result); 

      
    sprintf(string_write_count, "%d %s %s %s",write_count ,"sample result = ",string_ADC_result, "\n\r");
    write(file_fd, string_write_count, strlen(string_write_count));
    //write(file_fd, string_ADC_result, sizeof(string_ADC_result));        

    usleep(100*1000);//100*1000us=100ms
    write_count++;

    //break;
  }
 
 close(ADC_fd);
 close(file_fd);
 printf("finish %d write the ADC result\n\r",write_count);
 return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用STM32 HAL库读取ADC采样并计算电压的示例程序: ```c #include "main.h" #include "stdio.h" ADC_HandleTypeDef hadc1; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_ADC1_Init(void); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_ADC1_Init(); uint32_t adc_value; float voltage; while (1) { HAL_ADC_Start(&hadc1); // 启动ADC转换 HAL_ADC_PollForConversion(&hadc1, 100); // 等待转换完成 adc_value = HAL_ADC_GetValue(&hadc1); // 获取转换结果 voltage = adc_value * 3.3f / 4096.0f; // 计算电压(假设参考电压为3.3V) printf("ADC value: %d, voltage: %.2fV\r\n", adc_value, voltage); // 输出结果到串口 HAL_Delay(500); } } void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } } static void MX_ADC1_Init(void) { ADC_ChannelConfTypeDef sConfig = {0}; hadc1.Instance = ADC1; hadc1.Init.ScanConvMode = DISABLE; hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 1; if (HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); } sConfig.Channel = ADC_CHANNEL_0; // 选择ADC通道 sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } } static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin : PA0 */ GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } ``` 在该程序中,我们使用PA0作为ADC输入引脚,启动连续单次ADC转换模式,并通过串口输出采样和电压。需要注意的是,该程序中的参考电压为3.3V,如果使用其他参考电压,需要相应地调整计算公式。另外,ADC采样时间和通道等参数也需要根据实际情况进行配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值