
Bra idé med lysdioder. Så enkelt det bara går.
Jag plockar fram ett rutat papper och sätter igång.

Kod: Markera allt
Vid Power On visar lysdioderna 00000001(bit 7 - bit 0. Dvs RS = 1.)
När jag initierat portexpandern så visar lysdioderna 00000000.
Skickar Får
00000000 00000000 (Sätter RS=0)
Börjar initiering
00110000 00000000
00110110 00000000
00110000 00000000
#
00110000 00000000
00110110 00000000
00110000 00000000
#
00110000 00000000
00110110 00000000
00110000 00000000
#
00100000 00000000
00100110 00000000
00100000 00000000
#
00100000 00000000
00100110 00000000
00100000 00000000
# -------------
10000000 00000000
10000110 10000110
10000000 10000000
#
00010000 00010000
00010110 00010110
00010000 00010000
#
01000000 01000000
01000110 01000110
01000000 01000000
#
00000000 00000000
00000110 00000110
00000000 00000000
#
11100000 11100000
11100110 11100110
11100000 11100000
#
00000000 00000000
00000110 00000110
00000000 00000000
#
01100000 01100000
01100110 01100110
01100000 01100000
#
00000000 00000000
00000110 00000110
00000000 00000000
#
00100000 00100000
00100110 00100110
00100000 00100000
#
00000000 00000000
00000110 00000110
00000000 00000000
#
00010000 00010000
00010110 00010110
00010000 00010000
#Klar
Kod: Markera allt
# Definiera adresser
DEVICE = 0x22 # Adressen till min krets på I2C-bussen.
# A
IODIRA = 0x00 # Register for pin direction. In or Out
OLATA = 0x0A # Register for outputs (When IOCON.BANK=1)
GPIOA = 0x09 # Register for inputs (When IOCON.BANK=1)
GPPUA = 0x06
# B
IODIRB = 0x10
OLATB = 0x1A
GPIOB = 0x19
GPPUB = 0x16
#
IOCON = 0x04
# - - - - - -
bus = smbus.SMBus(1) # I2C-bussen och vilken kanal.
#Bank 1. No mirror in INT. Sequential operation enabled. (IOCON: Sid 18 i databladet.)
bus.write_byte_data(DEVICE, IOCON, 0b10000000)
# Pull-up disabled for inputs
bus.write_byte_data(DEVICE, GPPUA, 0x00)
bus.write_byte_data(DEVICE, GPPUB, 0x00)
# Set all GPA and GPB pins as outputs by setting
# all bits of IODIRA and IODIRB register to 0
bus.write_byte_data(DEVICE,IODIRA, 0x00)
bus.write_byte_data(DEVICE,IODIRB, 0x00)
# Set output all 7 output bits to 0 on GPA and GPB
bus.write_byte_data(DEVICE,OLATA, 0x00)
bus.write_byte_data(DEVICE,OLATB, 0x00)
Kod: Markera allt
# Definiera adresser
DEVICE = 0x22 # Adressen till min krets på I2C-bussen
# A
IODIRA = 0x00 # Register for pin direction. In or Out
OLATA = 0x0A # Register for outputs (When IOCON.BANK=1)
GPIOA = 0x09 # Register for inputs (When IOCON.BANK=1)
IPOLA = 0x01 # Register for input polarity
GPINTENA = 0x02 # Interrupt on change. Enable/Disable.
DEFVALA = 0x03 # Default value for interrupt-on-change.
INTCONA = 0x04 # Intterupt on change. Control.
# B
IODIRB = 0x10
OLATB = 0x1A
GPIOB = 0x19
IPOLB = 0x11
GPINTENB = 0x12
DEFVALB = 0x13
INTCONB = 0x14
#
IOCON = 0x04
GPPUA = 0x06
GPPUB = 0x16
# - - - - - -
bus = smbus.SMBus(1) # I2C-bussen och vilken kanal.
#Bank 1. No mirror in INT. Sequential operation enabled.
bus.write_byte_data(DEVICE, IOCON, 0b10000000)
#Interrupt on change. Disable GPIO input for interrupt-on-change
bus.write_byte_data(DEVICE, GPINTENA, 0b00000000)
bus.write_byte_data(DEVICE, GPINTENB, 0b00000000)
#Default value for interrupt-on-change-pins.
bus.write_byte_data(DEVICE, DEFVALA, 0b00000000)
bus.write_byte_data(DEVICE, DEFVALB, 0b00000000)
#Pin value is compared against the previous pin value.
bus.write_byte_data(DEVICE, INTCONA, 0b00000000)
bus.write_byte_data(DEVICE, INTCONB, 0b00000000)
#Input polarity. Input will reflect the same logic state.
bus.write_byte_data(DEVICE, IPOLA, 0b00000000)
bus.write_byte_data(DEVICE, IPOLB, 0b00000000)
# Pull-up disabled for inputs
bus.write_byte_data(DEVICE, GPPUA, 0b00000000)
bus.write_byte_data(DEVICE, GPPUB, 0b00000000)
# Set all GPA and GPB pins as outputs by setting
# all bits of IODIRA and IODIRB register to 0
bus.write_byte_data(DEVICE,IODIRA, 0x00)
bus.write_byte_data(DEVICE,IODIRB, 0x00)
#GPIO port. All to 0.
bus.write_byte_data(DEVICE, GPIOA, 0x00)
bus.write_byte_data(DEVICE, GPIOB, 0x00)
# Set output all 7 output bits to 0 on GPA and GPB
bus.write_byte_data(DEVICE,OLATA, 0x00)
bus.write_byte_data(DEVICE,OLATB, 0x00)
# - - - - - -
Kod: Markera allt
IPOLA = 01h
IPOLB = 11h
GPINTENA = 02h
GPINTENB = 12h
DEFVALA = 03h
DEFVALB = 13h
INTCONA = 04h
INTCONB = 14h
#Input polarity. Input will reflect the same logic state.
IPOL = 0b00000000
#Interrupt on change. Disable GPIO input for interrupt-on-change
GPINTEN = 0b00000000
#Default value for interrupt-on-change-pins.
DEFVAL = 0b00000000
#Pin value is compared against the previous pin value.
INTCON = 0b00000000
#GPIO pull-upp resistor. Disabled
GPPU = 0b00000000
#GPIO port. Alla 0.
GPIO = 0b00000000
Kod: Markera allt
for junk in range(1,11):
bus.write_byte_data(DEVICE, OLATA, 0b10000000)
sleep(0.01)
bus.write_byte_data(DEVICE, OLATA, 0b01000000)
sleep(0.01)
bus.write_byte_data(DEVICE, OLATA, 0b00000000)
Kod: Markera allt
print("Now, how about a character of our own?")
#
#
print("First, RS should be set to 0.")
raw_input("\n\nPress the any-key to set RS = 0.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000000) # RS
print("RS = 0")
print("Then, the CGRAM-adress.")
raw_input("\n\nPress the any-key to set the CGRAM.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b01000000) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b01000110) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b01000000) # E off
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000000) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000110) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000000) # E off
raw_input("\n\nDone! Press the any-key.\n\n")
print("Now, RS should be set to 1.")
raw_input("\n\nPress the any-key to set RS = 1.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # RS
raw_input("\n\nPress the any-key to send character-data.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00010001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00010111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00010001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00110001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00110111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00110001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b01110001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b01110111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b01110001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b11110001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b11110111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b11110001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b01110001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b01110111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b01110001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00110001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00110111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00110001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00010001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00010111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00010001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # DB
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000111) # E on
raw_input("\n\nPress the any-key.\n\n")
bus.write_byte_data(DEVICE, OLATA, 0b00000001) # E off
raw_input("\n\nPress the any-key.\n\n")
#
Kod: Markera allt
const unsigned char LCD_INIT[7] = {0x38,0x38,0x38,0x38,0x0C,0x01,0x06}; /* 16x2 */
const unsigned char LCD_Chars[64] = {
0x00,0x00,0x1C,0x04,0x1C,0x10,0x1C,0x00, /* 00h/08h Subscript '2' */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 01h/09h */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 02h/0Ah */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 03h/0Bh */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 04h/0Ch */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 05h/0Dh */
0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00, /* 06h/0Eh */
0xFF,0x00,0x00,0x00,0x00,0x00,0xFF,0x00};/* 07h/0Fh */
volatile unsigned char x;
LCD_NPower = 0; /* Turn on power to LCD */
LCD_Control &= ~LCD_Cont_Bit; /* Inactivate the bits */
LCD_Data = 0x00; /* Set to 0x00 */
LCD_Delay_40ms();
for(x = 0;x < sizeof(LCD_INIT);x++)
{ /* First initialize the LCD mode */
LCD_Data = LCD_INIT[x];
LCD_Delay_Setup();
LCD_Shake_Enable();
LCD_Delay_1530us();
}
LCD_Data = 0x40; /* Load own char's command */
LCD_Delay_Setup();
LCD_Shake_Enable();
LCD_RS = 1;
LCD_Delay_60us();
for(x = 0;x < sizeof(LCD_Chars);x++)
{
LCD_Data = LCD_Chars[x];
LCD_Delay_Setup();
LCD_Shake_Enable();
LCD_Delay_60us();
}
LCD_RS = 0;
LCD_Delay_60us();
}