20/10/2013
Embedded Systems Design ELEN90066
Lecture 19 SPI and LCD interface
David Jahshan
Notes from last lecture
• AVR studio
• AVR program tool and fuses
• Compile and run
• Watch window and disassembly • SET and GET
SET revisited
• #define SET(PORT,MASK,VALUE)
PORT = ((MASK & VALUE) | (PORT & ~MASK))
• PORT is the name of the register you want to set (does not need to be a port register)
• MASK is a bit mask that indicates the bit that you want to set
• VALUE is the value you want to set the masked bit to.
SET revisited
• Set the third bit of: MASK = 0000 0100
• Easier to set using _BV() Bit Vector sets a bit, remember counting starts from 0. _BV(2)
• If you want to set multiples OR them together #define SPI_ENABLE (_BV(SPIE)|_BV(SPE))
• SET(SPCR, SPI_ENABLE, ON);
An Example of SET
• SET (PORTB, _BV(PB1), ON)
• OR,both0’syouget0else1
• AND, both 1’s you get 1 else 0
• PORTB = 0010 1001, ON = 1111 1111, _BV(1) = 0000 0010
• PORT = ((MASK & VALUE) | (PORT & ~MASK))
• ~MASK = 1111 1101
• PORT & ~MASK = 0010 1001 why do we do this step? What if
we want to set the bit to 0.
• MASK & VALUE = 0000 0010 & 1111 1111 = 0000 0010
• 0000 0010 OR 0010 1001 = 0010 1011 so the bit is set.
SPI basics
• SPI bus is one of the simplest interfaces, three wires, Data IN, Data OUT and clock.
• Shift register connected to clock, each clock signal shifts in a data bit. (rising or falling depends on the register)
• SohowdoyousendabytetotheLCD?
• You do not need to use an SPI module you should know how to do this:
1
20/10/2013
LCD SPI data format
• LCD display has a microcontroller in it.
• Has two extra wires, Chip Select, and Command, Data. If Command it is an instruction for the LCD, else data.
Source: DOGS102-6e Data Sheet Page 4
SPI send one byte
• Send some data, PORTB PB0 is clock, PORTC PC0 is data
SET(PORTB, _BV(PB0), OFF); SET(PORTC, _BV(PC0), ON); SET(PORTB, _BV(PB0), ON); SET(PORTB, _BV(PB0), OFF); SET(PORTB, _BV(PB0), ON); SET(PORTB, _BV(PB0), OFF); SET(PORTC, _BV(PC0), OFF); SET(PORTC, _BV(PB0), ON);
SPI Send byte function
byte LCD_data_tx(byte tx_byte) //Sends a data byte {
}
byte tx_processed;
byte tx_mask = 0x80; LCD_CHIP_SELECT; LCD_DATA;
while (tx_mask != 0x00) {
tx_processed = tx_byte & tx_mask; SCK_SET_HIGH;
if(tx_processed)
MOSI_SET_HIGH;
else
MOSI_SET_LOW; SCK_SET_LOW;
tx_mask>>=1;
}
SCK_SET_HIGH; LCD_CHIP_DESELECT; return(TRUE);
Setting up the LCD Screen
byte LCD_initialise(void) {
}
LCD_command_tx(0x40);//Display start line 0 LCD_command_tx(0xA1);//SEG reverse LCD_command_tx(0xC0);//Normal COM0~COM63 LCD_command_tx(0xA4);//Disable -> Set All Pixel to ON LCD_command_tx(0xA6);//Display inverse off _delay_ms(120);
LCD_command_tx(0xA2);//Set LCD Bias Ratio A2/A3
LCD_command_tx(0x2F);//Set Power Control 28…2F
LCD_command_tx(0x27);//Set VLCD Resistor Ratio 20…27
LCD_command_tx(0x81);//Set Electronic Volume
LCD_command_tx(0x10);//Set Electronic Volume 00…3F
LCD_command_tx(0xFA);//Set Adv. Program Control
LCD_command_tx(0x90);//Set Adv. Program Control x00100yz yz column wrap x Temp Comp LCD_command_tx(0xAF);//Display on
return(TRUE);
SPI UNIT • Serial Peripheral Interface
Source: ATMEGA16 Datasheet Page 134
Using SPI
• Page138bottomhalf
• Learnhowtodothis,youcouldbeexaminedonthis • Set up the clock
• Set up the operating mode
• Decide if you want to use interrupts
• Write interrupt service routine
2
20/10/2013
Next Lecture
• Endeavour exhibition, strongly encourage everyone to go have a look
• Those of you who are not exhibiting, especially those who are here for their first semester, exams at Melbourne Uni.
• Next Monday, finish on code, Thursday revision lecture.
3