オンボード I2C 8x2 キャラクタLCD AQM0802A

投稿日 2013/07/09

小型オンボード用キャラクタLCD AQM0802AをI2Cインターフェースで動かしてみました。

 

8桁2行で非常に小型のLCDです。

AQM0802A_LCD_1.jpg

I2C 8x2 キャラクタLCD AQM0802A-RN-GBW

 

ピンはそのままでは汎用基板に刺せないので、広げてピン間隔2.54mmのピンヘッダに半田付けして使いました。

 

非常に小型なのでブレッドボードにも乗り、ちょっとした実験のモニタディスプレイに便利です。

 

制御方法は他の一般的なキャラクタディスプレイとほとんど同じですが、コントラスト調整は半固定抵抗ではなくソフトウェアで行います。

 

main.c

 

#include "p24fj256gb110.h"

 

// Primary HS OSC with PLL, USBPLL /2
_CONFIG2(FNOSC_PRIPLL & POSCMOD_HS & PLL_96MHZ_ON & PLLDIV_DIV2)
// JTAG off, watchdog timer off
_CONFIG1(JTAGEN_OFF & FWDTEN_OFF & ICS_PGx2)

 

char string[9];
unsigned int Timer1InterruptCount = 0;

 

int main(void){

AD1PCFGL = 0xFFFF; //all digital

AD1PCFGH = 0xFFFF;

PMD1 = 0xFFFF; //all periphral off
PMD2 = 0xFFFF;
PMD3 = 0xFFFF;
PMD4 = 0xFFFF;
PMD5 = 0xFFFF;
PMD6 = 0xFFFF;

_I2C1MD = 0; //I2C1 on
_T1MD = 0; //Timer1 on

LATA = 0;

LATB = 0;
LATC = 0;
LATD = 0;
LATE = 0;

TRISA = 0; //all I/O as Output
TRISB = 0;

TRISC = 0;
TRISD = 0;
TRISE = 0;

I2C1BRG = 157; //100KHz I2C
I2C1CON = 0x8000; //enable I2C1

lcd_init();
lcd_write(0, 0, "12345678" );
lcd_write(0, 1, "ABCDEFGH" );

 

while(1);
 

}

 

void Delay_ms (unsigned int delay){

Timer1InterruptCount = 0; // Clear Interrupt counter flag
PR1 = 0x3E80; // Fcy = 16MHz: (1ms / 62.5ns) = 16,000 = 0x3E80
IPC0bits.T1IP = 4; // Set Interrupt Priority lower then ADC
IEC0bits.T1IE = 1; // Enable Timer1 interrupts
T1CONbits.TON = 1; // Enable Timer1

while (Timer1InterruptCount < delay);// Wait for Interrupt counts to equal delay
T1CONbits.TON = 0; // Disable the Timer

}

 

void Delay_us (unsigned int delay){

Timer1InterruptCount = 0; // Clear Interrupt counter flag

PR1 = 0x0010; // Fcy = 16MHz: (1us / 62.5ns) = 16 = 0x0010
IPC0bits.T1IP = 4; // Set Interrupt Priority lower then ADC
IEC0bits.T1IE = 1; // Enable Timer1 interrupts

T1CONbits.TON = 1; // Enable Timer1

while (Timer1InterruptCount < delay);// Wait for Interrupt counts to equal delay
T1CONbits.TON = 0; // Disable the Timer

}

 

void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(){

Timer1InterruptCount ++; // Increment interrupt counter
IFS0bits.T1IF = 0; // Clear Interrupt Flag 

}


 

i2C_lcdAQM0802A.c

 

#include "p24fj256gb110.h"

 

#define LCD_I2C_ADDR 0x7C

 

void lcd_init(void);
void lcd_str(const unsigned char* ptr);
void lcd_clear(void);
void lcd_position(unsigned char xpos, unsigned char ypos);
void lcd_write(unsigned char xpos, unsigned char ypos, const unsigned char* ptr);

 

const int lcd_adrbase[2] = {0x80, 0xC0};

 

int i2c_lcd_IOCtl(unsigned char command, unsigned char data){
I2C1CONbits.SEN = 1;
while(I2C1CONbits.SEN == 1);
I2C1TRN = LCD_I2C_ADDR;
while(I2C1STATbits.TRSTAT);
if (I2C1STATbits.ACKSTAT){

I2C1CONbits.PEN = 1;
while(I2C1CONbits.PEN);
return(-1);

}
Delay_us(10);
I2C1TRN = command;
while(I2C1STATbits.TRSTAT);
if (I2C1STATbits.ACKSTAT){

I2C1CONbits.PEN = 1;
while(I2C1CONbits.PEN);
return(-1);

}
Delay_us(10);
I2C1TRN = data;
while(I2C1STATbits.TRSTAT);
if (I2C1STATbits.ACKSTAT){

I2C1CONbits.PEN = 1;
while(I2C1CONbits.PEN);
return(-1);

}
Delay_us(10);
I2C1CONbits.PEN = 1;
while(I2C1CONbits.PEN);
return(1);
}

 

void lcd_init(void){

Delay_ms(400);
i2c_lcd_IOCtl(0x00,0x38); //Function set
Delay_us(30);
i2c_lcd_IOCtl(0x00,0x39); //Function set
Delay_us(30);
i2c_lcd_IOCtl(0x00,0x14); //Internal OSC Frequency
Delay_us(30);
i2c_lcd_IOCtl(0x00,0x73); //Contrast set
Delay_us(30);
i2c_lcd_IOCtl(0x00,0x56); //Power/ICON/Contrast control
Delay_us(30);
i2c_lcd_IOCtl(0x00,0x6C); //Follower control
Delay_us(200);
i2c_lcd_IOCtl(0x00,0x38); //Function set
Delay_us(30);
i2c_lcd_IOCtl(0x00,0x0C); //Display ON/OFF control
Delay_us(30);
i2c_lcd_IOCtl(0x00,0x01); //Clear Display

}

 

void lcd_position(unsigned char xpos, unsigned char ypos){

i2c_lcd_IOCtl(0x00, lcd_adrbase[ypos] + xpos );

}

 

void lcd_write(unsigned char xpos, unsigned char ypos, const unsigned char* ptr){
lcd_position(xpos, ypos);

while(*ptr != 0x00){

i2c_lcd_IOCtl(0x40, *ptr++);

}
}

 

void lcd_clear(void){

i2c_lcd_IOCtl(0x00, 0x01);
Delay_ms(10);

}

 

void lcd_str(const unsigned char* ptr){
while(*ptr != 0)

i2c_lcd_IOCtl(0x40, *ptr++);

}





 

(JF1VRR)