Skip to content

Instantly share code, notes, and snippets.

@xiupos
Created February 9, 2019 10:28
Show Gist options
  • Save xiupos/7b865db68e9d15b1d27a3270c4b1c031 to your computer and use it in GitHub Desktop.
Save xiupos/7b865db68e9d15b1d27a3270c4b1c031 to your computer and use it in GitHub Desktop.
EEPROM読み出し機 by PIC18F14K50
// I2Cのテスト(AQM0802A)
// ha2zakura
#include <xc.h> // PIC のハードウエア定義
#define _XTAL_FREQ 48000000
#define true 1
#define false 0
#define int8_t signed char
#define int16_t signed short
#define int32_t signed int
#define int64_t signed long long
#define uint8_t unsigned char
#define uint16_t unsigned short
#define uint32_t unsigned int
#define uint64_t unsigned long long
#define LCDadr 0x3E
#define EEPadr 0b01010000
#pragma config FOSC = HS, PLLEN = ON, FCMEN = OFF
#pragma config IESO = OFF, USBDIV = OFF, CPUDIV = NOCLKDIV
#pragma config PWRTEN = OFF, BOREN = OFF, WDTEN = OFF
#pragma config HFOFST = OFF, MCLRE = ON
#pragma config STVREN = ON, BBSIZ = OFF, LVP = OFF
#pragma config XINST = OFF
#pragma config CP0 = OFF, CP1 = OFF, CPB = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRTB = OFF, WRTC = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTRB = OFF
void clock_init(void)
{
// 外部クロック(12MHz)を4xPLL
// config FOSC = HS, PLLEN = ON
OSCCON = 0b00000000;
OSCCON2 |= (1 << 2); // PRI_SD = 1
}
void i2c_init(void)
{
TRISB |= (1 << 4); // SDA
TRISB |= (1 << 6); // SCL
SSPSTAT = 0b10000000;
SSPCON1 = 0b00101000;
SSPCON2 = 0b00000000;
SSPADD = 0x77;
SSPIE = 1;
BCLIE = 1;
PEIE = 1;
GIE = 1;
SSPIF = 0;
BCLIF = 0;
}
void i2c_send(uint8_t adr, uint8_t *buf, uint32_t len)
{
uint16_t i;
while((SSPCON2 & 0x1F) | (SSPSTAT & 0x05));
SSPCON2bits.SEN = 1;
while((SSPCON2 & 0x1F) | (SSPSTAT & 0x05));
SSPBUF = (uint8_t)(adr << 1);
while(SSPCON2bits.ACKSTAT == 1);
for(i = 0; i < len; i++) {
while((SSPCON2 & 0x1F) | (SSPSTAT & 0x05));
SSPBUF = *buf;
buf++;
while(SSPCON2bits.ACKSTAT == 1);
}
while((SSPCON2 & 0x1F) | (SSPSTAT & 0x05));
SSPCON2bits.PEN = 1;
}
void i2c_receive(uint8_t adr, uint8_t *buf, uint16_t len)
{
uint16_t i;
while((SSPCON2 & 0x1F) | (SSPSTAT & 0x05));
SSPCON2bits.SEN = 1;
while((SSPCON2 & 0x1F) | (SSPSTAT & 0x05));
SSPBUF = (uint8_t)((adr << 1) + 1);
while(SSPCON2bits.ACKSTAT == 1);
for(i = 1; i <= len; i++) {
while((SSPCON2 & 0x1F) | (SSPSTAT & 0x05));
SSPCON2bits.RCEN = 1;
while((SSPCON2 & 0x1F) | (SSPSTAT & 0x04));
*buf = SSPBUF;
buf++;
while(SSPCON2bits.ACKSTAT == 1);
SSPCON2bits.ACKDT = (i == len ? 1 : 0);
SSPCON2bits.ACKEN = 1;
}
while((SSPCON2 & 0x1F) | (SSPSTAT & 0x05));
SSPCON2bits.PEN = 1;
}
void delay(uint32_t ms)
{
while(--ms > 0) __delay_us(1000);
}
void lcd_cmd(uint8_t cmd)
{
uint8_t data[2];
data[0] = 0b10000000;
data[1] = cmd;
i2c_send(LCDadr, data, 2);
}
void lcd_init(uint8_t con) // con = 0 ~ 63
{
__delay_ms(40);
lcd_cmd(0x38);
lcd_cmd(0x39);
lcd_cmd(0x14);
lcd_cmd(0x70 | (con & 0x0F));
lcd_cmd(0x54 | ((con & 0x30) >> 4));
lcd_cmd(0x6C);
__delay_ms(200);
lcd_cmd(0x38);
lcd_cmd(0x0C);
lcd_cmd(0x06);
lcd_cmd(0x01);
__delay_ms(1);
lcd_cmd(0x80);
__delay_ms(5);
}
void lcd_putc(char c)
{
uint8_t data[2];
data[0] = 0b11000000;
data[1] = c;
i2c_send(LCDadr, data, 2);
}
void lcd_print(char *s, uint8_t keta)
{
while(keta--) {
lcd_putc(*s);
s++;
}
}
void lcd_cursol(uint8_t x, uint8_t y)
{
lcd_cmd((y ? (0x40 + x) : (0x00 + x)) + 0x80);
}
void eep_write(uint16_t adr, uint8_t wdata)
{
uint8_t data[3];
data[0] = (adr & 0xFF00) >> 8;
data[1] = adr & 0xFF;
data[2] = wdata;
i2c_send(EEPadr, data, 3);
delay(5);
}
uint8_t eep_read(uint8_t adr)
{
uint8_t data[2];
data[0] = (adr & 0xFF00) >> 8;
data[1] = adr & 0xFF;
i2c_send(EEPadr, data, 2);
i2c_receive(EEPadr, data, 1);
return data[0];
}
char *s_itoa(uint16_t val, char *str, uint8_t rad, uint8_t keta) {
char *re = str;
uint8_t i;
str += keta - 1;
for(i = 0; i < keta; i++) {
*str = val % rad + (val % rad >= 10 ? 'A' - 10 : '0');
val /= rad;
str--;
}
return re;
}
void main(void)
{
clock_init();
i2c_init();
lcd_init(7);
uint16_t adr = 0;
lcd_cursol(0,0);
char str1[] = "A:0x ";
lcd_print(str1, 8);
lcd_cursol(0,1);
char str2[] = "0x ( )";
lcd_print(str2, 8);
char str3[4];
char str4[2];
while(1){
lcd_cursol(4,0);
lcd_print(s_itoa(adr, str3, 16, 4), 4);
lcd_cursol(2,1);
lcd_print(s_itoa(eep_read(adr), str4, 16, 2), 2);
lcd_cursol(6,1);
lcd_putc(eep_read(adr));
adr++;
delay(500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment