2019-03-25 18:53发布
15275185009 发表于 2017-5-3 11:30 include #define _XTAL_FREQ 8000000 #pragma config JTAGEN = 0 #pragma config LCS1 = 1 #pr ...
最多设置5个标签!
#define _XTAL_FREQ 8000000
#pragma config JTAGEN = 0
#pragma config LCS1 = 1
#pragma config LCS0 = 1
#pragma config FWDTEN = 0
#pragma config FNOSC2 = 0 /////01 HS
#pragma config FNOSC1 = 1
#pragma config FNOSC0 = 0
#pragma config POSCMD1 = 1
#pragma config POSCMD0 = 0
#pragma config FAMID7 = 0
#pragma config FAMID6 = 0
#pragma config FAMID5 = 0
#pragma config FAMID4 = 1
#pragma config FAMID3 = 0
#pragma config FAMID2 = 0
#pragma config FAMID1 = 0
#pragma config FAMID0 = 1
#pragma config DEV5 = 0
#pragma config DEV4 = 0
#pragma config DEV3 = 1
#pragma config DEV2 = 1
#pragma config DEV1 = 1
#pragma config DEV0 = 1
#define CS LATBbits.LATB11
#define WR LATBbits.LATB12
#define DATA LATBbits.LATB13
#define BL LATBbits.LATB10
/////////////////////
#define SCL PORTBbits.RB8
#define SDA PORTBbits.RB9
#define SDA_TRIS TRISBbits.TRISB8
#define SCL_TRIS TRISBbits.TRISB9
#define SCL2 PORTBbits.RB6
#define SDA2 PORTBbits.RB5
#define SCL2_TRIS TRISBbits.TRISB6
#define SDA2_TRIS TRISBbits.TRISB5
#define ACKBIT 0x00
#define NAKBIT 0x80
void lcd_init(void);
void lcd_write(unsigned char lcdbuf,unsigned char buflen);
void lcd_write_cmd(unsigned char cmd);
void lcd_write_data(unsigned char adr,unsigned char data);
void delay(unsigned int num);
void lcd_init(void)
{
TRISBbits.TRISB11 = 0;
TRISBbits.TRISB12 = 0;
TRISBbits.TRISB13 = 0;
TRISBbits.TRISB10 = 0;
lcd_write_cmd(0x18); //片内RC振荡器
lcd_write_cmd(0x03);// 启动LCD偏值
lcd_write_cmd(0x01); //启动系统时钟
lcd_write_cmd(0x08);//关闭声音输出
lcd_write_cmd(0x05);///关闭WDT
lcd_write_cmd(0x80);//IRQ关闭
lcd_write_cmd(0x0e);//普通模式
}
void lcd_write(unsigned char lcdbuf,unsigned char buflen)
{
unsigned char i;
for(i = 0;i < buflen;i++)
{
WR = 0;
delay(1);
if((lcdbuf & 0x80)==0x80)
{
DATA = 1;
}
else
{
DATA = 0;
}
delay(1);
WR = 1;
delay(1);
lcdbuf=lcdbuf<<1;
}
}
void lcd_write_cmd(unsigned char cmd)
{
CS = 0;
lcd_write(0x80,3);
lcd_write(cmd,9);
CS = 1;
}
void lcd_write_data(unsigned char adr,unsigned char data)
{
adr = adr<<1;
CS = 0;
lcd_write(0xa0,3);
lcd_write(adr,7);
lcd_write(data,8);
CS = 1;
}
void bstart(void)
{
SCL_TRIS = 0;
SDA_TRIS = 1;
SCL = 1;
__delay_us(10);
SDA_TRIS = 0;
SDA = 0;
__delay_us(10);
SCL = 0;
}
void bstop(void)
{
SCL_TRIS = 0;
SDA_TRIS = 0;
SDA = 0;
SCL = 1;
__delay_us(15);
SDA = 1;
__delay_us(10);
}
void bit_out(unsigned char data)
{
SCL_TRIS = 0;
SCL = 0;
__delay_us(5);
if (data & 0x80)
{
SDA_TRIS = 1;
}
else
{
SDA_TRIS = 0;
SDA = 0;
}
SCL = 1 ;
__delay_us(2);
}
void bit_in(unsigned char *data)
{
SCL_TRIS = 0;
SCL = 0;
SDA_TRIS = 1;
__delay_us(5);
SCL = 1;
__delay_us(3);
*data &= 0xFE;
if (SDA)
{
*data |= 0x01;
}
__delay_us(2);
SCL = 0;
}
unsigned char byte_out(unsigned char data)
{
unsigned char i;
unsigned char ack;
ack = 0;
for (i = 0; i < 8; i++)
{
bit_out(data);
data = data << 1;
}
bit_in(&ack);
return ack;
}
unsigned char byte_in(unsigned char ack)
{
unsigned char i;
unsigned char retval;
retval = 0;
for (i = 0; i < 8; i++)
{
retval = retval << 1;
bit_in(&retval);
}
bit_out(ack);
return retval;
}
///////////////////////I2C2
void bstart2(void)
{
SCL2_TRIS = 0;
SDA2_TRIS = 1;
SCL2 = 1;
__delay_us(10);
SDA2_TRIS = 0;
SDA2 = 0;
__delay_us(10);
SCL2 = 0;
}
void bstop2(void)
{
SCL2_TRIS = 0;
SDA2_TRIS = 0;
SDA2 = 0;
SCL2 = 1;
__delay_us(15);
SDA2 = 1;
__delay_us(10);
}
void bit_out2(unsigned char data)
{
SCL2_TRIS = 0;
SCL2 = 0;
__delay_us(5);
if (data & 0x80)
{
SDA2_TRIS = 1;
}
else
{
SDA2_TRIS = 0;
SDA2 = 0;
}
SCL2 = 1 ;
__delay_us(2);
}
void bit_in2(unsigned char *data)
{
SCL2_TRIS = 0;
SCL2 = 0;
SDA2_TRIS = 1;
__delay_us(5);
SCL2 = 1;
__delay_us(3);
*data &= 0xFE;
if (SDA2)
{
*data |= 0x01;
}
__delay_us(2);
SCL2 = 0;
}
unsigned char byte_out2(unsigned char data)
{
unsigned char i;
unsigned char ack;
ack = 0;
for (i = 0; i < 8; i++)
{
bit_out2(data);
data = data << 1;
}
bit_in2(&ack);
return ack;
}
unsigned char byte_in2(unsigned char ack)
{
unsigned char i;
unsigned char retval;
retval = 0;
for (i = 0; i < 8; i++)
{
retval = retval << 1;
bit_in2(&retval);
}
bit_out2(ack);
return retval;
}
////////////////////////////////////////////////////////////////////////////////DS1307
void write_DS1307_data(unsigned char address,unsigned char data)
{
bstart();
byte_out(0xd0);
byte_out(address);
byte_out(data);
bstop();
}
unsigned int read_DS1307_data(unsigned char address)
{
unsigned int data;
bstart();
byte_out(0xd0);
byte_out(address);
bstop();
bstart();
byte_out(0xd1);
data=byte_in(ACKBIT);
bstop();
return data;
}
///////////////////////////////////////////////////////////////////////////////EEPROM24C02
void write_24c02_data(unsigned char address,unsigned char data)
{
bstart();
byte_out(0xa0);// a1,a2,a3都接地,写地址为a0
byte_out(address);
byte_out(data);
bstop();
__delay_ms(3);
}
unsigned int read_24c02_data(unsigned char address)
{
unsigned int data;
bstart();
byte_out(0xa0);// a1,a2,a3都接地,写地址为a0
byte_out(address);
bstart();
byte_out(0xa1);
data=byte_in(NAKBIT);
bstop();
__delay_ms(3);
return data;
}
void delay(unsigned int num)
{
while(num--);
}
我现在是把各个功能挨个测试,这是i2c部分的;上面液晶显示屏的可以了。
这段DELAY函数
void delay(unsigned int num)
{
while(num--);
}
需要在使用前定义
或者,在头文件上声明后再使用
就不会出现未定义警告了
一周热门 更多>