此篇利用I2C訊號來操作16x2 LCD,使用的16x2 LCD為I2C控制類型,如下所示,
程式部分是以Microchip MCU例子,但觀念可以用在其他廠牌MCU,只要改變週遭IO配置就可以,
1. I2C程式
.h部份
#define SCL1_RB8() TRISBbits.TRISB8 = 1;
#define SDA1_RB9() TRISBbits.TRISB9 = 1;
#define I2C1_InterruptEnable() IEC1bits.MI2C1IE = true;
#define I2C1_InterruptDisable() IEC1bits.MI2C1IE = false;
#define I2C1_InterruptFlagClear() IFS1bits.MI2C1IF = false;
#define I2C1_InterruptPriority IPC4bits.MI2C1IP
/* I2C1 Master初始化 */
void I2C1_Master_Init(void);
/* I2C1 Master Set Clear */
void I2C1_Master_SetClear(void);
/* I2C1 Start */
void I2C1_Start(uint8_t address);
/* I2C1 Stop */
void I2C1_Stop(void);
/* I2C1 Write */
void I2C1_Write(uint8_t data);
/* I2C1 Read Ack */
uint8_t I2C1_Read_Ack(void);
/* I2C1 Read Nack */
uint8_t I2C1_Read_Nack(void);
.c部份
/*
Name: I2C1 Master初始化
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void I2C1_Master_Init(void){
I2C1_Master_SetClear();
SCL1_RB8();
SDA1_RB9();
/* I2CxCON Set */
I2C1CONbits.SMEN = 0;
I2C1CONbits.DISSLW = 0;
I2C1CONbits.A10M = 0;
I2C1CONbits.IPMIEN = 0;
I2C1CONbits.I2CSIDL = 0;
I2C1CONbits.I2CEN = 1;
/* Baud Rate Set */
I2C1_BRG = 395;
}
/*
Name: I2C1 Master Set Clear
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void I2C1_Master_SetClear(void){
I2C1CON = 0x0000;
I2C1STAT = 0x0000;
}
/*
Name: I2C1 Start
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void I2C1_Start(uint8_t address){
I2C1CONbits.SEN = true;
while(I2C1CONbits.SEN==true);
I2C1TRN = address;
while(I2C1STATbits.TBF==true || I2C1STATbits.TRSTAT==true);
while(I2C1STATbits.ACKSTAT==true);
}
/*
Name: I2C1 Stop
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void I2C1_Stop(void){
I2C1CONbits.PEN = true;
while(I2C1CONbits.PEN==true);
}
/*
Name: I2C1 Write
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void I2C1_Write(uint8_t data){
I2C1TRN = data;
while(I2C1STATbits.TBF==true || I2C1STATbits.TRSTAT==true);
while(I2C1STATbits.ACKSTAT==true);
}
/*
Name: I2C1 Read Ack
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
uint8_t I2C1_Read_Ack(void){
uint8_t data=NULL;
I2C1CONbits.RCEN = true;
while(I2C1CONbits.RCEN==true);
data = I2C1RCV;
I2C1CONbits.ACKDT = false;
I2C1CONbits.ACKEN = true;
while(I2C1CONbits.ACKEN==true);
return data;
}
/*
Name: I2C1 Read Nack
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
uint8_t I2C1_Read_Nack(void){
uint8_t data=NULL;
I2C1CONbits.RCEN = true;
while(I2C1CONbits.RCEN==true);
data = I2C1RCV;
I2C1CONbits.ACKDT = true;
I2C1CONbits.ACKEN = true;
while(I2C1CONbits.ACKEN==true);
I2C1CONbits.ACKDT = false;
return data;
}
2. LCD1206程式
.h部份
/* LCD1206 Init */
void LCD1206_Init(void);
/* LCD1206 Set Cursor */
void LCD1206_SetCursor(uint8_t x, uint8_t y);
/* LCD1206 String */
void LCD1206_String(uint8_t *str, uint16_t length);
/* LCD1206顯示數字 */
void LCD1206_ShowNum(uint32_t num, uint16_t length);
/* LCD1206 Clear */
void LCD1206_Clear(void);
/* LCD1206 Command Write */
void LCD1206_CommandWrite(uint8_t cmd);
/* LCD1206 Data Write */
void LCD1206_DataWrite(uint8_t data);
/* LCD1206 Byte Write */
void LCD1206_ByteWrite(uint8_t address, uint8_t *writeData);
.c部份
/*
Name: LCD1206 Init
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void LCD1206_Init(void){
/* I2C1 Master初始化 */
I2C1_Master_Init();
delay_ms(150);
LCD1206_CommandWrite(0x02);
//16 *2顯示,5 *7點陣,4位數據口
LCD1206_CommandWrite(0x28);
//開顯示,光標關閉
LCD1206_CommandWrite(0x0C);
LCD1206_CommandWrite(0x80);
}
/*
Name: LCD1206 Set Cursor
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void LCD1206_SetCursor(uint8_t x, uint8_t y){
uint8_t addr=0;
if (y == 0){
addr = 0x00 + x;
}else{
addr = 0x40 + x;
}
LCD1206_CommandWrite(addr | 0x80);
}
/*
Name: LCD1206 String
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void LCD1206_String(uint8_t *str, uint16_t length){
uint16_t i=0;
for(i=0; i<length; i++){
LCD1206_DataWrite(*(str + i));
}
}
/*
Name: LCD1206顯示數字
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
uint32_t num: 數字
uint16_t length: 長度
Updated Record:
*/
void LCD1206_ShowNum(uint32_t num, uint16_t length){
uint8_t i=0,temp=0,enshow=0;
for(i=0; i<length; i++){
temp = (num/mypow(10,length-i-1))%10;
if(enshow==0&&i<(length-1)){
if(temp==0){
continue;
}else{
enshow = 1;
}
}
LCD1206_DataWrite(temp + '0');
}
}
/*
Name: LCD1206 Clear
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void LCD1206_Clear(void){
LCD1206_CommandWrite(0x01);
}
/*
Name: LCD1206 Command Write
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void LCD1206_CommandWrite(uint8_t cmd){
uint8_t data_u=0, data_l=0;
uint8_t data_t[4];
data_u = cmd&0xF0;
data_l = (cmd<<4)&0xF0;
data_t[0] = data_u | 0x0C; //en=1, rs=0
data_t[1] = data_u | 0x08; //en=0, rs=0
data_t[2] = data_l | 0x0C; //en=1, rs=0
data_t[3] = data_l | 0x08; //en=0, rs=0
LCD1206_ByteWrite(0x4E, data_t);
}
/*
Name: LCD1206 Data Write
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void LCD1206_DataWrite(uint8_t data){
uint8_t data_u=0, data_l=0;
uint8_t data_t[4];
data_u = data&0xF0;
data_l = (data<<4)&0xF0;
data_t[0] = data_u | 0x0D; //en=1, rs=1
data_t[1] = data_u | 0x09; //en=0, rs=1
data_t[2] = data_l | 0x0D; //en=1, rs=1
data_t[3] = data_l | 0x09; //en=0, rs=1
LCD1206_ByteWrite(0x4E, data_t);
}
/*
Name: LCD1206 Byte Write
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
void LCD1206_ByteWrite(uint8_t address, uint8_t *writeData){
uint8_t i=0;
I2C1_Start(address);
for(i=0; i<4; i++){
I2C1_Write(*(writeData + i));
}
I2C1_Stop();
}
3. program_tools程式
.h部份
#define delay_mcnt 7937 // Delay Coefficient
/* Delay us */
void delay_us(unsigned int n);
/* Delay Millisecond */
void delay_ms(unsigned int n);
/* 平方函數 */
uint32_t mypow(uint8_t m, uint8_t n);
.c部份
/*
Name: Delay us
Version: V1.0
Author: Michael Cheng
Created Date: 2018/10/24
Updated Data: N/A
Description:
已us為單位做延時操作
Updated Record:
*/
void delay_us(unsigned int n){
unsigned int dly;
while(n!=0){
dly=delay_mcnt/1000; while(dly!=0) dly--;
n--;
}
}
/*
Name: Delay Millisecond
Version: V1.0
Author: Michael Cheng
Created Date: 2018/10/24
Updated Data: N/A
Description:
已ms為單位做延時操作
unsigned int n: 設置延時時間
Updated Record:
*/
void delay_ms(unsigned int n){
unsigned int dly;
while(n!=0){
dly=delay_mcnt; while(dly!=0) dly--;
n--;
}
}
/*
Name: 平方函數
Version: V1.0
Author: Michael Cheng
Created Date:
Updated Data: N/A
Description:
Updated Record:
*/
uint32_t mypow(uint8_t m, uint8_t n){
uint32_t result=1;
while(n--){
result*=m;
}
return result;
}
4. main程式
LCD1206_SetCursor(0, 0);
LCD1206_String((uint8_t*)"Hello, World!", 13);
LCD1206_SetCursor(0, 1);
LCD1206_ShowNum(12345, 5);
留言列表