大神老師好,我要弄個ADC獲取傳感器的數值,然后寫入到SD卡里,現在郁悶,單獨運行ADC可以成功獲取,單獨運行SD卡可以運行寫入數據,但合在一起就開始卡死,直接沒反應,小白一名,剛開始學習單片機,是少了步驟還是哪里問題。求教!!!- #include "system.h"
- #include "SysTick.h"
- #include "usart.h"
- #include "adc.h"
- #include "oled.h"
- #include "bmp.h"
- #include "ff.h"
- #include "bsp_spi_sdcard.h"
- #include "stdio.h"
- FATFS fs; /* FatFs文件系統對象 */
- FIL fnew; /* 文件對象 */
- UINT fnum;
- FRESULT res_flash; /* 文件操作結果 */
- #define PRESS_MIN 20
- #define PRESS_MAX 8000
- //壓力范圍設定
- #define VOLTAGE_MIN 100
- #define VOLTAGE_MAX 3300
- //電壓范圍設定
- u8 state = 0;
- u16 val = 0;
- u16 value_AD = 0;
- u8 buf[10];
- long PRESS_AO = 0;
- int VOLTAGE_AO = 0;
- long map(long x, long in_min, long in_max, long out_min, long out_max);
- void Writer_Press_AO(long press_ao,u8 ad_number)
- {
- u32 time_get_temp;
- char filename[20];
- char writerbuff[40];
- res_flash = f_mount(&fs,"0:",1);
- if(res_flash == FR_OK)
- {
- printf("》初始化成功。\r\n");
- }
- else
- {
- printf("》初始化失敗。錯誤號:%d\r\n",res_flash);
- }
- time_get_temp = get_fattime();
- printf("》時間戳:%d\r\n",time_get_temp);
- sprintf(filename,"0:date%d.txt",time_get_temp);
- printf("》文件名:%s\r\n",filename);
- res_flash = f_open(&fnew,filename,FA_OPEN_ALWAYS | FA_WRITE );
- if(res_flash == FR_OK)
- {
- printf("》打開文件成功。\r\n");
- }
- else
- {
- printf("》打開文件錯誤。錯誤號:%d\r\n",res_flash);
- }
- f_lseek(&fnew, f_size(&fnew));
- sprintf(writerbuff,"time:%d ad:%d press_ao:%ld\r\n",time_get_temp,ad_number,press_ao);
- printf("》記錄值,時間:%d,端口%d:,壓力值:%d\r\n",time_get_temp,ad_number,press_ao);
- if ( res_flash == FR_OK )
- {
- printf("》打開/創建文件成功,向文件寫入數據。\r\n");
- res_flash=f_write(&fnew,writerbuff,sizeof(writerbuff),&fnum);
- if(res_flash==FR_OK)
- {
- printf("》文件寫入成功,寫入字節數據:%d\n",fnum);
- printf("》向文件寫入的數據為:\r\n%s\r\n",writerbuff);
- }
- else
- {
- printf("》!!文件寫入失敗:(%d)\r\n",res_flash);
- }
- }
- f_close(&fnew);
- f_mount(NULL,"0:",1);
- }
- long Get_Adc_Press_Ao(u8 adc_port)
- {
- value_AD = Get_Adc_Average(adc_port,10); //10次平均值
- VOLTAGE_AO = map(value_AD, 0, 4095, 0, 3300);
- if(VOLTAGE_AO < VOLTAGE_MIN)
- {
- PRESS_AO = 0;
- }
- else if(VOLTAGE_AO > VOLTAGE_MAX)
- {
- PRESS_AO = PRESS_MAX;
- }
- else
- {
- PRESS_AO = map(VOLTAGE_AO, VOLTAGE_MIN, VOLTAGE_MAX, PRESS_MIN, PRESS_MAX);
- }
-
- return PRESS_AO;
- }
- int main()
- {
- u8 i;
- long result;
- u32 time_get_temp;
- char filename[20];
- char writerbuff[40];
- char* adc_temp;
- SysTick_Init();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中斷優先級分組 分2組
- USART1_Init(9600);
- Adc_Init();
- OLED_Init();
- OLED_ColorTurn(0);//0正常顯示,1 反色顯示
- OLED_DisplayTurn(1);//0正常顯示 1 屏幕翻轉顯示
- OLED_Refresh();
- delay_ms(1000);
- OLED_ShowChinese(9,14,0,16);
- OLED_ShowChinese(27,14,1,16);
- OLED_ShowChinese(45,14,2,16);
- OLED_ShowChinese(63,14,3,16);
- OLED_ShowChinese(81,14,4,16);
- OLED_ShowChinese(99,14,5,16);
- OLED_ShowChinese(9,36,6,16);
- OLED_ShowChinese(27,36,7,16);
- OLED_ShowChinese(45,36,8,16);
- OLED_ShowChinese(63,36,9,16);
- OLED_ShowChinese(81,36,10,16);
- OLED_ShowChinese(99,36,11,16);
- OLED_Refresh();
- delay_ms(1000);
- delay_ms(1000);
- OLED_Clear();
-
-
- while(1)
- {
- for(i=0;i<4;i++)
- {
- result = Get_Adc_Press_Ao(i);
- sprintf(adc_temp,"AD%d:",i+1);
- OLED_ShowString(0,i*16,adc_temp,16);
- OLED_ShowNum(40,i*16,result,4,16);
- OLED_ShowString(80,i*16,"g",16);
- <span style="background-color: lemonchiffon;"> if(result == 8000) //就是這個if調用后就不正常了,注釋后OLED正常顯示獲取的AD值,我想要的是當換算出來的AD值達到8000后,把這個記錄寫入到SD卡的TXT中
- {
- Writer_Press_AO(result,i);
- }</span>
- OLED_Refresh();
- delay_ms(1000);
- }
-
- }
- }
- long map(long x, long in_min, long in_max, long out_min, long out_max) {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
復制代碼
求教!!!標黃了這個if語句,注釋后OLED就能正常顯示運行,有這個就卡死,單獨運行SD寫入程序也是正常,求教了
|