/*
* I2CLCD_lib
*/
#include <xc.h>
#include "I2CLCD.h"
#define _XTAL_FREQ 8000000
void I2C_init(void){
TRISB |= 0b00010010; //RB4(SCL1),RB1(SDA1)
OPTION_REGbits.nWPUEN = 0; // 0:PORTB内部プルアップのため
WPUB |= 0b00010010; //RB4 and RB1 are weakpulluped!
SSP1CON1 = 0b00101000; // I2C master mode
SSP1STAT = 0b00000000; // I2C status
SSP1ADD = 39; // I2C clock 50khz=8mhz/(4(39+1))
return;
}
void I2C_start(void){
SSP1CON2bits.SEN=1;
while(SSP1CON2bits.SEN);
return;
}
void I2C_stop(void){
SSP1CON2bits.PEN=1;
while(SSP1CON2bits.PEN);
return;
}
void I2C_byte_write(unsigned char byte){
SSP1IF=0;
SSP1BUF=byte;
while(!SSP1IF);
while(SSP1CON2bits.ACKSTAT); // wait for ACK return
SSP1IF=0;
return;
}
void I2C_LCD_cmd_write(unsigned char command){
I2C_start();
I2C_byte_write(0xA0); //slave address
I2C_byte_write(0x00);
I2C_byte_write(command);
I2C_stop();
return;
}
void I2C_LCD_data_write(char data){
I2C_start();
I2C_byte_write(0xA0); //slave address
I2C_byte_write(0x80);
I2C_byte_write(data);
I2C_stop();
return;
}
void LCD_init(void){
I2C_init();
I2C_LCD_cmd_write(0x38);
__delay_ms(5);
I2C_LCD_cmd_write(0x0C);
__delay_ms(5);
I2C_LCD_cmd_write(0x06);
__delay_ms(5);
I2C_LCD_cmd_write(0x01);
__delay_ms(5);
return;
}
void LCD_clear(void){
I2C_LCD_cmd_write(0x01);
__delay_ms(5);
return;
}
void LCD_locate(unsigned char row,unsigned char col){
unsigned char addr;
switch(row){
case 0: addr=0x00;
break;
case 1: addr=0x40;
break;
}
if(col>15)
col=0;
addr+=col;
addr|=0x80;
I2C_LCD_cmd_write(addr);
return;
}
void LCD_putc(char word){
I2C_LCD_data_write(word);
return;
}
void LCD_puts(char *string){
unsigned char i;
for(i=0;string[i]!='\0';++i){
LCD_putc(string[i]);
__delay_ms(5);
}
return;
}
/*
* I2CLCD
*/
#include <xc.h>
#ifndef I2CLCD_LIB_H
#define I2CLCD_LIB_H
void I2C_init(void);
void I2C_start(void);
void I2C_stop(void);
void I2C_byte_write(unsigned char byte);
void I2C_LCD_cmd_write(unsigned char command);
void I2C_LCD_data_write(char data);
void LCD_init(void);
void LCD_clear(void);
void LCD_locate(unsigned char row,unsigned char col);
void LCD_putc(char word);
void LCD_puts(char *string);
#endif
/*
* I2C ACM1602NI 【16F1827】
* ACM1602NI/main.c Created on 2024/05/06, 12:02
*/
// config1
#pragma config FOSC = INTOSC , WDTE = OFF , PWRTE = ON , MCLRE = ON , CP = OFF , CPD = OFF
#pragma config BOREN = OFF , CLKOUTEN = OFF , IESO = OFF , FCMEN = OFF
// config2
#pragma config WRT = OFF , PLLEN = OFF , STVREN = ON , LVP = OFF
#include <xc.h>
#include "I2CLCD.h"
#define _XTAL_FREQ 8000000
char moji[] = "Hello,world!";
char moji2[] = "PIC16F1827";
char moji3[] = "OK";
int main(void){
OSCCON=0x72; // 8MHz
ANSELA=0x00;
ANSELB=0x00;
TRISA=0x00;
TRISB=0x12; // 0b00010010
PORTB=0x00; // これはあってもなくてもOKだった
PORTA=0x00; // これはあってもなくてもOKだった
LCD_init(); // LCDの初期化
LCD_clear(); // 画面をクリア
LCD_locate(0,1); // 1行目、2文字(列)目へ移動
LCD_puts(moji);
LCD_locate(1,3); // 2行目、4文字(列)目へ移動
LCD_puts(moji2);
LCD_locate(1,14); // 2行目、15文字(列)目へ移動
LCD_puts(moji3);
while(1){}
}
0 件のコメント:
コメントを投稿