找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6312|回复: 6
打印 上一主题 下一主题
收起左侧

基于STM32F0的ADS1118驱动源程序

[复制链接]
跳转到指定楼层
楼主
ID:304483 发表于 2018-4-9 16:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
项目用到ADS1118作为AD采集,项目使用MCU为STM32F030,使用STM32CubeMX建立工程

单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2018 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f0xx_hal.h"
  41. #include "spi.h"
  42. #include "gpio.h"

  43. /* USER CODE BEGIN Includes */
  44. #include "ads1118.h"
  45. /* USER CODE END Includes */

  46. /* Private variables ---------------------------------------------------------*/

  47. /* USER CODE BEGIN PV */
  48. /* Private variables ---------------------------------------------------------*/

  49. /* USER CODE END PV */

  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);

  52. /* USER CODE BEGIN PFP */
  53. /* Private function prototypes -----------------------------------------------*/

  54. /* USER CODE END PFP */

  55. /* USER CODE BEGIN 0 */
  56. uint16_t ConversionValue;
  57. float ad;
  58. uint16_t count = 0xC283;//0x8283;
  59. /* USER CODE END 0 */
  60. void delay1(uint32_t x)        //x=100W 延时1s(不精确)
  61. {
  62.         uint32_t i=0;
  63.         while(x--)
  64.         {
  65.                 i=10;
  66.                 while(i--);
  67.         }
  68. }
  69.         uint16_t adValue[4];
  70.         uint32_t conversion;
  71.         uint32_t config;
  72.         float adV[4];
  73. int main(void)
  74. {

  75.   /* USER CODE BEGIN 1 */
  76.         int i = 0;

  77.   /* USER CODE END 1 */

  78.   /* MCU Configuration----------------------------------------------------------*/

  79.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  80.   HAL_Init();

  81.   /* USER CODE BEGIN Init */

  82.   /* USER CODE END Init */

  83.   /* Configure the system clock */
  84.   SystemClock_Config();

  85.   /* USER CODE BEGIN SysInit */

  86.   /* USER CODE END SysInit */

  87.   /* Initialize all configured peripherals */
  88.   MX_GPIO_Init();
  89.   MX_SPI1_Init();
  90.        

  91.   /* USER CODE BEGIN 2 */

  92.   /* USER CODE END 2 */

  93.   /* Infinite loop */
  94.   /* USER CODE BEGIN WHILE */
  95.   while (1)
  96.   {
  97.   /* USER CODE END WHILE */

  98.   /* USER CODE BEGIN 3 */
  99.                 switch(i) {
  100.                 case 0:
  101.                         count = 0xc283;
  102.                         break;
  103.                 case 1:
  104.                         count = 0xd283;
  105.                         break;
  106.                 case 2:
  107.                         count = 0xe283;
  108.                         break;
  109.                 case 3:
  110.                         count = 0xf283;
  111.                         break;
  112.                 default:
  113.                         count = 0xc283;
  114.                         i = 0;
  115.                         break;
  116.                 }
  117. //                ConversionValue = SPI_RW_Reg(count);
  118. //                ad = ConversionValue * 4.096 / 32768;
  119.                
  120.                 conversion = SPI_RW_Reg(count);
  121.                 config = conversion & 0x0000FFFE;
  122.                 switch(config) {
  123.                 case 0x4282:
  124.                         adValue[3] = conversion >> 16;
  125.                         adV[3] = adValue[3] * 4.096 / 32768;
  126.                         break;
  127.                 case 0x5282:
  128.                         adValue[0] = conversion >> 16;
  129.                         adV[0] = adValue[0] * 4.096 / 32768;
  130.                         break;
  131.                 case 0x6282:
  132.                         adValue[1] = conversion >> 16;
  133.                         adV[1] = adValue[1] * 4.096 / 32768;
  134.                         break;
  135.                 case 0x7282:
  136.                         adValue[2] = conversion >> 16;
  137.                         adV[2] = adValue[2] * 4.096 / 32768;
  138.                         break;
  139.                 default:

  140.                         break;
  141.                 }
  142. //                adV[i] = adValue[i] * 4.096 / 32768;
  143. //               
  144. //                i++;
  145.                 if(++i > 3)        i = 0;
  146.                
  147.                 delay1(0x00009fff);
  148.   }
  149.   /* USER CODE END 3 */

  150. }

  151. /** System Clock Configuration
  152. */
  153. void SystemClock_Config(void)
  154. {

  155.   RCC_OscInitTypeDef RCC_OscInitStruct;
  156.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  157.     /**Initializes the CPU, AHB and APB busses clocks
  158.     */
  159.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  160.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  161.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  162.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  163.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  164.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  165.   RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  166.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  167.   {
  168.     _Error_Handler(__FILE__, __LINE__);
  169.   }

  170.     /**Initializes the CPU, AHB and APB busses clocks
  171.     */
  172.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  173.                               |RCC_CLOCKTYPE_PCLK1;
  174.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  175.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  176.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  177.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  178.   {
  179.     _Error_Handler(__FILE__, __LINE__);
  180.   }

  181.     /**Configure the Systick interrupt time
  182.     */
  183.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  184.     /**Configure the Systick
  185.     */
  186.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  187.   /* SysTick_IRQn interrupt configuration */
  188.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  189. }

  190. /* USER CODE BEGIN 4 */

  191. /* USER CODE END 4 */

  192. /**
  193.   * @brief  This function is executed in case of error occurrence.
  194.   * @param  None
  195.   * @retval None
  196.   */
  197. void _Error_Handler(char * file, int line)
  198. {
  199.   /* USER CODE BEGIN Error_Handler_Debug */
  200.   /* User can add his own implementation to report the HAL error return state */
  201.   while(1)
  202.   {
  203.   }
  204.   /* USER CODE END Error_Handler_Debug */
  205. }

  206. #ifdef USE_FULL_ASSERT

  207. /**
  208.    * @brief Reports the name of the source file and the source line number
  209.    * where the assert_param error has occurred.
  210.    * @param file: pointer to the source file name
  211.    * @param line: assert_param error line source number
  212.    * @retval None
  213.    */
  214. void assert_failed(uint8_t* file, uint32_t line)
  215. {
  216.   /* USER CODE BEGIN 6 */
  217.   /* User can add his own implementation to report the file name and line number,
  218.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  219.   /* USER CODE END 6 */

  220. }

  221. #endif

  222. /**
  223.   * @}
  224.   */

  225. /**
  226.   * @}
  227. */

  228. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
ADS1118.rar (3.33 MB, 下载次数: 143)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:268693 发表于 2018-5-10 16:56 | 只看该作者
学习了!感谢!
回复

使用道具 举报

板凳
ID:366340 发表于 2018-7-12 17:11 | 只看该作者
学写了,一起努力,发放
回复

使用道具 举报

地板
ID:259517 发表于 2018-7-12 18:53 | 只看该作者
感谢分享
回复

使用道具 举报

5#
ID:314158 发表于 2019-7-25 18:53 来自手机 | 只看该作者
谢谢分享
回复

使用道具 举报

6#
ID:457079 发表于 2019-7-29 22:40 | 只看该作者
有点东西啊!谢谢啦
回复

使用道具 举报

7#
ID:613280 发表于 2019-9-20 15:12 | 只看该作者
已下载,谢谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表