Code:
/////////////////////////////////////
// Generated Initialization File //
/////////////////////////////////////
#include "stdio.h"
#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;
void SendChar(unsigned char b);
void OLED_DrawRectangle(char cmd ,int x1, int y1, int x2, int y2,int color1,int color2);
void delay(int value);
void pensize(char cmd,char size);
void rectangle(void);
void display(void);
void OLED_DisplayControl(char cmd, char modeC, char valueC);
void OLED_Switch(char cmd, char option);
char valueC; //contrast range 0x00-0x0F
char option = 0x08;
char status;
void OLED_DisplayControl(char cmd, char modeC, char valueC)
{
SendChar(cmd);
SendChar(modeC);
SendChar(valueC);
}
void OLED_Switch(char cmd, char option)
{
SendChar(cmd);
SendChar(option);
}
void pensize(char cmd,char size)
{
SendChar(cmd);
SendChar(size);
}
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;
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();
}
void 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<20;b++)
{
}
}
}
void Switch(void)
{
OLED_Switch(0x4A,option);
Receive();
if (d_receive == 0x01)
{
valueC = 0x01;
}
else if (d_receive == 0x02)
{
valueC = 0x0F;
}
else
{
valueC = 0x05;
}
}
void DisplayControl(void)
{
OLED_DisplayControl(0x59, 0x02, valueC);
Receive();
if(d_receive!=0x06) //not equal to acknowledge
{
OLED_DisplayControl(0x59, 0x02, valueC);
Receive();
}
}
void OLED_DrawRectangle(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 rectangle(void)
{
int x1=0x00;
int y1=0x00;
int x2=0x50;
int y2=0x3F;
int color1=0x50;
int color2=0x50;
pensize(0x70,0x00);
OLED_DrawRectangle(0x72,x1,y1,x2,y2,color1,color2); //display rectangle
Receive();
if(d_receive!=0x06) //not equal to acknowledge
{
OLED_DrawRectangle(0x72,x1,y1,x2,y2,color1,color2);
Receive();
}
}
void main()
{
Init_Device();
TI=0;
RI=0;
x=1;
//qqq=0;
//aaa=0;
SendChar(GSGC_AUTOBAUD);
Receive();
SendChar(GSGC_CLS);
Receive();
Switch();
DisplayControl();
while (1)
{
rectangle();
}
}
what i wish to do is use the external switch to get the respond value to adjust the valueC.