欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標(biāo)題: STM32F103用ADC獲取了數(shù)值寫(xiě)入SD遇到問(wèn)題,求教 [打印本頁(yè)]

作者: antengly010    時(shí)間: 2022-2-3 00:28
標(biāo)題: STM32F103用ADC獲取了數(shù)值寫(xiě)入SD遇到問(wèn)題,求教
大神老師好,我要弄個(gè)ADC獲取傳感器的數(shù)值,然后寫(xiě)入到SD卡里,現(xiàn)在郁悶,單獨(dú)運(yùn)行ADC可以成功獲取,單獨(dú)運(yùn)行SD卡可以運(yùn)行寫(xiě)入數(shù)據(jù),但合在一起就開(kāi)始卡死,直接沒(méi)反應(yīng),小白一名,剛開(kāi)始學(xué)習(xí)單片機(jī),是少了步驟還是哪里問(wèn)題。求教!!!
  1. #include "system.h"
  2. #include "SysTick.h"
  3. #include "usart.h"
  4. #include "adc.h"
  5. #include "oled.h"
  6. #include "bmp.h"
  7. #include "ff.h"
  8. #include "bsp_spi_sdcard.h"
  9. #include "stdio.h"

  10. FATFS fs;                                                                                                        /* FatFs文件系統(tǒng)對(duì)象 */
  11. FIL fnew;                                                                                                        /* 文件對(duì)象 */
  12. UINT fnum;
  13. FRESULT res_flash;                /* 文件操作結(jié)果 */

  14. #define PRESS_MIN        20
  15. #define PRESS_MAX        8000
  16. //壓力范圍設(shè)定


  17. #define VOLTAGE_MIN 100
  18. #define VOLTAGE_MAX 3300
  19. //電壓范圍設(shè)定

  20. u8 state = 0;
  21. u16 val = 0;
  22. u16 value_AD = 0;
  23. u8 buf[10];

  24. long PRESS_AO = 0;
  25. int VOLTAGE_AO = 0;

  26. long map(long x, long in_min, long in_max, long out_min, long out_max);

  27. void Writer_Press_AO(long press_ao,u8 ad_number)
  28. {
  29.         u32 time_get_temp;
  30.         char filename[20];
  31.         char writerbuff[40];
  32.         res_flash = f_mount(&fs,"0:",1);
  33.         if(res_flash == FR_OK)
  34.         {
  35.                 printf("》初始化成功。\r\n");
  36.         }
  37.         else
  38.         {
  39.                 printf("》初始化失敗。錯(cuò)誤號(hào):%d\r\n",res_flash);
  40.         }
  41.         time_get_temp = get_fattime();
  42.         printf("》時(shí)間戳:%d\r\n",time_get_temp);
  43.         sprintf(filename,"0:date%d.txt",time_get_temp);
  44.         printf("》文件名:%s\r\n",filename);
  45.         res_flash = f_open(&fnew,filename,FA_OPEN_ALWAYS | FA_WRITE );
  46.         if(res_flash == FR_OK)
  47.         {
  48.                 printf("》打開(kāi)文件成功。\r\n");
  49.         }
  50.         else
  51.         {
  52.                 printf("》打開(kāi)文件錯(cuò)誤。錯(cuò)誤號(hào):%d\r\n",res_flash);
  53.         }
  54.         f_lseek(&fnew, f_size(&fnew));
  55.         sprintf(writerbuff,"time:%d ad:%d press_ao:%ld\r\n",time_get_temp,ad_number,press_ao);
  56.         printf("》記錄值,時(shí)間:%d,端口%d:,壓力值:%d\r\n",time_get_temp,ad_number,press_ao);
  57.         if ( res_flash == FR_OK )
  58.         {
  59.                 printf("》打開(kāi)/創(chuàng)建文件成功,向文件寫(xiě)入數(shù)據(jù)。\r\n");               
  60.                 res_flash=f_write(&fnew,writerbuff,sizeof(writerbuff),&fnum);
  61.                 if(res_flash==FR_OK)
  62.                 {
  63.                         printf("》文件寫(xiě)入成功,寫(xiě)入字節(jié)數(shù)據(jù):%d\n",fnum);
  64.                         printf("》向文件寫(xiě)入的數(shù)據(jù)為:\r\n%s\r\n",writerbuff);
  65.                 }
  66.                 else
  67.                 {
  68.                         printf("》!!文件寫(xiě)入失敗:(%d)\r\n",res_flash);
  69.                 }   
  70.         }
  71.         f_close(&fnew);       
  72.         f_mount(NULL,"0:",1);
  73. }

  74. long Get_Adc_Press_Ao(u8 adc_port)
  75. {
  76.         value_AD = Get_Adc_Average(adc_port,10);        //10次平均值
  77.         VOLTAGE_AO = map(value_AD, 0, 4095, 0, 3300);
  78.         if(VOLTAGE_AO < VOLTAGE_MIN)
  79.         {
  80.                 PRESS_AO = 0;
  81.         }
  82.         else if(VOLTAGE_AO > VOLTAGE_MAX)
  83.         {
  84.                 PRESS_AO = PRESS_MAX;
  85.         }
  86.         else
  87.         {
  88.                 PRESS_AO = map(VOLTAGE_AO, VOLTAGE_MIN, VOLTAGE_MAX, PRESS_MIN, PRESS_MAX);
  89.         }
  90.        
  91.         return PRESS_AO;
  92. }


  93. int main()
  94. {
  95.         u8 i;
  96.         long result;
  97.         u32 time_get_temp;
  98.         char filename[20];
  99.         char writerbuff[40];
  100.         char* adc_temp;
  101.         SysTick_Init();
  102.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  //中斷優(yōu)先級(jí)分組 分2組
  103.         USART1_Init(9600);
  104.         Adc_Init();
  105.         OLED_Init();
  106.         OLED_ColorTurn(0);//0正常顯示,1 反色顯示
  107.   OLED_DisplayTurn(1);//0正常顯示 1 屏幕翻轉(zhuǎn)顯示
  108.         OLED_Refresh();
  109.         delay_ms(1000);
  110.         OLED_ShowChinese(9,14,0,16);
  111.         OLED_ShowChinese(27,14,1,16);
  112.         OLED_ShowChinese(45,14,2,16);
  113.         OLED_ShowChinese(63,14,3,16);
  114.         OLED_ShowChinese(81,14,4,16);
  115.         OLED_ShowChinese(99,14,5,16);
  116.         OLED_ShowChinese(9,36,6,16);
  117.         OLED_ShowChinese(27,36,7,16);
  118.         OLED_ShowChinese(45,36,8,16);
  119.         OLED_ShowChinese(63,36,9,16);
  120.         OLED_ShowChinese(81,36,10,16);
  121.         OLED_ShowChinese(99,36,11,16);
  122.         OLED_Refresh();
  123.         delay_ms(1000);
  124.         delay_ms(1000);
  125.         OLED_Clear();
  126.                
  127.        
  128.         while(1)
  129.         {
  130.                 for(i=0;i<4;i++)
  131.                 {
  132.                         result = Get_Adc_Press_Ao(i);
  133.                         sprintf(adc_temp,"AD%d:",i+1);
  134.                         OLED_ShowString(0,i*16,adc_temp,16);
  135.                         OLED_ShowNum(40,i*16,result,4,16);
  136.                         OLED_ShowString(80,i*16,"g",16);
  137. <span style="background-color: lemonchiffon;">                        if(result == 8000)                //就是這個(gè)if調(diào)用后就不正常了,注釋后OLED正常顯示獲取的AD值,我想要的是當(dāng)換算出來(lái)的AD值達(dá)到8000后,把這個(gè)記錄寫(xiě)入到SD卡的TXT中
  138.                         {
  139.                                 Writer_Press_AO(result,i);
  140.                         }</span>
  141.                         OLED_Refresh();
  142.                         delay_ms(1000);
  143.                 }
  144.                
  145.         }
  146. }

  147. long map(long x, long in_min, long in_max, long out_min, long out_max) {
  148. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  149. }

復(fù)制代碼

求教!!!標(biāo)黃了這個(gè)if語(yǔ)句,注釋后OLED就能正常顯示運(yùn)行,有這個(gè)就卡死,單獨(dú)運(yùn)行SD寫(xiě)入程序也是正常,求教了




作者: antengly010    時(shí)間: 2022-2-4 02:54
謝謝各位老師,事情自己解決了,但又碰到點(diǎn)小問(wèn)題,不知道要怎么處理,還沒(méi)想好怎么形容我想要的東西,等著總結(jié)好了咨詢(xún)




歡迎光臨 (http://m.raoushi.com/bbs/) Powered by Discuz! X3.1