📄 i2c.c
字号:
#include <reg922.h>
#include "i2cexprt.h"
#define PCF8574A_WR 0x70 /* i2c address */
idata char hello[] = "LPC2 - I2C test March 2002\n";
static bit second = 0; /* one second passed flag */
static WORD count;
static BYTE iopBuf[1];
static I2C_MESSAGE iopMsg;
static void UART_Init(void)
{
P1M1 &= 0xFE;
P1M2 |= 0x01;
BRGR1 = 0x01;
BRGR0 = 0x70;
BRGCON = 3;
SCON = 0x52; /* mode 1, receiver enable */
}
static void ua_outchar(char c)
{
while (!TI) ;
SBUF = c;
TI = 0;
}
void PrintString( char *s)
{
while (*s)
{
if (*s == '\n')
ua_outchar('\r');
ua_outchar(*s);
s++;
}
}
// LPC2 - OSC = 7,373 MHz, PRE = 2
// reload_value = -OSC/(PRE*500Hz) = -7373 = 2 msec
void T0_Interrupt(void) interrupt 1
{
TR0 = 0; /* Stop timer 0 */
TL0 = 0x33; /* set timer 0 reload value */
TH0 = 0xe3;
TR0 = 1; /* Restart timer 0 */
if (++count > 500) /* 500 * 2 msec = 1 sec */
{
count = 0;
second = 1; /* one second passed */
}
}
static void T0_Init(void)
{
count = 0;
TMOD = 1; /* Timer 0 = Mode 1, 16 Bit */
TL0 = 0x33; /* set timer 0 reload value */
TH0 = 0xe3;
ET0 = 1; /* enable timer 0 interrupt */
TR0 = 1; /* start timer 0 */
}
static void RunLeds(void)
{
static BYTE ioport;
switch (ioport) /* run I2C IOport leds */
{
case 0x7f: ioport = 0xfe; break;
case 0xbf: ioport = 0x7f; break;
case 0xdf: ioport = 0xbf; break;
case 0xef: ioport = 0xdf; break;
case 0xf7: ioport = 0xef; break;
case 0xfb: ioport = 0xf7; break;
case 0xfd: ioport = 0xfb; break;
case 0xfe: ioport = 0xfd; break;
default: ioport = 0xfe; break;
}
iopBuf[0] = ioport;
I2C_Write(&iopMsg);
}
void main(void)
{
TRIM = 0x3C; /* clock out at P3.0 */
T0_Init(); /* initialize Timer 0 */
UART_Init(); /* initialize UART */
I2C_Init(); /* initialize I2C bus */
EA = 1; /* General interrupt enable */
iopMsg.address = PCF8574A_WR;
iopMsg.buf = iopBuf;
iopMsg.nrBytes = 1;
iopBuf[0] = 0xff;
I2C_Write(&iopMsg);
PrintString(hello);
while (1)
{
if (second)
{
second = 0;
Led = !Led; /* toggle the LED */
RunLeds();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -