- #include "reg51.h"
- #include "intrins.h"
- #include "math.h"
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- /*Declare SFR associated with the ADC */
- sfr ADC_CONTR = 0xBC; //ADC control register
- //ADC_POWER | SPEED1 | SPEED0 | ADC_FLAG | ADC_START | CHS2 | CHS1 | CHS0
- sfr ADC_RES = 0xBD; //ADC hight 8-bit result register
- sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
- sfr P1ASF = 0x9D; //P1 secondary function control register
- sfr AUXR1 = 0XA2;
- /*Define ADC operation const for ADC_CONTR*/
- #define ADC_POWER 0x80 //ADC power control bit
- #define ADC_FLAG 0x10 //ADC complete flag
- #define ADC_START 0x08 //ADC start control bit
- #define ADC_SPEEDLL 0x00 //540 clocks
- #define ADC_SPEEDL 0x20 //360 clocks
- #define ADC_SPEEDH 0x40 //180 clocks
- #define ADC_SPEEDHH 0x60 //90 clocks
- extern void Init_ST7920(); //液晶屏初始化
- extern void LCD_PutString(unsigned char x,unsigned char y,unsigned char code *s);
- extern void Write_Cmd(unsigned char Cmd);
- extern void Write_Data(unsigned char Data);
- unsigned char code Tab_LCD[]={0X30,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X38,0X39};
- void Delay(WORD n);
- void InitADC();
- BYTE ch = 0; //ADC channel NO.
- WORD Data;
- WORD V_Data;
- BYTE num=0;
- bit flag=0;
- unsigned char code Tab_BiaoTi[]={"12C5A60S2_A/D"};
- unsigned char code Tab_Bit[]={"8bit_A/D:"};
- unsigned char Tab_Count_Data[]={0,0,0,0};
- extern void DelayMs(unsigned char t);
- extern void LED_ScanDisplay(char Num);
- extern void Count_Data(unsigned int Data);
- /*----------------------------
- Initial ADC sfr
- ----------------------------*/
- void InitADC( )
- {
- P1ASF = 0xff; //Set P10 as analog input port
- ADC_RES = 0; //Clear previous result
- // ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
- // Delay(2); //ADC power-on delay and Start A/D conversion
- }
- void LCD_Update_Screen()
- {
- Write_Cmd(0x8b);
- Write_Data(Tab_LCD[Tab_Count_Data[3]]);
- Write_Data(Tab_LCD[Tab_Count_Data[2]]);
- Write_Data(Tab_LCD[Tab_Count_Data[1]]);
- Write_Data(Tab_LCD[Tab_Count_Data[0]]);
- DelayMs(5);
- }
- void Init_LCD_Screen()
- {
- LCD_PutString(0,1,Tab_BiaoTi);
- LCD_PutString(0,2,Tab_Bit);
- // LCD_Update_Screen();
- }
- void Count_Data(unsigned int Data)
- {
- Tab_Count_Data[3]=Data/1000;
- Tab_Count_Data[2]=Data%1000/100;
- Tab_Count_Data[1]=Data%1000%100/10;
- Tab_Count_Data[0]=Data%10;
- }
- void main()
- {
- Init_ST7920(); //液晶屏初始化
- Init_LCD_Screen();
- InitADC(); //Init ADC sfr
- while (1)
- {
- ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
- Delay(2); //ADC power-on delay and Start A/D conversion
- while(!(ADC_CONTR & ADC_FLAG));
- ADC_CONTR &= !ADC_FLAG;
- Data=ADC_RES;
- ADC_RES=0;
- V_Data=Data*5/256;
- Count_Data(V_Data);
-
- LCD_Update_Screen();
- }
- }
- /*----------------------------
- Software delay function
- ----------------------------*/
- void Delay(WORD n)
- {
- WORD x;
- while (n--)
- {
- x = 5000;
- while (x--);
- }
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
我程序跟你这个差不多啊,虽然不是同样的MCU,有点区别。
/******************************************************************************************
** 函数名称: Init_IO
** 函数描述: 初始化IO
** 输入参数: 无
** 输出参数: 无 //P20-27,P30-37,P00-03,P10-P17,
*******************************************************************************************/
void InitIo(void)
{
P4M1 = 0x00;
P4M0 = 0xff; //设置为输出口
P4 = 0;
P3M1 = 0x00;
P3M0 = 0xff; //p3.7,p3.6,p3.1,p3.0为输入口,其它设置为输出口
P3 = 0X03; //
P2M1 = 0x0; //p2口全部设置为输出口
P2M0 = 0xff; //p2口全部设置为输出口
P2 = 0Xff,
P1M1 = 0x27; //p1.3为输入口,p1.0 p1.1,P1.2,p1.5为ADC输入口
P1M0 = 0xe7; //p1.6-P1.7设置为输出口
P1 = 0x3f ;
P0M1 = 0; //p0.0-p0.7设置为输出口
P0M0 = 0Xff; //p0.0-p0.7设置为输出口
P0= 0X0c;
return;
}
谢谢哈,基本搞定了
一周热门 更多>