ailex1995
Joined: Apr 13, 2017 Posts: 3 Location: china
|
Posted: Thu Apr 13, 2017 2:17 am Post subject:
The program of driving OLED by PIC18F13K22 Subject description: programming,hardware |
|
|
Good afternoon~all, I met some troubles when I am working on the program about driving OLED by PIC18F13K22. Does anyone here can help me out?
Look! The program I wrote is as followings:
Code: | void init_fosc(void)
{
OSCCON = 0xF7;//16MHZ
}
void init_gpio(void)
{
ANSEL=0x00;//inputting enabled digital
ANSELH = 0x00;
PORTB=0;
LATB =0;
TRISB =0xFF;
PORTC=0;
LATC=0;
TRISC =0x00;
}
void init_i2c_master()
{
TRISB4 = input;
TRISB6 = input;
SSPCON1bits.SSPM0 = 0;
SSPCON1bits.SSPM1 = 0;
SSPCON1bits.SSPM2 = 0;
SSPCON1bits.SSPM3 = 1;// I2C Master mode ,clock=Fosc/(4*(SSPxADD+1))
SSPSTATbits.SMP = 0;//enabled slew rate: under the speed 400khz
SSPADD = 0x09;//SCL CLOCK Frequency 400KHZ
SSPCON1bits.SSPEN = 1;
}
void i2c_master_tx(unsigned char tx_deviceaddr,unsigned char tx_resaddr,unsigned char tx_data)
{
PIR1bits.SSPIF = 0;//the software need to set 0
SSPCON2bits.SEN = 1;//the start conditions
while(SSPCON2bits.SEN == 1);//SEN hardware will reset after finishing the start conditions
// PIR1bits.SSPIF = 1; Setting 1 by hardware
SSPBUF = tx_deviceaddr;//the device site
while(ACKSTAT); // ~ACK
while(PIR1bits.SSPIF == 0);
PIR1bits.SSPIF = 0;//the software need to set 0
SSPBUF = tx_resaddr;//writing the register which need to order
while(ACKSTAT); // ~ACK
while(PIR1bits.SSPIF == 0);
PIR1bits.SSPIF = 0;
SSPBUF = tx_data;//writing data
while(ACKSTAT); // ~ACK
while(PIR1bits.SSPIF == 0);
PIR1bits.SSPIF = 0;
SSPCON2bits.PEN = 1;//Stop condition
} |
I have also writen the OLED shows and initialization as well as the I2C agreement of hardware. It can operate perfectly by analogue I2C in the 51 SCM. Why can’t it communicate by using simulation and the I2C of hardware on PIC ?
This is the PIC18F13K22's datasheet: http://www.kynix.com/uploadfiles/pdf65976/PIC18F13K22-E2fML_965602.pdf ,please have a look if needed.
Thank you in advance!
Have a nice day~ |
|