Code:
/////////////////////////////////////
// Generated Initialization File //
/////////////////////////////////////
#include "C8051F200.h"
#include "GSGCdef.h"
//#include "dos.h"
//#include "conio.h"
// Peripheral specific initialization functions,
// Called from the Init_Device() function
sbit LED=P2^4;
sbit SWITCH=P2^5;
int ttt[10];
int qqq;
int aaa;
int x=1;
unsigned char d_receive;
unsigned char Receive();
void SendChar(unsigned char b);
void OLED_DrawLine(char cmd ,int x1, int y1, int x2, int y2,int color1,int color2);
void OLED_DrawCircle(char cmd ,int x3, int y3, int radius, int color3,int color4);
void OLED_String(char cmd, char col, char row, char font, char color5, char color6, char adc, char ter);
void delay(int value);
void pensize(char cmd,char size);
int x1=0x00;
int y1=0x20;
int x2=0x60;
int y2=0x20;
int color1=0xFF;
int color2=0xFF;
int x3=0x10;
int y3=0x30;
int radius = 0x02;
int color3=0xF8;
int color4=0x00;
char col=0x05;
char row=0x01;
char font=0x02;
char color5=0xFF;
char color6=0xFF;
char ter=0x00;
char adc;
int op;
void pensize(char cmd,char size)
{
SendChar(cmd);
SendChar(size);
}
void OLED_DrawLine(char cmd ,int x1, int y1, int x2, int y2,int color1,int color2)
{
SendChar(cmd);
SendChar(x1);
SendChar(y1);
SendChar(x2);
SendChar(y2);
SendChar(color1);
SendChar(color2);
}
void OLED_DrawCircle(char cmd ,int x3, int y3, radius, int color3,int color4)
{
SendChar(cmd);
SendChar(x3);
SendChar(y3);
SendChar(radius);
SendChar(color3);
SendChar(color4);
}
void OLED_String(char cmd, char col, char row, char font, char color5, char color6, char adc, char ter)
{
SendChar(cmd);
SendChar(col);
SendChar(row);
SendChar(font);
SendChar(color5);
SendChar(color6);
SendChar(adc);
SendChar(ter);
}
void Timer_Init()
{
CKCON = 0x30;
TMOD = 0x20;
TH1 = 0xFA;
T2CON = 0x34;
RCAP2L = 0xFA; //baud rate for 57600bps
RCAP2H = 0xFF;
}
void UART_Init()
{
PCON = 0x80;
SCON = 0x70;
}
void Port_IO_Init()
{
// P0.0 - TX (UART), Push-Pull Digital
// P0.1 - RX (UART), Push-Pull Digital
// P0.2 - Unassigned, Open-Drain Digital
// P0.3 - Unassigned, Open-Drain Digital
// P0.4 - Unassigned, Open-Drain Digital
// P0.5 - Unassigned, Open-Drain Digital
// P0.6 - Unassigned, Open-Drain Digital
// P0.7 - Unassigned, Open-Drain Digital
// P1.0 - Unassigned, Open-Drain Digital
// P1.1 - Unassigned, Open-Drain Digital
// P1.2 - Unassigned, Open-Drain Digital
// P1.3 - Unassigned, Open-Drain Digital
// P1.4 - Unassigned, Open-Drain Digital
// P1.5 - Unassigned, Open-Drain Digital
// P1.6 - Unassigned, Open-Drain Digital
// P1.7 - Unassigned, Open-Drain Digital
// P2.0 - Unassigned, Open-Drain Digital
// P2.1 - Unassigned, Open-Drain Digital
// P2.2 - Unassigned, Open-Drain Digital
// P2.3 - Unassigned, Open-Drain Digital
// P2.4 - Unassigned, Open-Drain Digital
// P2.5 - Unassigned, Open-Drain Digital
// P2.6 - Unassigned, Open-Drain Digital
// P2.7 - Unassigned, Open-Drain Digital
PRT0MX = 0x01;
PRT0CF = 0x03;
P3MODE = 0x00;
PRT1CF = 0x01;
PRT2CF = 0x10;
}
void Oscillator_Init()
{
int i = 0;
OSCXCN = 0x67;
for (i = 0; i < 3000; i++); // Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0);
OSCICN = 0x08;
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
Timer_Init();
UART_Init();
Port_IO_Init();
Oscillator_Init();
}
unsigned char Receive()
{
while(RI!=1);
RI=0;
qqq++;
d_receive=SBUF;
//RI=0;
}
void SendChar(unsigned char b)
{
SBUF=b;
//delay(1);
while(TI!=1);
TI=0;
}
void delay (int value)
{
int a,b;
for (a=0; a<value; a++)
{
for (b=0; b<20000;b++)
{
}
}
}
void Display(void)
{
pensize(0x70,0x00);
OLED_DrawCircle(0x43,x3,y3,radius,color3,color4); //display circle
OLED_String(0x73,col,row,font,color5,color6,adc,ter); // display value
Receive();
if(d_receive!=0x06) //not equal to acknowledge
{
OLED_DrawCircle(0x43,x3,y3,radius,color3,color4);
Receive();
OLED_String(0x73,col,row,font,color5,color6,adc,ter);
Receive();
}
Receive();
}
void main()
{
Init_Device();
TI=0;
RI=0;
x=1;
//qqq=0;
//aaa=0;
REF0CN = 0x03; //use VCC as reference
AMX0SL = 0x38; //use port1 pin0 as analog input
ADC0CF = 0x30;//conversion clock = SYSCLOCK/ 8, gain = 1
ADC0CN = 0x80;//ADC enabled, conversion started with ADBUSY, right justified
EA = 1; //enable all interrupts
SendChar(GSGC_AUTOBAUD);
Receive();
SendChar(GSGC_CLS);
Receive();
OLED_DrawLine(0x4C,x1,y1,x2,y2,color1,color2); //display rectangle
if(d_receive!=0x06) //not equal to acknowledge
{
OLED_DrawLine(0x4C,x1,y1,x2,y2,color1,color2);
Receive();
}
ADBUSY = 1;//start a conversion
while(1)
{
if (ADBUSY == 0)//wait until conversion is complete
{
op = ADC0H;//get the converted value
adc = op;
ADBUSY = 1;//start another conversion
}
Display();
delay(3);
}