2024年7月3日水曜日

yosino great lib for lcd1602 at pic16f1827

 /*

 *  I2Cモジュール付き1602LCD ヘッダーファイル

 *    i2c_lcd1602.h   2024.6.25

 */


#include <xc.h>


#ifndef I2C_LCD1602_H

#define I2C_LCD1602_H


void I2C_init();

void I2C_start();

void I2C_stop();

void I2C_write(unsigned char dat);

void Write_data(unsigned char data);

void LCD_write(unsigned char bits,unsigned char mode);

void LCD_init();

void LCD_clear();

void LCD_home();

void LCD_cursor(unsigned char x,unsigned char y);


#endif

----------------------------------------------------------------------

/*

 *  I2Cモジュール付き1602LCD ソースファイル 

 *   i2c_lcd1602.c  2024.6.25

 */


#include <xc.h>

#include "i2c_lcd1602.h"


#define _XTAL_FREQ 8000000


//LCD関係定義

#define LCD_EN 0b00000100   //Enable

#define LCD_BL 0b00001000   //Back Light

#define LCD_CMD 0x00

#define LCD_CHR 0x01

#define LCD_LINE1 0x80

#define LCD_LINE2 0xC0

#define LCD_addr 0x4E       //0x27<<1


/* I2C 初期化*/

void I2C_init(){

    SSP1CON1 = 0x28;        //SSPEN = 1,I2C Master Mode

    SSP1STATbits.SMP = 1;   //標準速度モード(100KHz)

    SSP1ADD = 0x31;         //Fosc/(4*Clock)-1 Clock=100kHz,Fosc=20MHz

//8MHzでも19ではなくこのままで

}


/* スタートコンディション */

void I2C_start(){

    SSP1CON2bits.SEN = 1;

    while(SSP1CON2bits.SEN);

}


/* ストップコンディション */

void I2C_stop(){

    SSP1IF = 0;

    SSP1CON2bits.PEN = 1;

    while(SSP1CON2bits.PEN);

    SSP1IF = 0;

}


/* I2Cに1byte送信 */

void I2C_write(unsigned char dat){

    SSP1IF = 0;

    SSP1BUF = dat;

    while(!SSP1IF);

}


void Write_data(unsigned char data){

    I2C_start();

    I2C_write(LCD_addr);

    I2C_write(data | LCD_EN | LCD_BL);

    I2C_write(data | LCD_BL);

    I2C_stop();

    __delay_us(100);

}


void LCD_write(unsigned char bits,unsigned char mode){

    //send High 4bits

    Write_data((bits & 0xF0) | mode);

    //send Low 4bits

    Write_data(((bits << 4) & 0xF0) | mode);

}

void LCD_init(){

    LCD_write(0x33,LCD_CMD);

    LCD_write(0x32,LCD_CMD);

    LCD_write(0x06,LCD_CMD);

    LCD_write(0x0C,LCD_CMD);

    LCD_write(0x28,LCD_CMD);

    LCD_write(0x01,LCD_CMD);

    __delay_ms(1);

}


void LCD_clear(){

    LCD_write(0x01,LCD_CMD);

    __delay_ms(1);

}


void LCD_home(){

LCD_write(0x02,LCD_CMD);

__delay_ms(1);

}


void LCD_cursor(unsigned char x,unsigned char y){

    if (y == 0){

        LCD_write(LCD_LINE1+x,LCD_CMD);

    }

    if (y == 1){

        LCD_write(LCD_LINE2+x,LCD_CMD);

    }

}

---------------------------------------------

/*

 *  I2C QAPASS 1602A  【16F1827】

 *    i2c_lcd1602/main.c  Created on 2024/06/21, 19:27

 */


#include <stdio.h>

#include <stdlib.h>

#include <xc.h>

#include "i2c_lcd1602.h"


#pragma config FOSC = INTOSC, WDTE = OFF , PWRTE = ON , MCLRE = ON , CP = OFF , CPD = OFF

#pragma config BOREN = OFF , CLKOUTEN = OFF , IESO = OFF , FCMEN = OFF

#pragma config WRT = OFF , PLLEN = OFF , STVREN = ON , LVP = OFF


#define _XTAL_FREQ 8000000


void putch(char ch){

    LCD_write(ch,0x01);

}


void main() {

    OSCCON = 0x72;

    ANSELA = 0b00000000 ; 

    ANSELB = 0b00000000 ; 

    TRISA = 0b00000000 ; 

    TRISB = 0b00010010 ;    // RB1(SDA),RB4(SDA)

    PORTA = 0b00000000 ;    

    PORTB = 0b00000000 ;    


    I2C_init();

    LCD_init();

    

    LCD_home();

    printf("Hello,world!");


    int count = 0;

    while(1){ 

        LCD_cursor(2,1);

printf("%3d",count);

count++;

__delay_ms(1000);

    }

}


0 件のコメント:

コメントを投稿