樓主要注意正確配置寄存器和使用恰當數據類型。改了幾處,不一定適合你的顯示器,自己斟酌修改。
- #include "oled.h"
- #include "stdio.h"
- #include <intrins.h>
- #define ADC_POWER 0x80
- #define ADC_START 0x40
- #define ADC_FLAG 0x20
- #define ADC_RESFMT 0x20
- unsigned char tstr[5];
- unsigned int tmp=0;
- unsigned int Vol;
- unsigned int GetADCResult(unsigned char ch)
- {
- ADC_CONTR = ADC_POWER | ADC_START | ch ; //啟動adc電源,開始A/D轉換 ,配置采集口
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- while (!(ADC_CONTR & ADC_FLAG));
- ADC_CONTR &= ~ADC_FLAG;
- tmp=(ADC_RES<<8| ADC_RESL);//12位ADC
- Vol=(long)tmp*3.3*1000/4095;
- sprintf(tstr,"%1.4f",Vol);
- return Vol;
- }
- void main()
- {
- uint temp;
- P1M0 = 0X00;//配置P1.0口為ADC檢測輸入
- P1M1 = 0X01;
- ADCCFG=0x2f;//轉換結果右對齊,速率512 CPU時鐘數
- IIC_InitIO();
- OLED_Init();
- OLED_Screen_All(0XFF);
- Delay_OLED(1000);
- P55=0;
- OLED_Screen_All(0);
- Write_GB16X16(0,0,"實際值:");
- Write_GB16X16(2,56,"2.000");
- Write_GB16X16(2,0,"設定值:");
- while(1)
- {
- temp=GetADCResult(0); //獲取ADC值
- tstr[0]=temp/1000%10+'0';
- tstr[1]='.';
- tstr[2]=temp/100%10+'0';
- tstr[3]=temp/10%10+'0';
- tstr[4]=temp%10+'0';
- Write_GB16X16(0,56,tstr);
- }
- }
復制代碼 |